- All Implemented Interfaces:
AddWorkerListener
,BuildableCellsListener
,BuildActionListener
,BuildingBlocksListener
,EndTurnListener
,MoveActionListener
,PlayerLostListener
,SelectWorkerListener
,WalkableCellsListener
public class ServerController extends java.lang.Object implements AddWorkerListener, BuildableCellsListener, BuildActionListener, BuildingBlocksListener, EndTurnListener, MoveActionListener, WalkableCellsListener, PlayerLostListener, SelectWorkerListener
class is responsible for changes in the game state: it
- makes actions to alter the game (model), based on the users' inputs received in the parser
- notifies the clients about the changes, propagated from the model using listeners
-
Constructor Summary
Constructors Constructor Description ServerController(GameInterface game, java.util.Map<User,PlayerInterface> players, MessageManagerParser parser, java.io.File gameToSave)
Default constructor -
Method Summary
Modifier and Type Method Description void
addWorker(java.lang.String username, Cell cell)
Adds a worker for the playervoid
handleBuildAction(java.lang.String username, BuildAction buildAction)
Handles a build actionvoid
handleGameRestore()
Resumes a previously saved game statevoid
handleMoveAction(java.lang.String username, MoveAction moveAction)
Handles a movement actionvoid
obtainBuildableCells(java.lang.String username)
Provides the cells on which the player's selected worker can build onvoid
obtainWalkableCells(java.lang.String username)
Provides the cells on which the player's selected worker can move tovoid
onBlocksObtained(java.lang.String name, java.util.List<Block> blocks)
Notifies a single user about the possible blocks its selected worker can build, if there's more than onevoid
onBuildableCell(java.lang.String name, java.util.List<Cell> cells)
Notifies a single user about the possible cells on which its selected worker can build onvoid
onBuildAction(java.util.List<Cell> cells)
Notifies all users about a successful build action
Saves the current game statevoid
onMoveAction(java.util.List<Cell> cells)
Notifies all users about a successful movement action
Saves the current game statevoid
onPlayerLoss(java.lang.String username, java.util.List<Cell> gameBoard)
Notifies all users that a player lostvoid
onSelectedWorker(java.lang.String username, java.util.List<PossibleActions> possibleActions, Worker selectedWorker)
Notifies a single user that its worker choice was correct
Saves the current game statevoid
onTurnEnd(java.lang.String name, java.util.List<Cell> workersCells)
Notifies all users about the end of the current turn and the next player who has to play
Saves the current statevoid
onWalkableCells(java.lang.String name, java.util.List<Cell> cells)
Notifies a single user about the possible cells on which its selected worker can walk tovoid
onWorkerAdd(java.util.List<Cell> cells)
Notifies all users about a successful worker placementvoid
passTurn(java.lang.String username)
Handles a turn end requestvoid
saveState()
Saves the current state of the game to a filevoid
selectBuildingCell(java.lang.String username, Cell selectedCell)
Provides the possible blocks which the current player's worker can buildvoid
selectWorker(java.lang.String username, Worker worker)
Sets a player's selected worker for the following actions to be performedvoid
setFile(java.io.File file)
Sets the file to save the game to and saves the current state to it
-
Constructor Details
-
ServerController
public ServerController(GameInterface game, java.util.Map<User,PlayerInterface> players, MessageManagerParser parser, java.io.File gameToSave)Default constructorSets the class attributes and the various listeners in Game and Player
- Parameters:
game
- the Game to manageplayers
- the Players playing the gameparser
- the MessageManagerParsergameToSave
- a File to store the game status
-
-
Method Details
-
handleGameRestore
public void handleGameRestore()Resumes a previously saved game stateBefore calling this method, the caller has to make sure to reload an already existing game; this method just sends the correct message based on the saved game state to resume the game, performing no check at all.
- See Also:
Lobby.reloadMatch(boolean)
-
setFile
public void setFile(java.io.File file)Sets the file to save the game to and saves the current state to it- Parameters:
file
- the file to store the game state
-
addWorker
Adds a worker for the playerTries to add a new worker for the player requesting it. This action can end with one and one only of the following omes:
- the worker is added successfully on the board, but the player still has workers to place: a new ChooseWorkerPositionRequest is sent to the same player;
- the worker is added successfully on the board, the current player has placed all of his workers, but there still are players which have to place their workers: a new ChooseWorkerPositionRequest is sent to the following player;
- the worker is added successfully on the board, all the players have placed their workers: the game can start, so a GameStartEvent is sent to the first player;
- the worker could not be added to the board, because the cell the player wants to place the worker on is already taken: a WorkerAddedEvent is sent with Type.ADDING_FAILED as outcome, followed by a ChooseWorkerPositionRequest.
- Parameters:
username
- the user's usernamecell
- the cell to add the worker on
-
selectWorker
Sets a player's selected worker for the following actions to be performedIf the chosen worker is not owned by the user which sent the message, a WorkerSelectedResponse is sent with Type.NOT_YOUR_WORKER as outcome; if the player owns the given worker, the model will notify the SelectWorkerListeners.
- Parameters:
username
- the user's usernameworker
- the worker to set as current- See Also:
PlayerInterface.setSelectedWorker(Worker)
-
obtainWalkableCells
public void obtainWalkableCells(java.lang.String username)Provides the cells on which the player's selected worker can move toIf the player has selected no worker, a WalkableCellsResponse is sent with Type.NO_WORKER_SELECTED as outcome; if the player owns the given worker, the model will notify the WalkableCellsListeners.
- Parameters:
username
- the user's username- See Also:
PlayerInterface.obtainWalkableCells()
-
obtainBuildableCells
public void obtainBuildableCells(java.lang.String username)Provides the cells on which the player's selected worker can build onIf the player has selected no worker, a BuildableCellsResponse is sent with Type.NO_WORKER_SELECTED as outcome; if the player owns the given worker, the model will notify the BuildableCellsListeners.
- Parameters:
username
- the user's username- See Also:
PlayerInterface.obtainBuildableCells()
-
selectBuildingCell
Provides the possible blocks which the current player's worker can buildIf the player cannot make a build action, a PlayerBuildEvent is sent with Type.ILLEGAL_BUILD as outcome; if the player can perform a build action, the model will notify the BuildingBlocksListeners.
- Parameters:
username
- the user's usernameselectedCell
- the cell on which the player wants to build- See Also:
PlayerInterface.obtainBuildingBlocks(Cell)
-
handleMoveAction
Handles a movement actionIf the player cannot perform a movement action, a PlayerMoveEvent is sent with Type.ILLEGAL_MOVEMENT as outcome; if the player can perform a movement action, the model will notify the MoveActionListeners.
- Parameters:
username
- the user's usernamemoveAction
- the action to handle- See Also:
PlayerInterface.useAction(Action)
-
handleBuildAction
Handles a build actionIf the player cannot perform a build action, a PlayerBuildEvent is sent with Type.ILLEGAL_BUILD as outcome; if the player can perform a build action, the model will notify the BuildActionListeners.
- Parameters:
username
- the user's usernamebuildAction
- the action to handle- See Also:
PlayerInterface.useAction(Action)
-
passTurn
public void passTurn(java.lang.String username)Handles a turn end requestIf the player cannot pass its turn, a EndTurnEvent is sent with Type.CANNOT_END_TURN as outcome; if the player can end its turn, the model will notify the EndTurnListeners.
- Parameters:
username
- the user's username- See Also:
PlayerInterface.askPassTurn()
-
saveState
public void saveState()Saves the current state of the game to a file -
onMoveAction
Notifies all users about a successful movement action
Saves the current game state- Specified by:
onMoveAction
in interfaceMoveActionListener
- Parameters:
cells
- the changed gameBoard as a List of cells- See Also:
Game.validateMoveAction(MoveAction)
-
onWorkerAdd
Notifies all users about a successful worker placement- Specified by:
onWorkerAdd
in interfaceAddWorkerListener
- Parameters:
cells
- the changed gameBoard as a List of cells- See Also:
Player.addWorker(Cell)
-
onBuildAction
Notifies all users about a successful build action
Saves the current game state- Specified by:
onBuildAction
in interfaceBuildActionListener
- Parameters:
cells
- the changed gameBoard as a List of cells- See Also:
Game.validateBuildAction(BuildAction)
-
onBuildableCell
Notifies a single user about the possible cells on which its selected worker can build on- Specified by:
onBuildableCell
in interfaceBuildableCellsListener
- Parameters:
name
- the player to notify's usernamecells
- the cells on which the player's selected worker can build on- See Also:
Game.getBuildableCells(Worker)
-
onBlocksObtained
Notifies a single user about the possible blocks its selected worker can build, if there's more than one- Specified by:
onBlocksObtained
in interfaceBuildingBlocksListener
- Parameters:
name
- the player to notify's usernameblocks
- the blocks the player's selected worker can build- See Also:
Player.obtainBuildingBlocks(Cell)
-
onTurnEnd
Notifies all users about the end of the current turn and the next player who has to play
Saves the current state- Specified by:
onTurnEnd
in interfaceEndTurnListener
- Parameters:
name
- the next player's username
-
onWalkableCells
Notifies a single user about the possible cells on which its selected worker can walk to- Specified by:
onWalkableCells
in interfaceWalkableCellsListener
- Parameters:
name
- the player to notify's usernamecells
- the cells on which the player's selected worker can walk to- See Also:
Game.getWalkableCells(Worker)
-
onPlayerLoss
Notifies all users that a player lostThe file containing the saved game is also deleted, to be replaced with a new one
- Specified by:
onPlayerLoss
in interfacePlayerLostListener
- Parameters:
username
- the player which lostgameBoard
- the changed gameBoard without the loser's workers
-
onSelectedWorker
public void onSelectedWorker(java.lang.String username, java.util.List<PossibleActions> possibleActions, Worker selectedWorker)Notifies a single user that its worker choice was correct
Saves the current game state- Specified by:
onSelectedWorker
in interfaceSelectWorkerListener
- Parameters:
username
- the player to notify's usernamepossibleActions
- a list of possible actions to be performed with the chosen workerselectedWorker
- the worker the player selected
-