From d470e951f865534844e33f21b8d0789ac95838ab Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 9 Mar 2022 14:44:56 +0100 Subject: [PATCH 1/6] Add Season Update Elo for later rework in FightSystem and BungeeCore --- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 6 +++- .../src/de/steamwar/sql/Provider.java | 21 ++++++++++- .../src/de/steamwar/sql/SQLProvider.java | 17 ++++++--- .../src/de/steamwar/sql/Season.java | 36 +++++++++++++++++++ .../de/steamwar/sql/StandaloneProvider.java | 7 +++- 5 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Season.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index 8eeeab7..07e8c47 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -23,7 +23,11 @@ public class Elo { private Elo(){} public static int getElo(int userId, String gameMode){ - return Provider.impl.getElo(userId, gameMode); + return Provider.impl.getCurrentElo(userId, gameMode); + } + + public static int getElo(int season, int userId, String gameMode){ + return Provider.impl.getElo(season, userId, gameMode); } public static void setElo(int userId, String gameMode, int elo){ diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java index 56737f5..af1209a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Provider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -39,7 +39,26 @@ public interface Provider { List getLastDeclinedOfNode(int node); List getLastDelined(int schemOwner); - int getElo(int userId, String gameMode); + int getCurrentSeason(); // returns -1 is in standalone mode + default String convertSeasonNumberToString(int season) { + if (season == -1) return ""; + int yearSeason = season % 3; + int year = (season - yearSeason) / 3; + return String.format("%d-%d", year, yearSeason); + } + default int convertSeasonStringToNumber(String season) { + if (season.isEmpty()) return -1; + String[] split = season.split("-"); + try { + return Integer.parseInt(split[0]) * 3 + Integer.parseInt(split[1]); + } catch (NumberFormatException e) { + return -1; + } + } + default int getCurrentElo(int userId, String gameMode) { + return getElo(getCurrentSeason(), userId, gameMode); + } + int getElo(int season, int userId, String gameMode); void setElo(int userId, String gameMode, int elo); Event getEvent(int eventID); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index 916f460..acb3d4a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -78,20 +78,27 @@ public class SQLProvider implements Provider { return new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason")); } - private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?"); @Override - public int getElo(int userId, String gameMode) { + public int getCurrentSeason() { + Calendar calendar = Calendar.getInstance(); + int yearIndex = calendar.get(Calendar.MONTH) / 3; + return (calendar.get(Calendar.YEAR) * 3 + yearIndex); + } + + private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); + @Override + public int getElo(int season, int userId, String gameMode) { return getElo.select(rs -> { if(rs.next()) return rs.getInt("Elo"); return 1000; - }, userId, gameMode); + }, userId, gameMode, season); } - private static final Statement setElo = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); + private static final Statement setElo = new Statement("UPDATE Elo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); @Override public void setElo(int userId, String gameMode, int elo) { - setElo.update(userId, gameMode, elo); + setElo.update(elo, getCurrentSeason(), userId, gameMode); } private static final Statement getEvent = new Statement("SELECT * FROM Event WHERE EventID = ?"); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Season.java b/SpigotCore_Main/src/de/steamwar/sql/Season.java new file mode 100644 index 0000000..9075ab4 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Season.java @@ -0,0 +1,36 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.sql; + +public class Season { + private Season() {} + + public static int getSeason() { + return Provider.impl.getCurrentSeason(); + } + + public static String convertSeasonToString(int season){ + return Provider.impl.convertSeasonNumberToString(season); + } + + public static int convertSeasonToNumber(String season){ + return Provider.impl.convertSeasonStringToNumber(season); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index 9c422f3..0499d99 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -65,7 +65,12 @@ public class StandaloneProvider implements Provider { } @Override - public int getElo(int userId, String gameMode) { + public int getCurrentSeason() { + return -1; + } + + @Override + public int getElo(int season, int userId, String gameMode) { return 1000; } From 4dedf824586c6a2eb61bdd339463fb28203732b8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 9 Mar 2022 20:09:41 +0100 Subject: [PATCH 2/6] Update stuff --- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 4 --- .../src/de/steamwar/sql/Season.java | 36 ------------------- 2 files changed, 40 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/Season.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index 07e8c47..fb78af4 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -26,10 +26,6 @@ public class Elo { return Provider.impl.getCurrentElo(userId, gameMode); } - public static int getElo(int season, int userId, String gameMode){ - return Provider.impl.getElo(season, userId, gameMode); - } - public static void setElo(int userId, String gameMode, int elo){ Provider.impl.setElo(userId, gameMode, elo); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Season.java b/SpigotCore_Main/src/de/steamwar/sql/Season.java deleted file mode 100644 index 9075ab4..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/Season.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.sql; - -public class Season { - private Season() {} - - public static int getSeason() { - return Provider.impl.getCurrentSeason(); - } - - public static String convertSeasonToString(int season){ - return Provider.impl.convertSeasonNumberToString(season); - } - - public static int convertSeasonToNumber(String season){ - return Provider.impl.convertSeasonStringToNumber(season); - } -} From 74b219994b444c1ca70637aae9e1d0e36e40bc0d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 9 Mar 2022 20:10:26 +0100 Subject: [PATCH 3/6] Update stuff --- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 2 +- SpigotCore_Main/src/de/steamwar/sql/Provider.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index fb78af4..8eeeab7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -23,7 +23,7 @@ public class Elo { private Elo(){} public static int getElo(int userId, String gameMode){ - return Provider.impl.getCurrentElo(userId, gameMode); + return Provider.impl.getElo(userId, gameMode); } public static void setElo(int userId, String gameMode, int elo){ diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java index af1209a..5429cdd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Provider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -55,7 +55,7 @@ public interface Provider { return -1; } } - default int getCurrentElo(int userId, String gameMode) { + default int getElo(int userId, String gameMode) { return getElo(getCurrentSeason(), userId, gameMode); } int getElo(int season, int userId, String gameMode); From 42c95f0ee8429557a9fea2bd77d2acdcdc4b6021 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 9 Mar 2022 20:24:28 +0100 Subject: [PATCH 4/6] Update stuff --- .../src/de/steamwar/sql/Provider.java | 21 +------------------ .../src/de/steamwar/sql/SQLProvider.java | 5 ++--- .../de/steamwar/sql/StandaloneProvider.java | 7 +------ 3 files changed, 4 insertions(+), 29 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java index 5429cdd..56737f5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Provider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -39,26 +39,7 @@ public interface Provider { List getLastDeclinedOfNode(int node); List getLastDelined(int schemOwner); - int getCurrentSeason(); // returns -1 is in standalone mode - default String convertSeasonNumberToString(int season) { - if (season == -1) return ""; - int yearSeason = season % 3; - int year = (season - yearSeason) / 3; - return String.format("%d-%d", year, yearSeason); - } - default int convertSeasonStringToNumber(String season) { - if (season.isEmpty()) return -1; - String[] split = season.split("-"); - try { - return Integer.parseInt(split[0]) * 3 + Integer.parseInt(split[1]); - } catch (NumberFormatException e) { - return -1; - } - } - default int getElo(int userId, String gameMode) { - return getElo(getCurrentSeason(), userId, gameMode); - } - int getElo(int season, int userId, String gameMode); + int getElo(int userId, String gameMode); void setElo(int userId, String gameMode, int elo); Event getEvent(int eventID); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index acb3d4a..f674038 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -78,7 +78,6 @@ public class SQLProvider implements Provider { return new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason")); } - @Override public int getCurrentSeason() { Calendar calendar = Calendar.getInstance(); int yearIndex = calendar.get(Calendar.MONTH) / 3; @@ -87,12 +86,12 @@ public class SQLProvider implements Provider { private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); @Override - public int getElo(int season, int userId, String gameMode) { + public int getElo(int userId, String gameMode) { return getElo.select(rs -> { if(rs.next()) return rs.getInt("Elo"); return 1000; - }, userId, gameMode, season); + }, userId, gameMode, getCurrentSeason()); } private static final Statement setElo = new Statement("UPDATE Elo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index 0499d99..9c422f3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -65,12 +65,7 @@ public class StandaloneProvider implements Provider { } @Override - public int getCurrentSeason() { - return -1; - } - - @Override - public int getElo(int season, int userId, String gameMode) { + public int getElo(int userId, String gameMode) { return 1000; } From 55e4419d1bf1e3b7423ceec2798019b467433bf8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 9 Mar 2022 20:41:21 +0100 Subject: [PATCH 5/6] Add FightEndsPacket --- .../de/steamwar/comms/PacketIdManager.java | 1 + .../comms/packets/FightEndsPacket.java | 73 +++++++++++++++++++ SpigotCore_Main/src/de/steamwar/sql/Elo.java | 32 -------- .../src/de/steamwar/sql/Provider.java | 3 - .../src/de/steamwar/sql/SQLProvider.java | 16 ---- .../de/steamwar/sql/StandaloneProvider.java | 8 -- 6 files changed, 74 insertions(+), 59 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/Elo.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java index d4e85a2..317ed7b 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -35,4 +35,5 @@ public class PacketIdManager { //0x2(X) Server Information System public static final byte I_AM_A_LOBBY = 0x20; public static final byte FIGHT_INFO = 0x21; + public static final byte FIGHT_ENDS = 0x22; } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java new file mode 100644 index 0000000..fd51d74 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java @@ -0,0 +1,73 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.comms.packets; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.comms.PacketIdManager; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.List; + +@AllArgsConstructor +@Getter +public class FightEndsPacket extends SpigotPacket { + + private byte win; + private int blueSchem; + private int redSchem; + private List bluePlayers; + private List redPlayers; + + public FightEndsPacket(ByteArrayDataInput byteArrayDataInput) { + win = byteArrayDataInput.readByte(); + blueSchem = byteArrayDataInput.readInt(); + redSchem = byteArrayDataInput.readInt(); + int blueSize = byteArrayDataInput.readInt(); + for (int i = 0; i < blueSize; i++) { + bluePlayers.add(byteArrayDataInput.readInt()); + } + int redSize = byteArrayDataInput.readInt(); + for (int i = 0; i < redSize; i++) { + redPlayers.add(byteArrayDataInput.readInt()); + } + } + + @Override + public int getName() { + return PacketIdManager.FIGHT_ENDS; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeByte(win); + byteArrayDataOutput.writeInt(blueSchem); + byteArrayDataOutput.writeInt(redSchem); + byteArrayDataOutput.writeInt(bluePlayers.size()); + for (int i : bluePlayers) { + byteArrayDataOutput.writeInt(i); + } + byteArrayDataOutput.writeInt(redPlayers.size()); + for (int i : redPlayers) { + byteArrayDataOutput.writeInt(i); + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java deleted file mode 100644 index 8eeeab7..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.sql; - -public class Elo { - private Elo(){} - - public static int getElo(int userId, String gameMode){ - return Provider.impl.getElo(userId, gameMode); - } - - public static void setElo(int userId, String gameMode, int elo){ - Provider.impl.setElo(userId, gameMode, elo); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java index 56737f5..21413d8 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Provider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -39,9 +39,6 @@ public interface Provider { List getLastDeclinedOfNode(int node); List getLastDelined(int schemOwner); - int getElo(int userId, String gameMode); - void setElo(int userId, String gameMode, int elo); - Event getEvent(int eventID); EventFight getEventFight(int fightID); void setEventFightResult(EventFight fight, int winner); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index f674038..c5de7a5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -84,22 +84,6 @@ public class SQLProvider implements Provider { return (calendar.get(Calendar.YEAR) * 3 + yearIndex); } - private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); - @Override - public int getElo(int userId, String gameMode) { - return getElo.select(rs -> { - if(rs.next()) - return rs.getInt("Elo"); - return 1000; - }, userId, gameMode, getCurrentSeason()); - } - - private static final Statement setElo = new Statement("UPDATE Elo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); - @Override - public void setElo(int userId, String gameMode, int elo) { - setElo.update(elo, getCurrentSeason(), userId, gameMode); - } - private static final Statement getEvent = new Statement("SELECT * FROM Event WHERE EventID = ?"); @Override public Event getEvent(int eventID) { diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index 9c422f3..5e8f0c3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -64,14 +64,6 @@ public class StandaloneProvider implements Provider { return new ArrayList<>(); } - @Override - public int getElo(int userId, String gameMode) { - return 1000; - } - - @Override - public void setElo(int userId, String gameMode, int elo) {} - @Override public Event getEvent(int eventID) { return new Event(eventID, "DummyEvent", Timestamp.from(Instant.now()), Timestamp.from(Instant.now()), 6, false, false); From d8b86b1263e16ff274c2727784074fb8b0456dfe Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 12:01:10 +0100 Subject: [PATCH 6/6] Remove SQLProvider.getCurrentSeason --- SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index c5de7a5..68839c5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -78,12 +78,6 @@ public class SQLProvider implements Provider { return new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason")); } - public int getCurrentSeason() { - Calendar calendar = Calendar.getInstance(); - int yearIndex = calendar.get(Calendar.MONTH) / 3; - return (calendar.get(Calendar.YEAR) * 3 + yearIndex); - } - private static final Statement getEvent = new Statement("SELECT * FROM Event WHERE EventID = ?"); @Override public Event getEvent(int eventID) {