Class NetworkEvent

java.lang.Object
it.polimi.ingsw.gc14.Network.NetworkEvent
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AddPlayer, ApplyNextRound, DisconnectedPlayer, DrawLowerBuildingCard, DrawLowerTribeCard, DrawUpperBuildingCard, DrawUpperTribeCard, EndedGame, ReconnectPlayer, SkipTurn, SlotChoice, TotemChoice

public abstract class NetworkEvent extends Object implements Serializable
Represents an event sent over the network.

A network event contains the information required to identify the requested action, determine whether it produced an error, and apply it either to the server-side GameController or to the client-side MiniModel.

See Also:
  • Field Details

    • errorType

      protected ErrorType errorType
      The error type associated with this event; set when the event failed to apply.
    • username

      protected final String username
      Username of the player requesting the event.
    • eventType

      protected final EventType eventType
      Type of the network event.
    • slotPlayerMap

      protected Map<Slot,Player> slotPlayerMap
      Map associating each occupied slot with the corresponding player.
    • orderLogicCard

      protected OrderLogicCard orderLogicCard
      Order logic card used to manage the player turn order.
    • currentState

      protected CurrentState currentState
      Current state of the game associated with the event.
    • playerList

      protected List<Player> playerList
      List of all players.
    • disconnectedPlayers

      protected ArrayList<String> disconnectedPlayers
      Usernames of players currently disconnected from the game. Sent with every event so clients always have an up-to-date list.
    • isError

      protected boolean isError
      Flag indicating whether the event could not be applied successfully.
  • Constructor Details

    • NetworkEvent

      protected NetworkEvent(String username, EventType eventType, boolean isError, ErrorType errorType)
      Constructs a network event.
      Parameters:
      username - the username of the player requesting the event.
      eventType - the type of the event.
      isError - true if the event represents an error, false otherwise.
  • Method Details

    • getErrorType

      public ErrorType getErrorType()
      Returns the error type associated with this event.
      Returns:
      the error type, or null if no error occurred.
    • getUsername

      public String getUsername()
      Returns the username of the player requesting the event.
      Returns:
      the username associated with the event.
    • enrichWithGameState

      public void enrichWithGameState(Game game, ArrayList<String> disconnectedUsernames)
      Populates this event with the current game state so clients can update their local model after receiving it. Subclasses that carry extra state (available totems, etc.) override this method and call super first.
      Parameters:
      game - the current game model.
      disconnectedUsernames - usernames of currently disconnected players.
    • setData

      public void setData(Map<Slot,Player> slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List<Player> playerList)
      Sets the game data associated with this network event.
      Parameters:
      slotPlayerMap - the map associating each slot with the player occupying it.
      orderLogicCard - the order logic card used to manage turn order.
      currentState - the current state of the game.
      playerList - the list of the actual state of the players
    • getEventType

      public EventType getEventType()
      Returns the type of this event.
      Returns:
      the event type.
    • isError

      public boolean isError()
      Returns whether the event represents an error.
      Returns:
      true if the event could not be applied successfully, false otherwise.
    • setIsError

      public void setIsError(boolean isError)
      Sets whether the event represents an error.
      Parameters:
      isError - true if the event could not be applied successfully, false otherwise.
    • setErrorType

      public void setErrorType(ErrorType errorType)
      Overrides the error type for this event.
      Parameters:
      errorType - the error type to set.
    • toString

      public String toString()
      Returns a textual description of the event and its outcome.
      Overrides:
      toString in class Object
      Returns:
      a string describing the event type and whether it represents an error.
    • apply

      public abstract boolean apply(GameController gameController)
      Applies this event to the specified server-side game controller.
      Parameters:
      gameController - the game controller on which the event must be applied.
      Returns:
      true if the event is applied successfully, false otherwise.
    • apply

      public abstract void apply(MiniModel model)
      Applies this event to the specified client-side mini model.
      Parameters:
      model - the mini model on which the event must be applied.