Class GameModel

java.lang.Object
it.polimi.ingsw.am13.model.GameModel
All Implemented Interfaces:
GameModelIF

public class GameModel extends Object implements GameModelIF
Model of the entire game. It can be used as an interface to handle the entire game, starting from its creation with a full room, till the natural end of the game. It handles also disconnections and reconnections, and is responsible for triggering all the notifies about game actions to the listeners
  • Field Details

    • gameId

      private final int gameId
      Unique number indicating the actual game model (hence the game/match to its all entirety)
    • match

      private final Match match
      Class match with all the information regarding the match itself and how to precess it
    • listenerHandler

      private final Room listenerHandler
      This is used to notify the view when a change occurs in the GameModel after a game event happens.
  • Constructor Details

    • GameModel

      public GameModel(Room listenerHandler) throws InvalidPlayersNumberException
      Creates a new instance of GameModel with the specified players. The players used here to create the model are the definitive players, and nobody can be added in a second time.
      Parameters:
      listenerHandler - Room with the players who will be the definite players of the game, corresponding to the handler of their listeners
      Throws:
      InvalidPlayersNumberException - If the list of players has size <2 or >4, the room did not reach the set number of players (the game for the room is not set as started), or there are duplicate chosen colors
  • Method Details

    • getListeners

      public List<GameListener> getListeners()
      Returns:
      Returns the list of GameListener handled by this class.
    • disconnectPlayer

      public void disconnectPlayer(PlayerLobby player) throws InvalidPlayerException, ConnectionException
      Disconnects the given player, by calling the corresponding method in match, removing the listener and notifying the listeners
      Parameters:
      player - Player to disconnect
      Throws:
      InvalidPlayerException - if the player associated to the GameListener is not a player of the match
      ConnectionException - if the player was already disconnected when this method was called
    • reconnectPlayer

      public void reconnectPlayer(GameListener gameListener, GameController controller) throws InvalidPlayerException, ConnectionException
      Reconnects the given player. It also notifies the players of this
      Parameters:
      gameListener - New listener of the player who want to reconnect
      controller - Controller of the game
      Throws:
      InvalidPlayerException - if the player associated to the GameListener is not a player of the match
      ConnectionException - if the player was already connected when this method was called
    • fetchIsConnected

      public boolean fetchIsConnected(PlayerLobby player) throws InvalidPlayerException
      Throws:
      InvalidPlayerException
    • countConnected

      public int countConnected()
    • getGameId

      public int getGameId()
      Specified by:
      getGameId in interface GameModelIF
      Returns:
      Unique number indicating the actual game model (hence the game/match to its all entirety)
    • fetchPlayersLobby

      public List<PlayerLobby> fetchPlayersLobby()
      Specified by:
      fetchPlayersLobby in interface GameModelIF
      Returns:
      List of players, decontextualized from game (their nicknames/tokens, nothing else)
    • fetchPlayers

      public List<PlayerIF> fetchPlayers()
      Specified by:
      fetchPlayers in interface GameModelIF
      Returns:
      List of players The order is the one followed in a round, from the first player to the last one
    • fetchFirstPlayer

      public PlayerLobby fetchFirstPlayer()
      The first player of the match is the one that will do the first play of the card in phase IN_GAME
      Specified by:
      fetchFirstPlayer in interface GameModelIF
      Returns:
      The first player of the match
    • fetchCurrentPlayer

      public PlayerLobby fetchCurrentPlayer()
      Specified by:
      fetchCurrentPlayer in interface GameModelIF
      Returns:
      the player who is playing in the current turn. Null if the game phase is different from IN_GAME or FINAL_PHASE (the 2 phases divided in turns)
    • fetchPoints

      public Map<PlayerLobby,Integer> fetchPoints()
      Specified by:
      fetchPoints in interface GameModelIF
      Returns:
      A map of points accumalated till this point for each player. If the turn-bases phases haven't started yet, the points will be 0
    • fetchGameStatus

      public GameStatus fetchGameStatus()
      Specified by:
      fetchGameStatus in interface GameModelIF
      Returns:
      the status of the game. See class GameStatus for more details
    • fetchPickables

      public List<CardPlayableIF> fetchPickables()
      List of all visible cards (that are pickable during turn phases). The list is of size 6, with order: top of deck (with getVisibleSide()==Side.SIDEBACK), and 2 visible cards (with getVisibleSide()==Side.SIDEFRONT), and repetion of this. Elements can be null. If a deck is empty but both its cards are present, only the first element of the set of 3 will be null. Besides this first element of the set, also one or both of the other ones can be null (if it remains only one or no cards of this type to be picked)
      Specified by:
      fetchPickables in interface GameModelIF
      Returns:
      a new list containing the 6 cards on the table that can be picked by players The order: top of resource deck, 2 visible resource cards, top of gold deck, 2 visible gold cards
    • fetchCommonObjectives

      public List<CardObjectiveIF> fetchCommonObjectives()
      Returns the visible cards for the common objectives. Note that the deck of objective cards is always visible, with top card visible from its back side. Hence, this information is not included in the list
      Specified by:
      fetchCommonObjectives in interface GameModelIF
      Returns:
      the list of the 2 common objectives.
    • fetchStarter

      public CardStarterIF fetchStarter(PlayerLobby player) throws InvalidPlayerException
      Specified by:
      fetchStarter in interface GameModelIF
      Parameters:
      player - One of the players of the match
      Returns:
      The starter card assigned to player. Null if player has not been assigned a starter card yet
      Throws:
      InvalidPlayerException - If the player is not among the playing players in the match
    • fetchHandPlayable

      public List<CardPlayableIF> fetchHandPlayable(PlayerLobby player) throws InvalidPlayerException
      Specified by:
      fetchHandPlayable in interface GameModelIF
      Parameters:
      player - one of the players of the match
      Returns:
      the cards in the hand of the player. The list if empty if player has no cards yet
      Throws:
      InvalidPlayerException - if the player is not one of the players of the match
    • fetchHandObjective

      public CardObjectiveIF fetchHandObjective(PlayerLobby player) throws InvalidPlayerException
      Specified by:
      fetchHandObjective in interface GameModelIF
      Parameters:
      player - one of the players of the match
      Returns:
      the personal objective of the player. Null if it hasn't been initialized yet
      Throws:
      InvalidPlayerException - if the passed player is not one of the players of the match
    • fetchAvailableCoord

      public List<Coordinates> fetchAvailableCoord(PlayerLobby player) throws InvalidPlayerException
      Specified by:
      fetchAvailableCoord in interface GameModelIF
      Returns:
      the List of coordinates in which new cards can be played If game has not started yet, the list is empty
      Throws:
      InvalidPlayerException - If player is not among this match's players
    • fetchPlayerField

      public FieldIF fetchPlayerField(PlayerLobby player) throws InvalidPlayerException
      Specified by:
      fetchPlayerField in interface GameModelIF
      Parameters:
      player - Player whose field is returned
      Returns:
      Field (interface) of the given player
      Throws:
      InvalidPlayerException - If the player is not among the playing players in the match
    • fetchWinner

      public List<PlayerLobby> fetchWinner()
      Specified by:
      fetchWinner in interface GameModelIF
      Returns:
      Winner of the game if the phase is ENDED, null otherwise
    • startGame

      public void startGame(GameController controller) throws GameStatusException
      Sets the starter Card, sets the possible objective cards, gives the initial cards to the players. Can be called only if the match has not started yet (gamePhase==null) and sets game phase to INIT.
      Throws:
      GameStatusException - if this method is called when game has already started (gamePhase!=null)
    • playStarter

      public void playStarter(PlayerLobby player, Side side) throws InvalidPlayerException, GameStatusException, InvalidPlayCardException
      Method callable only once per player during INIT phase. It plays the starter card of the given player on the passed side
      Parameters:
      player - Player who has chosen which side of the starting card he wants to play
      side - The side of the starting card
      Throws:
      InvalidPlayerException - If the player is not among the playing players in the match
      GameStatusException - If game phase is not INIT
      InvalidPlayCardException - Positioning error of the card at coordinates (0,0).
    • fetchPersonalObjectives

      public List<CardObjectiveIF> fetchPersonalObjectives(PlayerLobby player) throws InvalidPlayerException, GameStatusException
      Method callable only during INIT phase. Returns the 2 possible objective cards the player can choose
      Specified by:
      fetchPersonalObjectives in interface GameModelIF
      Parameters:
      player - who should be contained in players
      Returns:
      a list containing the two objective cards the player can choose from
      Throws:
      GameStatusException - if this method isn't called in the INIT phase
      InvalidPlayerException - if the player is not one of the players of this match
    • choosePersonalObjective

      Method callable only during INIT phase. Sets the personal objective of the player according to his choice. The objective card must be between the 2 possible cards given to the player. They can be retrieved with fetchPersonalObjectives(player)
      Parameters:
      player - one of the players of the match
      cardObj - the objective chosen by the player
      Throws:
      GameStatusException - if this method isn't called in the INIT phase
      InvalidPlayerException - if the player is not one of the players of this match
      InvalidChoiceException - if the objective card does not belong to the list of the possible objective cards for the player
      VariableAlreadySetException - if this method has been called before for the player
    • nextTurn

      public boolean nextTurn() throws GameStatusException
      Makes the match proceed from the turn that has just been played to the next one, by changing the currentPlayer and, if necessary, changing the GameStatus
      Returns:
      False if there is no other turn to play after the one that has just been played, true otherwise
      Throws:
      GameStatusException - If this method is called in the INIT or CALC POINTS phase
    • playCard

      Plays a given card side on the field, at the given coordinates
      Parameters:
      card - Card which is being played. It must be in player's hand
      side - Indicates whether the card is going to be played on the front or on the back
      coord - Coordinates in the field of the player where the card is going to be positioned
      Throws:
      RequirementsNotMetException - If the requirements for playing the specified card in player's field are not met
      InvalidPlayCardException - If the player doesn't have the specified card, or generic positioning error
      GameStatusException - If this method is called in the INIT or CALC POINTS phase
    • pickCard

      public void pickCard(CardPlayableIF card) throws InvalidDrawCardException, GameStatusException
      Picks one of the 6 cards on the table
      Parameters:
      card - The playable card to pick (that should be in the common field)
      Throws:
      InvalidDrawCardException - if the passed card is not on the table
      GameStatusException - if this method is called in the INIT or CALC POINTS phase
    • addObjectivePoints

      public void addObjectivePoints() throws GameStatusException
      Method callable once reached phase CALC_POINTS. This method adds the points given by Objective cards to each player. Make the game phase go on to ENDED
      Throws:
      GameStatusException - if this method is called in a phase which is not the CALC_POINTS phase
    • calcWinner

      public List<PlayerLobby> calcWinner() throws GameStatusException
      Method callable only once reached phase ENDED It finds the winner, ie the player(s) with the most points
      Returns:
      the winner of the match
      Throws:
      GameStatusException - if this method is called in a phase which is not the ENDED phase
    • transmitChatMessage

      public void transmitChatMessage(PlayerLobby sender, List<PlayerLobby> receivers, String text)
      Transmits a chat message (while this message will not cause any change in the model, nor will it be stored, it follows the same path as all the other messages)
      Parameters:
      sender - of the message
      receivers - of the message
      text - content of the message