Package it.polimi.ingsw.Controller
Class Controller
java.lang.Object
it.polimi.ingsw.Controller.Controller
This is the Controller of the whole game.
The Controller should be the only entity able to modify the model.
The Controller should be the only entity able to modify the model.
-
Field Summary
Modifier and TypeFieldDescriptionprivate final List<PlayerAction>
private final ModelWrapper
private boolean
-
Constructor Summary
ModifierConstructorDescriptionprivate
Controller
(ModelWrapper modelWrapper) Subscribes a newController
object to aModelWrapper
instance, allowing the creation of a Controller to Model connection.(package private)
Controller
(ModelWrapper modelWrapper, List<PlayerAction> history) Generates a new instance of theController
. -
Method Summary
Modifier and TypeMethodDescriptionstatic Controller
createGame
(GameMode gameMode, OptionalValue<Lobby> lobby, String... players) Generates a new instance.void
executeAction
(PlayerAction action) An execution request handler.private List<PlayerAction>
-
Field Details
-
history
-
modelWrapper
-
unsafeReferences
private boolean unsafeReferences
-
-
Constructor Details
-
Controller
Subscribes a newController
object to aModelWrapper
instance, allowing the creation of a Controller to Model connection.- Parameters:
modelWrapper
- an instance ofModelWrapper
-
Controller
Controller(ModelWrapper modelWrapper, List<PlayerAction> history) Generates a new instance of theController
. This is the debug method to call to create a game, since the internal attributes are set to the parameters.
Note: this method will not protect model references after editing actions to the model. Note: this method should be called ONLY by test code.- Parameters:
modelWrapper
- an instance ofModelWrapper
history
- an instance to a list ofPlayerAction
, used by the controller to check the flow of the game
-
-
Method Details
-
createGame
public static Controller createGame(GameMode gameMode, OptionalValue<Lobby> lobby, String... players) throws InputValidationException Generates a new instance. This is the static method to call for general purpose creation of a game.- Parameters:
gameMode
- the game mode the players are going to uselobby
- in case a server is used, insert the Lobby object wrapped in anOptionalValue
to let it receiveModelUpdateEvent
andGameOverEvent
players
- a list of minimum 2 and maximum 4 strings containing the nicknames of the players. In the case of 4 players: players at index 0 and 2 go together against players at index 1 and 3- Returns:
- the generated controller
- Throws:
InputValidationException
- if the supplied players are less than 2 or more than 4
-
executeAction
An execution request handler. Actions are passed in, validated and (if possible) executed.
Warning: this request is not thread safe, that job is delegated to the caller to handle.Note: if this Controller was generated using the debug constructor, then references to the model, once modified, are going to be kept unsafe, generally decreasing the security of the editing mechanism.
- Parameters:
action
- the action to be validated and executed.- Throws:
InputValidationException
- thrown when validation fails, carries information about the error. If thrown, the model is guaranteed to not have been modified.
-
getHistory
- Returns:
- an immutable copy of the list of player actions.
Note: the single actions are immutable by default, so do not get cloned
-