SteamWar/BungeeCore
Archiviert
13
2

Update some stuff
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-04-04 20:00:21 +02:00
Ursprung c2219dec4e
Commit 350ac8b66f

Datei anzeigen

@ -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<String, List<Game>> gameModeGames = new HashMap<>();
private int K = 20;
private long defaultFightRange = 1000 /* Milliseconds */ * 60 /* Seconds */ * 15L /* Minutes */;
private Map<String, Long> 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<Integer> bluePlayers, List<Integer> redPlayers, String gameMode) {
long time = Timestamp.from(Instant.now()).getTime() - fightRanges.getOrDefault(gameMode, defaultFightRange);
int page = 0;
do {
List<Fight> 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<Game> 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<Integer> bluePlayers;
private final List<Integer> redPlayers;
public long livedMillis() {
return System.currentTimeMillis() - time;
}
public boolean isSame(List<Integer> bluePlayers, List<Integer> redPlayers) {
return bluePlayers.containsAll(this.bluePlayers) && redPlayers.containsAll(this.redPlayers);
}
}
}