# Class ContextValidator

java.lang.Object
it.polimi.ingsw.is25am31.model.cards.ContextValidator

public class ContextValidator extends Object
The `ContextValidator` class provides static methods to validate and manage the context of card and effect activations within the game. It ensures that player actions align with the rules and the current state of the game, especially concerning card effects that may require specific parameters or involve multiple phases/players. It also keeps track of which players have applied an effect in multi-phase scenarios.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ContextValidator()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static int`
  `getEffectCalledCount()`
  Returns the current count of how many times a multi-phase effect has been called.
  `static boolean`
  `handleMultiPhaseEffect(int playerId, Game model)`
  Handles logic for multi-phase card effects, managing player turns and effect counts.
  `static int`
  `isEffectResolved(int playerId, Game model)`
  Checks if a card effect has been fully resolved for all relevant players or conditions.
  `static int`
  `postCardContextValidation(Game model)`
  Performs post-card context validation, returning an indicator based on the card type.
  `static int`
  `preCardContextValidation(Game model, int playerId, Map<String,Object> parameters)`
  Validates the parameters provided by a client before a card context is set.
  `static Map<String,Object>`
  `preEffectValidation(int playerId, Map<String,Object> parameters, Game model)`
  Performs pre-effect validation for specific card types before their effects are applied.
  `static void`
  `reset()`
  Resets the internal state of the `ContextValidator` class, clearing tracking data and counters.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ContextValidator

    public ContextValidator()

- ## Method Details

  - ### getEffectCalledCount

    public static int getEffectCalledCount()
    Returns the current count of how many times a multi-phase effect has been called.
    Returns:
      The current value of `effectCalledCount`.

  - ### reset

    public static void reset()
    Resets the internal state of the `ContextValidator` class, clearing tracking data and counters. This method is used to prepare the class for a new card effect application.

  - ### preCardContextValidation

    public static int preCardContextValidation(Game model, int playerId, Map<String,Object> parameters)
    Validates the parameters provided by a client before a card context is set. This method checks if the parameters are valid for the current card type and game state. It may also handle immediate game state changes like passing a turn.
    Parameters:
      `model` - The current `Game` instance.
      `playerId` - The ID of the player attempting to apply the card context.
      `parameters` - A `Map` containing the parameters for the card effect.
    Returns:
      An integer indicating the result of the validation:
      - 0: The player declined an action, turn has to be passed.
      - 1: The context is valid, but it doesn't need to be passed to the card.
      - 2: The context is valid and can be sent to the card.
      - 4: The player declined to apply ABANDONED card effect, and the turn has to be passed.
    Throws:
      `RuntimeException` - If it's not the player's turn or a prerequisite is missing.
      `IllegalArgumentException` - If the provided parameters are invalid or missing.

  - ### postCardContextValidation

    public static int postCardContextValidation(Game model)
    Performs post-card context validation, returning an indicator based on the card type. This method primarily checks if the card type is one that requires other updates.
    Parameters:
      `model` - The current `Game` instance.
    Returns:
      An integer indicating the type of post-validation needed:
      - 3: For some card types, indicating the updated ship needs to be sent to the client.
      - 2: Standard return for other card types, meaning no further actions are necessary.

  - ### preEffectValidation

    public static Map<String,Object> preEffectValidation(int playerId, Map<String,Object> parameters, Game model)
    Performs pre-effect validation for specific card types before their effects are applied. This method prepares the effect parameters, such as incorporating dice results, and checks for multi-phase effect constraints.
    Parameters:
      `playerId` - The ID of the player whose effect is being validated.
      `parameters` - A `Map` containing the updated parameters for the effect.
      `model` - The current `Game` instance.
    Returns:
      The updated `Map` of parameters, including necessary values like dice results.
    Throws:
      `RuntimeException` - If a dice roll is required but hasn't occurred.
      `IllegalStateException` - If a player attempts to apply a multi-phase effect out of order.

  - ### handleMultiPhaseEffect

    public static boolean handleMultiPhaseEffect(int playerId, Game model)
    Handles logic for multi-phase card effects, managing player turns and effect counts. This method is made for cards that have multiple phases and not just context setting and application.
    Parameters:
      `playerId` - The ID of the player who just performed an action for the effect.
      `model` - The current `Game` instance.
    Returns:
      `true` if actual card effect doesn't have to be called because it's just an effect setup. `false` if the actual card effect has to be called.

  - ### isEffectResolved

    public static int isEffectResolved(int playerId, Game model)
    Checks if a card effect has been fully resolved for all relevant players or conditions. This method is called after an effect has been applied by a player and determines if the turn should pass or if the overall effect is concluded.
    Parameters:
      `playerId` - The ID of the player who just applied an effect.
      `model` - The current `Game` instance.
    Returns:
      An integer indicating the resolution status:
      - 0: Effect not fully resolved for all necessary players, continue current phase.
      - 1: Effect partially resolved, turn passes to the next player.
      - 2: Effect fully resolved for all players/conditions, clear tracking and proceed.