diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 7392d04..f86eacf 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -24,18 +24,16 @@ import de.steamwar.bungeecore.ArenaMode; import de.steamwar.bungeecore.comms.SpigotHandler; import de.steamwar.bungeecore.comms.packets.FightEndsPacket; import de.steamwar.bungeecore.sql.*; +import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.config.ServerInfo; -import java.sql.Timestamp; -import java.time.Instant; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; public class FightEndsHandler implements SpigotHandler { + private Map> gameModeGames = new HashMap<>(); + private int K = 20; private long defaultFightRange = 1000 /* Milliseconds */ * 60 /* Seconds */ * 15L /* Minutes */; private Map fightRanges = new HashMap<>(); @@ -82,8 +80,12 @@ public class FightEndsHandler implements SpigotHandler { } } - if (teamComboExistedAlready(fightEndsPacket.getBluePlayers(), fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode())) { - return; + try { + if (teamComboExistedAlready(fightEndsPacket.getBluePlayers(), fightEndsPacket.getRedPlayers(), fightEndsPacket.getGameMode())) { + return; + } + } finally { + gameModeGames.computeIfAbsent(fightEndsPacket.getGameMode(), s -> new ArrayList<>()).add(new Game(fightEndsPacket.getBluePlayers(), fightEndsPacket.getRedPlayers())); } int blueSchemElo = SchemElo.getElo(fightEndsPacket.getBlueSchem()); @@ -118,28 +120,27 @@ public class FightEndsHandler implements SpigotHandler { } private boolean teamComboExistedAlready(List bluePlayers, List redPlayers, String gameMode) { - long time = Timestamp.from(Instant.now()).getTime() - fightRanges.getOrDefault(gameMode, defaultFightRange); - int page = 0; - do { - List fightsPage = Fight.getPage(page, 32); - if (fightsPage.isEmpty()) { - return false; - } - for (Fight fight : fightsPage) { - if (!gameMode.equals(fight.getGameMode().getSchemType())) continue; - if (fight.getStartTime().getTime() < time) return false; - if (fight.getBluePlayers().stream() - .map(FightPlayer::getUserID) - .collect(Collectors.toList()) - .containsAll(bluePlayers) - && fight.getRedPlayers().stream() - .map(FightPlayer::getUserID) - .collect(Collectors.toList()) - .containsAll(redPlayers)) { - return true; - } - } - page++; - } while (true); + if (!gameModeGames.containsKey(gameMode)) { + return false; + } + List games = gameModeGames.get(gameMode); + long lifetime = fightRanges.getOrDefault(gameMode, defaultFightRange); + games.removeIf(game -> game.livedMillis() > lifetime); + return games.stream().anyMatch(game -> game.isSame(bluePlayers, redPlayers)); + } + + @RequiredArgsConstructor + private static class Game { + private long time = System.currentTimeMillis(); + private final List bluePlayers; + private final List redPlayers; + + public long livedMillis() { + return System.currentTimeMillis() - time; + } + + public boolean isSame(List bluePlayers, List redPlayers) { + return bluePlayers.containsAll(this.bluePlayers) && redPlayers.containsAll(this.redPlayers); + } } }