java.lang.Object
it.polimi.ingsw.eriantys.controller.Game

public class Game extends Object
This class handles the control flow of a game according to the rules. It manages every round and turn and communicates with the players in order to provide updates about the game, as well as interacting with the model through GameManager. Game implements a state pattern in order to manage the various phases of the game and exposes several methods which are used to advance the game.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Game(Server server, int gameId, String creator, int lobbySize, boolean expertMode)
    Constructs a Game that fits the specified parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Responds to a GameMessage sent by a client with an Accepted message.
    addPlayer(String username)
    If the specified username is already in the game or the lobby is full returns null, otherwise adds the username to the players and returns the passcode which the player can use to reconnect to the game.
    void
    Advances to the next player's turn in the current round, updating the message handler, and advances to the next round if necessary.
    boolean
    checkCredentials(String username, String passcode)
    Returns true if and only if the specified credentials are valid and the player in question is allowed to reconnect to the game.
    void
    If the current player is disconnected calls the disconnected turn handler.
    void
    disconnect(String username)
    Disconnects the specified player from the game, removing them from the lobby if the game has not yet started or pausing the game if the game has started and there are less than 2 players currently connected.
    void
    Ends the game by notifying every player and deleting every reference to it.
    Returns a map having the nickname of every player as the key set and each player's remaining assistant cards as the values.
    int
    Returns the size of the clouds for this game.
    Returns the username of the current player, or null if the game has not started yet.
    Returns a GameInfo object containing the unique id of the game, the number of players allowed to participate, the expert mode flag and the username of the game's creator.
    void
    Handles a GameMessage sent by a client through the current phase's message handler.
    boolean
    Returns true if and only if no players are currently participating in the game.
    boolean
    Returns true if this game has started, or false if it is still waiting for new players to join.
    boolean
    Returns true if and only if the game has reached the desired number of participants and can start.
    boolean
    moveMotherNature(String destination)
    Handles Mother Nature's movement to the specified destination.
    void
    moveStudent(String username, String color, String destination)
    Handles the movement of the student specified by the parameters.
    void
    Advances to the next round of the game, or ends the game if the round which just ended was the last to be played.
    void
    newTurn(Map<String,String> playedCards)
    Handles the assistant cards played by the players and sets up the new order of the players.
    void
    Advances the current player to the next player in the current turn order.
    void
    Notifies every player in the game about the players currently waiting for the game to start.
    void
    playCharacterCard(int card, com.google.gson.JsonObject params)
    Handles the selection of a character card to play.
    void
    Sends an update to all players which prompts the first player to select the wizard and tower color.
    void
    Advances to the step of the turn when the current player can select a cloud tile.
    void
    Advances to the step of the turn when the current player can move Mother Nature.
    void
    reconnect(String username)
    Handles the reconnection of the specified player, notifying the rest of the players about the reconnection, resuming the game if necessary and broadcasting an update about the state of the game.
    void
    refuseRequest(GameMessage message, String details)
    Responds to a GameMessage sent by a client with a Refused message with the specified detail string.
    boolean
    removePlayer(String username)
    If the specified username is not in the game returns false, otherwise removes the username from the players and returns true.
    void
    selectCloud(String sender, int cloud)
    Handles the choice of a cloud tile from which the player takes new student discs.
    void
    Sends a BoardUpdate message to every player.
    void
    Sends a BoardUpdate message with the current PhaseName to every player.
    void
    sendHelp(HelpRequest helpRequest)
    Sends a HelpResponse detailing the possible user actions to the client which sent the HelpRequest.
    void
    Sends an InitialBoardStatus message to every player.
    void
    sendUpdate(UserActionUpdate message, boolean setNextPlayer)
    Sends the specified UserActionUpdate message to every player.
    void
    Sets up the game by setting the started flag to true, setting the game manager and the setting the starting message handler.
    void
    setupPlayer(String username, String towerColor, String wizard)
    Sets up a player with the specified parameters.
    void
    Starts the game and notifies each player about the initial status of the game objects.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Game

      public Game(Server server, int gameId, String creator, int lobbySize, boolean expertMode)
      Constructs a Game that fits the specified parameters.
      Parameters:
      server - the Server in charge of exchanging messages with the clients.
      gameId - the unique id of this Game.
      creator - the username of the player who created the game.
      lobbySize - the number of players allowed in the game.
      expertMode - true if and only if the instantiated game is to be played in expert mode.
  • Method Details

    • getInfo

      public GameInfo getInfo()
      Returns a GameInfo object containing the unique id of the game, the number of players allowed to participate, the expert mode flag and the username of the game's creator.
      Returns:
      a GameInfo object containing the unique id of the game, the number of players allowed to participate, the expert mode flag and the username of the game's creator.
    • getCurrentPlayer

      public String getCurrentPlayer()
      Returns the username of the current player, or null if the game has not started yet.
      Returns:
      the username of the current player, or null if the game has not started yet.
    • checkCredentials

      public boolean checkCredentials(String username, String passcode)
      Returns true if and only if the specified credentials are valid and the player in question is allowed to reconnect to the game.
      Parameters:
      username - the username of the player trying to reconnect to the game.
      passcode - the passcode sent by the player, to be checked against the one stored in the game.
      Returns:
      true if and only if the specified credentials are valid and the player in question is allowed to reconnect to the game.
    • isStarted

      public boolean isStarted()
      Returns true if this game has started, or false if it is still waiting for new players to join.
      Returns:
      true if this game has started, or false if it is still waiting for new players to join.
    • meetsStartupCondition

      public boolean meetsStartupCondition()
      Returns true if and only if the game has reached the desired number of participants and can start.
      Returns:
      true if and only if the game has reached the desired number of participants and can start.
    • setup

      public void setup()
      Sets up the game by setting the started flag to true, setting the game manager and the setting the starting message handler.
    • promptSelection

      public void promptSelection()
      Sends an update to all players which prompts the first player to select the wizard and tower color. The message contains all available wizards and tower colors.
    • start

      public void start()
      Starts the game and notifies each player about the initial status of the game objects.
    • newRound

      public void newRound()
      Advances to the next round of the game, or ends the game if the round which just ended was the last to be played.
    • newTurn

      public void newTurn(Map<String,String> playedCards)
      Handles the assistant cards played by the players and sets up the new order of the players.
      Parameters:
      playedCards - the assistant card played by each player.
    • advanceTurn

      public void advanceTurn()
      Advances to the next player's turn in the current round, updating the message handler, and advances to the next round if necessary.
    • receiveMotherNatureMovement

      public void receiveMotherNatureMovement()
      Advances to the step of the turn when the current player can move Mother Nature.
    • receiveCloudSelection

      public void receiveCloudSelection()
      Advances to the step of the turn when the current player can select a cloud tile.
    • addPlayer

      public String addPlayer(String username)
      If the specified username is already in the game or the lobby is full returns null, otherwise adds the username to the players and returns the passcode which the player can use to reconnect to the game.
      Parameters:
      username - the username of the player to add to the game.
      Returns:
      the passcode which the player can use to reconnect to the game, or null if the specified username is already in the game.
    • removePlayer

      public boolean removePlayer(String username)
      If the specified username is not in the game returns false, otherwise removes the username from the players and returns true.
      Parameters:
      username - the username of the player to remove from the game.
      Returns:
      true if and only if the specified username was a player of this game.
    • disconnect

      public void disconnect(String username)
      Disconnects the specified player from the game, removing them from the lobby if the game has not yet started or pausing the game if the game has started and there are less than 2 players currently connected.
      Parameters:
      username - the username of the player which has disconnected.
    • reconnect

      public void reconnect(String username)
      Handles the reconnection of the specified player, notifying the rest of the players about the reconnection, resuming the game if necessary and broadcasting an update about the state of the game.
      Parameters:
      username - the username of the player who has reconnected to the game.
    • notifyLobbyChange

      public void notifyLobbyChange()
      Notifies every player in the game about the players currently waiting for the game to start.
    • isEmpty

      public boolean isEmpty()
      Returns true if and only if no players are currently participating in the game.
      Returns:
      true if and only if no players are currently participating in the game.
    • setupPlayer

      public void setupPlayer(String username, String towerColor, String wizard) throws InvalidArgumentException
      Sets up a player with the specified parameters.
      Parameters:
      username - the player's username.
      towerColor - the player's tower color.
      wizard - the player's wizard.
      Throws:
      InvalidArgumentException - if no player matches the specified nickname or if towerColor or wizard are not legal enum literals.
    • getAssistantCards

      public Map<String,List<String>> getAssistantCards()
      Returns a map having the nickname of every player as the key set and each player's remaining assistant cards as the values.
      Returns:
      having the nickname of every player as the key set and each player's remaining assistant cards as the values.
    • moveStudent

      public void moveStudent(String username, String color, String destination) throws IslandNotFoundException, NoMovementException, InvalidArgumentException
      Handles the movement of the student specified by the parameters.
      Parameters:
      username - the player performing the action.
      color - the specified student's color.
      destination - the destination of the movement.
      Throws:
      IslandNotFoundException - if destination is not a valid island id.
      NoMovementException - if an error occurs during the student movement.
      InvalidArgumentException - if no player matches the specified nickname or no Color matches the specified color.
    • getCloudSize

      public int getCloudSize()
      Returns the size of the clouds for this game.
      Returns:
      the size of the clouds for this game.
    • moveMotherNature

      public boolean moveMotherNature(String destination) throws InvalidArgumentException, IslandNotFoundException, NotEnoughMovementsException
      Handles Mother Nature's movement to the specified destination.
      Parameters:
      destination - the destination of the movement, which could be an island or the player's dining room.
      Returns:
      true if and only if the game ends as a result of Mother Nature's movement.
      Throws:
      InvalidArgumentException - if an error occurs while resolving the destination island.
      IslandNotFoundException - if no island matching the specified id can be found.
      NotEnoughMovementsException - if the player performing this action does not have enough mother nature movements in order to complete it
    • selectCloud

      public void selectCloud(String sender, int cloud) throws InvalidArgumentException, NoMovementException
      Handles the choice of a cloud tile from which the player takes new student discs.
      Parameters:
      sender - the username of the player performing this action.
      cloud - the index of the chosen cloud tile.
      Throws:
      InvalidArgumentException - if there is an error while retrieving the player, or if the cloud index is out of bounds.
      NoMovementException - if an error occurs while transferring the students.
    • playCharacterCard

      public void playCharacterCard(int card, com.google.gson.JsonObject params) throws Exception, InvalidArgumentException, ItemNotAvailableException, DuplicateNoEntryTileException, NoMovementException
      Handles the selection of a character card to play.
      Parameters:
      card - the index of the desired character card.
      params - the parameters for the application of the specified character card's effect.
      Throws:
      InvalidArgumentException - if an error has occurred while applying the card's effect.
      ItemNotAvailableException - if an error has occurred while removing a No Entry tile from an island.
      DuplicateNoEntryTileException - if an error has occurred while placing a No Entry tile on an island.
      NoMovementException - if an error has occurred while moving one or more students.
      Exception
    • handleMessage

      public void handleMessage(GameMessage message) throws NoConnectionException
      Handles a GameMessage sent by a client through the current phase's message handler.
      Parameters:
      message - the received message.
      Throws:
      NoConnectionException - if no connection can be retrieved for the sender of the message.
    • refuseRequest

      public void refuseRequest(GameMessage message, String details) throws NoConnectionException
      Responds to a GameMessage sent by a client with a Refused message with the specified detail string.
      Parameters:
      message - the client request to refuse.
      details - a detail message explaining the reason why the request was refused.
      Throws:
      NoConnectionException - if no connection can be retrieved for the sender of the message.
    • acceptRequest

      public void acceptRequest(GameMessage message) throws NoConnectionException
      Responds to a GameMessage sent by a client with an Accepted message.
      Parameters:
      message - the client request to accept.
      Throws:
      NoConnectionException - if no connection can be retrieved for the sender of the message.
    • nextPlayer

      public void nextPlayer()
      Advances the current player to the next player in the current turn order.
    • sendUpdate

      public void sendUpdate(UserActionUpdate message, boolean setNextPlayer)
      Sends the specified UserActionUpdate message to every player.
      Parameters:
      message - the message to be sent to the players.
      setNextPlayer - specifies whether this method should set the next player property on the message being sent.
    • sendInitialBoardStatus

      public void sendInitialBoardStatus()
      Sends an InitialBoardStatus message to every player.
    • sendBoardUpdate

      public void sendBoardUpdate(PhaseName phase)
      Sends a BoardUpdate message with the current PhaseName to every player.
      Parameters:
      phase - the current game phase.
      See Also:
    • sendBoardUpdate

      public void sendBoardUpdate()
      Sends a BoardUpdate message to every player.
      See Also:
    • sendHelp

      public void sendHelp(HelpRequest helpRequest) throws NoConnectionException
      Sends a HelpResponse detailing the possible user actions to the client which sent the HelpRequest.
      Parameters:
      helpRequest - the help request being handled.
      Throws:
      NoConnectionException - if no connection can be retrieved for the sender of the help request.
    • gameOver

      public void gameOver()
      Ends the game by notifying every player and deleting every reference to it.
    • checkDisconnection

      public void checkDisconnection()
      If the current player is disconnected calls the disconnected turn handler.