package it.polimi.ingsw.gc14.Model; import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; import it.polimi.ingsw.gc14.Model.Cards.TribeCard; import it.polimi.ingsw.gc14.Model.GamePackage.Board; import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState; import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; 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 { /** Upper row of tribe cards currently on the board. */ public ArrayList upperListTribeCards; /** Lower row of tribe cards currently on the board. */ public ArrayList lowerListTribeCards; /** Upper row of building cards currently on the board. */ public ArrayList upperListBuildingCards; /** Lower row of building cards currently on the board. */ public ArrayList lowerListBuildingCards; /** Usernames of players currently disconnected from the game. */ public ArrayList disconnectedPlayers; /** The most recent network event applied to this model. */ public NetworkEvent lastEvent; /** * 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 with only the card lists populated. * * @param upperListTribeCards the upper row of tribe cards on the board. * @param lowerListTribeCards the lower row of tribe cards on the board. * @param upperListBuildingCards the upper row of building cards on the board. * @param lowerListBuildingCards the lower row of building cards on the board. */ public MiniModel(ArrayList upperListTribeCards,ArrayListlowerListTribeCards,ArrayList upperListBuildingCards,ArrayListlowerListBuildingCards) { this.upperListTribeCards = upperListTribeCards; this.lowerListTribeCards = lowerListTribeCards; this.upperListBuildingCards = upperListBuildingCards; this.lowerListBuildingCards = lowerListBuildingCards; this.disconnectedPlayers = new ArrayList<>(); } /** * Constructs a complete mini model from the current server-side game data. * * @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. * @param upperListTribeCards the upper row of tribe cards on the board. * @param lowerListTribeCards the lower row of tribe cards on the board. * @param upperListBuildingCards the upper row of building cards on the board. * @param lowerListBuildingCards the lower row of building cards on the board. * @param disconnectedPlayers the list of usernames of currently disconnected players. */ public MiniModel(Map slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List players, List availableTotems,ArrayList upperListTribeCards,ArrayListlowerListTribeCards,ArrayList upperListBuildingCards,ArrayListlowerListBuildingCards, ArrayList disconnectedPlayers) { this.slotPlayerMap = slotPlayerMap; this.orderLogicCard = orderLogicCard; this.currentState = currentState; this.players = new LinkedHashMap<>(); this.availableTotems = availableTotems; this.standingPlayers = new ArrayList<>(); this.upperListTribeCards = upperListTribeCards; this.lowerListTribeCards = lowerListTribeCards; this.upperListBuildingCards = upperListBuildingCards; this.lowerListBuildingCards = lowerListBuildingCards; this.disconnectedPlayers = disconnectedPlayers; setPlayers(players); } /** * Sets the last network event applied to the game state. * * @param lastEvent the most recent event received from the server. */ public void setLastEvent(NetworkEvent lastEvent) { this.lastEvent = lastEvent; } /** * 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(List standingPlayers) { this.standingPlayers = standingPlayers; } /** * Replaces the upper row of tribe cards on the board. * * @param cards the new upper tribe card list. */ public void setUpperListTribeCards(ArrayList cards) { this.upperListTribeCards = cards; } /** * Replaces the lower row of tribe cards on the board. * * @param cards the new lower tribe card list. */ public void setLowerListTribeCards(ArrayList cards) { this.lowerListTribeCards = cards; } /** * Replaces the upper row of building cards on the board. * * @param cards the new upper building card list. */ public void setUpperListBuildingCards(ArrayList cards) { this.upperListBuildingCards = cards; } /** * Replaces the lower row of building cards on the board. * * @param cards the new lower building card list. */ public void setLowerListBuildingCards(ArrayList cards) { this.lowerListBuildingCards = cards; } /** * Removes the card at the given position from the upper tribe card list. * * @param pos the zero-based index of the card to remove. */ public void removeUpperTribeCard(int pos) { if (upperListTribeCards != null) upperListTribeCards.remove(pos); } /** * Removes the card at the given position from the lower tribe card list. * * @param pos the zero-based index of the card to remove. */ public void removeLowerTribeCard(int pos) { if (lowerListTribeCards != null) lowerListTribeCards.remove(pos); } /** * Removes the card at the given position from the upper building card list. * * @param pos the zero-based index of the card to remove. */ public void removeUpperBuildingCard(int pos) { if (upperListBuildingCards != null) upperListBuildingCards.remove(pos); } /** * Removes the card at the given position from the lower building card list. * * @param pos the zero-based index of the card to remove. */ public void removeLowerBuildingCard(int pos) { if (lowerListBuildingCards != null) lowerListBuildingCards.remove(pos); } /** * 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; } /** * Sets the list of usernames of currently disconnected players. * * @param disconnectedPlayers the list of disconnected player usernames. */ public void setDisconnectedPlayers (ArrayList disconnectedPlayers){ this.disconnectedPlayers=disconnectedPlayers; } }