refactor: formatted Lobby files properly

This commit is contained in:
Yannik Bretschneider 2021-06-07 01:58:25 +02:00
parent c24121b6d9
commit 16c425c507
9 changed files with 90 additions and 83 deletions

View File

@ -30,14 +30,14 @@ public class Lobby {
private final TurnTimer turnTimer; private final TurnTimer turnTimer;
/** /**
* The {@link Lobby} is where the magic happens. * The {@link Lobby} is where the magic happens. In this class is a whole {@link GameInstance game} is processed. To
* In this class is a whole {@link GameInstance game} is processed. * initialize the game it gets the following parameters
* To initialize the game it gets the following parameters *
* @param gameID a String to identify the game * @param gameID a String to identify the game
* @param connection the Connection to the {@link uulm.teamname.marvelous.server.lobbymanager.LobbyManager} * @param connection the Connection to the {@link uulm.teamname.marvelous.server.lobbymanager.LobbyManager}
* @param partyConfig declared in Editor * @param partyConfig declared in Editor
* @param characterConfig declared in Editor * @param characterConfig declared in Editor
* @param scenarioConfig declared in Editor * @param scenarioConfig declared in Editor
*/ */
public Lobby( public Lobby(
String gameID, String gameID,
@ -84,15 +84,16 @@ public class Lobby {
/** /**
* Called by {@link LobbyConnection} to handle requests * Called by {@link LobbyConnection} to handle requests
*
* @param requests to be processed * @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) { public synchronized void receiveRequests(Request[] requests, Participant source) {
Logger.trace("Received {} requests from participant '{}' of type {}", Logger.trace("Received {} requests from participant '{}' of type {}",
requests.length, requests.length,
source.name, source.name,
source.type); 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"); Logger.trace("Resetting bad requests as new participant sent data");
activePlayer = source; activePlayer = source;
badRequests = 0; badRequests = 0;
@ -118,8 +119,8 @@ public class Lobby {
} }
/** /**
* This method is called at the end of receiveRequests, to start a timer. * This method is called at the end of receiveRequests, to start a timer. The active player has now a specific
* The active player has now a specific amount of time to do his moves. * amount of time to do his moves.
*/ */
void updateTimer() { void updateTimer() {
var currentActiveCharacterType = game.state.getActiveCharacter().type; var currentActiveCharacterType = game.state.getActiveCharacter().type;
@ -151,28 +152,29 @@ public class Lobby {
/** /**
* If the player executed a false request the request gets rejected. * If the player executed a false request the request gets rejected.
*
* @param source the executing player * @param source the executing player
*/ */
private void reject(Participant source) { private void reject(Participant source) {
connection.sendEvents(source, new EventBuilder(EventType.Nack).buildGameEvent(), game.getGameStateEvent()); 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 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); connection.removeParticipant(source);
if(connection.hasPlayer1()){ if (connection.hasPlayer1()) {
generateWin(connection.getPlayer1()); generateWin(connection.getPlayer1());
} } else if (connection.hasPlayer2()) {
else if(connection.hasPlayer2()) { generateWin(connection.getPlayer2());
generateWin(connection.getPlayer2());
} }
} }
} }
/** /**
* Warns the player he get timeouted soon. * Warns the player he get timeouted soon.
*
* @param source soon to be timeouted player * @param source soon to be timeouted player
*/ */
public synchronized void soonTimeout (Participant source){ public synchronized void soonTimeout(Participant source) {
connection.sendEvents( connection.sendEvents(
source, source,
new EventBuilder(EventType.TimeoutWarningEvent).buildGameStateEvent()); new EventBuilder(EventType.TimeoutWarningEvent).buildGameStateEvent());
@ -180,27 +182,26 @@ public class Lobby {
/** /**
* If a player times out the other player automatically wins. * If a player times out the other player automatically wins.
*
* @param source is the timeouted player * @param source is the timeouted player
*/ */
public synchronized void timeout(Participant source){ public synchronized void timeout(Participant source) {
connection.sendEvents( connection.sendEvents(
source, source,
new EventBuilder(EventType.TimeoutEvent).buildGameStateEvent()); new EventBuilder(EventType.TimeoutEvent).buildGameStateEvent());
connection.removeParticipant(source); connection.removeParticipant(source);
if(connection.hasPlayer1() && !connection.hasPlayer2()){ if (connection.hasPlayer1() && !connection.hasPlayer2()) {
generateWin(connection.getPlayer1()); generateWin(connection.getPlayer1());
} } else if (!connection.hasPlayer1() && connection.hasPlayer2()) {
else if(!connection.hasPlayer1() && connection.hasPlayer2()) {
generateWin(connection.getPlayer2()); generateWin(connection.getPlayer2());
} } else {
else {
throw new IllegalStateException("Spectator was time-outed which is impossible"); 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}. */ /** 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(); var nextTurnEvents = game.endTurn();
nextTurnEvents.add(game.getGameStateEvent()); nextTurnEvents.add(game.getGameStateEvent());
connection.broadcastEvents(); connection.broadcastEvents();
@ -208,12 +209,12 @@ public class Lobby {
} }
/** /**
* The method generates a Win event for a {@link Participant}. * The method generates a Win event for a {@link Participant}. Afterwards it sends a Disconnect to everyone, and
* Afterwards it sends a Disconnect to everyone, and terminates the connection. * terminates the connection.
* *
* @param winner is the {@link Participant} that won * @param winner is the {@link Participant} that won
*/ */
public synchronized void generateWin(Participant winner){ public synchronized void generateWin(Participant winner) {
connection.broadcastEvents( connection.broadcastEvents(
new EventBuilder(EventType.WinEvent) new EventBuilder(EventType.WinEvent)
.withPlayerWon(winner.type.equals(ParticipantType.PlayerOne) ? 1 : 2) .withPlayerWon(winner.type.equals(ParticipantType.PlayerOne) ? 1 : 2)

View File

@ -34,6 +34,7 @@ public class TurnTimer {
/** /**
* This method checks if the participant is not a spectator. Otherwise it won't start a timer. * This method checks if the participant is not a spectator. Otherwise it won't start a timer.
*
* @param participant the timer is for * @param participant the timer is for
*/ */
public void startTurnTimer(Participant participant) { public void startTurnTimer(Participant participant) {
@ -44,7 +45,10 @@ public class TurnTimer {
clear(); clear();
Logger.debug("Starting turn timer for participant '{}' with role {}", Logger.debug("Starting turn timer for participant '{}' with role {}",
participant.name, participant.type); participant.name, participant.type);
current = timer.schedule(() -> {callback.accept(participant); return participant;}, current = timer.schedule(() -> {
callback.accept(participant);
return participant;
},
maxRoundTime, maxRoundTime,
TimeUnit.SECONDS); TimeUnit.SECONDS);
} }

View File

@ -10,8 +10,8 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* The {@link DisconnectSegment} handles requests of {@link RequestType} DisconnectRequest. * The {@link DisconnectSegment} handles requests of {@link RequestType} DisconnectRequest. Therefore it removes the
* Therefore it removes the disconnecting player-connection from the {@link Lobby}. * disconnecting player-connection from the {@link Lobby}.
*/ */
public class DisconnectSegment implements Segment { public class DisconnectSegment implements Segment {
@ -29,10 +29,9 @@ public class DisconnectSegment implements Segment {
parent.getConnection().removeParticipant(packet.getOrigin()); parent.getConnection().removeParticipant(packet.getOrigin());
if (packet.getOrigin().type != ParticipantType.Spectator) { if (packet.getOrigin().type != ParticipantType.Spectator) {
if(parent.getConnection().hasPlayer1()){ if (parent.getConnection().hasPlayer1()) {
parent.generateWin(parent.getConnection().getPlayer1()); parent.generateWin(parent.getConnection().getPlayer1());
} } else if (parent.getConnection().hasPlayer2()) {
else if(parent.getConnection().hasPlayer2()) {
parent.generateWin(parent.getConnection().getPlayer2()); parent.generateWin(parent.getConnection().getPlayer2());
} }
} }

View File

@ -12,14 +12,14 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* The {@link GameStateSegment} handles all {@link GameInstance game} relevant {@link Request Requests}. * The {@link GameStateSegment} handles all {@link GameInstance game} relevant {@link Request Requests}. Therefore it
* Therefore it updates the game with the matching request. * updates the game with the matching request.
*/ */
public class GameStateSegment implements Segment { public class GameStateSegment implements Segment {
private GameInstance game; private GameInstance game;
public GameStateSegment(GameInstance game){ public GameStateSegment(GameInstance game) {
this.game = game; this.game = game;
} }

View File

@ -7,20 +7,21 @@ import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.*; import java.util.*;
/** /**
* The {@link Packet} contains all {@link Request Requests} and the belonging {@link Participant}. * The {@link Packet} contains all {@link Request Requests} and the belonging {@link Participant}. You can add and
* You can add and remove {@link Request Requests} at will. * remove {@link Request Requests} at will.
*/ */
public class Packet extends ArrayList<Request> { public class Packet extends ArrayList<Request> {
private final Participant origin; private final Participant origin;
public Packet (Request[] requests, Participant origin) { public Packet(Request[] requests, Participant origin) {
this.origin = origin; this.origin = origin;
addAll(Arrays.asList(requests)); addAll(Arrays.asList(requests));
} }
/** /**
* This method checks if a {@link RequestType} is part of the {@link Packet} or not * This method checks if a {@link RequestType} is part of the {@link Packet} or not
*
* @param requiredType RequestType to check. * @param requiredType RequestType to check.
* @return boolean * @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}. * This method removes all {@link Request Requests} of given {@link RequestType types} from the {@link Packet}.
*
* @param types RequestType(s) to remove * @param types RequestType(s) to remove
*/ */
public void removeRequestsOfTypes(RequestType... types) { 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 * @param types RequestType(s) to not remove
*/ */
public void removeRequestsNotOfTypes(RequestType... types) { public void removeRequestsNotOfTypes(RequestType... types) {

View File

@ -18,15 +18,13 @@ public class PauseSegment implements Segment {
private boolean paused; private boolean paused;
public PauseSegment(){ public PauseSegment() {
paused = false; paused = false;
} }
/** /** This method pauses the game if it is not already paused.*/
* This method pauses the game if it is not already paused. public void pauseGame() {
*/ if (!paused) {
public void pauseGame(){
if(!paused) {
paused = true; paused = true;
Logger.debug("Game paused."); Logger.debug("Game paused.");
} }
@ -35,8 +33,8 @@ public class PauseSegment implements Segment {
/** /**
* This method ends a paused game. * This method ends a paused game.
*/ */
public void pauseEnd(){ public void pauseEnd() {
if(paused) { if (paused) {
paused = false; paused = false;
Logger.debug("Game unpaused."); Logger.debug("Game unpaused.");
} }
@ -48,8 +46,8 @@ public class PauseSegment implements Segment {
/** /**
* Pipelining method to process a set of requests. * Pipelining method to process a set of requests. The list of requests will be processed according to the following
* The list of requests will be processed according to the following rules: * rules:
* <ul> * <ul>
* <li>If there is a PauseStartRequest, the paused value will be set to true</li> * <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: * <li>Any CharacterRequests will be removed from the requests if the game is paused. These include:

View File

@ -9,8 +9,8 @@ import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* The {@link Pipeline} class pipelines {@link Request Requests} through {@link Segment Segments} to * The {@link Pipeline} class pipelines {@link Request Requests} through {@link Segment Segments} to create {@link Event
* create {@link Event Events}. To add functionality without dabbling in the game library, create a new {@link Segment}. * Events}. To add functionality without dabbling in the game library, create a new {@link Segment}.
*/ */
public class Pipeline { 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. * Pipeline the {@link Request Requests} through all {@link Segment Segments} that are in the pipeline. The {@link
* The {@link Request Requests} are declared as the <b>Packet</b>. The {@link Request Requests} are filtered * Request Requests} are declared as the <b>Packet</b>. The {@link Request Requests} are filtered by each {@link
* by each {@link Segment segment}, whereby each {@link Segment segment} takes (and thereby removes) all * Segment segment}, whereby each {@link Segment segment} takes (and thereby removes) all {@link Event events}
* {@link Event events} relevant to the {@link Segment segment}. The {@link Event Events} are returned at the * relevant to the {@link Segment segment}. The {@link Event Events} are returned at the end of the pipeline.
* end of the pipeline. *
* @param requests are the requests that are being pipelined through 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 * @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 * execution of the {@link Pipeline} was successful. <b>If the optional is empty, the input requests were invalid,
* invalid, and an error message can be sent to the client</b>. To get the {@link Event Events} * and an error message can be sent to the client</b>. To get the {@link Event Events} out of the {@link Optional},
* out of the {@link Optional}, first check whether the {@link Optional} is empty by doing * first check whether the {@link Optional} is empty by doing {@link Optional#isEmpty()} or {@link
* {@link Optional#isEmpty()} or {@link Optional#isPresent()}, and act accordingly. * Optional#isPresent()}, and act accordingly.
*/ */
public Optional<List<Event>> processRequests(Request[] requests, Participant origin) { public Optional<List<Event>> processRequests(Request[] requests, Participant origin) {
Logger.trace("Pipeline started RequestProcessing"); Logger.trace("Pipeline started RequestProcessing");
@ -48,7 +48,7 @@ public class Pipeline {
Logger.trace("Iterating through segments"); Logger.trace("Iterating through segments");
// Loop through all 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 // Give the segment the packet, carrier and abort, and let it process requests
segment.processRequests(packet, carrier, abort); segment.processRequests(packet, carrier, abort);
if (packet.size() == 0 || abort.get()) { // if packet is empty (all requests processed) or abort initiated 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 * Adds a segment to the pipeline. Note that <b>ORDER MATTERS!</b> The first segment added will be the first segment
* first segment to process events. Also note that segments <b>cannot be removed once added</b>. This is by * to process events. Also note that segments <b>cannot be removed once added</b>. This is by design, as removing
* design, as removing segments would give the pipeline runtime customizability, which it is not supposed to * segments would give the pipeline runtime customizability, which it is not supposed to possess in the first
* possess in the first place. * place.
*
* @param toAdd is the segment that will be added to the pipeline * @param toAdd is the segment that will be added to the pipeline
* @return the pipeline itself for chaining * @return the pipeline itself for chaining
*/ */
@ -77,7 +78,7 @@ public class Pipeline {
return this; return this;
} }
public boolean contains(Segment segment){ public boolean contains(Segment segment) {
return segments.contains(segment); return segments.contains(segment);
} }
} }

View File

@ -11,8 +11,8 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* The {@link RequestGameStateSegment} handles requests of {@link RequestType} Req. * The {@link RequestGameStateSegment} handles requests of {@link RequestType} Req. Therefore it sends the active
* Therefore it sends the active gamestate and clears the {@link Packet} afterwards. * gamestate and clears the {@link Packet} afterwards.
*/ */
public class RequestGameStateSegment implements Segment { public class RequestGameStateSegment implements Segment {

View File

@ -8,18 +8,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
public interface Segment { public interface Segment {
/** /**
* Pipelining method to process a set of requests. * Pipelining method to process a set of requests. The list of requests will be processed according to some set of
* The list of requests will be processed according to some set of rules. Hereby, * rules. Hereby, the {@link Request Requests} in the packet will be filtered out as appropriate, whereby the
* the {@link Request Requests} in the packet will be filtered out as appropriate, * resulting {@link Event Events} are appended to the carrier.
* whereby the resulting {@link Event Events} are appended to the carrier. *
* @param packet is a {@link List} of {@link Request Requests} that is filtered * @param packet is a {@link List} of {@link Request Requests} that is filtered by the {@link Segment} as
* by the {@link Segment} as appropriate * appropriate
* @param carrier is a {@link List} of {@link Event Events} that is appended to * @param carrier is a {@link List} of {@link Event Events} that is appended to if new requests are generated from
* if new requests are generated from the execution of the {@link Segment} * the execution of the {@link Segment}
* @param abort is an {@link AtomicBoolean} describing whether an error has occurred during the execution of * @param abort is an {@link AtomicBoolean} describing whether an error has occurred during the execution of the
* the {@link Segment} Note that <b>error</b> here does <b>not describe an execution error</b> * {@link Segment} Note that <b>error</b> here does <b>not describe an execution error</b> of the
* of the segment, but instead an error in the events passed to it, like for example moving into a Rock. * segment, but instead an error in the events passed to it, like for example moving into a Rock. The
* The conventional way of setting this boolean is to write {@code abort.set(true); return;} * conventional way of setting this boolean is to write {@code abort.set(true); return;}
*/ */
public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort); public void processRequests(Packet packet, List<Event> carrier, AtomicBoolean abort);
} }