From 621cb64fb38892a36b221699b0dbbc50f4f4daf4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 18:52:21 +0100 Subject: [PATCH 01/70] Add SchemElo Add UserElo Remove Elo.java --- src/de/steamwar/bungeecore/sql/Elo.java | 52 ----------------- src/de/steamwar/bungeecore/sql/SchemElo.java | 48 +++++++++++++++ src/de/steamwar/bungeecore/sql/UserElo.java | 61 ++++++++++++++++++++ 3 files changed, 109 insertions(+), 52 deletions(-) delete mode 100644 src/de/steamwar/bungeecore/sql/Elo.java create mode 100644 src/de/steamwar/bungeecore/sql/SchemElo.java create mode 100644 src/de/steamwar/bungeecore/sql/UserElo.java diff --git a/src/de/steamwar/bungeecore/sql/Elo.java b/src/de/steamwar/bungeecore/sql/Elo.java deleted file mode 100644 index da1db904..00000000 --- a/src/de/steamwar/bungeecore/sql/Elo.java +++ /dev/null @@ -1,52 +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.bungeecore.sql; - -public class Elo { - - private static final Statement elo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); - private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM Elo WHERE GameMode = ? AND Elo > ? AND Season = ?"); - - private Elo(){} - - public static int getElo(int userID, String gameMode){ - return getElo(Season.getSeason(), userID, gameMode); - } - - public static int getElo(int season, int userID, String gameMode){ - return elo.select(rs -> { - if(rs.next()) - return rs.getInt("Elo"); - return 1000; - }, userID, gameMode, season); - } - - public static int getPlacement(int elo, String gameMode){ - return getPlacement(Season.getSeason(), elo, gameMode); - } - - public static int getPlacement(int season, int elo, String gameMode){ - return place.select(rs -> { - if(rs.next()) - return rs.getInt("Place"); - return -1; - }, gameMode, elo, season); - } -} diff --git a/src/de/steamwar/bungeecore/sql/SchemElo.java b/src/de/steamwar/bungeecore/sql/SchemElo.java new file mode 100644 index 00000000..d5253cf0 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchemElo.java @@ -0,0 +1,48 @@ +/* + * 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.bungeecore.sql; + +public class SchemElo { + private SchemElo() {} + + private static final Statement elo = new Statement("SELECT SchemElo FROM Elo WHERE SchemID = ? AND Season = ?"); + private static final Statement setElo = new Statement("UPDATE SchemElo SET Elo = ? WHERE Season = ? AND SchemID = ?"); + // private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM SchemElo WHERE Elo > ? AND Season = ?"); + + public static int getElo(int schemID) { + return getElo(Season.getSeason(), schemID); + } + + public static int getElo(int season, int schemID) { + return elo.select(rs -> { + if (rs.next()) + return rs.getInt("Elo"); + return 1000; + }, schemID, season); + } + + public static void setElo(int schemID, int elo) { + setElo(Season.getSeason(), schemID, elo); + } + + public static void setElo(int season, int schemID, int elo) { + setElo.update(elo, season, schemID); + } +} diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java new file mode 100644 index 00000000..1d45f44f --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -0,0 +1,61 @@ +/* + * 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.bungeecore.sql; + +public class UserElo { + private UserElo() { + } + + private static final Statement elo = new Statement("SELECT UserElo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); + private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); + private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); + + public static int getElo(int userID, String gameMode) { + return getElo(Season.getSeason(), userID, gameMode); + } + + public static int getElo(int season, int userID, String gameMode) { + return elo.select(rs -> { + if (rs.next()) + return rs.getInt("Elo"); + return 1000; + }, userID, gameMode, season); + } + + public static void setElo(int userId, String gameMode, int elo) { + setElo(Season.getSeason(), userId, gameMode, elo); + } + + public static void setElo(int season, int userId, String gameMode, int elo) { + setElo.update(elo, season, userId, gameMode); + } + + public static int getPlacement(int elo, String gameMode) { + return getPlacement(Season.getSeason(), elo, gameMode); + } + + public static int getPlacement(int season, int elo, String gameMode) { + return place.select(rs -> { + if (rs.next()) + return rs.getInt("Place"); + return -1; + }, gameMode, elo, season); + } +} From 54753ce1f8e835b7693f1543d0f5805dbaa471f5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 19:00:46 +0100 Subject: [PATCH 02/70] Add UserGroup shortforms --- .../listeners/ConnectionListener.java | 2 +- src/de/steamwar/bungeecore/sql/UserGroup.java | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java index 65855b39..a2f2fb27 100644 --- a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java +++ b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java @@ -76,7 +76,7 @@ public class ConnectionListener extends BasicListener { if(user.getUserGroup() != UserGroup.Member) { player.setPermission(YOUTUBER_MODS, true); - player.setDisplayName(user.getUserGroup().getColorCode() + user.getUserGroup().name() + " " + player.getName() + "§r"); + player.setDisplayName(user.getUserGroup().getColorCode() + user.getUserGroup().getChatPrefix() + " " + player.getName() + "§r"); player.setPermission("bungeecore.group." + user.getUserGroup().name().toLowerCase(), true); }else { player.setDisplayName(player.getName()); diff --git a/src/de/steamwar/bungeecore/sql/UserGroup.java b/src/de/steamwar/bungeecore/sql/UserGroup.java index e6c30f9a..e5abd97b 100644 --- a/src/de/steamwar/bungeecore/sql/UserGroup.java +++ b/src/de/steamwar/bungeecore/sql/UserGroup.java @@ -24,24 +24,26 @@ import java.util.stream.Collectors; public enum UserGroup { - Admin("§4", "§e", true, true, true, true), - Developer("§3", "§f", true, true, true, true), - Moderator("§c", "§f", true, true, true, true), - Supporter("§9", "§f", false, true, true, true), - Builder("§2", "§f", false, true, false, true), - YouTuber("§5", "§f", false, false, false, true), - Member("§7", "§7", false, false, false, false); + Admin("§4", "§e", "Admin", true, true, true, true), + Developer("§3", "§f", "Dev", true, true, true, true), + Moderator("§c", "§f", "Mod", true, true, true, true), + Supporter("§9", "§f", "Sup", false, true, true, true), + Builder("§2", "§f", "Archi", false, true, false, true), + YouTuber("§5", "§f", "YT", false, false, false, true), + Member("§7", "§7", "", false, false, false, false); private final String colorCode; private final String chatColorCode; + private final String chatPrefix; private final boolean adminGroup; private final boolean teamGroup; private final boolean checkSchematics; private final boolean privilegedMods; - UserGroup(String colorCode, String chatColorCode, boolean adminGroup, boolean teamGroup, boolean checkSchematics, boolean privilegedMods) { + UserGroup(String colorCode, String chatColorCode, String chatPrefix, boolean adminGroup, boolean teamGroup, boolean checkSchematics, boolean privilegedMods) { this.colorCode = colorCode; this.chatColorCode = chatColorCode; + this.chatPrefix = chatPrefix; this.adminGroup = adminGroup; this.teamGroup = teamGroup; this.checkSchematics = checkSchematics; @@ -75,4 +77,8 @@ public enum UserGroup { public static UserGroup getUsergroup(String name) { return Arrays.stream(UserGroup.values()).filter(userGroup -> userGroup.name().equalsIgnoreCase(name)).collect(Collectors.toList()).get(0); } + + public String getChatPrefix() { + return chatPrefix; + } } \ No newline at end of file From b6e1e80b000c18b9d5df8e134912dab4e2cd4aff Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 22:04:23 +0100 Subject: [PATCH 03/70] Update stuff --- .../comms/handlers/FightEndsHandler.java | 14 ++++++++ src/de/steamwar/bungeecore/sql/SchemNode.java | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/de/steamwar/bungeecore/sql/SchemNode.java diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 1592fccc..9b3e64a6 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -30,5 +30,19 @@ public class FightEndsHandler implements SpigotHandler { public void handle(ByteArrayDataInput in, ServerInfo info) { FightEndsPacket fightEndsPacket = new FightEndsPacket(in); + double playerRatio = Math.min(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()) / (double )Math.max(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()); + if (playerRatio >= 0.4) { + return; + } + + double blueResult; + if (fightEndsPacket.getWin() == 0) { + blueResult = 0.5; + } else if (fightEndsPacket.getWin() == 1) { + blueResult = 1; + } else { + blueResult = 0; + } + } } diff --git a/src/de/steamwar/bungeecore/sql/SchemNode.java b/src/de/steamwar/bungeecore/sql/SchemNode.java new file mode 100644 index 00000000..c08c06b2 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchemNode.java @@ -0,0 +1,32 @@ +/* + * 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.bungeecore.sql; + +public class SchemNode { + private SchemNode() {} + + private static final Statement getSchemOwner = new Statement("SELECT NodeOwner FROM SchematicNode WHERE NodeId = ?"); + + public static int getSchematicOwner(int schemId) { + return getSchemOwner.select(rs -> { + return rs.getInt("NodeOwner"); + }, schemId); + } +} From 64cb91a454834ec810e3cd908cb4a26b25e8427d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 22:15:20 +0100 Subject: [PATCH 04/70] Add elo calculations --- .../comms/handlers/FightEndsHandler.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 9b3e64a6..d64c9a22 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -20,17 +20,28 @@ package de.steamwar.bungeecore.comms.handlers; import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.ArenaMode; import de.steamwar.bungeecore.comms.SpigotHandler; import de.steamwar.bungeecore.comms.packets.FightEndsPacket; +import de.steamwar.bungeecore.sql.SchemElo; +import de.steamwar.bungeecore.sql.SchemNode; +import de.steamwar.bungeecore.sql.SchematicType; +import de.steamwar.bungeecore.sql.UserElo; import net.md_5.bungee.api.config.ServerInfo; public class FightEndsHandler implements SpigotHandler { + private int K = 20; + @Override public void handle(ByteArrayDataInput in, ServerInfo info) { FightEndsPacket fightEndsPacket = new FightEndsPacket(in); - double playerRatio = Math.min(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()) / (double )Math.max(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()); + if (!ArenaMode.getBySchemType(SchematicType.fromDB(fightEndsPacket.getGameMode())).isRanked()) { + return; + } + + double playerRatio = 1 - (Math.min(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()) / (double )Math.max(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size())); if (playerRatio >= 0.4) { return; } @@ -44,5 +55,31 @@ public class FightEndsHandler implements SpigotHandler { blueResult = 0; } + int blueSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getBlueSchem()); + int redSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getRedSchem()); + + if ((blueSchemOwner == 0 && redSchemOwner != 0) || (blueSchemOwner != 0 && redSchemOwner == 0)) { + return; + } + + int blueSchemElo = SchemElo.getElo(fightEndsPacket.getBlueSchem()); + int redSchemElo = SchemElo.getElo(fightEndsPacket.getRedSchem()); + + double blueWinExpectation = 1 / (1 + Math.pow(10, (redSchemElo - blueSchemElo) / 600f)); + double redWinExpectation = 1 / (1 + Math.pow(10, (blueSchemElo - redSchemElo) / 600f)); + + SchemElo.setElo(fightEndsPacket.getBlueSchem(), (int) Math.round(blueSchemElo + K * (blueResult - blueWinExpectation))); + SchemElo.setElo(fightEndsPacket.getRedSchem(), (int) Math.round(redSchemElo + K * (1 - blueResult - redWinExpectation))); + + if (blueSchemOwner != 0 && redSchemOwner != 0) { + for (int bluePlayer : fightEndsPacket.getBluePlayers()) { + int playerElo = UserElo.getElo(bluePlayer, fightEndsPacket.getGameMode()); + UserElo.setElo(bluePlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (blueResult - blueWinExpectation))); + } + for (int redPlayer : fightEndsPacket.getRedPlayers()) { + int playerElo = UserElo.getElo(redPlayer, fightEndsPacket.getGameMode()); + UserElo.setElo(redPlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (1 - blueResult - redWinExpectation))); + } + } } } From 7be744eb4595fce43f968b61361b7088bba51f2b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 22:15:49 +0100 Subject: [PATCH 05/70] Update elo calculations --- .../comms/handlers/FightEndsHandler.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index d64c9a22..bf65892b 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -46,6 +46,13 @@ public class FightEndsHandler implements SpigotHandler { return; } + int blueSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getBlueSchem()); + int redSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getRedSchem()); + + if ((blueSchemOwner == 0 && redSchemOwner != 0) || (blueSchemOwner != 0 && redSchemOwner == 0)) { + return; + } + double blueResult; if (fightEndsPacket.getWin() == 0) { blueResult = 0.5; @@ -55,13 +62,6 @@ public class FightEndsHandler implements SpigotHandler { blueResult = 0; } - int blueSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getBlueSchem()); - int redSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getRedSchem()); - - if ((blueSchemOwner == 0 && redSchemOwner != 0) || (blueSchemOwner != 0 && redSchemOwner == 0)) { - return; - } - int blueSchemElo = SchemElo.getElo(fightEndsPacket.getBlueSchem()); int redSchemElo = SchemElo.getElo(fightEndsPacket.getRedSchem()); From 530171c2e32cd28eb0caa4a646ec59e522b9ad07 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 10:12:03 +0100 Subject: [PATCH 06/70] Update UserElo --- .../bungeecore/comms/handlers/FightEndsHandler.java | 4 ++-- src/de/steamwar/bungeecore/sql/UserElo.java | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index bf65892b..d98ddd3d 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -73,11 +73,11 @@ public class FightEndsHandler implements SpigotHandler { if (blueSchemOwner != 0 && redSchemOwner != 0) { for (int bluePlayer : fightEndsPacket.getBluePlayers()) { - int playerElo = UserElo.getElo(bluePlayer, fightEndsPacket.getGameMode()); + int playerElo = UserElo.getElo(bluePlayer, fightEndsPacket.getGameMode()).orElse(1000); UserElo.setElo(bluePlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (blueResult - blueWinExpectation))); } for (int redPlayer : fightEndsPacket.getRedPlayers()) { - int playerElo = UserElo.getElo(redPlayer, fightEndsPacket.getGameMode()); + int playerElo = UserElo.getElo(redPlayer, fightEndsPacket.getGameMode()).orElse(1000); UserElo.setElo(redPlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (1 - blueResult - redWinExpectation))); } } diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 1d45f44f..c2c4be75 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -19,6 +19,8 @@ package de.steamwar.bungeecore.sql; +import java.util.Optional; + public class UserElo { private UserElo() { } @@ -27,15 +29,15 @@ public class UserElo { private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); - public static int getElo(int userID, String gameMode) { + public static Optional getElo(int userID, String gameMode) { return getElo(Season.getSeason(), userID, gameMode); } - public static int getElo(int season, int userID, String gameMode) { + public static Optional getElo(int season, int userID, String gameMode) { return elo.select(rs -> { if (rs.next()) - return rs.getInt("Elo"); - return 1000; + return Optional.of(rs.getInt("Elo")); + return Optional.empty(); }, userID, gameMode, season); } From 3ee878f26e42b813f9ab70e3a92b4b37383260c0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 10:53:16 +0100 Subject: [PATCH 07/70] Update UserElo Update BungeeCore --- src/de/steamwar/bungeecore/BungeeCore.java | 6 +- src/de/steamwar/bungeecore/sql/UserElo.java | 77 +++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index b7338afa..dc351398 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -25,10 +25,7 @@ import de.steamwar.bungeecore.commands.*; import de.steamwar.bungeecore.comms.SpigotReceiver; import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.mods.*; -import de.steamwar.bungeecore.sql.Punishment; -import de.steamwar.bungeecore.sql.Statement; -import de.steamwar.bungeecore.sql.SteamwarUser; -import de.steamwar.bungeecore.sql.Team; +import de.steamwar.bungeecore.sql.*; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; @@ -156,6 +153,7 @@ public class BungeeCore extends Plugin { getProxy().getScheduler().schedule(this, () -> { SteamwarUser.clearCache(); + UserElo.clearCache(); Team.clearCache(); }, 1, 1, TimeUnit.HOURS); diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index c2c4be75..cdfeced7 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -19,21 +19,61 @@ package de.steamwar.bungeecore.sql; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; + +import java.util.HashMap; +import java.util.Map; import java.util.Optional; public class UserElo { private UserElo() { } + private static Integer cachedSeason = null; + private static final Map> userEloCache = new HashMap<>(); + private static final Map maxEloCache = new HashMap<>(); + + @AllArgsConstructor + @EqualsAndHashCode + private static class Key { + private final int userId; + private final String gameMode; + } + private static final Statement elo = new Statement("SELECT UserElo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); + private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); + + private static void clearCacheIfNeeded(int season) { + int currentSeason = Season.getSeason(); + if (currentSeason != season) { + return; + } + if (cachedSeason != null && cachedSeason != currentSeason) { + clearCache(); + cachedSeason = currentSeason; + } + return; + } public static Optional getElo(int userID, String gameMode) { return getElo(Season.getSeason(), userID, gameMode); } public static Optional getElo(int season, int userID, String gameMode) { + clearCacheIfNeeded(season); + if (season == Season.getSeason()) { + Key key = new Key(userID, gameMode); + return userEloCache.computeIfAbsent(key, k -> { + return elo.select(rs -> { + if (rs.next()) + return Optional.of(rs.getInt("UserElo")); + return Optional.empty(); + }); + }); + } return elo.select(rs -> { if (rs.next()) return Optional.of(rs.getInt("Elo")); @@ -41,11 +81,43 @@ public class UserElo { }, userID, gameMode, season); } + public static int getMaxElo(String gameMode) { + return getMaxElo(Season.getSeason(), gameMode); + } + + public static int getMaxElo(int season, String gameMode) { + clearCacheIfNeeded(season); + if (season == Season.getSeason()) { + return maxEloCache.computeIfAbsent(gameMode, gm -> { + return maxElo.select(rs -> { + if (rs.next()) + return rs.getInt("MaxElo"); + return -1; + }); + }); + } + return maxElo.select(rs -> { + if (rs.next()) + return rs.getInt("MaxElo"); + return -1; + }, season, gameMode); + } + public static void setElo(int userId, String gameMode, int elo) { setElo(Season.getSeason(), userId, gameMode, elo); } public static void setElo(int season, int userId, String gameMode, int elo) { + clearCacheIfNeeded(season); + if (season == Season.getSeason()) { + Key key = new Key(userId, gameMode); + userEloCache.put(key, Optional.of(elo)); + maxEloCache.compute(gameMode, (gm, max) -> { + if (max == null || max < elo) + return elo; + return max; + }); + } setElo.update(elo, season, userId, gameMode); } @@ -60,4 +132,9 @@ public class UserElo { return -1; }, gameMode, elo, season); } + + public static void clearCache() { + userEloCache.clear(); + maxEloCache.clear(); + } } From e26692fdd23fc22147faffaf1fca670b0dcb1872 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 11:12:30 +0100 Subject: [PATCH 08/70] Add UserElo.getFightsOfSeason Add Season.getSeasonStart --- src/de/steamwar/bungeecore/sql/Season.java | 5 +++++ src/de/steamwar/bungeecore/sql/UserElo.java | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/Season.java b/src/de/steamwar/bungeecore/sql/Season.java index b1d6bdc4..86dd01c7 100644 --- a/src/de/steamwar/bungeecore/sql/Season.java +++ b/src/de/steamwar/bungeecore/sql/Season.java @@ -30,6 +30,11 @@ public class Season { return (calendar.get(Calendar.YEAR) * 3 + yearIndex); } + public static String getSeasonStart() { + Calendar calendar = Calendar.getInstance(); + return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) / 3 * 3 + 1) + "-1"; + } + public static String convertSeasonToString(int season){ if (season == -1) return ""; int yearSeason = season % 3; diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index cdfeced7..9c5fe897 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -45,6 +45,7 @@ public class UserElo { private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); + private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration > UNIX_TIMESTAMP(?)"); private static void clearCacheIfNeeded(int season) { int currentSeason = Season.getSeason(); @@ -55,7 +56,6 @@ public class UserElo { clearCache(); cachedSeason = currentSeason; } - return; } public static Optional getElo(int userID, String gameMode) { @@ -81,6 +81,14 @@ public class UserElo { }, userID, gameMode, season); } + public static int getFightsOfSeason(int userID, String gameMode) { + return fightsOfSeason.select(rs -> { + if (rs.next()) + return rs.getInt("Fights"); + return 0; + }, userID, gameMode, Season.getSeasonStart()); + } + public static int getMaxElo(String gameMode) { return getMaxElo(Season.getSeason(), gameMode); } From e570b3544d17ddf0998089ca52e830d900da679d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 11:21:23 +0100 Subject: [PATCH 09/70] Add Emblem to Chat --- .../bungeecore/listeners/ChatListener.java | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 96c628a4..b34c0b55 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -23,10 +23,8 @@ import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.bot.SteamwarDiscordBot; import de.steamwar.bungeecore.commands.TpCommand; import de.steamwar.bungeecore.comms.packets.PingPacket; -import de.steamwar.bungeecore.sql.Punishment; -import de.steamwar.bungeecore.sql.SteamwarUser; -import de.steamwar.bungeecore.sql.Team; -import de.steamwar.bungeecore.sql.UserGroup; +import de.steamwar.bungeecore.sql.*; +import lombok.val; import net.md_5.bungee.api.*; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -39,6 +37,7 @@ import net.md_5.bungee.event.EventHandler; import java.sql.Timestamp; import java.time.Instant; import java.util.List; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -200,7 +199,7 @@ public class ChatListener extends BasicListener { } private void publicChat(SteamwarUser user, ProxiedPlayer sender, String message){ - String name = sender.getDisplayName(); + String name = getEmblem(user) + sender.getDisplayName(); String chatcolor = user.getUserGroup().getChatColorCode(); if(user.getUserGroup() != UserGroup.Member || user.getTeam() == 12 || user.getTeam() == 285 || user.getTeam() == 54) @@ -228,6 +227,35 @@ public class ChatListener extends BasicListener { BungeeCore.log(sender.getServer().getInfo(), msg); } + private static String getEmblem(SteamwarUser user) { + int maxEloOfPlayer = 0; + ArenaMode arenaMode = null; + for (ArenaMode mode : ArenaMode.getAllModes()) { + if (!mode.isRanked()) continue; + if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) continue; + + Optional currentElo = UserElo.getElo(user.getId(), mode.getSchemType()); + if (currentElo.isPresent() && currentElo.get() > maxEloOfPlayer) { + maxEloOfPlayer = currentElo.get(); + arenaMode = mode; + } + } + + if (arenaMode == null) return ""; + int maxEloOfGameMode = UserElo.getMaxElo(arenaMode.getSchemType()); + return getEmblem(maxEloOfPlayer, maxEloOfGameMode); + } + + private static String getEmblem(int maxEloOfPlayer, int maxEloOfGameMode) { + if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§c✹ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.84) return "§b✸ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.69) return "§a✷ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.49) return "§e✶ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.26) return "§f✦ "; + return "§7✧ "; + } + private void localChat(ChatEvent e, String [] command){ ProxiedPlayer sender = (ProxiedPlayer) e.getSender(); BungeeCore.log(sender, e.getMessage()); From 201174497e4ce65b6f45f47491ccafaabbc3785d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 12:05:23 +0100 Subject: [PATCH 10/70] Fix UserElo --- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 9c5fe897..a056eb3f 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -45,7 +45,7 @@ public class UserElo { private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); - private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration > UNIX_TIMESTAMP(?)"); + private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); private static void clearCacheIfNeeded(int season) { int currentSeason = Season.getSeason(); From 4e5bd18ef9c694123b08cfab1f773ab1692ea93a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 13:20:57 +0100 Subject: [PATCH 11/70] Update some stuff --- .../comms/handlers/FightEndsHandler.java | 35 +++-- .../bungeecore/listeners/ChatListener.java | 31 +--- src/de/steamwar/bungeecore/sql/SchemElo.java | 1 - src/de/steamwar/bungeecore/sql/SchemNode.java | 32 ---- src/de/steamwar/bungeecore/sql/UserElo.java | 139 +++++++++--------- 5 files changed, 89 insertions(+), 149 deletions(-) delete mode 100644 src/de/steamwar/bungeecore/sql/SchemNode.java diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index d98ddd3d..86d45fe4 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -24,11 +24,13 @@ import de.steamwar.bungeecore.ArenaMode; import de.steamwar.bungeecore.comms.SpigotHandler; import de.steamwar.bungeecore.comms.packets.FightEndsPacket; import de.steamwar.bungeecore.sql.SchemElo; -import de.steamwar.bungeecore.sql.SchemNode; +import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SchematicType; import de.steamwar.bungeecore.sql.UserElo; import net.md_5.bungee.api.config.ServerInfo; +import java.util.List; + public class FightEndsHandler implements SpigotHandler { private int K = 20; @@ -41,15 +43,17 @@ public class FightEndsHandler implements SpigotHandler { return; } - double playerRatio = 1 - (Math.min(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()) / (double )Math.max(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size())); - if (playerRatio >= 0.4) { + int bluePlayerSize = fightEndsPacket.getBluePlayers().size(); + int redPlayerSize = fightEndsPacket.getRedPlayers().size(); + double playerRatio = bluePlayerSize > redPlayerSize ? (double) redPlayerSize / bluePlayerSize : (double) bluePlayerSize / redPlayerSize; + if (playerRatio < 0.6) { return; } - int blueSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getBlueSchem()); - int redSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getRedSchem()); + boolean bluePublic = SchematicNode.getSchematicNode(fightEndsPacket.getBlueSchem()).getId() == 0; + boolean redPublic = SchematicNode.getSchematicNode(fightEndsPacket.getRedSchem()).getId() == 0; - if ((blueSchemOwner == 0 && redSchemOwner != 0) || (blueSchemOwner != 0 && redSchemOwner == 0)) { + if (bluePublic ^ redPublic) { return; } @@ -71,15 +75,16 @@ public class FightEndsHandler implements SpigotHandler { SchemElo.setElo(fightEndsPacket.getBlueSchem(), (int) Math.round(blueSchemElo + K * (blueResult - blueWinExpectation))); SchemElo.setElo(fightEndsPacket.getRedSchem(), (int) Math.round(redSchemElo + K * (1 - blueResult - redWinExpectation))); - if (blueSchemOwner != 0 && redSchemOwner != 0) { - for (int bluePlayer : fightEndsPacket.getBluePlayers()) { - int playerElo = UserElo.getElo(bluePlayer, fightEndsPacket.getGameMode()).orElse(1000); - UserElo.setElo(bluePlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (blueResult - blueWinExpectation))); - } - for (int redPlayer : fightEndsPacket.getRedPlayers()) { - int playerElo = UserElo.getElo(redPlayer, fightEndsPacket.getGameMode()).orElse(1000); - UserElo.setElo(redPlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (1 - blueResult - redWinExpectation))); - } + if (!bluePublic) { + calculateEloOfTeam(fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode(), blueResult - blueWinExpectation); + calculateEloOfTeam(fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode(), 1 - blueResult - blueWinExpectation); + } + } + + private void calculateEloOfTeam(List team, String gameMode, double factor) { + for (int player : team) { + int playerElo = UserElo.getElo(player, gameMode).orElse(1000); + UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * factor)); } } } diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index b34c0b55..277591b4 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -199,7 +199,7 @@ public class ChatListener extends BasicListener { } private void publicChat(SteamwarUser user, ProxiedPlayer sender, String message){ - String name = getEmblem(user) + sender.getDisplayName(); + String name = UserElo.getEmblem(user) + sender.getDisplayName(); String chatcolor = user.getUserGroup().getChatColorCode(); if(user.getUserGroup() != UserGroup.Member || user.getTeam() == 12 || user.getTeam() == 285 || user.getTeam() == 54) @@ -227,35 +227,6 @@ public class ChatListener extends BasicListener { BungeeCore.log(sender.getServer().getInfo(), msg); } - private static String getEmblem(SteamwarUser user) { - int maxEloOfPlayer = 0; - ArenaMode arenaMode = null; - for (ArenaMode mode : ArenaMode.getAllModes()) { - if (!mode.isRanked()) continue; - if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) continue; - - Optional currentElo = UserElo.getElo(user.getId(), mode.getSchemType()); - if (currentElo.isPresent() && currentElo.get() > maxEloOfPlayer) { - maxEloOfPlayer = currentElo.get(); - arenaMode = mode; - } - } - - if (arenaMode == null) return ""; - int maxEloOfGameMode = UserElo.getMaxElo(arenaMode.getSchemType()); - return getEmblem(maxEloOfPlayer, maxEloOfGameMode); - } - - private static String getEmblem(int maxEloOfPlayer, int maxEloOfGameMode) { - if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§c✹ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.84) return "§b✸ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.69) return "§a✷ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.49) return "§e✶ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.26) return "§f✦ "; - return "§7✧ "; - } - private void localChat(ChatEvent e, String [] command){ ProxiedPlayer sender = (ProxiedPlayer) e.getSender(); BungeeCore.log(sender, e.getMessage()); diff --git a/src/de/steamwar/bungeecore/sql/SchemElo.java b/src/de/steamwar/bungeecore/sql/SchemElo.java index d5253cf0..5fa8d892 100644 --- a/src/de/steamwar/bungeecore/sql/SchemElo.java +++ b/src/de/steamwar/bungeecore/sql/SchemElo.java @@ -24,7 +24,6 @@ public class SchemElo { private static final Statement elo = new Statement("SELECT SchemElo FROM Elo WHERE SchemID = ? AND Season = ?"); private static final Statement setElo = new Statement("UPDATE SchemElo SET Elo = ? WHERE Season = ? AND SchemID = ?"); - // private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM SchemElo WHERE Elo > ? AND Season = ?"); public static int getElo(int schemID) { return getElo(Season.getSeason(), schemID); diff --git a/src/de/steamwar/bungeecore/sql/SchemNode.java b/src/de/steamwar/bungeecore/sql/SchemNode.java deleted file mode 100644 index c08c06b2..00000000 --- a/src/de/steamwar/bungeecore/sql/SchemNode.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.bungeecore.sql; - -public class SchemNode { - private SchemNode() {} - - private static final Statement getSchemOwner = new Statement("SELECT NodeOwner FROM SchematicNode WHERE NodeId = ?"); - - public static int getSchematicOwner(int schemId) { - return getSchemOwner.select(rs -> { - return rs.getInt("NodeOwner"); - }, schemId); - } -} diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index a056eb3f..ddeae96a 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -19,6 +19,7 @@ package de.steamwar.bungeecore.sql; +import de.steamwar.bungeecore.ArenaMode; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; @@ -30,9 +31,9 @@ public class UserElo { private UserElo() { } - private static Integer cachedSeason = null; private static final Map> userEloCache = new HashMap<>(); private static final Map maxEloCache = new HashMap<>(); + private static final Map emblemCache = new HashMap<>(); @AllArgsConstructor @EqualsAndHashCode @@ -47,38 +48,15 @@ public class UserElo { private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); - private static void clearCacheIfNeeded(int season) { - int currentSeason = Season.getSeason(); - if (currentSeason != season) { - return; - } - if (cachedSeason != null && cachedSeason != currentSeason) { - clearCache(); - cachedSeason = currentSeason; - } - } - public static Optional getElo(int userID, String gameMode) { - return getElo(Season.getSeason(), userID, gameMode); - } - - public static Optional getElo(int season, int userID, String gameMode) { - clearCacheIfNeeded(season); - if (season == Season.getSeason()) { - Key key = new Key(userID, gameMode); - return userEloCache.computeIfAbsent(key, k -> { - return elo.select(rs -> { - if (rs.next()) - return Optional.of(rs.getInt("UserElo")); - return Optional.empty(); - }); - }); - } - return elo.select(rs -> { - if (rs.next()) - return Optional.of(rs.getInt("Elo")); - return Optional.empty(); - }, userID, gameMode, season); + Key key = new Key(userID, gameMode); + return userEloCache.computeIfAbsent(key, k -> { + return elo.select(rs -> { + if (rs.next()) + return Optional.of(rs.getInt("Elo")); + return Optional.empty(); + }, userID, gameMode, Season.getSeason()); + }); } public static int getFightsOfSeason(int userID, String gameMode) { @@ -90,59 +68,78 @@ public class UserElo { } public static int getMaxElo(String gameMode) { - return getMaxElo(Season.getSeason(), gameMode); - } - - public static int getMaxElo(int season, String gameMode) { - clearCacheIfNeeded(season); - if (season == Season.getSeason()) { - return maxEloCache.computeIfAbsent(gameMode, gm -> { - return maxElo.select(rs -> { - if (rs.next()) - return rs.getInt("MaxElo"); - return -1; - }); - }); - } - return maxElo.select(rs -> { - if (rs.next()) - return rs.getInt("MaxElo"); - return -1; - }, season, gameMode); + return maxEloCache.computeIfAbsent(gameMode, gm -> { + return maxElo.select(rs -> { + if (rs.next()) + return rs.getInt("MaxElo"); + return 0; + }, Season.getSeason(), gameMode); + }); } public static void setElo(int userId, String gameMode, int elo) { - setElo(Season.getSeason(), userId, gameMode, elo); - } - - public static void setElo(int season, int userId, String gameMode, int elo) { - clearCacheIfNeeded(season); - if (season == Season.getSeason()) { - Key key = new Key(userId, gameMode); - userEloCache.put(key, Optional.of(elo)); - maxEloCache.compute(gameMode, (gm, max) -> { - if (max == null || max < elo) - return elo; - return max; - }); - } - setElo.update(elo, season, userId, gameMode); + emblemCache.remove(userId); + Key key = new Key(userId, gameMode); + userEloCache.put(key, Optional.of(elo)); + maxEloCache.compute(gameMode, (gm, max) -> { + if (max == null || max < elo) { + emblemCache.clear(); + return elo; + } + return max; + }); + setElo.update(elo, Season.getSeason(), userId, gameMode); } public static int getPlacement(int elo, String gameMode) { - return getPlacement(Season.getSeason(), elo, gameMode); - } - - public static int getPlacement(int season, int elo, String gameMode) { return place.select(rs -> { if (rs.next()) return rs.getInt("Place"); return -1; - }, gameMode, elo, season); + }, gameMode, elo, Season.getSeason()); + } + + public static String getEmblem(SteamwarUser user) { + if (emblemCache.containsKey(user.getId())) { + return emblemCache.get(user.getId()); + } + + int maxEloOfPlayer = 0; + ArenaMode arenaMode = null; + for (ArenaMode mode : ArenaMode.getAllModes()) { + if (!mode.isRanked()) continue; + if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) continue; + + Optional currentElo = UserElo.getElo(user.getId(), mode.getSchemType()); + if (currentElo.isPresent() && currentElo.get() > maxEloOfPlayer) { + maxEloOfPlayer = currentElo.get(); + arenaMode = mode; + } + } + + if (arenaMode == null) { + emblemCache.put(user.getId(), ""); + return ""; + } + int maxEloOfGameMode = UserElo.getMaxElo(arenaMode.getSchemType()); + String emblem = getEmblem(maxEloOfPlayer, maxEloOfGameMode); + emblemCache.put(user.getId(), emblem); + return emblem; + } + + private static String getEmblem(int maxEloOfPlayer, int maxEloOfGameMode) { + if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂"; + if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§c✹"; + if (maxEloOfPlayer > maxEloOfGameMode * 0.84) return "§b✸"; + if (maxEloOfPlayer > maxEloOfGameMode * 0.69) return "§a✷"; + if (maxEloOfPlayer > maxEloOfGameMode * 0.49) return "§e✶"; + if (maxEloOfPlayer > maxEloOfGameMode * 0.26) return "§f✦"; + return "§7✧"; } public static void clearCache() { userEloCache.clear(); maxEloCache.clear(); + emblemCache.clear(); } } From 68ba6a26f7c6c42c8d446ba9c617091078d48b4b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 13:23:50 +0100 Subject: [PATCH 12/70] Update some stuff --- .../steamwar/bungeecore/comms/handlers/FightEndsHandler.java | 4 +++- src/de/steamwar/bungeecore/sql/SchemElo.java | 4 +++- src/de/steamwar/bungeecore/sql/UserElo.java | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 86d45fe4..d54d2390 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -31,6 +31,8 @@ import net.md_5.bungee.api.config.ServerInfo; import java.util.List; +import static de.steamwar.bungeecore.sql.UserElo.ELO_DEFAULT; + public class FightEndsHandler implements SpigotHandler { private int K = 20; @@ -83,7 +85,7 @@ public class FightEndsHandler implements SpigotHandler { private void calculateEloOfTeam(List team, String gameMode, double factor) { for (int player : team) { - int playerElo = UserElo.getElo(player, gameMode).orElse(1000); + int playerElo = UserElo.getElo(player, gameMode).orElse(ELO_DEFAULT); UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * factor)); } } diff --git a/src/de/steamwar/bungeecore/sql/SchemElo.java b/src/de/steamwar/bungeecore/sql/SchemElo.java index 5fa8d892..378d2db3 100644 --- a/src/de/steamwar/bungeecore/sql/SchemElo.java +++ b/src/de/steamwar/bungeecore/sql/SchemElo.java @@ -19,6 +19,8 @@ package de.steamwar.bungeecore.sql; +import static de.steamwar.bungeecore.sql.UserElo.ELO_DEFAULT; + public class SchemElo { private SchemElo() {} @@ -33,7 +35,7 @@ public class SchemElo { return elo.select(rs -> { if (rs.next()) return rs.getInt("Elo"); - return 1000; + return ELO_DEFAULT; }, schemID, season); } diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index ddeae96a..8593cae1 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -31,6 +31,8 @@ public class UserElo { private UserElo() { } + public static final int ELO_DEFAULT = 1000; + private static final Map> userEloCache = new HashMap<>(); private static final Map maxEloCache = new HashMap<>(); private static final Map emblemCache = new HashMap<>(); From 0b452d30c3922e7402743c90c0aa25d62edd9f32 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 13:27:18 +0100 Subject: [PATCH 13/70] Update UserGroup Builder nach Absprache mit denen --- src/de/steamwar/bungeecore/sql/UserGroup.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/UserGroup.java b/src/de/steamwar/bungeecore/sql/UserGroup.java index e5abd97b..6ca7a9ae 100644 --- a/src/de/steamwar/bungeecore/sql/UserGroup.java +++ b/src/de/steamwar/bungeecore/sql/UserGroup.java @@ -28,7 +28,7 @@ public enum UserGroup { Developer("§3", "§f", "Dev", true, true, true, true), Moderator("§c", "§f", "Mod", true, true, true, true), Supporter("§9", "§f", "Sup", false, true, true, true), - Builder("§2", "§f", "Archi", false, true, false, true), + Builder("§2", "§f", "Arch", false, true, false, true), YouTuber("§5", "§f", "YT", false, false, false, true), Member("§7", "§7", "", false, false, false, false); From 2923d249dbc602fa99aeae05ff76b8de3534e05d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 13:31:38 +0100 Subject: [PATCH 14/70] Update UserElo.gameModeUserEloCache --- src/de/steamwar/bungeecore/sql/UserElo.java | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 8593cae1..ff49289e 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -20,8 +20,6 @@ package de.steamwar.bungeecore.sql; import de.steamwar.bungeecore.ArenaMode; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; import java.util.HashMap; import java.util.Map; @@ -33,17 +31,10 @@ public class UserElo { public static final int ELO_DEFAULT = 1000; - private static final Map> userEloCache = new HashMap<>(); + private static final Map>> gameModeUserEloCache = new HashMap<>(); private static final Map maxEloCache = new HashMap<>(); private static final Map emblemCache = new HashMap<>(); - @AllArgsConstructor - @EqualsAndHashCode - private static class Key { - private final int userId; - private final String gameMode; - } - private static final Statement elo = new Statement("SELECT UserElo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); @@ -51,8 +42,9 @@ public class UserElo { private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); public static Optional getElo(int userID, String gameMode) { - Key key = new Key(userID, gameMode); - return userEloCache.computeIfAbsent(key, k -> { + return gameModeUserEloCache.computeIfAbsent(gameMode, gm -> { + return new HashMap<>(); + }).computeIfAbsent(userID, uid -> { return elo.select(rs -> { if (rs.next()) return Optional.of(rs.getInt("Elo")); @@ -81,8 +73,7 @@ public class UserElo { public static void setElo(int userId, String gameMode, int elo) { emblemCache.remove(userId); - Key key = new Key(userId, gameMode); - userEloCache.put(key, Optional.of(elo)); + gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).put(userId, Optional.of(elo)); maxEloCache.compute(gameMode, (gm, max) -> { if (max == null || max < elo) { emblemCache.clear(); @@ -140,7 +131,7 @@ public class UserElo { } public static void clearCache() { - userEloCache.clear(); + gameModeUserEloCache.clear(); maxEloCache.clear(); emblemCache.clear(); } From 0d7ebc224c34222a6d6a5c24409c7c5258a3d234 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 16:45:00 +0100 Subject: [PATCH 15/70] Update FightEndsHandler Update UserElo --- .../steamwar/bungeecore/comms/handlers/FightEndsHandler.java | 2 +- src/de/steamwar/bungeecore/sql/UserElo.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index d54d2390..ee752877 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -85,7 +85,7 @@ public class FightEndsHandler implements SpigotHandler { private void calculateEloOfTeam(List team, String gameMode, double factor) { for (int player : team) { - int playerElo = UserElo.getElo(player, gameMode).orElse(ELO_DEFAULT); + int playerElo = UserElo.getEloOrDefault(player, gameMode); UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * factor)); } } diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index ff49289e..a216673d 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -41,6 +41,10 @@ public class UserElo { private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); + public static int getEloOrDefault(int userID, String gameMode) { + return getElo(userID, gameMode).orElse(ELO_DEFAULT); + } + public static Optional getElo(int userID, String gameMode) { return gameModeUserEloCache.computeIfAbsent(gameMode, gm -> { return new HashMap<>(); From febc7035d54cc8aea797577d2fd5e1df4a6d6e1e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 16:54:04 +0100 Subject: [PATCH 16/70] Update imports --- src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java | 2 -- src/de/steamwar/bungeecore/listeners/ChatListener.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index ee752877..63cbd799 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -31,8 +31,6 @@ import net.md_5.bungee.api.config.ServerInfo; import java.util.List; -import static de.steamwar.bungeecore.sql.UserElo.ELO_DEFAULT; - public class FightEndsHandler implements SpigotHandler { private int K = 20; diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 277591b4..54b9bb4d 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -24,7 +24,6 @@ import de.steamwar.bungeecore.bot.SteamwarDiscordBot; import de.steamwar.bungeecore.commands.TpCommand; import de.steamwar.bungeecore.comms.packets.PingPacket; import de.steamwar.bungeecore.sql.*; -import lombok.val; import net.md_5.bungee.api.*; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -37,7 +36,6 @@ import net.md_5.bungee.event.EventHandler; import java.sql.Timestamp; import java.time.Instant; import java.util.List; -import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.logging.Level; From 4f76442193af58c5fb0e7f5889831458f0684607 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 17:08:07 +0100 Subject: [PATCH 17/70] Update UserElo.getEmblem --- src/de/steamwar/bungeecore/sql/UserElo.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index a216673d..a592c1a4 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -125,13 +125,13 @@ public class UserElo { } private static String getEmblem(int maxEloOfPlayer, int maxEloOfGameMode) { - if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂"; - if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§c✹"; - if (maxEloOfPlayer > maxEloOfGameMode * 0.84) return "§b✸"; - if (maxEloOfPlayer > maxEloOfGameMode * 0.69) return "§a✷"; - if (maxEloOfPlayer > maxEloOfGameMode * 0.49) return "§e✶"; - if (maxEloOfPlayer > maxEloOfGameMode * 0.26) return "§f✦"; - return "§7✧"; + if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§c✹ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.84) return "§b✸ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.69) return "§a✷ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.49) return "§e✶ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.26) return "§f✦ "; + return "§7✧ "; } public static void clearCache() { From 5daf7f64a4cdef9f2a13a88aaa06b2611219b694 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 17:32:21 +0100 Subject: [PATCH 18/70] Fix UserElo --- src/de/steamwar/bungeecore/sql/UserElo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index a592c1a4..80a95bcc 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -35,8 +35,8 @@ public class UserElo { private static final Map maxEloCache = new HashMap<>(); private static final Map emblemCache = new HashMap<>(); - private static final Statement elo = new Statement("SELECT UserElo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); - private static final Statement setElo = new Statement("UPDATE UserELo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); + private static final Statement elo = new Statement("SELECT Elo FROM UserElo WHERE UserID = ? AND GameMode = ? AND Season = ?"); + private static final Statement setElo = new Statement("UPDATE UserElo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); From 2bd15d5c4f35e6edd8c1877250bc3bb81749642d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 20:07:15 +0100 Subject: [PATCH 19/70] Update UserElo.getEmblem percentages --- src/de/steamwar/bungeecore/sql/UserElo.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 80a95bcc..a4367fa9 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -126,11 +126,11 @@ public class UserElo { private static String getEmblem(int maxEloOfPlayer, int maxEloOfGameMode) { if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§c✹ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.84) return "§b✸ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.69) return "§a✷ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.49) return "§e✶ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.26) return "§f✦ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.97) return "§c✹ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§b✸ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.88) return "§a✷ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.76) return "§e✶ "; + if (maxEloOfPlayer > maxEloOfGameMode * 0.51) return "§f✦ "; return "§7✧ "; } From e015641086b01af0cb6b3fc0dd39ac19dd697aeb Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 20:13:58 +0100 Subject: [PATCH 20/70] Update FightEndsHandler --- .../comms/handlers/FightEndsHandler.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 63cbd799..97e263c0 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -69,22 +69,16 @@ public class FightEndsHandler implements SpigotHandler { int blueSchemElo = SchemElo.getElo(fightEndsPacket.getBlueSchem()); int redSchemElo = SchemElo.getElo(fightEndsPacket.getRedSchem()); - double blueWinExpectation = 1 / (1 + Math.pow(10, (redSchemElo - blueSchemElo) / 600f)); - double redWinExpectation = 1 / (1 + Math.pow(10, (blueSchemElo - redSchemElo) / 600f)); - - SchemElo.setElo(fightEndsPacket.getBlueSchem(), (int) Math.round(blueSchemElo + K * (blueResult - blueWinExpectation))); - SchemElo.setElo(fightEndsPacket.getRedSchem(), (int) Math.round(redSchemElo + K * (1 - blueResult - redWinExpectation))); - - if (!bluePublic) { - calculateEloOfTeam(fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode(), blueResult - blueWinExpectation); - calculateEloOfTeam(fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode(), 1 - blueResult - blueWinExpectation); - } + calculateEloOfTeam(fightEndsPacket.getBlueSchem(), blueSchemElo, redSchemElo, blueResult, fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode()); + calculateEloOfTeam(fightEndsPacket.getRedSchem(), redSchemElo, blueSchemElo, 1 - blueResult, fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode()); } - private void calculateEloOfTeam(List team, String gameMode, double factor) { - for (int player : team) { + private void calculateEloOfTeam(int schemId, int elo1, int elo2, double result, List players, String gameMode) { + double winExpectation = 1 / (1 + Math.pow(10, (elo1 - elo2) / 600f)); + SchemElo.setElo(schemId, (int) Math.round(elo1 + K * (result - winExpectation))); + for (int player : players) { int playerElo = UserElo.getEloOrDefault(player, gameMode); - UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * factor)); + UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * (result - winExpectation))); } } } From ef320718ef6b5493f049d26a02e3559aaeaf2b9e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 11 Mar 2022 20:14:39 +0100 Subject: [PATCH 21/70] Update FightEndsHandler --- .../bungeecore/comms/handlers/FightEndsHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 97e263c0..50afa5df 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -73,9 +73,9 @@ public class FightEndsHandler implements SpigotHandler { calculateEloOfTeam(fightEndsPacket.getRedSchem(), redSchemElo, blueSchemElo, 1 - blueResult, fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode()); } - private void calculateEloOfTeam(int schemId, int elo1, int elo2, double result, List players, String gameMode) { - double winExpectation = 1 / (1 + Math.pow(10, (elo1 - elo2) / 600f)); - SchemElo.setElo(schemId, (int) Math.round(elo1 + K * (result - winExpectation))); + private void calculateEloOfTeam(int schemId, int eloOwn, int eloEnemy, double result, List players, String gameMode) { + double winExpectation = 1 / (1 + Math.pow(10, (eloOwn - eloEnemy) / 600f)); + SchemElo.setElo(schemId, (int) Math.round(eloOwn + K * (result - winExpectation))); for (int player : players) { int playerElo = UserElo.getEloOrDefault(player, gameMode); UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * (result - winExpectation))); From 555d79d9328769a67d143791ee96a0a521732223 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 11 Mar 2022 23:06:31 +0100 Subject: [PATCH 22/70] Send subserver start packet on lobby Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/EventStarter.java | 2 +- .../steamwar/bungeecore/SubserverSystem.java | 17 +++++++++++++---- .../bungeecore/commands/ArenaCommand.java | 3 ++- .../bungeecore/commands/ChallengeCommand.java | 4 ++-- .../bungeecore/commands/EventCommand.java | 7 ++----- .../bungeecore/commands/FightCommand.java | 2 +- .../bungeecore/commands/HistoricCommand.java | 2 +- .../bungeecore/commands/JoinmeCommand.java | 4 ++-- .../bungeecore/commands/ReplayCommand.java | 2 +- .../bungeecore/commands/TpCommand.java | 4 ++-- .../bungeecore/comms/PacketIdManager.java | 1 + .../comms/handlers/FightInfoHandler.java | 4 ++++ .../comms/packets/StartingServerPacket.java | 18 ++++++++++++++++++ 13 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java diff --git a/src/de/steamwar/bungeecore/EventStarter.java b/src/de/steamwar/bungeecore/EventStarter.java index b1738285..e0e29af9 100644 --- a/src/de/steamwar/bungeecore/EventStarter.java +++ b/src/de/steamwar/bungeecore/EventStarter.java @@ -69,7 +69,7 @@ public class EventStarter implements Runnable { ProxiedPlayer leiter = ProxyServer.getInstance().getPlayer(SteamwarUser.get(next.getKampfleiter()).getUuid()); if(leiter != null) - subserver.sendPlayer(leiter); + SubserverSystem.sendPlayer(subserver, leiter); eventServer.put(blue.getTeamId(), subserver); eventServer.put(red.getTeamId(), subserver); diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index 102b64fd..015393b9 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -19,6 +19,8 @@ package de.steamwar.bungeecore; +import de.steamwar.bungeecore.comms.handlers.FightInfoHandler; +import de.steamwar.bungeecore.comms.packets.StartingServerPacket; import de.steamwar.bungeecore.sql.EventFight; import de.steamwar.bungeecore.sql.IgnoreSystem; import de.steamwar.bungeecore.sql.SteamwarUser; @@ -142,7 +144,7 @@ public class SubserverSystem { } public static void startTestServer(ProxiedPlayer p, ArenaMode m, String map, int checkSchemId, int prepareSchemId){ - startArena(m, map, -1, checkSchemId, prepareSchemId, 0, p.getName() + "s Bau", p.getName(), p.getUniqueId(), null).sendPlayer(p); + sendPlayer(startArena(m, map, -1, checkSchemId, prepareSchemId, 0, p.getName() + "s Bau", p.getName(), p.getUniqueId(), null), p); } private static synchronized void sendToBau(ProxiedPlayer p, UUID owner, String prototype, String worldFolder, String serverJar, String worldDir, String worldName, String xmx, String serverName){ @@ -155,9 +157,10 @@ public class SubserverSystem { Node node = Node.getNode(); int port = bauPorts.freePort(); - new Bauserver(user.getUserName() + "s Bau", owner, port, node.startServer( + + sendPlayer(new Bauserver(user.getUserName() + "s Bau", owner, port, node.startServer( serverJar, directory, worldDir, worldName, port, xmx, "logPath=" + worldName - ), () -> {}).sendPlayer(p); + ), () -> {}), p); } public static void sendToBauServer(ProxiedPlayer p, UUID owner){ @@ -183,10 +186,16 @@ public class SubserverSystem { new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/bau addmember " + p.getName())); } + public static void sendPlayer(Subserver subserver, ProxiedPlayer player) { + subserver.sendPlayer(player); + if(!subserver.hasStarted() && FightInfoHandler.onLobby(player)) + new StartingServerPacket().send(player); + } + private static boolean bauRunning(ProxiedPlayer p, UUID owner){ for(Subserver subserver : Subserver.getServerList()){ if(subserver.getType() == Servertype.BAUSERVER && ((Bauserver)subserver).getOwner().equals(owner)){ - subserver.sendPlayer(p); + sendPlayer(subserver, p); return true; } } diff --git a/src/de/steamwar/bungeecore/commands/ArenaCommand.java b/src/de/steamwar/bungeecore/commands/ArenaCommand.java index 9abd0f5b..d88950f7 100644 --- a/src/de/steamwar/bungeecore/commands/ArenaCommand.java +++ b/src/de/steamwar/bungeecore/commands/ArenaCommand.java @@ -22,6 +22,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.Servertype; import de.steamwar.bungeecore.Subserver; +import de.steamwar.bungeecore.SubserverSystem; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; @@ -51,6 +52,6 @@ public class ArenaCommand extends BasicCommand { return; } - subserver.sendPlayer(player); + SubserverSystem.sendPlayer(subserver, player); } } diff --git a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java index df477c9a..27d9ba73 100644 --- a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java +++ b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java @@ -78,8 +78,8 @@ public class ChallengeCommand extends BasicCommand { Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, 0, 0, null, null, player.getUniqueId(), target.getUniqueId()); - arena.sendPlayer(player); - arena.sendPlayer(target); + SubserverSystem.sendPlayer(arena, player); + SubserverSystem.sendPlayer(arena, target); Message.broadcast("CHALLENGE_BROADCAST", "CHALLENGE_BROADCAST_HOVER", new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/arena " + arena.getServer().getName()), mode.getDisplayName(), player.getName(), target.getName()); diff --git a/src/de/steamwar/bungeecore/commands/EventCommand.java b/src/de/steamwar/bungeecore/commands/EventCommand.java index 680b0a93..79ae6208 100644 --- a/src/de/steamwar/bungeecore/commands/EventCommand.java +++ b/src/de/steamwar/bungeecore/commands/EventCommand.java @@ -19,10 +19,7 @@ package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.BungeeCore; -import de.steamwar.bungeecore.EventStarter; -import de.steamwar.bungeecore.Message; -import de.steamwar.bungeecore.Subserver; +import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.sql.Event; import de.steamwar.bungeecore.sql.EventFight; import de.steamwar.bungeecore.sql.Team; @@ -72,7 +69,7 @@ public class EventCommand extends BasicCommand { return; } - eventArena.sendPlayer(player); + SubserverSystem.sendPlayer(eventArena, player); } private void noCurrentEvent(ProxiedPlayer player){ diff --git a/src/de/steamwar/bungeecore/commands/FightCommand.java b/src/de/steamwar/bungeecore/commands/FightCommand.java index 653db48d..1d2e396f 100644 --- a/src/de/steamwar/bungeecore/commands/FightCommand.java +++ b/src/de/steamwar/bungeecore/commands/FightCommand.java @@ -165,7 +165,7 @@ public class FightCommand extends BasicCommand { public void execute(CommandSender sender, String[] args) { createArena(sender, "/fight ", args, 0, false, (player, mode, map) -> { Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, 0, 0, null, null, player.getUniqueId(), null); - arena.sendPlayer(player); + SubserverSystem.sendPlayer(arena, player); Message.broadcast("FIGHT_BROADCAST", "FIGHT_BROADCAST_HOVER" , new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/arena " + arena.getServer().getName()), mode.getDisplayName(), player.getName()); }); diff --git a/src/de/steamwar/bungeecore/commands/HistoricCommand.java b/src/de/steamwar/bungeecore/commands/HistoricCommand.java index 7e34836c..6c9c0709 100644 --- a/src/de/steamwar/bungeecore/commands/HistoricCommand.java +++ b/src/de/steamwar/bungeecore/commands/HistoricCommand.java @@ -37,7 +37,7 @@ public class HistoricCommand extends BasicCommand { public void execute(CommandSender sender, String[] args) { FightCommand.createArena(sender, "/historic ", args, 0, true, (player, mode, map) -> { Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, 0, 0, null, null, player.getUniqueId(), null); - arena.sendPlayer(player); + SubserverSystem.sendPlayer(arena, player); Message.broadcast("HISTORIC_BROADCAST", "HISTORIC_BROADCAST_HOVER" , new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/arena " + arena.getServer().getName()), mode.getDisplayName(), player.getName()); }); diff --git a/src/de/steamwar/bungeecore/commands/JoinmeCommand.java b/src/de/steamwar/bungeecore/commands/JoinmeCommand.java index 37fb8061..105063c0 100644 --- a/src/de/steamwar/bungeecore/commands/JoinmeCommand.java +++ b/src/de/steamwar/bungeecore/commands/JoinmeCommand.java @@ -59,12 +59,12 @@ public class JoinmeCommand extends BasicCommand { if(subserver != null) { Servertype type = subserver.getType(); if (type == Servertype.ARENA) { - subserver.sendPlayer(player); + SubserverSystem.sendPlayer(subserver, player); } else if (type == Servertype.BAUSERVER) { Bauserver bauserver = (Bauserver) subserver; if (bauserver.getOwner().equals(player.getUniqueId()) || BauweltMember.getBauMember(bauserver.getOwner(), player.getUniqueId()) != null) { - subserver.sendPlayer(player); + SubserverSystem.sendPlayer(subserver, player); } else { SubserverSystem.sendDeniedMessage(player, bauserver.getOwner()); Message.send("JOIN_PLAYER_BLOCK", player); diff --git a/src/de/steamwar/bungeecore/commands/ReplayCommand.java b/src/de/steamwar/bungeecore/commands/ReplayCommand.java index 806df5a3..f5dd44ac 100644 --- a/src/de/steamwar/bungeecore/commands/ReplayCommand.java +++ b/src/de/steamwar/bungeecore/commands/ReplayCommand.java @@ -35,7 +35,7 @@ public class ReplayCommand extends BasicCommand { } ArenaMode mode = fight.getGameMode(); - SubserverSystem.startArena(mode, mode.getRandomMap(), 0, 0, 0, fight.getFightID(), null, null, null, null).sendPlayer(player); + SubserverSystem.sendPlayer(SubserverSystem.startArena(mode, mode.getRandomMap(), 0, 0, 0, fight.getFightID(), null, null, null, null), player); }, page -> Fight.getPage(page, 45).stream().map(fight -> new SWListInv.SWListEntry<>(getFightItem(player, fight), fight)).collect(Collectors.toList())).open(); } diff --git a/src/de/steamwar/bungeecore/commands/TpCommand.java b/src/de/steamwar/bungeecore/commands/TpCommand.java index ae2f9a99..c3a81b25 100644 --- a/src/de/steamwar/bungeecore/commands/TpCommand.java +++ b/src/de/steamwar/bungeecore/commands/TpCommand.java @@ -118,12 +118,12 @@ public class TpCommand extends BasicCommand { if(subserver != null) { Servertype type = subserver.getType(); if (type == Servertype.ARENA) { - subserver.sendPlayer(player); + SubserverSystem.sendPlayer(subserver, player); } else if (type == Servertype.BAUSERVER) { Bauserver bauserver = (Bauserver) subserver; if (bauserver.getOwner().equals(player.getUniqueId()) || BauweltMember.getBauMember(bauserver.getOwner(), player.getUniqueId()) != null) { - subserver.sendPlayer(player); + SubserverSystem.sendPlayer(subserver, player); } else { SubserverSystem.sendDeniedMessage(player, bauserver.getOwner()); Message.send("JOIN_PLAYER_BLOCK", player); diff --git a/src/de/steamwar/bungeecore/comms/PacketIdManager.java b/src/de/steamwar/bungeecore/comms/PacketIdManager.java index 1ee0c27a..8689fc6a 100644 --- a/src/de/steamwar/bungeecore/comms/PacketIdManager.java +++ b/src/de/steamwar/bungeecore/comms/PacketIdManager.java @@ -37,4 +37,5 @@ public class PacketIdManager { public static final byte I_AM_A_LOBBY = 0x20; public static final byte FIGHT_INFO = 0x21; public static final byte FIGHT_ENDS = 0x22; + public static final byte STARTING_SERVER = 0x23; } diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightInfoHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightInfoHandler.java index 998e53e3..eb708dfb 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightInfoHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightInfoHandler.java @@ -38,6 +38,10 @@ public class FightInfoHandler implements SpigotHandler { lobbys.add(lobby); } + public static boolean onLobby(ProxiedPlayer player) { + return lobbys.contains(player.getServer().getInfo()); + } + @Override public void handle(ByteArrayDataInput in, ServerInfo info) { FightInfoPacket packet = new FightInfoPacket(in); diff --git a/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java b/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java new file mode 100644 index 00000000..b7058c2b --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java @@ -0,0 +1,18 @@ +package de.steamwar.bungeecore.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.comms.PacketIdManager; + +public class StartingServerPacket extends BungeePacket { + + @Override + public int getId() { + return PacketIdManager.STARTING_SERVER; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + //nothing to write + } +} From 44660d6f9fba5502e2fa7e2c60b81c14164c1e97 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 11 Mar 2022 23:29:05 +0100 Subject: [PATCH 23/70] Send waiting user Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/SubserverSystem.java | 2 +- .../comms/packets/StartingServerPacket.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index 015393b9..b0085f07 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -189,7 +189,7 @@ public class SubserverSystem { public static void sendPlayer(Subserver subserver, ProxiedPlayer player) { subserver.sendPlayer(player); if(!subserver.hasStarted() && FightInfoHandler.onLobby(player)) - new StartingServerPacket().send(player); + new StartingServerPacket(SteamwarUser.get(player.getUniqueId())).send(player); } private static boolean bauRunning(ProxiedPlayer p, UUID owner){ diff --git a/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java b/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java index b7058c2b..1e5edf15 100644 --- a/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/StartingServerPacket.java @@ -3,16 +3,23 @@ package de.steamwar.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; import de.steamwar.bungeecore.comms.BungeePacket; import de.steamwar.bungeecore.comms.PacketIdManager; +import de.steamwar.bungeecore.sql.SteamwarUser; public class StartingServerPacket extends BungeePacket { + private final int user; + + public StartingServerPacket(SteamwarUser user) { + this.user = user.getId(); + } + @Override public int getId() { return PacketIdManager.STARTING_SERVER; } @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - //nothing to write + public void writeVars(ByteArrayDataOutput out) { + out.writeInt(user); } } From 98e76cee3c5d98eddb9a7e9de200799c3bb62b6e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 13 Mar 2022 07:42:30 +0100 Subject: [PATCH 24/70] Temp disable replay Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/BungeeCore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index b7338afa..aa3e3b37 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -123,7 +123,7 @@ public class BungeeCore extends Plugin { new ListCommand(); new StatCommand(); new VerifyCommand(); - new ReplayCommand(); + //new ReplayCommand(); new GDPRQuery(); new PlaytimeCommand(); new ArenaCommand(); From fc7527453e7249530c21d567d48dd0d1dcfb8a66 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 13 Mar 2022 07:49:47 +0100 Subject: [PATCH 25/70] Temp disable replay Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/BungeeCore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index aa3e3b37..b7338afa 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -123,7 +123,7 @@ public class BungeeCore extends Plugin { new ListCommand(); new StatCommand(); new VerifyCommand(); - //new ReplayCommand(); + new ReplayCommand(); new GDPRQuery(); new PlaytimeCommand(); new ArenaCommand(); From 2de1eee79c8c402c1059c6add967e25a5de98640 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 13 Mar 2022 09:13:29 +0100 Subject: [PATCH 26/70] Fix SchemElo Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/SchemElo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/SchemElo.java b/src/de/steamwar/bungeecore/sql/SchemElo.java index 378d2db3..d14eaecb 100644 --- a/src/de/steamwar/bungeecore/sql/SchemElo.java +++ b/src/de/steamwar/bungeecore/sql/SchemElo.java @@ -24,7 +24,7 @@ import static de.steamwar.bungeecore.sql.UserElo.ELO_DEFAULT; public class SchemElo { private SchemElo() {} - private static final Statement elo = new Statement("SELECT SchemElo FROM Elo WHERE SchemID = ? AND Season = ?"); + private static final Statement elo = new Statement("SELECT Elo FROM SchemElo WHERE SchemID = ? AND Season = ?"); private static final Statement setElo = new Statement("UPDATE SchemElo SET Elo = ? WHERE Season = ? AND SchemID = ?"); public static int getElo(int schemID) { From 1ff92e5d0bf29efd06ee819e162d1d56fcec21ca Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 13 Mar 2022 20:59:11 +0100 Subject: [PATCH 27/70] Fix database insertion Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/SchemElo.java | 4 ++-- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/SchemElo.java b/src/de/steamwar/bungeecore/sql/SchemElo.java index d14eaecb..55df604e 100644 --- a/src/de/steamwar/bungeecore/sql/SchemElo.java +++ b/src/de/steamwar/bungeecore/sql/SchemElo.java @@ -25,7 +25,7 @@ public class SchemElo { private SchemElo() {} private static final Statement elo = new Statement("SELECT Elo FROM SchemElo WHERE SchemID = ? AND Season = ?"); - private static final Statement setElo = new Statement("UPDATE SchemElo SET Elo = ? WHERE Season = ? AND SchemID = ?"); + private static final Statement setElo = new Statement("INSERT INTO SchemElo (Season, SchemID, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); public static int getElo(int schemID) { return getElo(Season.getSeason(), schemID); @@ -44,6 +44,6 @@ public class SchemElo { } public static void setElo(int season, int schemID, int elo) { - setElo.update(elo, season, schemID); + setElo.update(season, schemID, elo); } } diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index a4367fa9..bf95b3c2 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -36,7 +36,7 @@ public class UserElo { private static final Map emblemCache = new HashMap<>(); private static final Statement elo = new Statement("SELECT Elo FROM UserElo WHERE UserID = ? AND GameMode = ? AND Season = ?"); - private static final Statement setElo = new Statement("UPDATE UserElo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); + private static final Statement setElo = new Statement("INSERT INTO UserElo (Season, GameMode, UserID, Elo) SET (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); From d22abd0707589cddd6b0e429ad53248537f20100 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 13 Mar 2022 21:01:57 +0100 Subject: [PATCH 28/70] Fix database insertion Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index bf95b3c2..c61bcf63 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -36,7 +36,7 @@ public class UserElo { private static final Map emblemCache = new HashMap<>(); private static final Statement elo = new Statement("SELECT Elo FROM UserElo WHERE UserID = ? AND GameMode = ? AND Season = ?"); - private static final Statement setElo = new Statement("INSERT INTO UserElo (Season, GameMode, UserID, Elo) SET (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); + private static final Statement setElo = new Statement("INSERT INTO UserElo (Season, GameMode, UserID, Elo) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); From 0964ca78ab702d063ae1fce64324867dc91f55a1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 13 Mar 2022 21:04:22 +0100 Subject: [PATCH 29/70] Fix database insertion Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index c61bcf63..88f0844a 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -85,7 +85,7 @@ public class UserElo { } return max; }); - setElo.update(elo, Season.getSeason(), userId, gameMode); + setElo.update(Season.getSeason(), gameMode, userId, elo); } public static int getPlacement(int elo, String gameMode) { From 9bf2e6e3544fcf911244e0c38ba633f88a218e7c Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 14 Mar 2022 11:34:41 +0100 Subject: [PATCH 30/70] New replay available calc + admin replay Signed-off-by: Lixfel --- .../bungeecore/commands/ReplayCommand.java | 12 ++++++--- src/de/steamwar/bungeecore/sql/Fight.java | 27 ++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/ReplayCommand.java b/src/de/steamwar/bungeecore/commands/ReplayCommand.java index f5dd44ac..a999d721 100644 --- a/src/de/steamwar/bungeecore/commands/ReplayCommand.java +++ b/src/de/steamwar/bungeecore/commands/ReplayCommand.java @@ -29,13 +29,17 @@ public class ReplayCommand extends BasicCommand { ProxiedPlayer player = (ProxiedPlayer) sender; new SWStreamInv<>(player, Message.parse("REPLAY_TITLE", player), (click, fight) -> { - if(!fight.isReplayAvailable()) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + boolean adminReplay = false; + if (user.getUserGroup().isAdminGroup() && click.isShiftClick() && fight.replayExists()) { + adminReplay = true; + } else if(!fight.replayAllowed()) { Message.send("REPLAY_UNAVAILABLE", player); return; } ArenaMode mode = fight.getGameMode(); - SubserverSystem.sendPlayer(SubserverSystem.startArena(mode, mode.getRandomMap(), 0, 0, 0, fight.getFightID(), null, null, null, null), player); + SubserverSystem.sendPlayer(SubserverSystem.startArena(mode, mode.getRandomMap(), adminReplay ? -1 : 0, 0, 0, fight.getFightID(), null, null, null, null), player); }, page -> Fight.getPage(page, 45).stream().map(fight -> new SWListInv.SWListEntry<>(getFightItem(player, fight), fight)).collect(Collectors.toList())).open(); } @@ -48,11 +52,11 @@ public class ReplayCommand extends BasicCommand { lore.add(Message.parse("REPLAY_TIME", player, fight.getStartTime())); lore.add(""); lore.add(Message.parse("REPLAY_SERVER", player, fight.getServer())); - if(!fight.isReplayAvailable()) + if(!fight.replayAllowed()) lore.add(Message.parse("REPLAY_UNAVAILABLE", player)); item.setLore(lore); - if(fight.isReplayAvailable()) + if(fight.replayAllowed()) item.setEnchanted(true); return item; diff --git a/src/de/steamwar/bungeecore/sql/Fight.java b/src/de/steamwar/bungeecore/sql/Fight.java index 77ceefde..514946d6 100644 --- a/src/de/steamwar/bungeecore/sql/Fight.java +++ b/src/de/steamwar/bungeecore/sql/Fight.java @@ -5,30 +5,25 @@ import de.steamwar.bungeecore.ArenaMode; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; -import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Fight { - private static final Statement getPage = new Statement("SELECT FightID, GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition, ReplayLock, Replay is not NULL AS ReplayAvailable FROM Fight ORDER BY FightID DESC LIMIT ?, ?"); + private static final Statement getPage = new Statement("SELECT f.FightID, f.GameMode, f.Server, f.StartTime, f.BlueLeader, f.RedLeader, (b.NodeId IS NULL OR b.AllowReplay) AND (r.NodeId IS NULL OR r.AllowReplay) AS ReplayAllowed, f.Win, f.Replay IS NOT NULL AS ReplayExists FROM Fight f LEFT OUTER JOIN SchematicNode b ON f.BlueSchem = b.NodeId LEFT OUTER JOIN SchematicNode r ON f.RedSchem = r.NodeId ORDER BY FightID DESC LIMIT ?, ?"); private final int fightID; private final String gameMode; private final String server; private final Timestamp startTime; - private final int duration; private final int blueLeader; private final int redLeader; - private final int blueSchem; - private final int redSchem; private final int win; - private final String winCondition; - private final Timestamp replayLock; - private final boolean replayAvailable; + private final boolean replayExists; + private final boolean replayAllowed; private final List bluePlayers = new ArrayList<>(); private final List redPlayers = new ArrayList<>(); @@ -38,15 +33,11 @@ public class Fight { gameMode = rs.getString("GameMode"); server = rs.getString("Server"); startTime = rs.getTimestamp("StartTime"); - duration = rs.getInt("Duration"); blueLeader = rs.getInt("BlueLeader"); redLeader = rs.getInt("RedLeader"); - blueSchem = rs.getInt("BlueSchem"); - redSchem = rs.getInt("RedSchem"); + replayAllowed = rs.getBoolean("ReplayAllowed"); win = rs.getInt("Win"); - winCondition = rs.getString("WinCondition"); - replayLock = rs.getTimestamp("ReplayLock"); - replayAvailable = rs.getBoolean("ReplayAvailable"); + replayExists = rs.getBoolean("ReplayExists"); } private void initPlayers(List fightPlayers) { @@ -122,7 +113,11 @@ public class Fight { return redPlayers; } - public boolean isReplayAvailable() { - return replayAvailable && replayLock.before(Timestamp.from(Instant.now())) && getGameMode() != null; + public boolean replayAllowed() { + return replayExists() && replayAllowed; + } + + public boolean replayExists() { + return replayExists && getGameMode() != null; } } From 6993ccce05198b43d48f0945944a668138f31259 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 14 Mar 2022 14:15:28 +0100 Subject: [PATCH 31/70] Fix member color Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/listeners/ConnectionListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java index a2f2fb27..95f6a89c 100644 --- a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java +++ b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java @@ -79,7 +79,7 @@ public class ConnectionListener extends BasicListener { player.setDisplayName(user.getUserGroup().getColorCode() + user.getUserGroup().getChatPrefix() + " " + player.getName() + "§r"); player.setPermission("bungeecore.group." + user.getUserGroup().name().toLowerCase(), true); }else { - player.setDisplayName(player.getName()); + player.setDisplayName("§f" + player.getName()); } for(Subserver subserver : Subserver.getServerList()){ From 404c4c735bf56fc942a77c10aed2022c5d9a40d4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 14 Mar 2022 19:09:17 +0100 Subject: [PATCH 32/70] Add RankCommand --- src/de/steamwar/bungeecore/BungeeCore.java | 1 + .../bungeecore/commands/RankCommand.java | 61 +++++++++++++++++++ src/de/steamwar/bungeecore/sql/UserElo.java | 45 +++++++++++++- .../steamwar/messages/BungeeCore.properties | 6 ++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/bungeecore/commands/RankCommand.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index dc351398..5fdbecfc 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -140,6 +140,7 @@ public class BungeeCore extends Plugin { new ChallengeCommand(); new HistoricCommand(); new CheckCommand(); + new RankCommand(); new Broadcaster(); }else{ diff --git a/src/de/steamwar/bungeecore/commands/RankCommand.java b/src/de/steamwar/bungeecore/commands/RankCommand.java new file mode 100644 index 00000000..5a2a5f32 --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/RankCommand.java @@ -0,0 +1,61 @@ +/* + * 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.bungeecore.commands; + +import de.steamwar.bungeecore.ArenaMode; +import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.UserElo; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.Optional; + +public class RankCommand extends BasicCommand { + + public RankCommand() { + super("rank", null); + } + + @Override + public void execute(CommandSender sender, String[] args) { + if(!(sender instanceof ProxiedPlayer)) + return; + + ProxiedPlayer player = (ProxiedPlayer) sender; + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + for(ArenaMode mode : ArenaMode.getAllModes()) { + if (!mode.isRanked()) + continue; + + Message.send("RANK_HEADER", player, mode.getChatName()); + + Optional elo = UserElo.getElo(user.getId(), mode.getSchemType()); + + if (elo.isPresent()) { + int placement = UserElo.getPlacement(elo.get(), mode.getSchemType()); + Message.send("RANK_PLACED", player, elo.get(), placement); + Message.send("RANK_EMBLEM", player, UserElo.getEmblemProgression(mode.getChatName(), user.getId())); + } else { + Message.send("RANK_UNPLACED", player); + } + } + } +} diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 88f0844a..be2fb1b5 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -24,6 +24,7 @@ import de.steamwar.bungeecore.ArenaMode; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; public class UserElo { private UserElo() { @@ -77,14 +78,22 @@ public class UserElo { public static void setElo(int userId, String gameMode, int elo) { emblemCache.remove(userId); - gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).put(userId, Optional.of(elo)); + Optional previousElo = gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).put(userId, Optional.of(elo)); + AtomicBoolean invalidate = new AtomicBoolean(false); maxEloCache.compute(gameMode, (gm, max) -> { if (max == null || max < elo) { emblemCache.clear(); return elo; } + if (previousElo != null && previousElo.isPresent() && previousElo.get() == (int) max) { + invalidate.set(true); + } return max; }); + if (invalidate.get()) { + maxEloCache.remove(gameMode); + emblemCache.clear(); + } setElo.update(Season.getSeason(), gameMode, userId, elo); } @@ -134,6 +143,40 @@ public class UserElo { return "§7✧ "; } + public static String getEmblemProgression(String gameMode, int userId) { + Optional currentElo = UserElo.getElo(userId, gameMode); + if (!currentElo.isPresent()) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; + int maxEloOfGameMode = UserElo.getMaxElo(gameMode); + int progression = getProgression(currentElo.get(), maxEloOfGameMode); + switch (progression) { + case 0: + return "§7✧ §8✦ ✶ ✷ ✸ ✹ ❂"; + case 1: + return "§8✧ §f✦ §8✶ ✷ ✸ ✹ ❂"; + case 2: + return "§8✧ ✦ §e✶ §8✷ ✸ ✹ ❂"; + case 3: + return "§8✧ ✦ ✶ §a✷ §8✸ ✹ ❂"; + case 4: + return "§8✧ ✦ ✶ ✷ §b✸ §8✹ ❂"; + case 5: + return "§8✧ ✦ ✶ ✷ ✸ §c✹ §8❂"; + case 6: + return "§8✧ ✦ ✶ ✷ ✸ ✹ §5❂"; + } + throw new SecurityException("Progression is not in range"); + } + + private static int getProgression(int maxEloOfPlayer, int maxEloOfGameMode) { + if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return 6; + if (maxEloOfPlayer > maxEloOfGameMode * 0.97) return 5; + if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return 4; + if (maxEloOfPlayer > maxEloOfGameMode * 0.88) return 3; + if (maxEloOfPlayer > maxEloOfGameMode * 0.76) return 2; + if (maxEloOfPlayer > maxEloOfGameMode * 0.51) return 1; + return 0; + } + public static void clearCache() { gameModeUserEloCache.clear(); maxEloCache.clear(); diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index c872b856..2d03a665 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -561,3 +561,9 @@ HOURS_PLAYED=§7Deine Spielzeit beträgt§8: §e{0}h #Arena command ARENA_NOT_FOUND=§cDie angegebene Arena konnte nicht gefunden werden + +#Rank +RANK_HEADER=§7§lModus {0} +RANK_UNPLACED=§eunplatziert +RANK_PLACED=§e{1}§8. §7mit §e{2} §7Elo§8. +RANK_EMBLEM=§eEmblem§8: {0} From af978cab2b8418891c36e0d0c1f57e2bb3e2422d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 14 Mar 2022 19:11:07 +0100 Subject: [PATCH 33/70] Update RankCommand --- src/de/steamwar/bungeecore/commands/RankCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/commands/RankCommand.java b/src/de/steamwar/bungeecore/commands/RankCommand.java index 5a2a5f32..9b64602f 100644 --- a/src/de/steamwar/bungeecore/commands/RankCommand.java +++ b/src/de/steamwar/bungeecore/commands/RankCommand.java @@ -52,10 +52,10 @@ public class RankCommand extends BasicCommand { if (elo.isPresent()) { int placement = UserElo.getPlacement(elo.get(), mode.getSchemType()); Message.send("RANK_PLACED", player, elo.get(), placement); - Message.send("RANK_EMBLEM", player, UserElo.getEmblemProgression(mode.getChatName(), user.getId())); } else { Message.send("RANK_UNPLACED", player); } + Message.send("RANK_EMBLEM", player, UserElo.getEmblemProgression(mode.getChatName(), user.getId())); } } } From 92c0e58590000fc12fe844dcec00c74fb98503fb Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 14 Mar 2022 20:05:16 +0100 Subject: [PATCH 34/70] Update UserElo --- src/de/steamwar/bungeecore/sql/UserElo.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index be2fb1b5..3f937f7b 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -24,7 +24,6 @@ import de.steamwar.bungeecore.ArenaMode; import java.util.HashMap; import java.util.Map; import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; public class UserElo { private UserElo() { @@ -79,18 +78,14 @@ public class UserElo { public static void setElo(int userId, String gameMode, int elo) { emblemCache.remove(userId); Optional previousElo = gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).put(userId, Optional.of(elo)); - AtomicBoolean invalidate = new AtomicBoolean(false); - maxEloCache.compute(gameMode, (gm, max) -> { + int newElo = maxEloCache.compute(gameMode, (gm, max) -> { if (max == null || max < elo) { emblemCache.clear(); return elo; } - if (previousElo != null && previousElo.isPresent() && previousElo.get() == (int) max) { - invalidate.set(true); - } return max; }); - if (invalidate.get()) { + if (previousElo != null && previousElo.isPresent() && previousElo.get() == newElo) { maxEloCache.remove(gameMode); emblemCache.clear(); } From 0b08732817658126fa57a05f883e73e44a741edc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 09:47:27 +0100 Subject: [PATCH 35/70] Update UserElo --- src/de/steamwar/bungeecore/sql/UserElo.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 3f937f7b..a5fc2a0d 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -139,6 +139,7 @@ public class UserElo { } public static String getEmblemProgression(String gameMode, int userId) { + if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; Optional currentElo = UserElo.getElo(userId, gameMode); if (!currentElo.isPresent()) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; int maxEloOfGameMode = UserElo.getMaxElo(gameMode); From ca4a0c202253120036982b1b09b7b794b7ea579e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 15 Mar 2022 09:53:24 +0100 Subject: [PATCH 36/70] Fix class Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index a5fc2a0d..3541968f 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -139,7 +139,7 @@ public class UserElo { } public static String getEmblemProgression(String gameMode, int userId) { - if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; + if (UserElo.getFightsOfSeason(userId, gameMode) < 10) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; Optional currentElo = UserElo.getElo(userId, gameMode); if (!currentElo.isPresent()) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; int maxEloOfGameMode = UserElo.getMaxElo(gameMode); From f8b0d13676eb13a8e46120e358a0289fe74395b8 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 15 Mar 2022 10:00:47 +0100 Subject: [PATCH 37/70] Fix String Signed-off-by: Lixfel --- src/de/steamwar/messages/BungeeCore.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 2d03a665..885f01b5 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -565,5 +565,5 @@ ARENA_NOT_FOUND=§cDie angegebene Arena konnte nicht gefunden werden #Rank RANK_HEADER=§7§lModus {0} RANK_UNPLACED=§eunplatziert -RANK_PLACED=§e{1}§8. §7mit §e{2} §7Elo§8. +RANK_PLACED=§e{0}§8. §7mit §e{1} §7Elo§8. RANK_EMBLEM=§eEmblem§8: {0} From 23f8e687ad18dad4bc1cf33a4c2c6fdcd55d2b2f Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 15 Mar 2022 10:03:00 +0100 Subject: [PATCH 38/70] Fix Placement Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/commands/RankCommand.java | 2 +- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/RankCommand.java b/src/de/steamwar/bungeecore/commands/RankCommand.java index 9b64602f..73f358b5 100644 --- a/src/de/steamwar/bungeecore/commands/RankCommand.java +++ b/src/de/steamwar/bungeecore/commands/RankCommand.java @@ -51,7 +51,7 @@ public class RankCommand extends BasicCommand { if (elo.isPresent()) { int placement = UserElo.getPlacement(elo.get(), mode.getSchemType()); - Message.send("RANK_PLACED", player, elo.get(), placement); + Message.send("RANK_PLACED", player, placement, elo.get()); } else { Message.send("RANK_UNPLACED", player); } diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 3541968f..cc418a06 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -95,7 +95,7 @@ public class UserElo { public static int getPlacement(int elo, String gameMode) { return place.select(rs -> { if (rs.next()) - return rs.getInt("Place"); + return rs.getInt("Place") + 1; return -1; }, gameMode, elo, Season.getSeason()); } From 6a6e7046d6a7d6b4a9b1245d684ee7083c17e201 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 10:16:26 +0100 Subject: [PATCH 39/70] Update RankCommand --- src/de/steamwar/bungeecore/commands/RankCommand.java | 9 +++++++++ src/de/steamwar/messages/BungeeCore.properties | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/de/steamwar/bungeecore/commands/RankCommand.java b/src/de/steamwar/bungeecore/commands/RankCommand.java index 73f358b5..8bb0b71d 100644 --- a/src/de/steamwar/bungeecore/commands/RankCommand.java +++ b/src/de/steamwar/bungeecore/commands/RankCommand.java @@ -41,6 +41,15 @@ public class RankCommand extends BasicCommand { ProxiedPlayer player = (ProxiedPlayer) sender; SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + if (args.length > 0) { + SteamwarUser nUser = SteamwarUser.get(args[0]); + if (nUser == null) { + Message.send("RANK_PLAYER_NOT_FOUND", player); + return; + } + Message.send("RANK_PLAYER_FOUND", player, nUser.getUserName()); + user = nUser; + } for(ArenaMode mode : ArenaMode.getAllModes()) { if (!mode.isRanked()) continue; diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 885f01b5..ab2d88ec 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -563,6 +563,8 @@ HOURS_PLAYED=§7Deine Spielzeit beträgt§8: §e{0}h ARENA_NOT_FOUND=§cDie angegebene Arena konnte nicht gefunden werden #Rank +RANK_PLAYER_NOT_FOUND=§cSpieler nicht gefunden +RANK_PLAYER_FOUND=§eRanks §7für §e{0} RANK_HEADER=§7§lModus {0} RANK_UNPLACED=§eunplatziert RANK_PLACED=§e{0}§8. §7mit §e{1} §7Elo§8. From 5bcd094080ce8f8ee06929f3f2b17f1607a803e8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 10:19:18 +0100 Subject: [PATCH 40/70] Update RankCommand --- src/de/steamwar/messages/BungeeCore.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index ab2d88ec..acd2fbc7 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -564,7 +564,7 @@ ARENA_NOT_FOUND=§cDie angegebene Arena konnte nicht gefunden werden #Rank RANK_PLAYER_NOT_FOUND=§cSpieler nicht gefunden -RANK_PLAYER_FOUND=§eRanks §7für §e{0} +RANK_PLAYER_FOUND=§eRang §7von §e{0} RANK_HEADER=§7§lModus {0} RANK_UNPLACED=§eunplatziert RANK_PLACED=§e{0}§8. §7mit §e{1} §7Elo§8. From 0d888dff1c93374dfeab24610e310bd930e5a44f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 10:35:11 +0100 Subject: [PATCH 41/70] Update RankCommand --- src/de/steamwar/bungeecore/commands/RankCommand.java | 2 +- src/de/steamwar/bungeecore/sql/UserElo.java | 9 +++++++-- src/de/steamwar/messages/BungeeCore.properties | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/RankCommand.java b/src/de/steamwar/bungeecore/commands/RankCommand.java index 8bb0b71d..f76932e0 100644 --- a/src/de/steamwar/bungeecore/commands/RankCommand.java +++ b/src/de/steamwar/bungeecore/commands/RankCommand.java @@ -64,7 +64,7 @@ public class RankCommand extends BasicCommand { } else { Message.send("RANK_UNPLACED", player); } - Message.send("RANK_EMBLEM", player, UserElo.getEmblemProgression(mode.getChatName(), user.getId())); + Message.send("RANK_EMBLEM", player, UserElo.getEmblemProgression(player, mode.getChatName(), user.getId())); } } } diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index cc418a06..db30f5c8 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -20,6 +20,8 @@ package de.steamwar.bungeecore.sql; import de.steamwar.bungeecore.ArenaMode; +import de.steamwar.bungeecore.Message; +import net.md_5.bungee.api.connection.ProxiedPlayer; import java.util.HashMap; import java.util.Map; @@ -138,8 +140,11 @@ public class UserElo { return "§7✧ "; } - public static String getEmblemProgression(String gameMode, int userId) { - if (UserElo.getFightsOfSeason(userId, gameMode) < 10) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; + public static String getEmblemProgression(ProxiedPlayer player, String gameMode, int userId) { + int fightsOfSeason = getFightsOfSeason(userId, gameMode); + if (fightsOfSeason < 10) { + return Message.parse("RANK_NEEDED_FIGHTS_LEFT", player, "§8✧ ✦ ✶ ✷ ✸ ✹ ❂", 11 - fightsOfSeason); + } Optional currentElo = UserElo.getElo(userId, gameMode); if (!currentElo.isPresent()) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; int maxEloOfGameMode = UserElo.getMaxElo(gameMode); diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index acd2fbc7..4133ce92 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -569,3 +569,4 @@ RANK_HEADER=§7§lModus {0} RANK_UNPLACED=§eunplatziert RANK_PLACED=§e{0}§8. §7mit §e{1} §7Elo§8. RANK_EMBLEM=§eEmblem§8: {0} +RANK_NEEDED_FIGHTS_LEFT={0} §8(§7noch §e{1}§7 Fights nötig§8) From 527576aa14e19a5b8cdefcd4f6447dd66a11beca Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 11:26:21 +0100 Subject: [PATCH 42/70] Fix UserElo --- src/de/steamwar/bungeecore/sql/UserElo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index db30f5c8..657661a0 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -143,7 +143,7 @@ public class UserElo { public static String getEmblemProgression(ProxiedPlayer player, String gameMode, int userId) { int fightsOfSeason = getFightsOfSeason(userId, gameMode); if (fightsOfSeason < 10) { - return Message.parse("RANK_NEEDED_FIGHTS_LEFT", player, "§8✧ ✦ ✶ ✷ ✸ ✹ ❂", 11 - fightsOfSeason); + return Message.parse("RANK_NEEDED_FIGHTS_LEFT", player, "§8✧ ✦ ✶ ✷ ✸ ✹ ❂", 10 - fightsOfSeason); } Optional currentElo = UserElo.getElo(userId, gameMode); if (!currentElo.isPresent()) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; From bd9e3e5d94fe2dfae29c3f0476ec7275ef7ee04b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 11:40:20 +0100 Subject: [PATCH 43/70] Maybe fix resource bundle --- src/de/steamwar/bungeecore/Message.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index a54694ed..447b8402 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -41,6 +41,11 @@ public class Message { public long getTimeToLive(String arg0, Locale arg1) { return 60000; //Cache only 1 minute } + + @Override + public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) { + return System.currentTimeMillis() - loadTime > 60000; //Reload if older than 1 minute + } }; public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ From 6f5a3c352f9cc6298e279da22b29f5cdc5f7017d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 13:23:37 +0100 Subject: [PATCH 44/70] Fix emblem not being the highest --- src/de/steamwar/bungeecore/sql/UserElo.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 657661a0..42632c87 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -107,6 +107,8 @@ public class UserElo { return emblemCache.get(user.getId()); } + int maxEmblemProgression = 0; + int maxEloOfGameMode = 0; int maxEloOfPlayer = 0; ArenaMode arenaMode = null; for (ArenaMode mode : ArenaMode.getAllModes()) { @@ -114,9 +116,15 @@ public class UserElo { if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) continue; Optional currentElo = UserElo.getElo(user.getId(), mode.getSchemType()); - if (currentElo.isPresent() && currentElo.get() > maxEloOfPlayer) { - maxEloOfPlayer = currentElo.get(); - arenaMode = mode; + if (currentElo.isPresent()) { + int currentMaxEloOfGameMode = UserElo.getMaxElo(mode.getSchemType()); + int progression = getProgression(currentElo.get(), currentMaxEloOfGameMode); + if (progression > maxEmblemProgression) { + maxEmblemProgression = progression; + maxEloOfGameMode = currentMaxEloOfGameMode; + maxEloOfPlayer = currentElo.get(); + arenaMode = mode; + } } } @@ -124,7 +132,6 @@ public class UserElo { emblemCache.put(user.getId(), ""); return ""; } - int maxEloOfGameMode = UserElo.getMaxElo(arenaMode.getSchemType()); String emblem = getEmblem(maxEloOfPlayer, maxEloOfGameMode); emblemCache.put(user.getId(), emblem); return emblem; From 641c165782e5fee8b249e5c817cffc8d6a4bc10e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 13:23:37 +0100 Subject: [PATCH 45/70] Fix emblem not being the highest --- src/de/steamwar/bungeecore/sql/UserElo.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index cc418a06..0263b68e 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -105,6 +105,8 @@ public class UserElo { return emblemCache.get(user.getId()); } + int maxEmblemProgression = 0; + int maxEloOfGameMode = 0; int maxEloOfPlayer = 0; ArenaMode arenaMode = null; for (ArenaMode mode : ArenaMode.getAllModes()) { @@ -112,9 +114,15 @@ public class UserElo { if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) continue; Optional currentElo = UserElo.getElo(user.getId(), mode.getSchemType()); - if (currentElo.isPresent() && currentElo.get() > maxEloOfPlayer) { - maxEloOfPlayer = currentElo.get(); - arenaMode = mode; + if (currentElo.isPresent()) { + int currentMaxEloOfGameMode = UserElo.getMaxElo(mode.getSchemType()); + int progression = getProgression(currentElo.get(), currentMaxEloOfGameMode); + if (progression > maxEmblemProgression) { + maxEmblemProgression = progression; + maxEloOfGameMode = currentMaxEloOfGameMode; + maxEloOfPlayer = currentElo.get(); + arenaMode = mode; + } } } @@ -122,7 +130,6 @@ public class UserElo { emblemCache.put(user.getId(), ""); return ""; } - int maxEloOfGameMode = UserElo.getMaxElo(arenaMode.getSchemType()); String emblem = getEmblem(maxEloOfPlayer, maxEloOfGameMode); emblemCache.put(user.getId(), emblem); return emblem; From 9047c8ef6ac7cb944ee72a6648d992f1a1c239cc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 14:22:12 +0100 Subject: [PATCH 46/70] Fix Message --- src/de/steamwar/bungeecore/Message.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 447b8402..f995694d 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -44,7 +44,7 @@ public class Message { @Override public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) { - return System.currentTimeMillis() - loadTime > 60000; //Reload if older than 1 minute + return true; } }; @@ -71,7 +71,9 @@ public class Message { private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); + System.out.println("parse " + message + " " + locale); ResourceBundle resourceBundle = ResourceBundle.getBundle("de.steamwar.messages.BungeeCore", locale, CONTROL); + System.out.println(resourceBundle); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; From 6ddfe27ed14118aa7c70b5c6007764ac7540aa53 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 14:23:28 +0100 Subject: [PATCH 47/70] Remove useless sout --- src/de/steamwar/bungeecore/Message.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index f995694d..992cedff 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -71,9 +71,7 @@ public class Message { private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - System.out.println("parse " + message + " " + locale); ResourceBundle resourceBundle = ResourceBundle.getBundle("de.steamwar.messages.BungeeCore", locale, CONTROL); - System.out.println(resourceBundle); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; From 7ccfef823c02622ea1ff7895d07323efbcdf2974 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 15:12:32 +0100 Subject: [PATCH 48/70] Update BungeeCore.properties --- src/de/steamwar/messages/BungeeCore.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 4133ce92..e7116508 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -569,4 +569,4 @@ RANK_HEADER=§7§lModus {0} RANK_UNPLACED=§eunplatziert RANK_PLACED=§e{0}§8. §7mit §e{1} §7Elo§8. RANK_EMBLEM=§eEmblem§8: {0} -RANK_NEEDED_FIGHTS_LEFT={0} §8(§7noch §e{1}§7 Fights nötig§8) +RANK_NEEDED_FIGHTS_LEFT={0} §8(§7noch §e{1}§7 Kämpfe nötig§8) From 01b500e2e7a5393799703ecb265dc5900e956846 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:16:08 +0100 Subject: [PATCH 49/70] Fix Message again for the final time! --- src/de/steamwar/bungeecore/Message.java | 45 ++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 992cedff..3975eeb4 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -28,25 +28,15 @@ import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; +import java.io.IOException; +import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; -import java.util.Date; -import java.util.Locale; -import java.util.ResourceBundle; +import java.util.*; public class Message { - private static final ResourceBundle.Control CONTROL = new ResourceBundle.Control() { - @Override - public long getTimeToLive(String arg0, Locale arg1) { - return 60000; //Cache only 1 minute - } - - @Override - public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) { - return true; - } - }; + private static Map bundles = new HashMap<>(); public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), params))); @@ -68,10 +58,35 @@ public class Message { return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); } + private static Optional createSingle(String name) { + InputStream inputStream = Message.class.getResourceAsStream(name); + if(inputStream == null) return Optional.empty(); + try { + PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle(inputStream); + if (propertyResourceBundle.getKeys().hasMoreElements()) { + return Optional.of(propertyResourceBundle); + } else { + return Optional.empty(); + } + } catch (IOException e) { + return Optional.empty(); + } + } + + private static ResourceBundle createResourceBundle(String baseName, Locale locale) { + return bundles.computeIfAbsent(locale, l -> { + String path = baseName.replace('.', '/'); + return createSingle("/" + path + "_" + locale.toString() + ".properties") + .or(() -> createSingle("/" + path + "_" + locale.getLanguage() + ".properties")) + .or(() -> createSingle("/" + path + ".properties")) + .orElseThrow(() -> new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage())); + }); + } + private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - ResourceBundle resourceBundle = ResourceBundle.getBundle("de.steamwar.messages.BungeeCore", locale, CONTROL); + ResourceBundle resourceBundle = createResourceBundle("de.steamwar.messages.BungeeCore", locale); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; From 9accdb599e15233f3d33be91d5e892184372ba7a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:18:16 +0100 Subject: [PATCH 50/70] Fix Message again for the final time! --- src/de/steamwar/bungeecore/Message.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 3975eeb4..98dceccc 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -73,7 +73,7 @@ public class Message { } } - private static ResourceBundle createResourceBundle(String baseName, Locale locale) { + private static ResourceBundle getResourceBundle(String baseName, Locale locale) { return bundles.computeIfAbsent(locale, l -> { String path = baseName.replace('.', '/'); return createSingle("/" + path + "_" + locale.toString() + ".properties") @@ -86,7 +86,7 @@ public class Message { private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - ResourceBundle resourceBundle = createResourceBundle("de.steamwar.messages.BungeeCore", locale); + ResourceBundle resourceBundle = getResourceBundle("de.steamwar.messages.BungeeCore", locale); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; From 4df39ebe7565defd73b542c443a6006ba478fba2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:53:50 +0100 Subject: [PATCH 51/70] Fix Message again for the final time! --- src/de/steamwar/bungeecore/Message.java | 68 ++++++++++++++++++------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 98dceccc..06fa54fe 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -36,7 +36,7 @@ import java.util.*; public class Message { - private static Map bundles = new HashMap<>(); + private static Map bundles = new HashMap<>(); public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), params))); @@ -58,29 +58,52 @@ public class Message { return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); } - private static Optional createSingle(String name) { + private static SteamwarResourceBundle getOrCreateSingle(String name, String cacheKey) { + if (bundles.containsKey(cacheKey)) { + return bundles.get(cacheKey); + } InputStream inputStream = Message.class.getResourceAsStream(name); - if(inputStream == null) return Optional.empty(); + if(inputStream == null) return null; try { - PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle(inputStream); - if (propertyResourceBundle.getKeys().hasMoreElements()) { - return Optional.of(propertyResourceBundle); - } else { - return Optional.empty(); - } + SteamwarResourceBundle steamwarResourceBundle = new SteamwarResourceBundle(inputStream); + bundles.put(cacheKey, steamwarResourceBundle); + return steamwarResourceBundle; } catch (IOException e) { - return Optional.empty(); + return null; } } private static ResourceBundle getResourceBundle(String baseName, Locale locale) { - return bundles.computeIfAbsent(locale, l -> { - String path = baseName.replace('.', '/'); - return createSingle("/" + path + "_" + locale.toString() + ".properties") - .or(() -> createSingle("/" + path + "_" + locale.getLanguage() + ".properties")) - .or(() -> createSingle("/" + path + ".properties")) - .orElseThrow(() -> new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage())); - }); + if (bundles.containsKey(locale.toString())) { + return bundles.get(locale.toString()); + } + String path = baseName.replace('.', '/'); + + SteamwarResourceBundle current = null; + SteamwarResourceBundle temp = getOrCreateSingle("/" + path + "_" + locale + ".properties", locale.toString()); + if (temp != null) { + current = temp; + bundles.put(locale.toString(), temp); + } + + temp = getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()); + if (current != null) current.setParent(temp); + if (temp != null) { + current = temp; + bundles.put(locale.toString(), temp); + } + + temp = getOrCreateSingle("/" + path + ".properties", ""); + if (current != null) current.setParent(temp); + if (temp != null) { + current = temp; + bundles.put(locale.toString(), temp); + } + + if (current == null) { + throw new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage()); + } + return bundles.get(locale.toString()); } private static String parse(String message, boolean prefixed, Locale locale, Object... params){ @@ -194,4 +217,15 @@ public class Message { public Object[] getParams() { return params; } + + private static class SteamwarResourceBundle extends PropertyResourceBundle { + public SteamwarResourceBundle(InputStream stream) throws IOException { + super(stream); + } + + @Override + protected void setParent(ResourceBundle parent) { + super.setParent(parent); + } + } } \ No newline at end of file From 8ee960e0028d171f1e7c215cc5491f8eaf9adfa8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 17:54:10 +0100 Subject: [PATCH 52/70] Fix message --- src/de/steamwar/bungeecore/Message.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 06fa54fe..4356bcff 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -101,7 +101,7 @@ public class Message { } if (current == null) { - throw new SecurityException("Could not find resource bundle for " + path + " with locale " + locale + " or " + locale.getLanguage()); + throw new SecurityException("Could not find resource bundle for " + path); } return bundles.get(locale.toString()); } From a78da0e2cf558b113a159cbf0412ec720b12867b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 18:00:28 +0100 Subject: [PATCH 53/70] Update Message stuff --- src/de/steamwar/bungeecore/Message.java | 58 ++++++++++--------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 4356bcff..feca756d 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,6 +33,7 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; public class Message { @@ -59,18 +60,15 @@ public class Message { } private static SteamwarResourceBundle getOrCreateSingle(String name, String cacheKey) { - if (bundles.containsKey(cacheKey)) { - return bundles.get(cacheKey); - } - InputStream inputStream = Message.class.getResourceAsStream(name); - if(inputStream == null) return null; - try { - SteamwarResourceBundle steamwarResourceBundle = new SteamwarResourceBundle(inputStream); - bundles.put(cacheKey, steamwarResourceBundle); - return steamwarResourceBundle; - } catch (IOException e) { - return null; - } + return bundles.computeIfAbsent(cacheKey, s -> { + InputStream inputStream = Message.class.getResourceAsStream(name); + if(inputStream == null) return null; + try { + return new SteamwarResourceBundle(inputStream); + } catch (IOException e) { + return null; + } + }); } private static ResourceBundle getResourceBundle(String baseName, Locale locale) { @@ -79,33 +77,23 @@ public class Message { } String path = baseName.replace('.', '/'); - SteamwarResourceBundle current = null; - SteamwarResourceBundle temp = getOrCreateSingle("/" + path + "_" + locale + ".properties", locale.toString()); - if (temp != null) { - current = temp; - bundles.put(locale.toString(), temp); - } + AtomicReference current = new AtomicReference<>(); + apply(current, getOrCreateSingle("/" + path + "_" + locale.toString() + ".properties", locale.toString()), locale.toString()); + apply(current, getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()), locale.toString()); + apply(current, getOrCreateSingle("/" + path + ".properties", ""), locale.toString()); - temp = getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()); - if (current != null) current.setParent(temp); - if (temp != null) { - current = temp; - bundles.put(locale.toString(), temp); - } - - temp = getOrCreateSingle("/" + path + ".properties", ""); - if (current != null) current.setParent(temp); - if (temp != null) { - current = temp; - bundles.put(locale.toString(), temp); - } - - if (current == null) { - throw new SecurityException("Could not find resource bundle for " + path); - } + if (current.get() == null) throw new SecurityException("Could not find resource bundle for " + path); return bundles.get(locale.toString()); } + private static void apply(AtomicReference current, SteamwarResourceBundle now, String bundleKey) { + if (current.get() != null) current.get().setParent(now); + if (now != null) { + current.set(now); + bundles.put(bundleKey, now); + } + } + private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); From 1fd34f8ccd2dbb75611eb17f1057552125c808d3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 18:12:55 +0100 Subject: [PATCH 54/70] Update Message stuff --- src/de/steamwar/bungeecore/Message.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index feca756d..2ab6963f 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -90,7 +90,7 @@ public class Message { if (current.get() != null) current.get().setParent(now); if (now != null) { current.set(now); - bundles.put(bundleKey, now); + bundles.putIfAbsent(bundleKey, now); } } From e038f807e97fd5df0ca6db3bebaf10d41b4dd2ba Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 18:31:47 +0100 Subject: [PATCH 55/70] Hotfix FightEndsHandler --- src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 50afa5df..c71521d9 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -74,7 +74,7 @@ public class FightEndsHandler implements SpigotHandler { } private void calculateEloOfTeam(int schemId, int eloOwn, int eloEnemy, double result, List players, String gameMode) { - double winExpectation = 1 / (1 + Math.pow(10, (eloOwn - eloEnemy) / 600f)); + double winExpectation = 1 / (1 + Math.pow(10, (eloEnemy - eloOwn) / 600f)); SchemElo.setElo(schemId, (int) Math.round(eloOwn + K * (result - winExpectation))); for (int player : players) { int playerElo = UserElo.getEloOrDefault(player, gameMode); From 1f50bf15aa8323daad95bc22d07c920283be4f59 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 15 Mar 2022 19:05:08 +0100 Subject: [PATCH 56/70] Fix Message --- src/de/steamwar/bungeecore/Message.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 2ab6963f..47d4b825 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,11 +33,12 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; public class Message { - private static Map bundles = new HashMap<>(); + private static Map bundles = new ConcurrentHashMap<>(); public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), params))); From 46499633d034b1888c6791ba617c9abbb96c01a1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 14:56:37 +0100 Subject: [PATCH 57/70] Code cleanup, cache invalidation fixes Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/UserElo.java | 168 +++++++++----------- 1 file changed, 75 insertions(+), 93 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index 42632c87..ca263454 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -28,8 +28,7 @@ import java.util.Map; import java.util.Optional; public class UserElo { - private UserElo() { - } + private UserElo() {} public static final int ELO_DEFAULT = 1000; @@ -39,8 +38,9 @@ public class UserElo { private static final Statement elo = new Statement("SELECT Elo FROM UserElo WHERE UserID = ? AND GameMode = ? AND Season = ?"); private static final Statement setElo = new Statement("INSERT INTO UserElo (Season, GameMode, UserID, Elo) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); - private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement maxElo = new Statement("SELECT MAX(Elo) AS MaxElo FROM UserElo WHERE Season = ? AND GameMode = ?"); + + private static final Statement place = new Statement("SELECT COUNT(*) AS Place FROM UserElo WHERE GameMode = ? AND Elo > ? AND Season = ?"); private static final Statement fightsOfSeason = new Statement("SELECT COUNT(*) AS Fights FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID WHERE UserID = ? AND GameMode = ? AND UNIX_TIMESTAMP(StartTime) + Duration >= UNIX_TIMESTAMP(?)"); public static int getEloOrDefault(int userID, String gameMode) { @@ -48,18 +48,14 @@ public class UserElo { } public static Optional getElo(int userID, String gameMode) { - return gameModeUserEloCache.computeIfAbsent(gameMode, gm -> { - return new HashMap<>(); - }).computeIfAbsent(userID, uid -> { - return elo.select(rs -> { - if (rs.next()) - return Optional.of(rs.getInt("Elo")); - return Optional.empty(); - }, userID, gameMode, Season.getSeason()); - }); + return gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).computeIfAbsent(userID, uid -> elo.select(rs -> { + if (rs.next()) + return Optional.of(rs.getInt("Elo")); + return Optional.empty(); + }, userID, gameMode, Season.getSeason())); } - public static int getFightsOfSeason(int userID, String gameMode) { + private static int getFightsOfSeason(int userID, String gameMode) { return fightsOfSeason.select(rs -> { if (rs.next()) return rs.getInt("Fights"); @@ -67,30 +63,24 @@ public class UserElo { }, userID, gameMode, Season.getSeasonStart()); } - public static int getMaxElo(String gameMode) { - return maxEloCache.computeIfAbsent(gameMode, gm -> { - return maxElo.select(rs -> { - if (rs.next()) - return rs.getInt("MaxElo"); - return 0; - }, Season.getSeason(), gameMode); - }); + private static int getMaxElo(String gameMode) { + return maxEloCache.computeIfAbsent(gameMode, gm -> maxElo.select(rs -> { + if (rs.next()) + return rs.getInt("MaxElo"); + return 0; + }, Season.getSeason(), gameMode)); } public static void setElo(int userId, String gameMode, int elo) { emblemCache.remove(userId); - Optional previousElo = gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).put(userId, Optional.of(elo)); - int newElo = maxEloCache.compute(gameMode, (gm, max) -> { - if (max == null || max < elo) { - emblemCache.clear(); - return elo; - } - return max; - }); - if (previousElo != null && previousElo.isPresent() && previousElo.get() == newElo) { + + Optional oldElo = Optional.ofNullable(gameModeUserEloCache.computeIfAbsent(gameMode, gm -> new HashMap<>()).put(userId, Optional.of(elo))).orElse(Optional.empty()); + int maxElo = getMaxElo(gameMode); + if (elo > maxElo || (oldElo.isPresent() && oldElo.get() == maxElo)) { maxEloCache.remove(gameMode); emblemCache.clear(); } + setElo.update(Season.getSeason(), gameMode, userId, elo); } @@ -103,86 +93,78 @@ public class UserElo { } public static String getEmblem(SteamwarUser user) { - if (emblemCache.containsKey(user.getId())) { - return emblemCache.get(user.getId()); - } - - int maxEmblemProgression = 0; - int maxEloOfGameMode = 0; - int maxEloOfPlayer = 0; - ArenaMode arenaMode = null; - for (ArenaMode mode : ArenaMode.getAllModes()) { - if (!mode.isRanked()) continue; - if (UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) < 10) continue; - - Optional currentElo = UserElo.getElo(user.getId(), mode.getSchemType()); - if (currentElo.isPresent()) { - int currentMaxEloOfGameMode = UserElo.getMaxElo(mode.getSchemType()); - int progression = getProgression(currentElo.get(), currentMaxEloOfGameMode); - if (progression > maxEmblemProgression) { - maxEmblemProgression = progression; - maxEloOfGameMode = currentMaxEloOfGameMode; - maxEloOfPlayer = currentElo.get(); - arenaMode = mode; - } + return emblemCache.computeIfAbsent(user.getId(), userId -> { + switch( + ArenaMode.getAllModes().stream().filter( + ArenaMode::isRanked + ).filter( + mode -> UserElo.getFightsOfSeason(user.getId(), mode.getSchemType()) >= 10 + ).map( + mode -> getProgression(user.getId(), mode.getSchemType()) + ).max(Integer::compareTo).orElse(0) + ) { + case 0: + return ""; + case 1: + return "§7✧ "; + case 2: + return "§f✦ "; + case 3: + return "§e✶ "; + case 4: + return "§a✷ "; + case 5: + return "§b✸ "; + case 6: + return "§c✹ "; + case 7: + return "§5❂ "; + default: + throw new SecurityException("Progression out of range"); } - } - - if (arenaMode == null) { - emblemCache.put(user.getId(), ""); - return ""; - } - String emblem = getEmblem(maxEloOfPlayer, maxEloOfGameMode); - emblemCache.put(user.getId(), emblem); - return emblem; - } - - private static String getEmblem(int maxEloOfPlayer, int maxEloOfGameMode) { - if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return "§5❂ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.97) return "§c✹ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return "§b✸ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.88) return "§a✷ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.76) return "§e✶ "; - if (maxEloOfPlayer > maxEloOfGameMode * 0.51) return "§f✦ "; - return "§7✧ "; + }); } public static String getEmblemProgression(ProxiedPlayer player, String gameMode, int userId) { int fightsOfSeason = getFightsOfSeason(userId, gameMode); - if (fightsOfSeason < 10) { + if (fightsOfSeason < 10) return Message.parse("RANK_NEEDED_FIGHTS_LEFT", player, "§8✧ ✦ ✶ ✷ ✸ ✹ ❂", 10 - fightsOfSeason); - } - Optional currentElo = UserElo.getElo(userId, gameMode); - if (!currentElo.isPresent()) return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; - int maxEloOfGameMode = UserElo.getMaxElo(gameMode); - int progression = getProgression(currentElo.get(), maxEloOfGameMode); - switch (progression) { + + switch (getProgression(userId, gameMode)) { case 0: - return "§7✧ §8✦ ✶ ✷ ✸ ✹ ❂"; + return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; case 1: - return "§8✧ §f✦ §8✶ ✷ ✸ ✹ ❂"; + return "§7✧ §8✦ ✶ ✷ ✸ ✹ ❂"; case 2: - return "§8✧ ✦ §e✶ §8✷ ✸ ✹ ❂"; + return "§8✧ §f✦ §8✶ ✷ ✸ ✹ ❂"; case 3: - return "§8✧ ✦ ✶ §a✷ §8✸ ✹ ❂"; + return "§8✧ ✦ §e✶ §8✷ ✸ ✹ ❂"; case 4: - return "§8✧ ✦ ✶ ✷ §b✸ §8✹ ❂"; + return "§8✧ ✦ ✶ §a✷ §8✸ ✹ ❂"; case 5: - return "§8✧ ✦ ✶ ✷ ✸ §c✹ §8❂"; + return "§8✧ ✦ ✶ ✷ §b✸ §8✹ ❂"; case 6: + return "§8✧ ✦ ✶ ✷ ✸ §c✹ §8❂"; + case 7: return "§8✧ ✦ ✶ ✷ ✸ ✹ §5❂"; + default: + throw new SecurityException("Progression is not in range"); } - throw new SecurityException("Progression is not in range"); } - private static int getProgression(int maxEloOfPlayer, int maxEloOfGameMode) { - if (maxEloOfPlayer > maxEloOfGameMode * 0.99) return 6; - if (maxEloOfPlayer > maxEloOfGameMode * 0.97) return 5; - if (maxEloOfPlayer > maxEloOfGameMode * 0.94) return 4; - if (maxEloOfPlayer > maxEloOfGameMode * 0.88) return 3; - if (maxEloOfPlayer > maxEloOfGameMode * 0.76) return 2; - if (maxEloOfPlayer > maxEloOfGameMode * 0.51) return 1; - return 0; + private static int getProgression(int userId, String gameMode) { + int elo = UserElo.getElo(userId, gameMode).orElse(0); + if(elo == 0) + return 0; + int maxElo = UserElo.getMaxElo(gameMode); + + if (maxElo > elo * 0.99) return 7; + if (maxElo > elo * 0.97) return 6; + if (maxElo > elo * 0.94) return 5; + if (maxElo > elo * 0.88) return 4; + if (maxElo > elo * 0.76) return 3; + if (maxElo > elo * 0.51) return 2; + return 1; } public static void clearCache() { From c0301a80140cbf8f49104455a9468625da01f8b2 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 16:24:19 +0100 Subject: [PATCH 58/70] Code simplification Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/Message.java | 52 +++++++------------------ 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 47d4b825..cca89f78 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,12 +33,10 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicReference; public class Message { - private static Map bundles = new ConcurrentHashMap<>(); + private static final Map bundles = new HashMap<>(); public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, locale(sender), params))); @@ -60,45 +58,29 @@ public class Message { return sender instanceof ProxiedPlayer ? ((ProxiedPlayer)sender).getLocale() : Locale.getDefault(); } - private static SteamwarResourceBundle getOrCreateSingle(String name, String cacheKey) { - return bundles.computeIfAbsent(cacheKey, s -> { - InputStream inputStream = Message.class.getResourceAsStream(name); - if(inputStream == null) return null; + private static final String BASE_PATH = "/" + "de.steamwar.messages.BungeeCore".replace('.', '/'); + + private static ResourceBundle getResourceBundle(String locale, ResourceBundle parent) { + return bundles.computeIfAbsent(locale, locale1 -> { + InputStream inputStream = Message.class.getResourceAsStream(BASE_PATH + ("".equals(locale) ? "" : "_" + locale) + ".properties"); + if(inputStream == null) + return parent; try { - return new SteamwarResourceBundle(inputStream); + return new SteamwarResourceBundle(inputStream, parent); } catch (IOException e) { - return null; + return parent; } }); } - private static ResourceBundle getResourceBundle(String baseName, Locale locale) { - if (bundles.containsKey(locale.toString())) { - return bundles.get(locale.toString()); - } - String path = baseName.replace('.', '/'); - - AtomicReference current = new AtomicReference<>(); - apply(current, getOrCreateSingle("/" + path + "_" + locale.toString() + ".properties", locale.toString()), locale.toString()); - apply(current, getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()), locale.toString()); - apply(current, getOrCreateSingle("/" + path + ".properties", ""), locale.toString()); - - if (current.get() == null) throw new SecurityException("Could not find resource bundle for " + path); - return bundles.get(locale.toString()); - } - - private static void apply(AtomicReference current, SteamwarResourceBundle now, String bundleKey) { - if (current.get() != null) current.get().setParent(now); - if (now != null) { - current.set(now); - bundles.putIfAbsent(bundleKey, now); - } + private static ResourceBundle getResourceBundle(Locale locale) { + return bundles.computeIfAbsent(locale.toString(), locale1 -> getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", (ResourceBundle) null)))); } private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault(); - ResourceBundle resourceBundle = getResourceBundle("de.steamwar.messages.BungeeCore", locale); + ResourceBundle resourceBundle = getResourceBundle(locale); String pattern = ""; if(prefixed) pattern = resourceBundle.getObject("PREFIX") + " "; @@ -208,13 +190,9 @@ public class Message { } private static class SteamwarResourceBundle extends PropertyResourceBundle { - public SteamwarResourceBundle(InputStream stream) throws IOException { + public SteamwarResourceBundle(InputStream stream, ResourceBundle parent) throws IOException { super(stream); - } - - @Override - protected void setParent(ResourceBundle parent) { - super.setParent(parent); + setParent(parent); } } } \ No newline at end of file From f04ccc6ea762954b78b39491359295fe041442b5 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 16:25:43 +0100 Subject: [PATCH 59/70] Fix emblems Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/sql/UserElo.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/bungeecore/sql/UserElo.java b/src/de/steamwar/bungeecore/sql/UserElo.java index ca263454..ca7e7237 100644 --- a/src/de/steamwar/bungeecore/sql/UserElo.java +++ b/src/de/steamwar/bungeecore/sql/UserElo.java @@ -158,12 +158,12 @@ public class UserElo { return 0; int maxElo = UserElo.getMaxElo(gameMode); - if (maxElo > elo * 0.99) return 7; - if (maxElo > elo * 0.97) return 6; - if (maxElo > elo * 0.94) return 5; - if (maxElo > elo * 0.88) return 4; - if (maxElo > elo * 0.76) return 3; - if (maxElo > elo * 0.51) return 2; + if (elo > maxElo * 0.99) return 7; + if (elo > maxElo * 0.97) return 6; + if (elo > maxElo * 0.94) return 5; + if (elo > maxElo * 0.88) return 4; + if (elo > maxElo * 0.76) return 3; + if (elo > maxElo * 0.51) return 2; return 1; } From c08bfe75b907e81014d4749756bbc3ce3d5cd6fd Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 16:30:38 +0100 Subject: [PATCH 60/70] Fix ConcurrentModificationException Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/Message.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index cca89f78..acaa1891 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -74,7 +74,7 @@ public class Message { } private static ResourceBundle getResourceBundle(Locale locale) { - return bundles.computeIfAbsent(locale.toString(), locale1 -> getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", (ResourceBundle) null)))); + return getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", (ResourceBundle) null))); } private static String parse(String message, boolean prefixed, Locale locale, Object... params){ From 87dbca480e158fc3dc20bae1f492c500a670a58a Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 16 Mar 2022 12:32:52 +0100 Subject: [PATCH 61/70] Tutorial first steps Signed-off-by: Lixfel --- .../bungeecore/commands/ReplayCommand.java | 19 ++++ .../bungeecore/commands/TutorialCommand.java | 67 +++++++++++++ src/de/steamwar/bungeecore/sql/Tutorial.java | 98 +++++++++++++++++++ .../steamwar/messages/BungeeCore.properties | 6 ++ 4 files changed, 190 insertions(+) create mode 100644 src/de/steamwar/bungeecore/commands/TutorialCommand.java create mode 100644 src/de/steamwar/bungeecore/sql/Tutorial.java diff --git a/src/de/steamwar/bungeecore/commands/ReplayCommand.java b/src/de/steamwar/bungeecore/commands/ReplayCommand.java index a999d721..9ca94b4c 100644 --- a/src/de/steamwar/bungeecore/commands/ReplayCommand.java +++ b/src/de/steamwar/bungeecore/commands/ReplayCommand.java @@ -1,3 +1,22 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.bungeecore.commands; import de.steamwar.bungeecore.ArenaMode; diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java new file mode 100644 index 00000000..253d8890 --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -0,0 +1,67 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.bungeecore.commands; + +import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.inventory.SWItem; +import de.steamwar.bungeecore.inventory.SWListInv; +import de.steamwar.bungeecore.inventory.SWStreamInv; +import de.steamwar.bungeecore.sql.Tutorial; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class TutorialCommand extends BasicCommand { + + public TutorialCommand() { + super("tutorial", null); + } + + @Override + public void execute(CommandSender sender, String[] args) { + if(!(sender instanceof ProxiedPlayer)) + return; + ProxiedPlayer player = (ProxiedPlayer) sender; + + //TODO: Tutorial rating + //TODO: Tutorial creation + + new SWStreamInv<>(player, Message.parse("TUTORIAL_TITLE", player), (click, tutorial) -> { + //TODO: Tutorial start + }, page -> Tutorial.getPage(page, 45).stream().map(tutorial -> new SWListInv.SWListEntry<>(getTutorialItem(player, tutorial), tutorial)).collect(Collectors.toList())).open(); + } + + private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial) { + SWItem item = new SWItem(tutorial.item(), Message.parse("TUTORIAL_NAME", player, tutorial.name())); + + List lore = new ArrayList<>(); + lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); + lore.add(""); + lore.add(Message.parse("TUTORIAL_BASED_ON", player)); + lore.add(Message.parse("TUTORIAL_NAME", player, tutorial.parent().name())); + lore.add(Message.parse("TUTORIAL_BY", player, tutorial.parent().creator().getUserName())); + item.setLore(lore); + + return item; + } +} diff --git a/src/de/steamwar/bungeecore/sql/Tutorial.java b/src/de/steamwar/bungeecore/sql/Tutorial.java new file mode 100644 index 00000000..f9b49f15 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Tutorial.java @@ -0,0 +1,98 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.bungeecore.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Tutorial { + + private static final Statement by_popularity = new Statement("SELECT t.TutorialID, t.Creator, t.Name, t.Item, AVG(r.Stars) AS Stars, p.TutorialID AS ParentID, p.Creator AS ParentCreator, p.Name AS ParentName FROM Tutorial t INNER JOIN Tutorial p ON t.Parent = p.TutorialID LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID GROUP BY t.TutorialID ORDER BY SUM(r.Stars) DESC LIMIT ?, ?"); + private static final Statement rate = new Statement("INSERT INTO TutorialRating (TutorialID, UserID, Stars) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Stars = VALUES(Stars)"); + private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item, Parent) VALUES (?, ?, ?, ?)"); + + public static List getPage(int page, int elementsPerPage) { + List tutorials = by_popularity.select(rs -> { + List t = new ArrayList<>(); + while(rs.next()) + t.add(new Tutorial(rs)); + return t; + }, page * elementsPerPage, elementsPerPage); + + SteamwarUser.batchCache(tutorials.stream().flatMap(tutorial -> Stream.of(tutorial.creator, tutorial.parent.creator)).collect(Collectors.toSet())); + return tutorials; + } + + public static void create(SteamwarUser creator, String name, Tutorial parent) { + create.update(creator.getId(), name, parent.id); + } + + private final int id; + private final int creator; + private final String name; + private final String item; + private Tutorial parent; + private final int stars; + + public Tutorial(ResultSet rs) throws SQLException { + this(rs.getInt("TutorialID"), rs.getInt("Creator"), rs.getString("Name"), rs.getString("Item"), rs.getInt("Stars")); + parent = new Tutorial(rs.getInt("ParentID"), rs.getInt("ParentCreator"), rs.getString("ParentName"), "", 0); + } + + private Tutorial(int id, int creator, String name, String item, int stars) { + this.id = id; + this.creator = creator; + this.name = name; + this.item = item; + this.stars = stars; + } + + public SteamwarUser creator() { + return SteamwarUser.get(creator); + } + + public Tutorial parent() { + return parent; + } + + public int stars() { + return stars; + } + + public int id() { + return id; + } + + public String name() { + return name; + } + + public String item() { + return item; + } + + public void rate(SteamwarUser user, int rating) { + rate.update(id, user.getId(), rating); + } +} diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index e7116508..60b69c35 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -333,6 +333,12 @@ REPLAY_LOSER=§e{0} §7+§e{1} REPLAY_TIME=§7{0} REPLAY_SERVER=§7{0} +#TutorialCommand +TUTORIAL_TITLE=Tutorials +TUTORIAL_NAME=§e{0} +TUTORIAL_BY=§8von §7{0} +TUTORIAL_BASED_ON=§8Basierend auf + #ServerTeamchatCommand STC_USAGE=§8/§7stc §8[§eNachricht an das Team§8] STC_FORMAT=§8STC §e{0}» §r{1} From 19de4fd1d881a89f54a908a9a2c74eaee4da22af Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 16 Mar 2022 20:02:16 +0100 Subject: [PATCH 62/70] Fix Ranked calculation --- .../comms/handlers/FightEndsHandler.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index c71521d9..ffb725ef 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -69,16 +69,21 @@ public class FightEndsHandler implements SpigotHandler { int blueSchemElo = SchemElo.getElo(fightEndsPacket.getBlueSchem()); int redSchemElo = SchemElo.getElo(fightEndsPacket.getRedSchem()); - calculateEloOfTeam(fightEndsPacket.getBlueSchem(), blueSchemElo, redSchemElo, blueResult, fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode()); - calculateEloOfTeam(fightEndsPacket.getRedSchem(), redSchemElo, blueSchemElo, 1 - blueResult, fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode()); + int blueTeamElo = fightEndsPacket.getBluePlayers().stream().mapToInt(player -> UserElo.getEloOrDefault(player, fightEndsPacket.getGameMode())).sum(); + int redTeamElo = fightEndsPacket.getRedPlayers().stream().mapToInt(player -> UserElo.getEloOrDefault(player, fightEndsPacket.getGameMode())).sum(); + + calculateEloOfTeam(fightEndsPacket.getBlueSchem(), blueSchemElo, redSchemElo, blueTeamElo, redTeamElo, blueResult, fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode()); + calculateEloOfTeam(fightEndsPacket.getRedSchem(), redSchemElo, blueSchemElo, redTeamElo, blueTeamElo, 1 - blueResult, fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode()); } - private void calculateEloOfTeam(int schemId, int eloOwn, int eloEnemy, double result, List players, String gameMode) { - double winExpectation = 1 / (1 + Math.pow(10, (eloEnemy - eloOwn) / 600f)); - SchemElo.setElo(schemId, (int) Math.round(eloOwn + K * (result - winExpectation))); + private void calculateEloOfTeam(int schemId, int eloSchemOwn, int eloSchemEnemy, int eloTeamOwn, int eloTeamEnemy, double result, List players, String gameMode) { + double winSchemExpectation = 1 / (1 + Math.pow(10, (eloSchemEnemy - eloSchemOwn) / 600f)); + SchemElo.setElo(schemId, (int) Math.round(eloSchemOwn + K * (result - winSchemExpectation))); + + double winTeamExpectation = 1 / (1 + Math.pow(10, (eloTeamEnemy - eloTeamOwn) / 600f)); for (int player : players) { int playerElo = UserElo.getEloOrDefault(player, gameMode); - UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * (result - winExpectation))); + UserElo.setElo(player, gameMode, (int) Math.round(playerElo + K * (result - winTeamExpectation))); } } } From 908e54fedd364cbd244b6a6a0e4d3a1e54bc93a0 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 19 Mar 2022 16:10:09 +0100 Subject: [PATCH 63/70] Server start refactoring, tutorial system Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/BungeeCore.java | 5 +- src/de/steamwar/bungeecore/EventStarter.java | 7 +- src/de/steamwar/bungeecore/ServerStarter.java | 272 ++++++++++++++++++ .../steamwar/bungeecore/SubserverSystem.java | 191 +----------- .../bungeecore/commands/BauCommand.java | 52 +--- .../bungeecore/commands/ChallengeCommand.java | 6 +- .../bungeecore/commands/CheckCommand.java | 5 +- .../bungeecore/commands/FightCommand.java | 3 +- .../bungeecore/commands/HistoricCommand.java | 8 +- .../bungeecore/commands/ReplayCommand.java | 16 +- .../bungeecore/commands/TutorialCommand.java | 91 +++++- .../comms/handlers/PrepareSchemHandler.java | 6 +- src/de/steamwar/bungeecore/sql/Tutorial.java | 31 +- .../steamwar/messages/BungeeCore.properties | 6 +- 14 files changed, 407 insertions(+), 292 deletions(-) create mode 100644 src/de/steamwar/bungeecore/ServerStarter.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 5fdbecfc..846dd5c9 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -120,10 +120,10 @@ public class BungeeCore extends Plugin { new ListCommand(); new StatCommand(); new VerifyCommand(); - new ReplayCommand(); new GDPRQuery(); new PlaytimeCommand(); new ArenaCommand(); + new RankCommand(); // Punishment Commands: new PunishmentCommand("ban", Punishment.PunishmentType.Ban); @@ -140,7 +140,8 @@ public class BungeeCore extends Plugin { new ChallengeCommand(); new HistoricCommand(); new CheckCommand(); - new RankCommand(); + new ReplayCommand(); + new TutorialCommand(); new Broadcaster(); }else{ diff --git a/src/de/steamwar/bungeecore/EventStarter.java b/src/de/steamwar/bungeecore/EventStarter.java index e0e29af9..98d75e39 100644 --- a/src/de/steamwar/bungeecore/EventStarter.java +++ b/src/de/steamwar/bungeecore/EventStarter.java @@ -64,12 +64,13 @@ public class EventStarter implements Runnable { Team blue = Team.get(next.getTeamBlue()); Team red = Team.get(next.getTeamRed()); - String serverName = blue.getTeamKuerzel() + " vs " + red.getTeamKuerzel(); - Subserver subserver = SubserverSystem.startEventArena(next, serverName); + ServerStarter starter = new ServerStarter().event(next); ProxiedPlayer leiter = ProxyServer.getInstance().getPlayer(SteamwarUser.get(next.getKampfleiter()).getUuid()); if(leiter != null) - SubserverSystem.sendPlayer(subserver, leiter); + starter.send(leiter); + + Subserver subserver = starter.start(); eventServer.put(blue.getTeamId(), subserver); eventServer.put(red.getTeamId(), subserver); diff --git a/src/de/steamwar/bungeecore/ServerStarter.java b/src/de/steamwar/bungeecore/ServerStarter.java new file mode 100644 index 00000000..39c1693f --- /dev/null +++ b/src/de/steamwar/bungeecore/ServerStarter.java @@ -0,0 +1,272 @@ +package de.steamwar.bungeecore; + +import de.steamwar.bungeecore.sql.EventFight; +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.Team; +import de.steamwar.bungeecore.sql.Tutorial; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.io.File; +import java.net.InetSocketAddress; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.function.BooleanSupplier; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class ServerStarter { + + private static final boolean MAIN_SERVER = ProxyServer.getInstance().getConfig().getListeners().stream().anyMatch(info -> ((InetSocketAddress) info.getSocketAddress()).getPort() == 25565); + private static final Portrange BAU_PORTS = MAIN_SERVER ? new Portrange(10100, 20000) : new Portrange(2100, 2200); + private static final Portrange ARENA_PORTS = MAIN_SERVER ? new Portrange(3000, 3100) : (BungeeCore.EVENT_MODE ? new Portrange(4000, 5000) : BAU_PORTS); + + private static final String BACKBONE = "/home/minecraft/"; + private static final String SERVER_PATH = BACKBONE + "server/"; + private static final String EVENT_PATH = BACKBONE + "event/"; + public static final String TEMP_WORLD_PATH = BACKBONE + "arenaserver/"; + public static final String TUTORIAL_PATH = BACKBONE + "tutorials/"; + + private File directory = null; + private String worldDir = null; + + private Node node = null; + private String serverJar = "spigot-1.15.2.jar"; + private String xmx = "768M"; + private Portrange portrange = BAU_PORTS; + private Function serverNameProvider = port -> node.getName() + port; + private BooleanSupplier startCondition = () -> true; + private ServerConstructor constructor = (serverName, port, builder, shutdownCallback) -> new Subserver(Servertype.ARENA, serverName, port, builder, shutdownCallback); + private Runnable worldSetup = () -> {}; + private String worldName = null; + private Runnable worldCleanup = () -> {}; + + private final Set playersToSend = new HashSet<>(); + private final Map arguments = new HashMap<>(); + + public ServerStarter arena(ArenaMode mode, String map) { + portrange = ARENA_PORTS; + serverNameProvider = port -> mode.getChatName() + (port - portrange.start); + serverJar = mode.serverJar(); + directory = new File(SERVER_PATH, mode.getFolder()); + arguments.put("config", mode.getConfig()); + tempWorld(SERVER_PATH + mode.getFolder() + "/arenas/" + map); + return this; + } + + public ServerStarter event(EventFight eventFight) { + arena(eventFight.getSpielmodus(), eventFight.getMap()); + node = Node.local; + worldDir = EVENT_PATH; + worldCleanup = () -> {}; + arguments.put("fightID", String.valueOf(eventFight.getFightID())); + + String serverName = Team.get(eventFight.getTeamBlue()).getTeamKuerzel() + " vs " + Team.get(eventFight.getTeamRed()).getTeamKuerzel(); + serverNameProvider = port -> serverName; + worldName = serverToWorldName(serverName + eventFight.getStartTime().toLocalDateTime().format(DateTimeFormatter.ISO_TIME)); + return this; + } + + public ServerStarter test(ArenaMode mode, String map, ProxiedPlayer owner) { + arena(mode, map); + buildWithTemp(owner); + portrange = BAU_PORTS; + arguments.put("fightID", String.valueOf(-1)); + return send(owner); + } + + public ServerStarter blueLeader(ProxiedPlayer player) { + arguments.put("blueLeader", player.getUniqueId().toString()); + return send(player); + } + + public ServerStarter redLeader(ProxiedPlayer player) { + arguments.put("redLeader", player.getUniqueId().toString()); + return send(player); + } + + public ServerStarter check(int schemID) { + arguments.put("checkSchemID", String.valueOf(schemID)); + return this; + } + + public ServerStarter prepare(int schemID) { + arguments.put("prepareSchemID", String.valueOf(schemID)); + return this; + } + + public ServerStarter replay(int replayID) { + arguments.put("replay", String.valueOf(replayID)); + return this; + } + + public ServerStarter build15(UUID owner) { + directory = new File(SERVER_PATH, "Bau15"); + worldDir = BungeeCore.USERWORLDS15; + worldName = String.valueOf(SteamwarUser.get(owner).getId()); + buildWithWorld(owner, BungeeCore.BAUWELT15); + return this; + } + + public ServerStarter build12(UUID owner) { + directory = new File(SERVER_PATH, "UserBau"); + serverJar = "spigot-1.12.2.jar"; + xmx = "256M"; + worldDir = BungeeCore.WORLD_FOLDER; + worldName = owner.toString(); + buildWithWorld(owner, BungeeCore.BAUWELT_PROTOTYP); + return this; + } + + public ServerStarter tutorial(ProxiedPlayer owner, Tutorial tutorial) { + directory = new File(SERVER_PATH, "Tutorial"); + buildWithTemp(owner); + tempWorld(TUTORIAL_PATH + tutorial.id()); + arguments.put("tutorial", String.valueOf(tutorial.id())); + return send(owner); + } + + private void tempWorld(String template) { + worldDir = TEMP_WORLD_PATH; + worldSetup = () -> copyWorld(node, template, worldDir + worldName); + worldCleanup = () -> SubserverSystem.deleteFolder(node, worldDir + worldName); + } + + private void buildWithWorld(UUID owner, String prototype) { + build(owner); + + worldSetup = () -> { + File world = new File(worldDir, worldName); + if (!world.exists()) + copyWorld(node, prototype, world.getPath()); + }; + + // Send players to existing server + startCondition = () -> { + for(Subserver subserver : Subserver.getServerList()) { + if(subserver.getType() == Servertype.BAUSERVER && ((Bauserver)subserver).getOwner().equals(owner)) { + for(ProxiedPlayer p : playersToSend) + SubserverSystem.sendPlayer(subserver, p); + return false; + } + } + return true; + }; + } + + private void buildWithTemp(ProxiedPlayer owner) { + build(owner.getUniqueId()); + + // Stop existing build server + startCondition = () -> { + if(startingBau(owner)) + return false; + + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(owner.getUniqueId()) && subserver.hasStarted()) { + subserver.stop(); + break; + } + } + + return !startingBau(owner); + }; + } + + private void build(UUID owner) { + constructor = (serverName, port, builder, shutdownCallback) -> new Bauserver(serverName, owner, port, builder, shutdownCallback); + serverNameProvider = port -> bauServerName(SteamwarUser.get(owner)); + } + + public ServerStarter send(ProxiedPlayer player) { + playersToSend.add(player); + return this; + } + + public Subserver start() { + if(!startCondition.getAsBoolean()) + return null; + + int port = portrange.freePort(); + String serverName = serverNameProvider.apply(port); + + if(node == null) + node = Node.getNode(); + if(worldName == null) + worldName = serverToWorldName(serverName); + + worldSetup.run(); + arguments.put("logPath", worldName); + + Subserver subserver = constructor.construct(serverName, port, node.startServer( + serverJar, directory, worldDir, worldName, port, xmx, (String[]) arguments.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).toArray() + ), worldCleanup); + + for(ProxiedPlayer p : playersToSend) + SubserverSystem.sendPlayer(subserver, p); + + return subserver; + } + + private static boolean startingBau(ProxiedPlayer p) { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId()) && !subserver.hasStarted()) { + Message.send("BAU_START_ALREADY", p); + return true; + } + } + return false; + } + + public static String bauServerName(SteamwarUser user) { + return user.getUserName() + "s Bau"; + } + + public static String serverToWorldName(String serverName) { + return serverName.replace(' ', '_').replace("[", "").replace("]", ""); + } + + public static void copyWorld(Node node, String template, String target) { + node.execute("cp", "-r", template, target); + } + + private interface ServerConstructor { + Subserver construct(String serverName, int port, ProcessBuilder builder, Runnable shutdownCallback); + } + + private static class Portrange { + + private final int start; + private final int end; + private int current; + + private Portrange(int start, int end) { + this.start = start; + this.end = end; + current = start; + } + + private void increment() { + current++; + + if(current == end) + current = start; + } + + private synchronized int freePort() { + Set usedPorts; + + synchronized (Subserver.getServerList()) { + usedPorts = Subserver.getServerList().stream().map(server -> ((InetSocketAddress) server.getServer().getSocketAddress()).getPort()).collect(Collectors.toSet()); + } + + while(usedPorts.contains(current)) { + increment(); + } + + int result = current; + increment(); + return result; + } + } +} diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index b0085f07..2908acb1 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -21,157 +21,21 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.comms.handlers.FightInfoHandler; import de.steamwar.bungeecore.comms.packets.StartingServerPacket; -import de.steamwar.bungeecore.sql.EventFight; import de.steamwar.bungeecore.sql.IgnoreSystem; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.connection.ProxiedPlayer; -import java.io.File; -import java.net.InetSocketAddress; -import java.time.format.DateTimeFormatter; -import java.util.Set; import java.util.UUID; -import java.util.stream.Collectors; public class SubserverSystem { private SubserverSystem(){} - private static final String BACKBONE = "/home/minecraft/"; - private static final String ARENA_PATH = BACKBONE + "arenaserver/"; - private static final String SERVER_PATH = BACKBONE + "server/"; - private static final String EVENT_PATH = BACKBONE + "event/"; - - private static final boolean MAIN_SERVER = ProxyServer.getInstance().getConfig().getListeners().stream().anyMatch(info -> ((InetSocketAddress) info.getSocketAddress()).getPort() == 25565); - private static final Portrange bauPorts = MAIN_SERVER ? new Portrange(10100, 20000) : new Portrange(2100, 2200); - private static final Portrange arenaPorts = MAIN_SERVER ? new Portrange(3000, 3100) : (BungeeCore.EVENT_MODE ? new Portrange(4000, 5000) : bauPorts); - - /** - * This function starts every arena (even test- and eventarenas). - * - * @param modus - * This has to be a valid arena mode - * - * @param map - * This String must be the internal map name of the selected map - * - * @param eventFightID - * For a normal arena: 0 - * For a test arena: -1 - * For an event arena: EventFightID of the fight - * - * @param replayID - * @param serverName - * The name of the server (for event and test arenas) - * or null (autogenerated arena name modus.getDisplayName() + number) - * - * @param mapName - * The name of the map copy the server runs on (for event and test arenas) - * or null (autogenerated map name equals server name) - * - * @param player1 - * For event and normal arenas: The UUID of the designated leader for the blue team - * or null (no designated leader). - * For test arenas: The owner's UUID of the test arena (disables normal function). - * - * @param player2 - * For event, test and normal arenas: The UUID of the designated leader for the red team - * or null (no designated leader). - * - * @return - * The new started subserver. - */ - public static synchronized Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, int prepareSchemID, int replayID, String serverName, String mapName, UUID player1, UUID player2){ - //Generate missing parameters - Node node = eventFightID > 0 ? Node.local : Node.getNode(); - int port = arenaPorts.freePort(); - - if(serverName == null){ - serverName = modus.getDisplayName() + (port - arenaPorts.start); - } - if(mapName == null) - mapName = serverName; - - mapName = mapName.replace(' ', '_').replace("[", "").replace("]", ""); - - String worldDir; - if(eventFightID > 0) - worldDir = EVENT_PATH; - else - worldDir = ARENA_PATH; - - //Copy world - node.execute("cp", "-r", SERVER_PATH + modus.getFolder() + "/arenas/" + map, worldDir + mapName); - - File directory = new File(SERVER_PATH, modus.getFolder()); - ProcessBuilder builder = node.startServer( - modus.serverJar(), directory, worldDir, mapName, port, "768M", - "logPath=" + mapName, "config=" + modus.getConfig(), - "fightID=" + eventFightID, - "checkSchemID=" + checkSchemID, "prepareSchemID=" + prepareSchemID, - "replay=" + replayID, - player1 != null && eventFightID != -1 ? "blueLeader=" + player1 : null, - player2 != null ? "redLeader=" + player2 : null - ); - - String finalMapName = mapName; - if(eventFightID == -1) - return new Bauserver(serverName, player1, port, builder, () -> deleteFolder(node, ARENA_PATH + finalMapName)); - else - return new Subserver(Servertype.ARENA, serverName, port, builder, () -> { - if(eventFightID > 0) - return; - deleteFolder(node, ARENA_PATH + finalMapName); - }); - } - - public static void deleteFolder(Node node, String worldName){ + public static void deleteFolder(Node node, String worldName) { node.execute("rm", "-r", worldName); } - public static Subserver startEventArena(EventFight eventFight, String serverName){ - return startArena( - eventFight.getSpielmodus(), - eventFight.getMap(), - eventFight.getFightID(), - 0, - 0, - 0, serverName, - serverName + eventFight.getStartTime().toLocalDateTime().format(DateTimeFormatter.ISO_TIME), - null, - null); - } - - public static void startTestServer(ProxiedPlayer p, ArenaMode m, String map, int checkSchemId, int prepareSchemId){ - sendPlayer(startArena(m, map, -1, checkSchemId, prepareSchemId, 0, p.getName() + "s Bau", p.getName(), p.getUniqueId(), null), p); - } - - private static synchronized void sendToBau(ProxiedPlayer p, UUID owner, String prototype, String worldFolder, String serverJar, String worldDir, String worldName, String xmx, String serverName){ - if(bauRunning(p, owner)) - return; - - copyBauweltIfRequired(prototype, worldFolder + worldName); - SteamwarUser user = SteamwarUser.get(owner); - File directory = new File(SERVER_PATH, serverName); - Node node = Node.getNode(); - int port = bauPorts.freePort(); - - - sendPlayer(new Bauserver(user.getUserName() + "s Bau", owner, port, node.startServer( - serverJar, directory, worldDir, worldName, port, xmx, "logPath=" + worldName - ), () -> {}), p); - } - - public static void sendToBauServer(ProxiedPlayer p, UUID owner){ - sendToBau(p, owner, BungeeCore.BAUWELT_PROTOTYP, BungeeCore.WORLD_FOLDER, "spigot-1.12.2.jar", "/home/minecraft/userworlds", owner.toString(), "256M", "UserBau"); - } - - public static void sendToBau15(ProxiedPlayer p, UUID owner){ - SteamwarUser user = SteamwarUser.get(owner); - sendToBau(p, owner, BungeeCore.BAUWELT15, BungeeCore.USERWORLDS15, "spigot-1.15.2.jar", "/home/minecraft/userworlds15", String.valueOf(user.getId()), "768M", "Bau15"); - } - public static void sendDeniedMessage(ProxiedPlayer p, UUID owner){ ProxiedPlayer o = ProxyServer.getInstance().getPlayer(owner); if(o == null) @@ -191,57 +55,4 @@ public class SubserverSystem { if(!subserver.hasStarted() && FightInfoHandler.onLobby(player)) new StartingServerPacket(SteamwarUser.get(player.getUniqueId())).send(player); } - - private static boolean bauRunning(ProxiedPlayer p, UUID owner){ - for(Subserver subserver : Subserver.getServerList()){ - if(subserver.getType() == Servertype.BAUSERVER && ((Bauserver)subserver).getOwner().equals(owner)){ - sendPlayer(subserver, p); - return true; - } - } - return false; - } - - private static void copyBauweltIfRequired(String sourcePath, String targetPath){ - File w = new File(targetPath); - if (!w.exists() || !w.isDirectory()) - Node.local.execute("cp", "-r", sourcePath, targetPath); - } - - - private static class Portrange { - - private final int start; - private final int end; - private int current; - - private Portrange(int start, int end) { - this.start = start; - this.end = end; - current = start; - } - - private void increment() { - current++; - - if(current == end) - current = start; - } - - private synchronized int freePort() { - Set usedPorts; - - synchronized (Subserver.getServerList()) { - usedPorts = Subserver.getServerList().stream().map(server -> ((InetSocketAddress) server.getServer().getSocketAddress()).getPort()).collect(Collectors.toSet()); - } - - while(usedPorts.contains(current)) { - increment(); - } - - int result = current; - increment(); - return result; - } - } } diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index cc9b8ef6..5a58a738 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -45,20 +45,17 @@ public class BauCommand extends BasicCommand { ProxiedPlayer p = (ProxiedPlayer) sender; if(args.length == 0){ - if(bau15(p, args, 0)) - SubserverSystem.sendToBau15(p, p.getUniqueId()); - else - SubserverSystem.sendToBauServer(p, p.getUniqueId()); + (bau15(p, args, 0) ? new ServerStarter().build15(p.getUniqueId()) : new ServerStarter().build12(p.getUniqueId())).send(p).start(); return; } switch(args[0].toLowerCase()){ - case "ws": - case "warship": case "12": case "1.12": - SubserverSystem.sendToBauServer(p, p.getUniqueId()); + new ServerStarter().build12(p.getUniqueId()).send(p).start(); break; + case "ws": + case "warship": case "as": case "airship": case "mwg": @@ -67,7 +64,7 @@ public class BauCommand extends BasicCommand { case "wargear": case "15": case "1.15": - SubserverSystem.sendToBau15(p, p.getUniqueId()); + new ServerStarter().build15(p.getUniqueId()).send(p).start(); break; case "addmember": addmember(p, args); @@ -143,10 +140,7 @@ public class BauCommand extends BasicCommand { return; } - if(bau15(p, args, 2)) - SubserverSystem.sendToBau15(p, worldOwner.getUuid()); - else - SubserverSystem.sendToBauServer(p, worldOwner.getUuid()); + (bau15(p, args, 2) ? new ServerStarter().build15(worldOwner.getUuid()) : new ServerStarter().build12(worldOwner.getUuid())).send(p).start(); } private static boolean bau15(ProxiedPlayer p, String[] args, int pos){ @@ -259,40 +253,8 @@ public class BauCommand extends BasicCommand { }); } - private static boolean startingBau(ProxiedPlayer p) { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId()) && !subserver.hasStarted()) { - Message.send("BAU_START_ALREADY", p); - return true; - } - } - return false; - } - - public static boolean stopBauserver(ProxiedPlayer p){ - if(startingBau(p)) - return false; - - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId()) && subserver.hasStarted()) { - subserver.stop(); - try { - Thread.sleep(200); // Wait until possible testarena-World has been deleted - } catch (InterruptedException e) { - throw new SecurityException("Subserver stop interrupted", e); - } - break; - } - } - - return !startingBau(p); - } - private static void testarena(ProxiedPlayer p, String[] args){ - FightCommand.createArena(p, "/bau testarena ", args, 1, false, (player, mode, map) -> ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - if(stopBauserver(p)) - SubserverSystem.startTestServer(p, mode, map, 0, 0); - })); + FightCommand.createArena(p, "/bau testarena ", args, 1, false, (player, mode, map) -> ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> new ServerStarter().test(mode, map, p).start())); } private static BauweltMember member(ProxiedPlayer p, SteamwarUser member){ diff --git a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java index 27d9ba73..7f480c1b 100644 --- a/src/de/steamwar/bungeecore/commands/ChallengeCommand.java +++ b/src/de/steamwar/bungeecore/commands/ChallengeCommand.java @@ -76,11 +76,7 @@ public class ChallengeCommand extends BasicCommand { challenges.remove(target); challenges.remove(player); - Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, 0, 0, null, null, player.getUniqueId(), target.getUniqueId()); - - SubserverSystem.sendPlayer(arena, player); - SubserverSystem.sendPlayer(arena, target); - + Subserver arena = new ServerStarter().arena(mode, map).blueLeader(player).redLeader(target).start(); Message.broadcast("CHALLENGE_BROADCAST", "CHALLENGE_BROADCAST_HOVER", new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/arena " + arena.getServer().getName()), mode.getDisplayName(), player.getName(), target.getName()); }else{ diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index a74dc788..b65b1cc4 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -228,13 +228,12 @@ public class CheckCommand extends BasicCommand { this.checkList = checkQuestions.get(schematic.getSchemtype()).listIterator(); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - if(!BauCommand.stopBauserver(checker)){ + ArenaMode mode = ArenaMode.getBySchemType(schematic.getSchemtype().fightType()); + if(new ServerStarter().test(mode, mode.getRandomMap(), checker).check(schematic.getId()).start() == null) { remove(); return; } - ArenaMode mode = ArenaMode.getBySchemType(schematic.getSchemtype().fightType()); - SubserverSystem.startTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getId(), 0); currentCheckers.put(checker.getUniqueId(), this); currentSchems.put(schematic.getId(), this); for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic)) diff --git a/src/de/steamwar/bungeecore/commands/FightCommand.java b/src/de/steamwar/bungeecore/commands/FightCommand.java index 1d2e396f..736d78dc 100644 --- a/src/de/steamwar/bungeecore/commands/FightCommand.java +++ b/src/de/steamwar/bungeecore/commands/FightCommand.java @@ -164,8 +164,7 @@ public class FightCommand extends BasicCommand { @Override public void execute(CommandSender sender, String[] args) { createArena(sender, "/fight ", args, 0, false, (player, mode, map) -> { - Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, 0, 0, null, null, player.getUniqueId(), null); - SubserverSystem.sendPlayer(arena, player); + Subserver arena = new ServerStarter().arena(mode, map).blueLeader(player).start(); Message.broadcast("FIGHT_BROADCAST", "FIGHT_BROADCAST_HOVER" , new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/arena " + arena.getServer().getName()), mode.getDisplayName(), player.getName()); }); diff --git a/src/de/steamwar/bungeecore/commands/HistoricCommand.java b/src/de/steamwar/bungeecore/commands/HistoricCommand.java index 6c9c0709..f07a6a3e 100644 --- a/src/de/steamwar/bungeecore/commands/HistoricCommand.java +++ b/src/de/steamwar/bungeecore/commands/HistoricCommand.java @@ -19,10 +19,7 @@ package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.ArenaMode; -import de.steamwar.bungeecore.Message; -import de.steamwar.bungeecore.Subserver; -import de.steamwar.bungeecore.SubserverSystem; +import de.steamwar.bungeecore.*; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.chat.ClickEvent; @@ -36,8 +33,7 @@ public class HistoricCommand extends BasicCommand { @Override public void execute(CommandSender sender, String[] args) { FightCommand.createArena(sender, "/historic ", args, 0, true, (player, mode, map) -> { - Subserver arena = SubserverSystem.startArena(mode, map, 0, 0, 0, 0, null, null, player.getUniqueId(), null); - SubserverSystem.sendPlayer(arena, player); + Subserver arena = new ServerStarter().arena(mode, map).blueLeader(player).start(); Message.broadcast("HISTORIC_BROADCAST", "HISTORIC_BROADCAST_HOVER" , new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/arena " + arena.getServer().getName()), mode.getDisplayName(), player.getName()); }); diff --git a/src/de/steamwar/bungeecore/commands/ReplayCommand.java b/src/de/steamwar/bungeecore/commands/ReplayCommand.java index 9ca94b4c..9ff3876f 100644 --- a/src/de/steamwar/bungeecore/commands/ReplayCommand.java +++ b/src/de/steamwar/bungeecore/commands/ReplayCommand.java @@ -19,9 +19,7 @@ package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.ArenaMode; -import de.steamwar.bungeecore.Message; -import de.steamwar.bungeecore.SubserverSystem; +import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; @@ -49,16 +47,16 @@ public class ReplayCommand extends BasicCommand { new SWStreamInv<>(player, Message.parse("REPLAY_TITLE", player), (click, fight) -> { SteamwarUser user = SteamwarUser.get(player.getUniqueId()); - boolean adminReplay = false; + ArenaMode mode = fight.getGameMode(); + ServerStarter starter = new ServerStarter().replay(fight.getFightID()).blueLeader(player); + if (user.getUserGroup().isAdminGroup() && click.isShiftClick() && fight.replayExists()) { - adminReplay = true; + starter.test(mode, mode.getRandomMap(), player).start(); } else if(!fight.replayAllowed()) { Message.send("REPLAY_UNAVAILABLE", player); - return; + } else { + starter.arena(mode, mode.getRandomMap()).start(); } - ArenaMode mode = fight.getGameMode(); - - SubserverSystem.sendPlayer(SubserverSystem.startArena(mode, mode.getRandomMap(), adminReplay ? -1 : 0, 0, 0, fight.getFightID(), null, null, null, null), player); }, page -> Fight.getPage(page, 45).stream().map(fight -> new SWListInv.SWListEntry<>(getFightItem(player, fight), fight)).collect(Collectors.toList())).open(); } diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index 253d8890..3a5b07c4 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -19,16 +19,20 @@ package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; +import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Tutorial; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; +import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.logging.Level; import java.util.stream.Collectors; public class TutorialCommand extends BasicCommand { @@ -43,12 +47,47 @@ public class TutorialCommand extends BasicCommand { return; ProxiedPlayer player = (ProxiedPlayer) sender; - //TODO: Tutorial rating - //TODO: Tutorial creation + if(args.length != 0) { + switch(args[0]) { + case "rate": + if(args.length < 2) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with missing argument"); + return; + } - new SWStreamInv<>(player, Message.parse("TUTORIAL_TITLE", player), (click, tutorial) -> { - //TODO: Tutorial start - }, page -> Tutorial.getPage(page, 45).stream().map(tutorial -> new SWListInv.SWListEntry<>(getTutorialItem(player, tutorial), tutorial)).collect(Collectors.toList())).open(); + int id; + try { + id = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with non number: " + args[1]); + return; + } + + Tutorial tutorial = Tutorial.get(id); + if(tutorial == null) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with nonexistent id: " + id); + return; + } + + rate(player, tutorial); + break; + case "create": + if(args.length < 3) { + Message.send("TUTORIAL_CREATE_HELP", player); + return; + } + + String material = args[1].toUpperCase(); + String name = Arrays.stream(args).skip(2).collect(Collectors.joining(" ")); + create(player, name, material); + break; + default: + Message.send("TUTORIAL_CREATE_HELP", player); + } + return; + } + + new SWStreamInv<>(player, Message.parse("TUTORIAL_TITLE", player), (click, tutorial) -> new ServerStarter().tutorial(player, tutorial).start(), page -> Tutorial.getPage(page, 45).stream().map(tutorial -> new SWListInv.SWListEntry<>(getTutorialItem(player, tutorial), tutorial)).collect(Collectors.toList())).open(); } private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial) { @@ -56,12 +95,44 @@ public class TutorialCommand extends BasicCommand { List lore = new ArrayList<>(); lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); - lore.add(""); - lore.add(Message.parse("TUTORIAL_BASED_ON", player)); - lore.add(Message.parse("TUTORIAL_NAME", player, tutorial.parent().name())); - lore.add(Message.parse("TUTORIAL_BY", player, tutorial.parent().creator().getUserName())); item.setLore(lore); return item; } + + private void rate(ProxiedPlayer player, Tutorial tutorial) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + int[] rates = new int[]{1, 2, 3, 4, 5}; + + new SWListInv<>(player, Message.parse("TUTORIAL_RATE_TITLE", player), Arrays.stream(rates).mapToObj(rate -> new SWListInv.SWListEntry<>(new SWItem("NETHER_STAR", Message.parse("TUTORIAL_RATE", player, rate)), rate)).collect(Collectors.toList()), (click, rate) -> { + tutorial.rate(user, rate); + }).open(); + } + + private void create(ProxiedPlayer player, String name, String item) { + Subserver subserver = Subserver.getSubserver(player); + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + File tempWorld = new File(ServerStarter.TEMP_WORLD_PATH, ServerStarter.serverToWorldName(ServerStarter.bauServerName(user))); + + if(subserver == null || !subserver.hasStarted() || subserver.getType() != Servertype.BAUSERVER || !tempWorld.exists()) { + Message.send("TUTORIAL_CREATE_MISSING", player); + return; + } + + subserver.execute("save-all"); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + BungeeCore.get().getLogger().log(Level.WARNING, "Tutorial creation interrupted", e); + Thread.currentThread().interrupt(); + } + + Tutorial tutorial = Tutorial.create(user, name, item); + File tutorialWorld = new File(ServerStarter.TUTORIAL_PATH, String.valueOf(tutorial.id())); + + if (tutorialWorld.exists()) + SubserverSystem.deleteFolder(Node.local, tutorialWorld.getPath()); + ServerStarter.copyWorld(Node.local, tempWorld.getPath(), tutorialWorld.getPath()); + Message.send("TUTORIAL_CREATED", player); + } } diff --git a/src/de/steamwar/bungeecore/comms/handlers/PrepareSchemHandler.java b/src/de/steamwar/bungeecore/comms/handlers/PrepareSchemHandler.java index 14171c57..d9681781 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/PrepareSchemHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/PrepareSchemHandler.java @@ -21,8 +21,7 @@ package de.steamwar.bungeecore.comms.handlers; import com.google.common.io.ByteArrayDataInput; import de.steamwar.bungeecore.ArenaMode; -import de.steamwar.bungeecore.SubserverSystem; -import de.steamwar.bungeecore.commands.BauCommand; +import de.steamwar.bungeecore.ServerStarter; import de.steamwar.bungeecore.comms.SpigotHandler; import de.steamwar.bungeecore.sql.SchematicType; import de.steamwar.bungeecore.sql.SteamwarUser; @@ -37,7 +36,6 @@ public class PrepareSchemHandler implements SpigotHandler { int schematicID = in.readInt(); ArenaMode mode = ArenaMode.getBySchemType(SchematicType.fromDB(in.readUTF())); - BauCommand.stopBauserver(player); - SubserverSystem.startTestServer(player, mode, mode.getRandomMap(), 0, schematicID); + new ServerStarter().test(mode, mode.getRandomMap(), player).prepare(schematicID).start(); } } diff --git a/src/de/steamwar/bungeecore/sql/Tutorial.java b/src/de/steamwar/bungeecore/sql/Tutorial.java index f9b49f15..8249d479 100644 --- a/src/de/steamwar/bungeecore/sql/Tutorial.java +++ b/src/de/steamwar/bungeecore/sql/Tutorial.java @@ -24,13 +24,14 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; public class Tutorial { - private static final Statement by_popularity = new Statement("SELECT t.TutorialID, t.Creator, t.Name, t.Item, AVG(r.Stars) AS Stars, p.TutorialID AS ParentID, p.Creator AS ParentCreator, p.Name AS ParentName FROM Tutorial t INNER JOIN Tutorial p ON t.Parent = p.TutorialID LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID GROUP BY t.TutorialID ORDER BY SUM(r.Stars) DESC LIMIT ?, ?"); + private static final Statement by_popularity = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID GROUP BY t.TutorialID ORDER BY SUM(r.Stars) DESC LIMIT ?, ?"); + private static final Statement by_creator_name = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID WHERE t.Creator = ? AND t.Name = ? GROUP BY t.TutorialID"); + private static final Statement by_id = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID WHERE t.TutorialID = ? GROUP BY t.TutorialID"); private static final Statement rate = new Statement("INSERT INTO TutorialRating (TutorialID, UserID, Stars) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Stars = VALUES(Stars)"); - private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item, Parent) VALUES (?, ?, ?, ?)"); + private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item)"); public static List getPage(int page, int elementsPerPage) { List tutorials = by_popularity.select(rs -> { @@ -40,24 +41,34 @@ public class Tutorial { return t; }, page * elementsPerPage, elementsPerPage); - SteamwarUser.batchCache(tutorials.stream().flatMap(tutorial -> Stream.of(tutorial.creator, tutorial.parent.creator)).collect(Collectors.toSet())); + SteamwarUser.batchCache(tutorials.stream().map(tutorial -> tutorial.creator).collect(Collectors.toSet())); return tutorials; } - public static void create(SteamwarUser creator, String name, Tutorial parent) { - create.update(creator.getId(), name, parent.id); + public static Tutorial create(SteamwarUser creator, String name, String item) { + create.update(creator.getId(), name, item); + return by_creator_name.select(rs -> { + rs.next(); + return new Tutorial(rs); + }, creator.getId(), name); + } + + public static Tutorial get(int id) { + return by_id.select(rs -> { + if(rs.next()) + return new Tutorial(rs); + return null; + }, id); } private final int id; private final int creator; private final String name; private final String item; - private Tutorial parent; private final int stars; public Tutorial(ResultSet rs) throws SQLException { this(rs.getInt("TutorialID"), rs.getInt("Creator"), rs.getString("Name"), rs.getString("Item"), rs.getInt("Stars")); - parent = new Tutorial(rs.getInt("ParentID"), rs.getInt("ParentCreator"), rs.getString("ParentName"), "", 0); } private Tutorial(int id, int creator, String name, String item, int stars) { @@ -72,10 +83,6 @@ public class Tutorial { return SteamwarUser.get(creator); } - public Tutorial parent() { - return parent; - } - public int stars() { return stars; } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 60b69c35..016aaf03 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -337,7 +337,11 @@ REPLAY_SERVER=§7{0} TUTORIAL_TITLE=Tutorials TUTORIAL_NAME=§e{0} TUTORIAL_BY=§8von §7{0} -TUTORIAL_BASED_ON=§8Basierend auf +TUTORIAL_RATE_TITLE=Tutorial bewerten +TUTORIAL_RATE=§e{0} §7Stern(e) +TUTORIAL_CREATE_HELP=§8/§7tutorial create §8[§eMaterial§8] §8[§eName§8] +TUTORIAL_CREATE_MISSING=§cEin Tutorial kann nur von einem Tutorialserver aus erstellt werden! +TUTORIAL_CREATED=§7Das Tutorial wurde erstellt§8. #ServerTeamchatCommand STC_USAGE=§8/§7stc §8[§eNachricht an das Team§8] From e2fd1754ce5639efd8ca5e33d4555eb463320d0e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 20 Mar 2022 19:08:55 +0100 Subject: [PATCH 64/70] Fix some bugs + minor improvements Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/ServerStarter.java | 2 +- .../bungeecore/commands/TutorialCommand.java | 5 ++++- .../bungeecore/inventory/SWInventory.java | 4 ++++ src/de/steamwar/bungeecore/sql/Tutorial.java | 18 +++++++----------- src/de/steamwar/messages/BungeeCore.properties | 1 + 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/de/steamwar/bungeecore/ServerStarter.java b/src/de/steamwar/bungeecore/ServerStarter.java index 39c1693f..622b7a42 100644 --- a/src/de/steamwar/bungeecore/ServerStarter.java +++ b/src/de/steamwar/bungeecore/ServerStarter.java @@ -199,7 +199,7 @@ public class ServerStarter { arguments.put("logPath", worldName); Subserver subserver = constructor.construct(serverName, port, node.startServer( - serverJar, directory, worldDir, worldName, port, xmx, (String[]) arguments.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).toArray() + serverJar, directory, worldDir, worldName, port, xmx, arguments.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).toArray(String[]::new) ), worldCleanup); for(ProxiedPlayer p : playersToSend) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index 3a5b07c4..340a0fdd 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.*; +import de.steamwar.bungeecore.inventory.SWInventory; import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; @@ -95,6 +96,7 @@ public class TutorialCommand extends BasicCommand { List lore = new ArrayList<>(); lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); + lore.add(Message.parse("TUTORIAL_STARS", player, tutorial.stars())); item.setLore(lore); return item; @@ -106,6 +108,7 @@ public class TutorialCommand extends BasicCommand { new SWListInv<>(player, Message.parse("TUTORIAL_RATE_TITLE", player), Arrays.stream(rates).mapToObj(rate -> new SWListInv.SWListEntry<>(new SWItem("NETHER_STAR", Message.parse("TUTORIAL_RATE", player, rate)), rate)).collect(Collectors.toList()), (click, rate) -> { tutorial.rate(user, rate); + SWInventory.close(player); }).open(); } @@ -121,7 +124,7 @@ public class TutorialCommand extends BasicCommand { subserver.execute("save-all"); try { - Thread.sleep(500); + Thread.sleep(1000); } catch (InterruptedException e) { BungeeCore.get().getLogger().log(Level.WARNING, "Tutorial creation interrupted", e); Thread.currentThread().interrupt(); diff --git a/src/de/steamwar/bungeecore/inventory/SWInventory.java b/src/de/steamwar/bungeecore/inventory/SWInventory.java index 31cb89ed..b827e884 100644 --- a/src/de/steamwar/bungeecore/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/inventory/SWInventory.java @@ -116,6 +116,10 @@ public class SWInventory { } public void close() { + close(player); + } + + public static void close(ProxiedPlayer player) { new CloseInventoryPacket(SteamwarUser.get(player).getId()).send(player); } diff --git a/src/de/steamwar/bungeecore/sql/Tutorial.java b/src/de/steamwar/bungeecore/sql/Tutorial.java index 8249d479..92272825 100644 --- a/src/de/steamwar/bungeecore/sql/Tutorial.java +++ b/src/de/steamwar/bungeecore/sql/Tutorial.java @@ -65,25 +65,21 @@ public class Tutorial { private final int creator; private final String name; private final String item; - private final int stars; + private final double stars; public Tutorial(ResultSet rs) throws SQLException { - this(rs.getInt("TutorialID"), rs.getInt("Creator"), rs.getString("Name"), rs.getString("Item"), rs.getInt("Stars")); - } - - private Tutorial(int id, int creator, String name, String item, int stars) { - this.id = id; - this.creator = creator; - this.name = name; - this.item = item; - this.stars = stars; + this.id = rs.getInt("TutorialID"); + this.creator = rs.getInt("Creator"); + this.name = rs.getString("Name"); + this.item = rs.getString("Item"); + this.stars = rs.getDouble("Stars"); } public SteamwarUser creator() { return SteamwarUser.get(creator); } - public int stars() { + public double stars() { return stars; } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 016aaf03..ff51762a 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -337,6 +337,7 @@ REPLAY_SERVER=§7{0} TUTORIAL_TITLE=Tutorials TUTORIAL_NAME=§e{0} TUTORIAL_BY=§8von §7{0} +TUTORIAL_STARS=§e{0} §7Sterne TUTORIAL_RATE_TITLE=Tutorial bewerten TUTORIAL_RATE=§e{0} §7Stern(e) TUTORIAL_CREATE_HELP=§8/§7tutorial create §8[§eMaterial§8] §8[§eName§8] From 0881878aa509e1ac0ce7dba1ebd5a338231923b2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 20 Mar 2022 21:54:33 +0100 Subject: [PATCH 65/70] Fix stuff --- .../bungeecore/comms/handlers/FightEndsHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index ffb725ef..0dda622d 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -50,8 +50,8 @@ public class FightEndsHandler implements SpigotHandler { return; } - boolean bluePublic = SchematicNode.getSchematicNode(fightEndsPacket.getBlueSchem()).getId() == 0; - boolean redPublic = SchematicNode.getSchematicNode(fightEndsPacket.getRedSchem()).getId() == 0; + boolean bluePublic = SchematicNode.getSchematicNode(fightEndsPacket.getBlueSchem()).getOwner() == 0; + boolean redPublic = SchematicNode.getSchematicNode(fightEndsPacket.getRedSchem()).getOwner() == 0; if (bluePublic ^ redPublic) { return; @@ -72,14 +72,15 @@ public class FightEndsHandler implements SpigotHandler { int blueTeamElo = fightEndsPacket.getBluePlayers().stream().mapToInt(player -> UserElo.getEloOrDefault(player, fightEndsPacket.getGameMode())).sum(); int redTeamElo = fightEndsPacket.getRedPlayers().stream().mapToInt(player -> UserElo.getEloOrDefault(player, fightEndsPacket.getGameMode())).sum(); - calculateEloOfTeam(fightEndsPacket.getBlueSchem(), blueSchemElo, redSchemElo, blueTeamElo, redTeamElo, blueResult, fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode()); - calculateEloOfTeam(fightEndsPacket.getRedSchem(), redSchemElo, blueSchemElo, redTeamElo, blueTeamElo, 1 - blueResult, fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode()); + calculateEloOfTeam(fightEndsPacket.getBlueSchem(), blueSchemElo, redSchemElo, blueTeamElo, redTeamElo, blueResult, fightEndsPacket.getBluePlayers(), fightEndsPacket.getGameMode(), bluePublic || redPublic); + calculateEloOfTeam(fightEndsPacket.getRedSchem(), redSchemElo, blueSchemElo, redTeamElo, blueTeamElo, 1 - blueResult, fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode(), bluePublic || redPublic); } - private void calculateEloOfTeam(int schemId, int eloSchemOwn, int eloSchemEnemy, int eloTeamOwn, int eloTeamEnemy, double result, List players, String gameMode) { + private void calculateEloOfTeam(int schemId, int eloSchemOwn, int eloSchemEnemy, int eloTeamOwn, int eloTeamEnemy, double result, List players, String gameMode, boolean noPlayerRank) { double winSchemExpectation = 1 / (1 + Math.pow(10, (eloSchemEnemy - eloSchemOwn) / 600f)); SchemElo.setElo(schemId, (int) Math.round(eloSchemOwn + K * (result - winSchemExpectation))); + if (noPlayerRank) return; double winTeamExpectation = 1 / (1 + Math.pow(10, (eloTeamEnemy - eloTeamOwn) / 600f)); for (int player : players) { int playerElo = UserElo.getEloOrDefault(player, gameMode); From 364422af2f9722b6f5c6b680bccf3db9b80b5ec5 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 22 Mar 2022 09:21:15 +0100 Subject: [PATCH 66/70] Tutorial release mechanism Signed-off-by: Lixfel --- .../bungeecore/commands/TutorialCommand.java | 38 +++++++++++++++++-- src/de/steamwar/bungeecore/sql/Tutorial.java | 29 ++++++++++++-- .../steamwar/messages/BungeeCore.properties | 1 + 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index 340a0fdd..c4d6ee3c 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -49,7 +49,7 @@ public class TutorialCommand extends BasicCommand { ProxiedPlayer player = (ProxiedPlayer) sender; if(args.length != 0) { - switch(args[0]) { + switch(args[0].toLowerCase()) { case "rate": if(args.length < 2) { BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with missing argument"); @@ -82,23 +82,55 @@ public class TutorialCommand extends BasicCommand { String name = Arrays.stream(args).skip(2).collect(Collectors.joining(" ")); create(player, name, material); break; + case "own": + openInventory(player, false, true); + break; + + case "unreleased": + if (SteamwarUser.get(player.getUniqueId()).getUserGroup().isTeamGroup()) { + openInventory(player, false, false); + break; + } default: Message.send("TUTORIAL_CREATE_HELP", player); + Message.send("TUTORIAL_OWN_HELP", player); } return; } - new SWStreamInv<>(player, Message.parse("TUTORIAL_TITLE", player), (click, tutorial) -> new ServerStarter().tutorial(player, tutorial).start(), page -> Tutorial.getPage(page, 45).stream().map(tutorial -> new SWListInv.SWListEntry<>(getTutorialItem(player, tutorial), tutorial)).collect(Collectors.toList())).open(); + openInventory(player, true, false); } - private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial) { + private void openInventory(ProxiedPlayer player, boolean released, boolean own) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + + new SWStreamInv<>( + player, + Message.parse("TUTORIAL_TITLE", player), + (click, tutorial) -> { + if(!released && click.isShiftClick() && user.getUserGroup().isTeamGroup() && user != tutorial.creator()) { + tutorial.release(); + return; + } + + new ServerStarter().tutorial(player, tutorial).start(); + }, + page -> (own ? Tutorial.getOwn(user, page, 45) : Tutorial.getPage(page, 45, released)).stream().map(tutorial -> new SWListInv.SWListEntry<>(getTutorialItem(player, tutorial, own), tutorial)).collect(Collectors.toList()) + ).open(); + } + + private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial, boolean highlightReleased) { SWItem item = new SWItem(tutorial.item(), Message.parse("TUTORIAL_NAME", player, tutorial.name())); + item.setHideAttributes(true); List lore = new ArrayList<>(); lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); lore.add(Message.parse("TUTORIAL_STARS", player, tutorial.stars())); item.setLore(lore); + if (highlightReleased && tutorial.released()) + item.setEnchanted(true); + return item; } diff --git a/src/de/steamwar/bungeecore/sql/Tutorial.java b/src/de/steamwar/bungeecore/sql/Tutorial.java index 92272825..4bbe7c43 100644 --- a/src/de/steamwar/bungeecore/sql/Tutorial.java +++ b/src/de/steamwar/bungeecore/sql/Tutorial.java @@ -27,24 +27,35 @@ import java.util.stream.Collectors; public class Tutorial { - private static final Statement by_popularity = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID GROUP BY t.TutorialID ORDER BY SUM(r.Stars) DESC LIMIT ?, ?"); + private static final Statement by_popularity = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID WHERE t.Released = ? GROUP BY t.TutorialID ORDER BY SUM(r.Stars) DESC LIMIT ?, ?"); + private static final Statement own = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID WHERE t.Creator = ? GROUP BY t.TutorialID ORDER BY t.TutorialID ASC LIMIT ?, ?"); private static final Statement by_creator_name = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID WHERE t.Creator = ? AND t.Name = ? GROUP BY t.TutorialID"); private static final Statement by_id = new Statement("SELECT t.*, AVG(r.Stars) AS Stars FROM Tutorial t LEFT OUTER JOIN TutorialRating r ON t.TutorialID = r.TutorialID WHERE t.TutorialID = ? GROUP BY t.TutorialID"); private static final Statement rate = new Statement("INSERT INTO TutorialRating (TutorialID, UserID, Stars) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Stars = VALUES(Stars)"); - private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item)"); + private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item), Released = 0"); + private static final Statement release = new Statement("UPDATE Tutorial SET Released = 1 WHERE TutorialID = ?"); - public static List getPage(int page, int elementsPerPage) { + public static List getPage(int page, int elementsPerPage, boolean released) { List tutorials = by_popularity.select(rs -> { List t = new ArrayList<>(); while(rs.next()) t.add(new Tutorial(rs)); return t; - }, page * elementsPerPage, elementsPerPage); + }, released, page * elementsPerPage, elementsPerPage); SteamwarUser.batchCache(tutorials.stream().map(tutorial -> tutorial.creator).collect(Collectors.toSet())); return tutorials; } + public static List getOwn(SteamwarUser user, int page, int elementsPerPage) { + return own.select(rs -> { + List t = new ArrayList<>(); + while(rs.next()) + t.add(new Tutorial(rs)); + return t; + }, user.getId(), page * elementsPerPage, elementsPerPage); + } + public static Tutorial create(SteamwarUser creator, String name, String item) { create.update(creator.getId(), name, item); return by_creator_name.select(rs -> { @@ -66,12 +77,14 @@ public class Tutorial { private final String name; private final String item; private final double stars; + private final boolean released; public Tutorial(ResultSet rs) throws SQLException { this.id = rs.getInt("TutorialID"); this.creator = rs.getInt("Creator"); this.name = rs.getString("Name"); this.item = rs.getString("Item"); + this.released = rs.getBoolean("Released"); this.stars = rs.getDouble("Stars"); } @@ -95,6 +108,14 @@ public class Tutorial { return item; } + public boolean released() { + return released; + } + + public void release() { + release.update(id); + } + public void rate(SteamwarUser user, int rating) { rate.update(id, user.getId(), rating); } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index ff51762a..4d66ebb2 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -343,6 +343,7 @@ TUTORIAL_RATE=§e{0} §7Stern(e) TUTORIAL_CREATE_HELP=§8/§7tutorial create §8[§eMaterial§8] §8[§eName§8] TUTORIAL_CREATE_MISSING=§cEin Tutorial kann nur von einem Tutorialserver aus erstellt werden! TUTORIAL_CREATED=§7Das Tutorial wurde erstellt§8. +TUTORIAL_OWN_HELP=§8/§7tutorial own §8- §7Liste der eigenen Tutorials #ServerTeamchatCommand STC_USAGE=§8/§7stc §8[§eNachricht an das Team§8] From 91f7f5edfc6d4a76a704efd9f068485e60e1d258 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 22 Mar 2022 10:37:10 +0100 Subject: [PATCH 67/70] Add tutorial rate command --- src/de/steamwar/bungeecore/commands/TutorialCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index c4d6ee3c..9b5bc0a9 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -26,6 +26,7 @@ import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Tutorial; +import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -52,7 +53,7 @@ public class TutorialCommand extends BasicCommand { switch(args[0].toLowerCase()) { case "rate": if(args.length < 2) { - BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with missing argument"); + BungeeCord.getInstance().getPluginManager().dispatchCommand(sender, "tutorial rate"); return; } From 577503114d7460380c1a99f78f8a7ebb413d8181 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 22 Mar 2022 10:42:47 +0100 Subject: [PATCH 68/70] Maybe now? --- src/de/steamwar/bungeecore/commands/TutorialCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index 9b5bc0a9..d855715d 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -53,7 +53,7 @@ public class TutorialCommand extends BasicCommand { switch(args[0].toLowerCase()) { case "rate": if(args.length < 2) { - BungeeCord.getInstance().getPluginManager().dispatchCommand(sender, "tutorial rate"); + player.chat("/tutorial rate"); return; } From c380bd82d3211a74c274f4f66e1834789a319208 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 22 Mar 2022 11:46:34 +0100 Subject: [PATCH 69/70] Tutorial deletion option Signed-off-by: Lixfel --- .../bungeecore/commands/TutorialCommand.java | 130 +++++++++--------- src/de/steamwar/bungeecore/sql/Tutorial.java | 15 ++ .../steamwar/messages/BungeeCore.properties | 1 + 3 files changed, 82 insertions(+), 64 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index d855715d..3a031e5d 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -26,14 +26,15 @@ import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.inventory.SWStreamInv; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Tutorial; -import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.stream.Collectors; @@ -49,57 +50,54 @@ public class TutorialCommand extends BasicCommand { return; ProxiedPlayer player = (ProxiedPlayer) sender; - if(args.length != 0) { - switch(args[0].toLowerCase()) { - case "rate": - if(args.length < 2) { - player.chat("/tutorial rate"); - return; - } - - int id; - try { - id = Integer.parseInt(args[1]); - } catch (NumberFormatException e) { - BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with non number: " + args[1]); - return; - } - - Tutorial tutorial = Tutorial.get(id); - if(tutorial == null) { - BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with nonexistent id: " + id); - return; - } - - rate(player, tutorial); - break; - case "create": - if(args.length < 3) { - Message.send("TUTORIAL_CREATE_HELP", player); - return; - } - - String material = args[1].toUpperCase(); - String name = Arrays.stream(args).skip(2).collect(Collectors.joining(" ")); - create(player, name, material); - break; - case "own": - openInventory(player, false, true); - break; - - case "unreleased": - if (SteamwarUser.get(player.getUniqueId()).getUserGroup().isTeamGroup()) { - openInventory(player, false, false); - break; - } - default: - Message.send("TUTORIAL_CREATE_HELP", player); - Message.send("TUTORIAL_OWN_HELP", player); - } + if(args.length == 0) { + openInventory(player, true, false); return; } - openInventory(player, true, false); + switch(args[0].toLowerCase()) { + case "rate": + if(args.length < 2) { + player.chat("/tutorial rate"); + return; + } + + int id; + try { + id = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with non number: " + args[1]); + return; + } + + Tutorial tutorial = Tutorial.get(id); + if(tutorial == null) { + BungeeCore.get().getLogger().log(Level.SEVERE, "rate executed with nonexistent id: " + id); + return; + } + + rate(player, tutorial); + break; + case "create": + if(args.length < 3) { + Message.send("TUTORIAL_CREATE_HELP", player); + return; + } + + create(player, Arrays.stream(args).skip(2).collect(Collectors.joining(" ")), args[1].toUpperCase()); + break; + case "own": + openInventory(player, false, true); + break; + case "unreleased": + if (SteamwarUser.get(player.getUniqueId()).getUserGroup().isTeamGroup()) { + openInventory(player, false, false); + break; + } + default: + Message.send("TUTORIAL_CREATE_HELP", player); + Message.send("TUTORIAL_OWN_HELP", player); + } } private void openInventory(ProxiedPlayer player, boolean released, boolean own) { @@ -111,6 +109,11 @@ public class TutorialCommand extends BasicCommand { (click, tutorial) -> { if(!released && click.isShiftClick() && user.getUserGroup().isTeamGroup() && user != tutorial.creator()) { tutorial.release(); + openInventory(player, released, own); + return; + } else if(own && click.isShiftClick() && click.isRightClick()) { + tutorial.delete(); + openInventory(player, released, own); return; } @@ -120,16 +123,20 @@ public class TutorialCommand extends BasicCommand { ).open(); } - private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial, boolean highlightReleased) { + private SWItem getTutorialItem(ProxiedPlayer player, Tutorial tutorial, boolean personalHighlights) { SWItem item = new SWItem(tutorial.item(), Message.parse("TUTORIAL_NAME", player, tutorial.name())); item.setHideAttributes(true); List lore = new ArrayList<>(); lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); lore.add(Message.parse("TUTORIAL_STARS", player, tutorial.stars())); + + if (personalHighlights) + lore.add(Message.parse("TUTORIAL_DELETE", player)); + item.setLore(lore); - if (highlightReleased && tutorial.released()) + if (personalHighlights && tutorial.released()) item.setEnchanted(true); return item; @@ -156,19 +163,14 @@ public class TutorialCommand extends BasicCommand { } subserver.execute("save-all"); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - BungeeCore.get().getLogger().log(Level.WARNING, "Tutorial creation interrupted", e); - Thread.currentThread().interrupt(); - } + ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { + Tutorial tutorial = Tutorial.create(user, name, item); + File tutorialWorld = tutorial.world(); - Tutorial tutorial = Tutorial.create(user, name, item); - File tutorialWorld = new File(ServerStarter.TUTORIAL_PATH, String.valueOf(tutorial.id())); - - if (tutorialWorld.exists()) - SubserverSystem.deleteFolder(Node.local, tutorialWorld.getPath()); - ServerStarter.copyWorld(Node.local, tempWorld.getPath(), tutorialWorld.getPath()); - Message.send("TUTORIAL_CREATED", player); + if (tutorialWorld.exists()) + SubserverSystem.deleteFolder(Node.local, tutorialWorld.getPath()); + ServerStarter.copyWorld(Node.local, tempWorld.getPath(), tutorialWorld.getPath()); + Message.send("TUTORIAL_CREATED", player); + }, 1, TimeUnit.SECONDS); } } diff --git a/src/de/steamwar/bungeecore/sql/Tutorial.java b/src/de/steamwar/bungeecore/sql/Tutorial.java index 4bbe7c43..75916f2e 100644 --- a/src/de/steamwar/bungeecore/sql/Tutorial.java +++ b/src/de/steamwar/bungeecore/sql/Tutorial.java @@ -19,6 +19,11 @@ package de.steamwar.bungeecore.sql; +import de.steamwar.bungeecore.Node; +import de.steamwar.bungeecore.ServerStarter; +import de.steamwar.bungeecore.SubserverSystem; + +import java.io.File; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -34,6 +39,7 @@ public class Tutorial { private static final Statement rate = new Statement("INSERT INTO TutorialRating (TutorialID, UserID, Stars) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Stars = VALUES(Stars)"); private static final Statement create = new Statement("INSERT INTO Tutorial (Creator, Name, Item) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item), Released = 0"); private static final Statement release = new Statement("UPDATE Tutorial SET Released = 1 WHERE TutorialID = ?"); + private static final Statement delete = new Statement("DELETE FROM Tutorial WHERE TutorialID = ?"); public static List getPage(int page, int elementsPerPage, boolean released) { List tutorials = by_popularity.select(rs -> { @@ -116,6 +122,15 @@ public class Tutorial { release.update(id); } + public void delete() { + delete.update(id); + SubserverSystem.deleteFolder(Node.local, world().getPath()); + } + + public File world() { + return new File(ServerStarter.TUTORIAL_PATH, String.valueOf(id)); + } + public void rate(SteamwarUser user, int rating) { rate.update(id, user.getId(), rating); } diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 4d66ebb2..19226ef8 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -340,6 +340,7 @@ TUTORIAL_BY=§8von §7{0} TUTORIAL_STARS=§e{0} §7Sterne TUTORIAL_RATE_TITLE=Tutorial bewerten TUTORIAL_RATE=§e{0} §7Stern(e) +TUTORIAL_DELETE=§cMit Shift+Rechtsklick löschen TUTORIAL_CREATE_HELP=§8/§7tutorial create §8[§eMaterial§8] §8[§eName§8] TUTORIAL_CREATE_MISSING=§cEin Tutorial kann nur von einem Tutorialserver aus erstellt werden! TUTORIAL_CREATED=§7Das Tutorial wurde erstellt§8. From 72a5027de084cf35c623ebcb929d1bec2bf4320b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 22 Mar 2022 19:07:47 +0100 Subject: [PATCH 70/70] Fix precision Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/commands/TutorialCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/commands/TutorialCommand.java b/src/de/steamwar/bungeecore/commands/TutorialCommand.java index 3a031e5d..4f557426 100644 --- a/src/de/steamwar/bungeecore/commands/TutorialCommand.java +++ b/src/de/steamwar/bungeecore/commands/TutorialCommand.java @@ -129,7 +129,7 @@ public class TutorialCommand extends BasicCommand { List lore = new ArrayList<>(); lore.add(Message.parse("TUTORIAL_BY", player, tutorial.creator().getUserName())); - lore.add(Message.parse("TUTORIAL_STARS", player, tutorial.stars())); + lore.add(Message.parse("TUTORIAL_STARS", player, String.format("%.1f", tutorial.stars()))); if (personalHighlights) lore.add(Message.parse("TUTORIAL_DELETE", player));