From 727a27a9fb855dfe022084ed3070b8cfc7f58cc1 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 11 May 2023 16:05:21 +0200 Subject: [PATCH] Update UserElo for new ranked system --- src/de/steamwar/sql/UserElo.java | 134 +++++++++++++++++-------------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/src/de/steamwar/sql/UserElo.java b/src/de/steamwar/sql/UserElo.java index 5de692c..c13c603 100644 --- a/src/de/steamwar/sql/UserElo.java +++ b/src/de/steamwar/sql/UserElo.java @@ -33,22 +33,18 @@ import java.util.concurrent.ConcurrentHashMap; @AllArgsConstructor public class UserElo { - private static final int ELO_DEFAULT = 1000; private static final Map>> gameModeUserEloCache = new ConcurrentHashMap<>(); - private static final Map maxEloCache = new ConcurrentHashMap<>(); private static final Map emblemCache = new ConcurrentHashMap<>(); public static void clear() { gameModeUserEloCache.clear(); - maxEloCache.clear(); emblemCache.clear(); } private static final Table table = new Table<>(UserElo.class); private static final SelectStatement getElo = table.select(Table.PRIMARY); private static final Statement setElo = table.insertAll(); - 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(?)"); @@ -63,7 +59,7 @@ public class UserElo { private final int elo; public static int getEloOrDefault(int userID, String gameMode) { - return getElo(userID, gameMode).orElse(ELO_DEFAULT); + return getElo(userID, gameMode).orElse(0); } public static Optional getElo(int userID, String gameMode) { @@ -78,21 +74,13 @@ public class UserElo { }, userID, gameMode, Season.getSeasonStart()); } - 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 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); + int oldPlacement = getPlacement(getElo(userId, gameMode).orElse(0), gameMode); + int newPlacement = getPlacement(elo, gameMode); + + if (oldPlacement <= 3 || newPlacement <= 3) { emblemCache.clear(); } @@ -109,70 +97,92 @@ public class UserElo { public static String getEmblem(SteamwarUser user, List rankedModes) { return emblemCache.computeIfAbsent(user.getId(), userId -> { - switch( - rankedModes.stream().filter( - mode -> UserElo.getFightsOfSeason(user.getId(), mode) >= 10 - ).map( - mode -> getProgression(user.getId(), mode) - ).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"); + int emblemProgression = -1; + for (String mode : rankedModes) { + if (UserElo.getFightsOfSeason(userId, mode) == 0) continue; + int progression = getProgression(userId, mode); + if (progression > emblemProgression) { + emblemProgression = progression; + } } + return toEmblem(emblemProgression); }); } public static String getEmblemProgression(String gameMode, int userId) { switch (getProgression(userId, gameMode)) { + case -1: + return "§f/ §6∨ ∧ §7∨ ∧ §e∨ ∧ §5❂ III II I"; case 0: - return "§8✧ ✦ ✶ ✷ ✸ ✹ ❂"; + return "§8/ §6∨ §8∧ ∨ ∧ ∨ ∧ ❂ III II I"; case 1: - return "§7✧ §8✦ ✶ ✷ ✸ ✹ ❂"; + return "§8/ ∨ §6∧ §8∨ ∧ ∨ ∧ ❂ III II I"; case 2: - return "§8✧ §f✦ §8✶ ✷ ✸ ✹ ❂"; + return "§8/ ∨ ∧ §7∨ §8∧ ∨ ∧ ❂ III II I"; case 3: - return "§8✧ ✦ §e✶ §8✷ ✸ ✹ ❂"; + return "§8/ ∨ ∧ ∨ §7∧ §8∨ ∧ ❂ III II I"; case 4: - return "§8✧ ✦ ✶ §a✷ §8✸ ✹ ❂"; + return "§8/ ∨ ∧ ∨ ∧ §e∨ §8∧ ❂ III II I"; case 5: - return "§8✧ ✦ ✶ ✷ §b✸ §8✹ ❂"; + return "§8/ ∨ ∧ ∨ ∧ ∨ §e∧ §8❂ III II I"; case 6: - return "§8✧ ✦ ✶ ✷ ✸ §c✹ §8❂"; + return "§8/ ∨ ∧ ∨ ∧ ∨ ∧ §5❂ §8III II I"; case 7: - return "§8✧ ✦ ✶ ✷ ✸ ✹ §5❂"; + return "§8/ ∨ ∧ ∨ ∧ ∨ ∧ ❂ §5III §8II I"; + case 8: + return "§8/ ∨ ∧ ∨ ∧ ∨ ∧ ❂ III §5II §8I"; + case 9: + return "§8/ ∨ ∧ ∨ ∧ ∨ ∧ ❂ III II §5I"; default: throw new SecurityException("Progression is not in range"); } } - private static int getProgression(int userId, String gameMode) { - int elo = getElo(userId, gameMode).orElse(0); - if(elo == 0) - return 0; - int maxElo = getMaxElo(gameMode); + public static int getProgression(int userId, String gameMode) { + int elo = getElo(userId, gameMode).orElse(-1); + if (elo < 0) return -1; - 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; + if (elo <= 100) return 0; + if (elo <= 200) return 1; + if (elo <= 400) return 2; + if (elo <= 600) return 3; + if (elo <= 900) return 4; + if (elo <= 1200) return 5; + + int placement = getPlacement(elo, gameMode); + if (placement == 1) return 9; + if (placement == 2) return 8; + if (placement == 3) return 7; + + return 6; + } + + public static String toEmblem(int progression) { + switch(progression) { + case -1: + return ""; + case 0: + return "§6∨ "; + case 1: + return "§6∧ "; + case 2: + return "§7∨ "; + case 3: + return "§7∧ "; + case 4: + return "§e∨ "; + case 5: + return "§e∧ "; + case 6: + return "§5❂ "; + case 7: + return "§5III "; + case 8: + return "§5II "; + case 9: + return "§5I "; + default: + throw new SecurityException("Progression out of range"); + } } }