Class Game
java.lang.Object
it.polimi.ingsw.eriantys.controller.Game
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidacceptRequest(GameMessage message) Responds to aGameMessagesent by a client with anAcceptedmessage.If the specified username is already in the game or the lobby is full returnsnull, otherwise adds the username to the players and returns the passcode which the player can use to reconnect to the game.voidAdvances to the next player's turn in the current round, updating the message handler, and advances to the next round if necessary.booleancheckCredentials(String username, String passcode) Returnstrueif and only if the specified credentials are valid and the player in question is allowed to reconnect to the game.voidIf the current player is disconnected calls the disconnected turn handler.voiddisconnect(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.voidgameOver()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.intReturns the size of the clouds for this game.Returns the username of the current player, ornullif the game has not started yet.getInfo()Returns aGameInfoobject 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.voidhandleMessage(GameMessage message) Handles aGameMessagesent by a client through the current phase's message handler.booleanisEmpty()Returnstrueif and only if no players are currently participating in the game.booleanReturnstrueif this game has started, orfalseif it is still waiting for new players to join.booleanReturnstrueif and only if the game has reached the desired number of participants and can start.booleanmoveMotherNature(String destination) Handles Mother Nature's movement to the specified destination.voidmoveStudent(String username, String color, String destination) Handles the movement of the student specified by the parameters.voidnewRound()Advances to the next round of the game, or ends the game if the round which just ended was the last to be played.voidHandles the assistant cards played by the players and sets up the new order of the players.voidAdvances the current player to the next player in the current turn order.voidNotifies every player in the game about the players currently waiting for the game to start.voidplayCharacterCard(int card, com.google.gson.JsonObject params) Handles the selection of a character card to play.voidSends an update to all players which prompts the first player to select the wizard and tower color.voidAdvances to the step of the turn when the current player can select a cloud tile.voidAdvances to the step of the turn when the current player can move Mother Nature.voidHandles 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.voidrefuseRequest(GameMessage message, String details) Responds to aGameMessagesent by a client with aRefusedmessage with the specified detail string.booleanremovePlayer(String username) If the specified username is not in the game returnsfalse, otherwise removes the username from the players and returnstrue.voidselectCloud(String sender, int cloud) Handles the choice of a cloud tile from which the player takes new student discs.voidSends aBoardUpdatemessage to every player.voidsendBoardUpdate(PhaseName phase) Sends aBoardUpdatemessage with the currentPhaseNameto every player.voidsendHelp(HelpRequest helpRequest) Sends aHelpResponsedetailing the possible user actions to the client which sent theHelpRequest.voidSends anInitialBoardStatusmessage to every player.voidsendUpdate(UserActionUpdate message, boolean setNextPlayer) Sends the specifiedUserActionUpdatemessage to every player.voidsetup()Sets up the game by setting thestartedflag totrue, setting the game manager and the setting the starting message handler.voidsetupPlayer(String username, String towerColor, String wizard) Sets up a player with the specified parameters.voidstart()Starts the game and notifies each player about the initial status of the game objects.
-
Constructor Details
-
Game
Constructs aGamethat fits the specified parameters.- Parameters:
server- theServerin charge of exchanging messages with the clients.gameId- the unique id of thisGame.creator- the username of the player who created the game.lobbySize- the number of players allowed in the game.expertMode-trueif and only if the instantiated game is to be played in expert mode.
-
-
Method Details
-
getInfo
Returns aGameInfoobject 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
GameInfoobject 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
Returns the username of the current player, ornullif the game has not started yet.- Returns:
- the username of the current player, or
nullif the game has not started yet.
-
checkCredentials
Returnstrueif 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:
trueif and only if the specified credentials are valid and the player in question is allowed to reconnect to the game.
-
isStarted
public boolean isStarted()Returnstrueif this game has started, orfalseif it is still waiting for new players to join.- Returns:
trueif this game has started, orfalseif it is still waiting for new players to join.
-
meetsStartupCondition
public boolean meetsStartupCondition()Returnstrueif and only if the game has reached the desired number of participants and can start.- Returns:
trueif 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 thestartedflag totrue, 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
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
If the specified username is already in the game or the lobby is full returnsnull, 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
nullif the specified username is already in the game.
-
removePlayer
If the specified username is not in the game returnsfalse, otherwise removes the username from the players and returnstrue.- Parameters:
username- the username of the player to remove from the game.- Returns:
trueif and only if the specified username was a player of this game.
-
disconnect
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
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()Returnstrueif and only if no players are currently participating in the game.- Returns:
trueif 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 iftowerColororwizardare not legal enum literals.
-
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- ifdestinationis not a valid island id.NoMovementException- if an error occurs during the student movement.InvalidArgumentException- if no player matches the specified nickname or noColormatches 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:
trueif 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 thecloudindex 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
Handles aGameMessagesent 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
Responds to aGameMessagesent by a client with aRefusedmessage 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
Responds to aGameMessagesent by a client with anAcceptedmessage.- 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
Sends the specifiedUserActionUpdatemessage 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 anInitialBoardStatusmessage to every player. -
sendBoardUpdate
Sends aBoardUpdatemessage with the currentPhaseNameto every player.- Parameters:
phase- the current game phase.- See Also:
-
sendBoardUpdate
public void sendBoardUpdate()Sends aBoardUpdatemessage to every player.- See Also:
-
sendHelp
Sends aHelpResponsedetailing the possible user actions to the client which sent theHelpRequest.- 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.
-