Class GameController

java.lang.Object
it.polimi.ingsw.am13.controller.GameController

public class GameController extends Object
This is a controller for a single game/match
  • Field Details

    • gameModel

      private final GameModel gameModel
      The model of the game
    • reconnectionThread

      private Thread reconnectionThread
      Thread used to manage the reconnection timer which is started when there only 1 player left
  • Constructor Details

    • GameController

      GameController(Room listenerHandler) throws InvalidPlayersNumberException
      Creates a new instance of the controller of the game with the specified room, which must be full. The players used here to create the model are the definitive players, and nobody can be added afterwards. It also starts the game
      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 lists nicks, colors have size <2 or >4, or there are duplicate chosen colors
  • Method Details

    • updatePing

      public void updatePing(PlayerLobby playerLobby)
      Updates the ping of the given game listener by setting it to the current time
      Parameters:
      playerLobby - one of players of the match
    • checkPing

      public void checkPing()
      This method checks the pings of each game listener every sleepTime ms and disconnects players whose game listener ping is too old
    • disconnectPlayer

      public void disconnectPlayer(PlayerLobby player, long timeToWait) throws InvalidPlayerException
      disconnection method only for tests
      Throws:
      InvalidPlayerException
    • disconnectPlayer

      public void disconnectPlayer(PlayerLobby player) throws InvalidPlayerException, ConnectionException, LobbyException
      Disconnects the specified player and starts the reconnection timer if there is only one player left It ends the game if, after the disconnection, no players are
      Parameters:
      player - Player to disconnect
      Throws:
      InvalidPlayerException - if the player corresponding to gameListener is not one of the players of the match
      ConnectionException - if the player had already been disconnected
      LobbyException - if gameListener didn't belong to ListenerHandler
    • reconnectPlayer

      void reconnectPlayer(GameListener gameListener) throws InvalidPlayerException, ConnectionException, GameStatusException
      Reconnects the player corresponding to the game listener and triggers the notification of this. If more than one player is connected, it stops the reconnection timer If two players are connected, it advances to the next turn. Note that for a client to reconnect, they should invoke Lobby's reconnection method
      Parameters:
      gameListener - one of the listeners of ListenerHandler
      Throws:
      InvalidPlayerException - if the player corresponding to gameListener is not one of the players of the match
      ConnectionException - if the player was already connected when this method was called
      GameStatusException
    • startReconnectionTimer

      private void startReconnectionTimer(long timeToWait)
      Starts the reconnection timer. When it expires, it checks the number of connected players. If there are none, it deletes the game. If there is only one, it calculates the winner (who will be the only connected player) and then deletes the game. Otherwise, it resets the timer by setting the reconnectionThread to null.
      Parameters:
      timeToWait - Time in millis to wait for reconnection
    • stopReconnectionTimer

      private void stopReconnectionTimer()
      Stops the reconnection timer by setting the reconnection timer by interrupting the reconnection thread and then setting it to null
    • getGameId

      public int getGameId()
      Returns:
      Unique number indicating the actual game model (hence the game/match to its all entirety)
    • playStarter

      public void playStarter(PlayerLobby player, Side side) throws InvalidPlayerException, InvalidPlayCardException, GameStatusException
      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).
    • 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
    • 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
      GameStatusException - If this method is called in the INIT or CALC POINTS phase
      InvalidPlayerException - If the passed player is not the current player
    • pickCard

      Picks one of the 6 cards on the table, and makes the game proceed to the next Turn.
      Parameters:
      card - A playable card that should be in the 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
      InvalidPlayerException - If the passed player is not the current player
    • nextTurn

      private void nextTurn() throws GameStatusException
      Advances to the next turn. If the game is finished, it also adds the objective points and calculates the winner. If, after advancing to the next turn, the current player is not connected, it calls playCard and pickCard with null parameters so that the game can proceed correctly
      Throws:
      GameStatusException - if any of the methods it calls is called in the wrong game phase
    • getPlayers

      public List<PlayerLobby> getPlayers()
      Returns:
      the players of the match
    • 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