Class PlayerAction
java.lang.Object
it.polimi.ingsw.Controller.Actions.PlayerAction
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ChooseCloudTile
,EndTurnOfActionPhase
,MoveMotherNature
,MoveStudent
,PlayAssistantCard
,PlayCharacterCard
The
The various Classes that extend it are the only possible way to interact safely with the model.
PlayerAction
object is the definition of a user's intention. The various Classes that extend it are the only possible way to interact safely with the model.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprivate final int
private static final long
private final boolean
-
Constructor Summary
ModifierConstructorDescriptionprotected
PlayerAction
(int playerBoardID, boolean uniquePerTurn) Package protected constructor used to initialize the playerBoardID. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract OptionalValue<InputValidationException>
customValidation
(List<PlayerAction> history, Model ctx) This function is used byvalidate(List, Model)
to check whether or not the declaredPlayerAction
is possible.
This function will check for the following requirements:final int
private OptionalValue<InputValidationException>
isCorrectTurn
(Model ctx) SUB-VALIDATION FUNCTION:
if thePlayerAction
's declared player is not the current player that needs to play, this function will return a non-empty value.private OptionalValue<InputValidationException>
isDuplicate
(List<PlayerAction> history) SUB-VALIDATION FUNCTION:
if thePlayerAction
is marked as unique per turn, this function will return a non-empty value in case of a duplicate action being present in the historyprivate OptionalValue<InputValidationException>
isGameRunning
(Model ctx) SUB-VALIDATION FUNCTION:
if the game is not active anymore (i.e. the game is over and no actions can be made), this function will return a non-empty value.abstract void
unsafeExecute
(Model ctx) Every class extendingPlayerAction
must implement the following method, which takes aModel
reference and applies the concrete effect of the action.validate
(List<PlayerAction> history, Model ctx) The validate function is used to check whether or not the declaredPlayerAction
is possible.
The validate function will check for: The game must not be over The action must be called during the correct turn If the action is unique, check that no duplicates have been played before Any additional constraint imposed bycustomValidation(List, Model)
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
playerBoardID
private final int playerBoardID -
uniquePerTurn
private final boolean uniquePerTurn
-
-
Constructor Details
-
PlayerAction
protected PlayerAction(int playerBoardID, boolean uniquePerTurn) Package protected constructor used to initialize the playerBoardID.- Parameters:
playerBoardID
- the ID of the player who wishes to interact with the controller.uniquePerTurn
- if set to false, the user can submit multiple actions of the same type during his turn.
-
-
Method Details
-
validate
public final OptionalValue<InputValidationException> validate(List<PlayerAction> history, Model ctx) The validate function is used to check whether or not the declaredPlayerAction
is possible.
The validate function will check for:- The game must not be over
- The action must be called during the correct turn
- If the action is unique, check that no duplicates have been played before
- Any additional constraint imposed by
customValidation(List, Model)
- Parameters:
history
- the controller stores aList
of previousPlayerAction
s related to the player taking the current turn (at every new turn, the history is cleared). Some actions may use thisList
to check for duplicates.ctx
- a reference toModel
. Some actions may use this reference to check for consistency between what the actions declares and what the Model offers.- Returns:
- An empty
OptionalValue
in case of a successful validation. Otherwise the returnedOptionalValue
contains the relatedInputValidationException
-
isGameRunning
SUB-VALIDATION FUNCTION:
if the game is not active anymore (i.e. the game is over and no actions can be made), this function will return a non-empty value.- Parameters:
ctx
- theModel
object, used during verification.- Returns:
- an
OptionalValue
value, the value is empty if no issues are found during the validation of the function. Else the value will contain aThrowable
Exception
that can be used to propagate the error message.
-
isCorrectTurn
SUB-VALIDATION FUNCTION:
if thePlayerAction
's declared player is not the current player that needs to play, this function will return a non-empty value.- Parameters:
ctx
- theModel
object, used during verification.- Returns:
- an
OptionalValue
value, the value is empty if no issues are found during the validation of the function. Else the value will contain aThrowable
Exception
that can be used to propagate the error message.
-
isDuplicate
SUB-VALIDATION FUNCTION:
if thePlayerAction
is marked as unique per turn, this function will return a non-empty value in case of a duplicate action being present in the history- Parameters:
history
- a list of previous actions submitted by the player- Returns:
- an
OptionalValue
value, the value is empty if no issues are found during the validation of the function. Else the value will contain aThrowable
Exception
that can be used to propagate the error message.
-
customValidation
protected abstract OptionalValue<InputValidationException> customValidation(List<PlayerAction> history, Model ctx) This function is used byvalidate(List, Model)
to check whether or not the declaredPlayerAction
is possible.
This function will check for the following requirements:- Parameters:
history
- the controller stores aList
of previousPlayerAction
s related to the player taking the current turn (at every new turn, the history is cleared). Some actions may use thisList
to check for duplicates.ctx
- a reference toModel
. Some actions may use this reference to check for consistency between what the actions declares and what the Model offers.- Returns:
- An empty
OptionalValue
in case of a successful validation. Otherwise the returnedOptionalValue
contains the relatedInputValidationException
-
getPlayerBoardID
public final int getPlayerBoardID()- Returns:
- the
PlayerBoard
id set during construction of the Action.
-
unsafeExecute
Every class extendingPlayerAction
must implement the following method, which takes aModel
reference and applies the concrete effect of the action.
Warning: this function, as implied by the name, is unsafe. it should never be called by called outside the scope of the classController
, which takes adequate precautions in order to guarantee a coherent execution of the method.- Parameters:
ctx
- theModel
reference, once the method finishes running the game state will be altered.- Throws:
Exception
- Should an error occur during the execution of the method, such error will be reported through the thrownException
. Note that anyPlayerAction
inheritor should guarantee the absence of Exceptions for any positive return value yielded byvalidate(List, Model)
-