Class PlayerAction

java.lang.Object
it.polimi.ingsw.Controller.Actions.PlayerAction
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ChooseCloudTile, EndTurnOfActionPhase, MoveMotherNature, MoveStudent, PlayAssistantCard, PlayCharacterCard

public abstract class PlayerAction extends Object implements Serializable
The 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 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 declared PlayerAction 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 a List of previous PlayerActions related to the player taking the current turn (at every new turn, the history is cleared). Some actions may use this List to check for duplicates.
      ctx - a reference to Model. 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 returned OptionalValue contains the related InputValidationException
    • isGameRunning

      private 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.
      Parameters:
      ctx - the Model 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 a Throwable Exception that can be used to propagate the error message.
    • isCorrectTurn

      private OptionalValue<InputValidationException> isCorrectTurn(Model ctx)
      SUB-VALIDATION FUNCTION:
      if the PlayerAction's declared player is not the current player that needs to play, this function will return a non-empty value.
      Parameters:
      ctx - the Model 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 a Throwable Exception that can be used to propagate the error message.
    • isDuplicate

      private OptionalValue<InputValidationException> isDuplicate(List<PlayerAction> history)
      SUB-VALIDATION FUNCTION:
      if the PlayerAction 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 a Throwable 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 by validate(List, Model) to check whether or not the declared PlayerAction is possible.
      This function will check for the following requirements:
      Parameters:
      history - the controller stores a List of previous PlayerActions related to the player taking the current turn (at every new turn, the history is cleared). Some actions may use this List to check for duplicates.
      ctx - a reference to Model. 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 returned OptionalValue contains the related InputValidationException
    • getPlayerBoardID

      public final int getPlayerBoardID()
      Returns:
      the PlayerBoard id set during construction of the Action.
    • unsafeExecute

      public abstract void unsafeExecute(Model ctx) throws Exception
      Every class extending PlayerAction must implement the following method, which takes a Model 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 class Controller, which takes adequate precautions in order to guarantee a coherent execution of the method.
      Parameters:
      ctx - the Model 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 thrown Exception. Note that any PlayerAction inheritor should guarantee the absence of Exceptions for any positive return value yielded by validate(List, Model)