package app.playmancala.game; import java.util.ArrayList; import java.util.List; import java.util.Map; import app.playmancala.bot.BotSettings; import app.playmancala.engine.MancalaGame; import app.playmancala.model.GameClock; import app.playmancala.model.GameUsersConnectionInfo; import app.playmancala.model.PlayerSummary; // A live game and the server-side state around it. Only touched on the game loop. public class BackendGame { public final String id; public final MancalaGame mancalaGame; public GameUsersConnectionInfo gameUsersConnectionInfo; // Users watching right now. public final List spectatorIds = new ArrayList<>(); // Everyone who watched at some point, and the most at once. public final List allSpectatorIds; public int peakSpectators; // Keyed by user id, captured when the game is created. public final Map players; public final long startedAt; // Time of each move, aligned with mancalaGame.history. public final List moveTimes; public GameClock clock; // The mover's clock after each move, aligned with mancalaGame.history; null for moves made before the game clock. public final List moveRemainingMs; // How the bot plays in a game against the bot; null in games between people. public final BotSettings bot; public BackendGame(MancalaGame mancalaGame, GameUsersConnectionInfo gameUsersConnectionInfo, List allSpectatorIds, int peakSpectators, Map players, long startedAt, List moveTimes, GameClock clock, List moveRemainingMs, BotSettings bot) { this.id = mancalaGame.getId(); this.mancalaGame = mancalaGame; this.gameUsersConnectionInfo = gameUsersConnectionInfo; this.allSpectatorIds = new ArrayList<>(allSpectatorIds); this.peakSpectators = peakSpectators; this.players = Map.copyOf(players); this.startedAt = startedAt; this.moveTimes = new ArrayList<>(moveTimes); this.clock = clock; this.moveRemainingMs = new ArrayList<>(moveRemainingMs); this.bot = bot; } public boolean isBotTurn() { return bot != null && bot.botId().equals(mancalaGame.getTurnPlayerId()); } public PlayerSummary player(String userId) { PlayerSummary player = players.get(userId); return player != null ? player : PlayerSummary.unknown(userId); } public boolean isPlayer(String userId) { return mancalaGame.getPlayer1Id().equals(userId) || mancalaGame.getPlayer2Id().equals(userId); } public String opponentOf(String userId) { return userId.equals(mancalaGame.getPlayer1Id()) ? mancalaGame.getPlayer2Id() : mancalaGame.getPlayer1Id(); } }