java.lang.Object
it.polimi.ingsw.am13.controller.GameController
This is a controller for a single game/match
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGameController
(Room listenerHandler) Creates a new instance of the controller of the game with the specified room, which must be full. -
Method Summary
Modifier and TypeMethodDescriptionvoid
This method checks the pings of each game listener everysleepTime
ms and disconnects players whose game listener ping is too oldvoid
choosePersonalObjective
(PlayerLobby player, CardObjectiveIF cardObj) Method callable only during INIT phase.void
disconnectPlayer
(PlayerLobby player) 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 arevoid
disconnectPlayer
(PlayerLobby player, long timeToWait) disconnection method only for testsint
private void
nextTurn()
Advances to the next turn.void
pickCard
(PlayerLobby playerLobby, CardPlayableIF card) Picks one of the 6 cards on the table, and makes the game proceed to the next Turn.void
playCard
(PlayerLobby playerLobby, CardPlayableIF card, Side side, Coordinates coord) Plays a given card side on the field, at the given coordinatesvoid
playStarter
(PlayerLobby player, Side side) Method callable only once per player during INIT phase.(package private) void
reconnectPlayer
(GameListener gameListener) Reconnects the player corresponding to the game listener and triggers the notification of this.private void
startReconnectionTimer
(long timeToWait) Starts the reconnection timer.private void
Stops the reconnection timer by setting the reconnection timer by interrupting the reconnection thread and then setting it to nullvoid
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)void
updatePing
(PlayerLobby playerLobby) Updates the ping of the given game listener by setting it to the current time
-
Field Details
-
gameModel
The model of the game -
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
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 everysleepTime
ms and disconnects players whose game listener ping is too old -
disconnectPlayer
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 matchConnectionException
- if the player had already been disconnectedLobbyException
- 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 invokeLobby
'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 matchConnectionException
- if the player was already connected when this method was calledGameStatusException
-
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 playside
- The side of the starting card- Throws:
InvalidPlayerException
- If the player is not among the playing players in the matchGameStatusException
- If game phase is not INITInvalidPlayCardException
- Positioning error of the card at coordinates (0,0).
-
choosePersonalObjective
public void choosePersonalObjective(PlayerLobby player, CardObjectiveIF cardObj) throws InvalidPlayerException, InvalidChoiceException, VariableAlreadySetException, GameStatusException 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 withfetchPersonalObjectives(player)
- Parameters:
player
- one of the players of the matchcardObj
- the objective chosen by the player- Throws:
GameStatusException
- if this method isn't called in the INIT phaseInvalidPlayerException
- if the player is not one of the players of this matchInvalidChoiceException
- if the objective card does not belong to the list of the possible objective cards for the playerVariableAlreadySetException
- if this method has been called before for the player
-
playCard
public void playCard(PlayerLobby playerLobby, CardPlayableIF card, Side side, Coordinates coord) throws RequirementsNotMetException, InvalidPlayCardException, GameStatusException, InvalidPlayerException 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 handside
- Indicates whether the card is going to be played on the front or on the backcoord
- 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 metInvalidPlayCardException
- If the player doesn't have the specified cardGameStatusException
- If this method is called in the INIT or CALC POINTS phaseInvalidPlayerException
- If the passed player is not the current player
-
pickCard
public void pickCard(PlayerLobby playerLobby, CardPlayableIF card) throws InvalidDrawCardException, GameStatusException, InvalidPlayerException 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 tableGameStatusException
- if this method is called in the INIT or CALC POINTS phaseInvalidPlayerException
- If the passed player is not the current player
-
nextTurn
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
- Returns:
- the players of the match
-
transmitChatMessage
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 messagereceivers
- of the messagetext
- content of the message
-