package xyz.stachel.zombiesutils.game; import org.jetbrains.annotations.NotNull; import xyz.stachel.zombiesutils.handlers.Location; import xyz.stachel.zombiesutils.game.GameMode.Map; import xyz.stachel.zombiesutils.util.Utils; import java.util.HashMap; import java.util.Optional; import java.util.Set; public class GameManager { private final HashMap games = new HashMap<>(); private Optional queuedDifficulty = Optional.empty(); private String queuedDifficultyServer = "INVALID"; private void addGame(final String serverNumber, final GameMode mode, final int round) { if (serverNumber.equals(queuedDifficultyServer)) { this.queuedDifficulty.ifPresent(mode::changeDifficulty); } this.queuedDifficulty = Optional.empty(); this.games.put(serverNumber, new Game(new GameFile(serverNumber, mode), mode, round)); } public void onRound(final int round) { final String sn = Location.getServerNumber(); final String mode = Location.getMode(); if (sn == null || mode == null || !Utils.isZombies()) return; if (this.getGame(sn) == null) { this.addGame(sn, new GameMode(Utils.getMap(mode)), round); } else { this.getGame(sn).split(round); } } public Game getGame(String sn) { return this.games.get(sn); } public void endGame(final String serverNumber, final boolean isWin) { if (!games.containsKey(serverNumber)) return; final Game game = this.getGame(serverNumber); if (isWin) { switch (game.mode.getMap()) { case DEAD_END: case BAD_BLOOD: case PRISON: game.pass(30); break; case ALIEN_ARCADIUM: game.pass(105); break; } } games.remove(serverNumber); } public void splitOrNew(GameMode mode, int round) { final String serverNumber = Location.getServerNumber(); if (games.containsKey(serverNumber)) { if (round == 0) addGame(serverNumber, mode, round); else this.getGame(serverNumber).pass(round); } else addGame(serverNumber, mode, round); } public void setDifficulty(@NotNull GameMode.Difficulty difficulty) { this.queuedDifficultyServer = Location.getServerNumber(); if (this.games.containsKey(this.queuedDifficultyServer)) { this.getGame(this.queuedDifficultyServer).mode.changeDifficulty(difficulty); } else this.queuedDifficulty = Optional.of(difficulty); } public Set getGames() { return this.games.keySet(); } public void killAll() { games.clear(); } }