package it.polimi.ingsw.gc14.Model; import it.polimi.ingsw.gc14.Model.GamePackage.Board; import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState; import java.io.Serializable; import java.util.*; /** * Lightweight representation of the game model shared with clients. * *

The mini model contains only the information required by the client-side * view and controllers to render the current match state and process incoming * network updates. */ public class MiniModel implements Serializable { /** * Current game board. */ public Board board; /** * Map associating each occupied slot with the corresponding player. */ public Map slotPlayerMap; /** * Card used to manage the player turn order. */ public OrderLogicCard orderLogicCard; /** * Current state of the game. */ public CurrentState currentState; /** * Map of players indexed by username. */ public Map players; /** * List of totems still available for selection. */ public List availableTotems; /** * Final standing of the players at the end of the game. */ public List standingPlayers; /** * Constructs an empty mini model. */ public MiniModel() { } /** * Constructs a mini model containing only the game board. * * @param board the current game board. */ public MiniModel(Board board) { this.board = board; } /** * Constructs a complete mini model from the current server-side game data. * * @param board the current game board. * @param slotPlayerMap the map associating occupied slots with players. * @param orderLogicCard the card managing player turn order. * @param currentState the current state of the game. * @param players the list of players in the match. * @param availableTotems the list of totems still available for selection. */ public MiniModel(Board board, Map slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List players, List availableTotems) { this.board = board; this.slotPlayerMap = slotPlayerMap; this.orderLogicCard = orderLogicCard; this.currentState = currentState; this.players = new HashMap<>(); this.availableTotems = availableTotems; this.standingPlayers = new ArrayList<>(); setPlayers(players); } /** * Sets the current game board. * * @param board the board to assign. */ public void setBoard(Board board) { this.board = board; } /** * Sets the list of totems still available for selection. * * @param availableTotems the available totems. */ public void setAvailableTotems(List availableTotems) { this.availableTotems = availableTotems; } /** * Sets the map associating occupied slots with players. * * @param slotPlayerMap the slot-player map to assign. */ public void setSlotPlayerMap(Map slotPlayerMap) { this.slotPlayerMap = slotPlayerMap; } /** * Sets the card used to manage the player turn order. * * @param orderLogicCard the order logic card to assign. */ public void setOrderLogicCard(OrderLogicCard orderLogicCard) { this.orderLogicCard = orderLogicCard; } /** * Sets the current state of the game. * * @param currentState the current game state. */ public void setCurrentState(CurrentState currentState) { this.currentState = currentState; } /** * Replaces or updates the players stored in the mini model. * *

Each player is indexed by username. * * @param players the list of players to store. */ public void setPlayers(List players) { for (Player player : players) { this.players.put(player.getUserName(), player); } } /** * Adds or updates a single player in the mini model. * * @param player the player to store. */ public void setPlayer(Player player) { players.put(player.getUserName(), player); } /** * Sets the final standing of the players. * * @param standingPlayers the final ordered list of players. */ public void setStandingPlayers(ArrayList standingPlayers) { this.standingPlayers = standingPlayers; } /** * Returns the position of the player with the specified username * within the player collection. * * @param username the username of the player to search for. * @return the zero-based position of the player, or {@code -1} * if no matching username is found. */ public int getPositionByUsername(String username) { int i = 0; for (Player player : players.values()) { if (player.getUserName().equals(username)) { return i; } i++; } return -1; } }