Coverage Summary for Class: NetworkEvent (it.polimi.ingsw.gc14.Network)
| Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
| NetworkEvent |
0%
(0/1)
|
0%
(0/10)
|
0%
(0/2)
|
0%
(0/19)
|
package it.polimi.ingsw.gc14.Network;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.ErrorType;
import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Model.OrderLogicCard;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Slot;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Represents an event sent over the network.
*
* <p>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 {@link GameController}
* or to the client-side {@link MiniModel}.
*/
public abstract class NetworkEvent implements Serializable {
/** The error type associated with this event; set when the event failed to apply. */
protected ErrorType errorType;
/**
* Returns the error type associated with this event.
*
* @return the error type, or {@code null} if no error occurred.
*/
public ErrorType getErrorType() {
return errorType;
}
/**
* Username of the player requesting the event.
*/
protected final String username;
/**
* Returns the username of the player requesting the event.
*
* @return the username associated with the event.
*/
public String getUsername() {
return username;
}
/**
* Type of the network event.
*/
protected final EventType eventType;
/**
* Map associating each occupied slot with the corresponding player.
*/
protected Map<Slot, Player> slotPlayerMap;
/**
* Order logic card used to manage the player turn order.
*/
protected OrderLogicCard orderLogicCard;
/**
* Current state of the game associated with the event.
*/
protected CurrentState currentState;
/**
* List of all players.
*/
protected List<Player> playerList;
/**
* Usernames of players currently disconnected from the game.
* Sent with every event so clients always have an up-to-date list.
*/
protected ArrayList<String> disconnectedPlayers;
/**
* 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 {@code super} first.
*
* @param game the current game model.
* @param disconnectedUsernames usernames of currently disconnected players.
*/
public void enrichWithGameState(Game game, ArrayList<String> disconnectedUsernames) {
setData(game.getSlotMap(), game.getOrderLogicCard(), game.getCurrentState(), game.getPlayers());
}
/**
* Sets the game data associated with this network event.
*
* @param slotPlayerMap the map associating each slot with the player occupying it.
* @param orderLogicCard the order logic card used to manage turn order.
* @param currentState the current state of the game.
* @param playerList the list of the actual state of the players
*/
public void setData(Map<Slot, Player> slotPlayerMap,
OrderLogicCard orderLogicCard,
CurrentState currentState,
List<Player> playerList) {
this.slotPlayerMap = slotPlayerMap;
this.orderLogicCard = orderLogicCard;
this.currentState = currentState;
this.playerList = playerList;
}
/**
* Returns the type of this event.
*
* @return the event type.
*/
public EventType getEventType() {
return eventType;
}
/**
* Flag indicating whether the event could not be applied successfully.
*/
protected boolean isError;
/**
* Returns whether the event represents an error.
*
* @return {@code true} if the event could not be applied successfully,
* {@code false} otherwise.
*/
public boolean isError() {
return isError;
}
/**
* Sets whether the event represents an error.
*
* @param isError {@code true} if the event could not be applied successfully,
* {@code false} otherwise.
*/
public void setIsError(boolean isError) {
this.isError = isError;
}
/**
* Overrides the error type for this event.
*
* @param errorType the error type to set.
*/
public void setErrorType(ErrorType errorType) {
this.errorType = errorType;
}
/**
* Constructs a network event.
*
* @param username the username of the player requesting the event.
* @param eventType the type of the event.
* @param isError {@code true} if the event represents an error,
* {@code false} otherwise.
*/
protected NetworkEvent(String username, EventType eventType, boolean isError, ErrorType errorType) {
this.username = username;
this.eventType = eventType;
this.isError = isError;
this.errorType = errorType;
}
/**
* Returns a textual description of the event and its outcome.
*
* @return a string describing the event type and whether it represents an error.
*/
@Override
public String toString() {
if (isError) {
return "ERROR: " + eventType;
} else {
return "ACTION: " + eventType;
}
}
/**
* Applies this event to the specified server-side game controller.
*
* @param gameController the game controller on which the event must be applied.
* @return {@code true} if the event is applied successfully,
* {@code false} otherwise.
*/
public abstract boolean apply(GameController gameController);
/**
* Applies this event to the specified client-side mini model.
*
* @param model the mini model on which the event must be applied.
*/
public abstract void apply(MiniModel model);
}