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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
acceptRequest
(GameMessage message) Responds to aGameMessage
sent by a client with anAccepted
message.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.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) Returnstrue
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
gameOver()
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, ornull
if the game has not started yet.getInfo()
Returns aGameInfo
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
handleMessage
(GameMessage message) Handles aGameMessage
sent by a client through the current phase's message handler.boolean
isEmpty()
Returnstrue
if and only if no players are currently participating in the game.boolean
Returnstrue
if this game has started, orfalse
if it is still waiting for new players to join.boolean
Returnstrue
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
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.void
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
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 aGameMessage
sent by a client with aRefused
message with the specified detail string.boolean
removePlayer
(String username) If the specified username is not in the game returnsfalse
, otherwise removes the username from the players and returnstrue
.void
selectCloud
(String sender, int cloud) Handles the choice of a cloud tile from which the player takes new student discs.void
Sends aBoardUpdate
message to every player.void
sendBoardUpdate
(PhaseName phase) Sends aBoardUpdate
message with the currentPhaseName
to every player.void
sendHelp
(HelpRequest helpRequest) Sends aHelpResponse
detailing the possible user actions to the client which sent theHelpRequest
.void
Sends anInitialBoardStatus
message to every player.void
sendUpdate
(UserActionUpdate message, boolean setNextPlayer) Sends the specifiedUserActionUpdate
message to every player.void
setup()
Sets up the game by setting thestarted
flag totrue
, 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
start()
Starts the game and notifies each player about the initial status of the game objects.
-
Constructor Details
-
Game
Constructs aGame
that fits the specified parameters.- Parameters:
server
- theServer
in 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
-true
if and only if the instantiated game is to be played in expert mode.
-
-
Method Details
-
getInfo
Returns aGameInfo
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
Returns the username of the current player, ornull
if the game has not started yet.- Returns:
- the username of the current player, or
null
if the game has not started yet.
-
checkCredentials
Returnstrue
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()Returnstrue
if this game has started, orfalse
if it is still waiting for new players to join.- Returns:
true
if this game has started, orfalse
if it is still waiting for new players to join.
-
meetsStartupCondition
public boolean meetsStartupCondition()Returnstrue
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 thestarted
flag 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
null
if 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:
true
if 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()Returnstrue
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 iftowerColor
orwizard
are 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
- ifdestination
is not a valid island id.NoMovementException
- if an error occurs during the student movement.InvalidArgumentException
- if no player matches the specified nickname or noColor
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 thecloud
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
Handles aGameMessage
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
Responds to aGameMessage
sent by a client with aRefused
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
Responds to aGameMessage
sent by a client with anAccepted
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
Sends the specifiedUserActionUpdate
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 anInitialBoardStatus
message to every player. -
sendBoardUpdate
Sends aBoardUpdate
message with the currentPhaseName
to every player.- Parameters:
phase
- the current game phase.- See Also:
-
sendBoardUpdate
public void sendBoardUpdate()Sends aBoardUpdate
message to every player.- See Also:
-
sendHelp
Sends aHelpResponse
detailing 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.
-