refactor: formatted Lobby files properly
This commit is contained in:
parent
c24121b6d9
commit
16c425c507
@ -30,14 +30,14 @@ public class Lobby {
|
||||
private final TurnTimer turnTimer;
|
||||
|
||||
/**
|
||||
* The {@link Lobby} is where the magic happens.
|
||||
* In this class is a whole {@link GameInstance game} is processed.
|
||||
* To initialize the game it gets the following parameters
|
||||
* @param gameID a String to identify the game
|
||||
* @param connection the Connection to the {@link uulm.teamname.marvelous.server.lobbymanager.LobbyManager}
|
||||
* @param partyConfig declared in Editor
|
||||
* The {@link Lobby} is where the magic happens. In this class is a whole {@link GameInstance game} is processed. To
|
||||
* initialize the game it gets the following parameters
|
||||
*
|
||||
* @param gameID a String to identify the game
|
||||
* @param connection the Connection to the {@link uulm.teamname.marvelous.server.lobbymanager.LobbyManager}
|
||||
* @param partyConfig declared in Editor
|
||||
* @param characterConfig declared in Editor
|
||||
* @param scenarioConfig declared in Editor
|
||||
* @param scenarioConfig declared in Editor
|
||||
*/
|
||||
public Lobby(
|
||||
String gameID,
|
||||
@ -84,15 +84,16 @@ public class Lobby {
|
||||
|
||||
/**
|
||||
* Called by {@link LobbyConnection} to handle requests
|
||||
*
|
||||
* @param requests to be processed
|
||||
* @param source the player executing the requests
|
||||
* @param source the player executing the requests
|
||||
*/
|
||||
public synchronized void receiveRequests(Request[] requests, Participant source) {
|
||||
Logger.trace("Received {} requests from participant '{}' of type {}",
|
||||
requests.length,
|
||||
source.name,
|
||||
source.type);
|
||||
if(activePlayer != source && source.type != ParticipantType.Spectator) {
|
||||
if (activePlayer != source && source.type != ParticipantType.Spectator) {
|
||||
Logger.trace("Resetting bad requests as new participant sent data");
|
||||
activePlayer = source;
|
||||
badRequests = 0;
|
||||
@ -118,8 +119,8 @@ public class Lobby {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called at the end of receiveRequests, to start a timer.
|
||||
* The active player has now a specific amount of time to do his moves.
|
||||
* This method is called at the end of receiveRequests, to start a timer. The active player has now a specific
|
||||
* amount of time to do his moves.
|
||||
*/
|
||||
void updateTimer() {
|
||||
var currentActiveCharacterType = game.state.getActiveCharacter().type;
|
||||
@ -151,28 +152,29 @@ public class Lobby {
|
||||
|
||||
/**
|
||||
* If the player executed a false request the request gets rejected.
|
||||
*
|
||||
* @param source the executing player
|
||||
*/
|
||||
private void reject(Participant source) {
|
||||
connection.sendEvents(source, new EventBuilder(EventType.Nack).buildGameEvent(), game.getGameStateEvent());
|
||||
badRequests ++;
|
||||
badRequests++;
|
||||
//if the player sends 2 bad messages after one another, the player gets kicked out of the lobby.
|
||||
if(badRequests >= 5){
|
||||
if (badRequests >= 5) {
|
||||
connection.removeParticipant(source);
|
||||
if(connection.hasPlayer1()){
|
||||
generateWin(connection.getPlayer1());
|
||||
}
|
||||
else if(connection.hasPlayer2()) {
|
||||
generateWin(connection.getPlayer2());
|
||||
if (connection.hasPlayer1()) {
|
||||
generateWin(connection.getPlayer1());
|
||||
} else if (connection.hasPlayer2()) {
|
||||
generateWin(connection.getPlayer2());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warns the player he get timeouted soon.
|
||||
*
|
||||
* @param source soon to be timeouted player
|
||||
*/
|
||||
public synchronized void soonTimeout (Participant source){
|
||||
public synchronized void soonTimeout(Participant source) {
|
||||
connection.sendEvents(
|
||||
source,
|
||||
new EventBuilder(EventType.TimeoutWarningEvent).buildGameStateEvent());
|
||||
@ -180,27 +182,26 @@ public class Lobby {
|
||||
|
||||
/**
|
||||
* If a player times out the other player automatically wins.
|
||||
*
|
||||
* @param source is the timeouted player
|
||||
*/
|
||||
public synchronized void timeout(Participant source){
|
||||
public synchronized void timeout(Participant source) {
|
||||
connection.sendEvents(
|
||||
source,
|
||||
new EventBuilder(EventType.TimeoutEvent).buildGameStateEvent());
|
||||
connection.removeParticipant(source);
|
||||
|
||||
if(connection.hasPlayer1() && !connection.hasPlayer2()){
|
||||
if (connection.hasPlayer1() && !connection.hasPlayer2()) {
|
||||
generateWin(connection.getPlayer1());
|
||||
}
|
||||
else if(!connection.hasPlayer1() && connection.hasPlayer2()) {
|
||||
} else if (!connection.hasPlayer1() && connection.hasPlayer2()) {
|
||||
generateWin(connection.getPlayer2());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
throw new IllegalStateException("Spectator was time-outed which is impossible");
|
||||
}
|
||||
}
|
||||
|
||||
/** Skips the current turn, and starts a new one. Exclusively called in the {@link TurnTimer}. */
|
||||
private synchronized void turnTimeout(Participant source){
|
||||
private synchronized void turnTimeout(Participant source) {
|
||||
var nextTurnEvents = game.endTurn();
|
||||
nextTurnEvents.add(game.getGameStateEvent());
|
||||
connection.broadcastEvents();
|
||||
@ -208,12 +209,12 @@ public class Lobby {
|
||||
}
|
||||
|
||||
/**
|
||||
* The method generates a Win event for a {@link Participant}.
|
||||
* Afterwards it sends a Disconnect to everyone, and terminates the connection.
|
||||
* The method generates a Win event for a {@link Participant}. Afterwards it sends a Disconnect to everyone, and
|
||||
* terminates the connection.
|
||||
*
|
||||
* @param winner is the {@link Participant} that won
|
||||
*/
|
||||
public synchronized void generateWin(Participant winner){
|
||||
public synchronized void generateWin(Participant winner) {
|
||||
connection.broadcastEvents(
|
||||
new EventBuilder(EventType.WinEvent)
|
||||
.withPlayerWon(winner.type.equals(ParticipantType.PlayerOne) ? 1 : 2)
|
||||
|
@ -34,6 +34,7 @@ public class TurnTimer {
|
||||
|
||||
/**
|
||||
* This method checks if the participant is not a spectator. Otherwise it won't start a timer.
|
||||
*
|
||||
* @param participant the timer is for
|
||||
*/
|
||||
public void startTurnTimer(Participant participant) {
|
||||
@ -44,7 +45,10 @@ public class TurnTimer {
|
||||
clear();
|
||||
Logger.debug("Starting turn timer for participant '{}' with role {}",
|
||||
participant.name, participant.type);
|
||||
current = timer.schedule(() -> {callback.accept(participant); return participant;},
|
||||
current = timer.schedule(() -> {
|
||||
callback.accept(participant);
|
||||
return participant;
|
||||
},
|
||||
maxRoundTime,
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The {@link DisconnectSegment} handles requests of {@link RequestType} DisconnectRequest.
|
||||
* Therefore it removes the disconnecting player-connection from the {@link Lobby}.
|
||||
* The {@link DisconnectSegment} handles requests of {@link RequestType} DisconnectRequest. Therefore it removes the
|
||||
* disconnecting player-connection from the {@link Lobby}.
|
||||
*/
|
||||
public class DisconnectSegment implements Segment {
|
||||
|
||||
@ -29,10 +29,9 @@ public class DisconnectSegment implements Segment {
|
||||
parent.getConnection().removeParticipant(packet.getOrigin());
|
||||
|
||||
if (packet.getOrigin().type != ParticipantType.Spectator) {
|
||||
if(parent.getConnection().hasPlayer1()){
|
||||
if (parent.getConnection().hasPlayer1()) {
|
||||
parent.generateWin(parent.getConnection().getPlayer1());
|
||||
}
|
||||
else if(parent.getConnection().hasPlayer2()) {
|
||||
} else if (parent.getConnection().hasPlayer2()) {
|
||||
parent.generateWin(parent.getConnection().getPlayer2());
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,14 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The {@link GameStateSegment} handles all {@link GameInstance game} relevant {@link Request Requests}.
|
||||
* Therefore it updates the game with the matching request.
|
||||
* The {@link GameStateSegment} handles all {@link GameInstance game} relevant {@link Request Requests}. Therefore it
|
||||
* updates the game with the matching request.
|
||||
*/
|
||||
public class GameStateSegment implements Segment {
|
||||
private GameInstance game;
|
||||
|
||||
|
||||
public GameStateSegment(GameInstance game){
|
||||
public GameStateSegment(GameInstance game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
|
@ -7,20 +7,21 @@ import uulm.teamname.marvelous.server.lobbymanager.Participant;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* The {@link Packet} contains all {@link Request Requests} and the belonging {@link Participant}.
|
||||
* You can add and remove {@link Request Requests} at will.
|
||||
* The {@link Packet} contains all {@link Request Requests} and the belonging {@link Participant}. You can add and
|
||||
* remove {@link Request Requests} at will.
|
||||
*/
|
||||
public class Packet extends ArrayList<Request> {
|
||||
|
||||
private final Participant origin;
|
||||
|
||||
public Packet (Request[] requests, Participant origin) {
|
||||
public Packet(Request[] requests, Participant origin) {
|
||||
this.origin = origin;
|
||||
addAll(Arrays.asList(requests));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks if a {@link RequestType} is part of the {@link Packet} or not
|
||||
*
|
||||
* @param requiredType RequestType to check.
|
||||
* @return boolean
|
||||
*/
|
||||
@ -35,6 +36,7 @@ public class Packet extends ArrayList<Request> {
|
||||
|
||||
/**
|
||||
* This method removes all {@link Request Requests} of given {@link RequestType types} from the {@link Packet}.
|
||||
*
|
||||
* @param types RequestType(s) to remove
|
||||
*/
|
||||
public void removeRequestsOfTypes(RequestType... types) {
|
||||
@ -43,7 +45,9 @@ public class Packet extends ArrayList<Request> {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method removes all {@link Request Requests} except of given {@link RequestType types} from the {@link Packet}.
|
||||
* This method removes all {@link Request Requests} except of given {@link RequestType types} from the {@link
|
||||
* Packet}.
|
||||
*
|
||||
* @param types RequestType(s) to not remove
|
||||
*/
|
||||
public void removeRequestsNotOfTypes(RequestType... types) {
|
||||
|
@ -18,15 +18,13 @@ public class PauseSegment implements Segment {
|
||||
|
||||
private boolean paused;
|
||||
|
||||
public PauseSegment(){
|
||||
public PauseSegment() {
|
||||
paused = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method pauses the game if it is not already paused.
|
||||
*/
|
||||
public void pauseGame(){
|
||||
if(!paused) {
|
||||
/** This method pauses the game if it is not already paused.*/
|
||||
public void pauseGame() {
|
||||
if (!paused) {
|
||||
paused = true;
|
||||
Logger.debug("Game paused.");
|
||||
}
|
||||
@ -35,8 +33,8 @@ public class PauseSegment implements Segment {
|
||||
/**
|
||||
* This method ends a paused game.
|
||||
*/
|
||||
public void pauseEnd(){
|
||||
if(paused) {
|
||||
public void pauseEnd() {
|
||||
if (paused) {
|
||||
paused = false;
|
||||
Logger.debug("Game unpaused.");
|
||||
}
|
||||
@ -48,8 +46,8 @@ public class PauseSegment implements Segment {
|
||||
|
||||
|
||||
/**
|
||||
* Pipelining method to process a set of requests.
|
||||
* The list of requests will be processed according to the following rules:
|
||||
* Pipelining method to process a set of requests. The list of requests will be processed according to the following
|
||||
* rules:
|
||||
* <ul>
|
||||
* <li>If there is a PauseStartRequest, the paused value will be set to true</li>
|
||||
* <li>Any CharacterRequests will be removed from the requests if the game is paused. These include:
|
||||
|
@ -9,8 +9,8 @@ import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The {@link Pipeline} class pipelines {@link Request Requests} through {@link Segment Segments} to
|
||||
* create {@link Event Events}. To add functionality without dabbling in the game library, create a new {@link Segment}.
|
||||
* The {@link Pipeline} class pipelines {@link Request Requests} through {@link Segment Segments} to create {@link Event
|
||||
* Events}. To add functionality without dabbling in the game library, create a new {@link Segment}.
|
||||
*/
|
||||
public class Pipeline {
|
||||
|
||||
@ -25,17 +25,17 @@ public class Pipeline {
|
||||
}
|
||||
|
||||
/**
|
||||
* Pipeline the {@link Request Requests} through all {@link Segment Segments} that are in the pipeline.
|
||||
* The {@link Request Requests} are declared as the <b>Packet</b>. The {@link Request Requests} are filtered
|
||||
* by each {@link Segment segment}, whereby each {@link Segment segment} takes (and thereby removes) all
|
||||
* {@link Event events} relevant to the {@link Segment segment}. The {@link Event Events} are returned at the
|
||||
* end of the pipeline.
|
||||
* Pipeline the {@link Request Requests} through all {@link Segment Segments} that are in the pipeline. The {@link
|
||||
* Request Requests} are declared as the <b>Packet</b>. The {@link Request Requests} are filtered by each {@link
|
||||
* Segment segment}, whereby each {@link Segment segment} takes (and thereby removes) all {@link Event events}
|
||||
* relevant to the {@link Segment segment}. The {@link Event Events} are returned at the end of the pipeline.
|
||||
*
|
||||
* @param requests are the requests that are being pipelined through the pipeline
|
||||
* @return a {@link Optional}<{@link Event}[]>, whereby the state of the {@link Optional} declares whether the
|
||||
* execution of the {@link Pipeline} was successful. <b>If the optional is empty, the input requests were
|
||||
* invalid, and an error message can be sent to the client</b>. To get the {@link Event Events}
|
||||
* out of the {@link Optional}, first check whether the {@link Optional} is empty by doing
|
||||
* {@link Optional#isEmpty()} or {@link Optional#isPresent()}, and act accordingly.
|
||||
* execution of the {@link Pipeline} was successful. <b>If the optional is empty, the input requests were invalid,
|
||||
* and an error message can be sent to the client</b>. To get the {@link Event Events} out of the {@link Optional},
|
||||
* first check whether the {@link Optional} is empty by doing {@link Optional#isEmpty()} or {@link
|
||||
* Optional#isPresent()}, and act accordingly.
|
||||
*/
|
||||
public Optional<List<Event>> processRequests(Request[] requests, Participant origin) {
|
||||
Logger.trace("Pipeline started RequestProcessing");
|
||||
@ -48,7 +48,7 @@ public class Pipeline {
|
||||
|
||||
Logger.trace("Iterating through segments");
|
||||
// Loop through all segments
|
||||
for (Segment segment: segments) {
|
||||
for (Segment segment : segments) {
|
||||
// Give the segment the packet, carrier and abort, and let it process requests
|
||||
segment.processRequests(packet, carrier, abort);
|
||||
if (packet.size() == 0 || abort.get()) { // if packet is empty (all requests processed) or abort initiated
|
||||
@ -65,10 +65,11 @@ public class Pipeline {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a segment to the pipeline. Note that <b>ORDER MATTERS!</b> The first segment added will be the
|
||||
* first segment to process events. Also note that segments <b>cannot be removed once added</b>. This is by
|
||||
* design, as removing segments would give the pipeline runtime customizability, which it is not supposed to
|
||||
* possess in the first place.
|
||||
* Adds a segment to the pipeline. Note that <b>ORDER MATTERS!</b> The first segment added will be the first segment
|
||||
* to process events. Also note that segments <b>cannot be removed once added</b>. This is by design, as removing
|
||||
* segments would give the pipeline runtime customizability, which it is not supposed to possess in the first
|
||||
* place.
|
||||
*
|
||||
* @param toAdd is the segment that will be added to the pipeline
|
||||
* @return the pipeline itself for chaining
|
||||
*/
|
||||
@ -77,7 +78,7 @@ public class Pipeline {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean contains(Segment segment){
|
||||
public boolean contains(Segment segment) {
|
||||
return segments.contains(segment);
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The {@link RequestGameStateSegment} handles requests of {@link RequestType} Req.
|
||||
* Therefore it sends the active gamestate and clears the {@link Packet} afterwards.
|
||||
* The {@link RequestGameStateSegment} handles requests of {@link RequestType} Req. Therefore it sends the active
|
||||
* gamestate and clears the {@link Packet} afterwards.
|
||||
*/
|
||||
public class RequestGameStateSegment implements Segment {
|
||||
|
||||
|
@ -8,18 +8,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public interface Segment {
|
||||
/**
|
||||
* Pipelining method to process a set of requests.
|
||||
* The list of requests will be processed according to some set of rules. Hereby,
|
||||
* the {@link Request Requests} in the packet will be filtered out as appropriate,
|
||||
* whereby the resulting {@link Event Events} are appended to the carrier.
|
||||
* @param packet is a {@link List} of {@link Request Requests} that is filtered
|
||||
* by the {@link Segment} as appropriate
|
||||
* @param carrier is a {@link List} of {@link Event Events} that is appended to
|
||||
* if new requests are generated from the execution of the {@link Segment}
|
||||
* @param abort is an {@link AtomicBoolean} describing whether an error has occurred during the execution of
|
||||
* the {@link Segment} Note that <b>error</b> here does <b>not describe an execution error</b>
|
||||
* of the segment, but instead an error in the events passed to it, like for example moving into a Rock.
|
||||
* The conventional way of setting this boolean is to write {@code abort.set(true); return;}
|
||||
* Pipelining method to process a set of requests. The list of requests will be processed according to some set of
|
||||
* rules. Hereby, the {@link Request Requests} in the packet will be filtered out as appropriate, whereby the
|
||||
* resulting {@link Event Events} are appended to the carrier.
|
||||
*
|
||||
* @param packet is a {@link List} of {@link Request Requests} that is filtered by the {@link Segment} as
|
||||
* appropriate
|
||||
* @param carrier is a {@link List} of {@link Event Events} that is appended to if new requests are generated from
|
||||
* the execution of the {@link Segment}
|
||||
* @param abort is an {@link AtomicBoolean} describing whether an error has occurred during the execution of the
|
||||
* {@link Segment} Note that <b>error</b> here does <b>not describe an execution error</b> of the
|
||||
* segment, but instead an error in the events passed to it, like for example moving into a Rock. The
|
||||
* conventional way of setting this boolean is to write {@code abort.set(true); return;}
|
||||
*/
|
||||
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user