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
FieldsModifier and TypeFieldDescriptionprivate final intprivate static final longprivate final boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedPlayerAction(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 declaredPlayerActionis possible.
This function will check for the following requirements:final intprivate 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 thePlayerActionis 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 voidunsafeExecute(Model ctx) Every class extendingPlayerActionmust implement the following method, which takes aModelreference 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 declaredPlayerActionis 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 declaredPlayerActionis 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 aListof previousPlayerActions related to the player taking the current turn (at every new turn, the history is cleared). Some actions may use thisListto 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
OptionalValuein case of a successful validation. Otherwise the returnedOptionalValuecontains 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- theModelobject, used during verification.- Returns:
- an
OptionalValuevalue, the value is empty if no issues are found during the validation of the function. Else the value will contain aThrowableExceptionthat 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- theModelobject, used during verification.- Returns:
- an
OptionalValuevalue, the value is empty if no issues are found during the validation of the function. Else the value will contain aThrowableExceptionthat can be used to propagate the error message.
-
isDuplicate
SUB-VALIDATION FUNCTION:
if thePlayerActionis 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
OptionalValuevalue, the value is empty if no issues are found during the validation of the function. Else the value will contain aThrowableExceptionthat 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 declaredPlayerActionis possible.
This function will check for the following requirements:- Parameters:
history- the controller stores aListof previousPlayerActions related to the player taking the current turn (at every new turn, the history is cleared). Some actions may use thisListto 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
OptionalValuein case of a successful validation. Otherwise the returnedOptionalValuecontains the relatedInputValidationException
-
getPlayerBoardID
public final int getPlayerBoardID()- Returns:
- the
PlayerBoardid set during construction of the Action.
-
unsafeExecute
Every class extendingPlayerActionmust implement the following method, which takes aModelreference 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- theModelreference, 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 anyPlayerActioninheritor should guarantee the absence of Exceptions for any positive return value yielded byvalidate(List, Model)
-