From fcf43fcd831559f03556e39104c9a8eb9013c182 Mon Sep 17 00:00:00 2001 From: Richard Reiber Date: Wed, 19 May 2021 19:02:57 +0200 Subject: [PATCH] wip: Request pipelining --- .../marvelous/server/Lobby/Lobby.java | 78 ++++++++++--------- .../marvelous/server/Lobby/PauseHandler.java | 21 +++++ .../marvelous/server/Lobby/Pipeline.java | 23 ++++++ .../marvelous/server/Lobby/Segment.java | 8 ++ 4 files changed, 92 insertions(+), 38 deletions(-) create mode 100644 Server/src/main/java/uulm/teamname/marvelous/server/Lobby/PauseHandler.java create mode 100644 Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Pipeline.java create mode 100644 Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Segment.java diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Lobby.java b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Lobby.java index 6edac7e..6628539 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Lobby.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Lobby.java @@ -1,6 +1,10 @@ package uulm.teamname.marvelous.server.Lobby; import uulm.teamname.marvelous.gamelibrary.IntVector2; +import uulm.teamname.marvelous.gamelibrary.entities.Entity; +import uulm.teamname.marvelous.gamelibrary.entities.Character; +import uulm.teamname.marvelous.gamelibrary.entities.EntityID; +import uulm.teamname.marvelous.gamelibrary.entities.EntityType; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventType; @@ -8,7 +12,9 @@ import uulm.teamname.marvelous.gamelibrary.gamelogic.EventObserver; import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance; import uulm.teamname.marvelous.gamelibrary.gamelogic.ParticipantType; import uulm.teamname.marvelous.gamelibrary.requests.Request; +import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import uulm.teamname.marvelous.server.LobbyManager.LobbyConnection; +import uulm.teamname.marvelous.server.LobbyManager.Participant; import java.util.*; @@ -16,6 +22,7 @@ public class Lobby extends EventObserver { public final String gameID; public final LobbyConnection connection; public final GameInstance game; + private final PauseHandler pause = new PauseHandler(); public Lobby(String gameID, IntVector2 mapSize, LobbyConnection connection) { this.gameID = gameID; @@ -30,16 +37,31 @@ public class Lobby extends EventObserver { connection.broadcastEvents(events); } - public void receiveRequests(ParticipantType source, Request[] requests) { + public void receiveRequests(Participant source, Request[] requests) { ArrayList stateRelevant = new ArrayList<>(); for(Request request: requests) { switch(request.type) { case MeleeAttackRequest, RangedAttackRequest, MoveRequest, ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> { - stateRelevant.add(request); // these need to be checked all together by the game instance itself + if(!pause.isPaused()) { + //if game is paused, no actions should happen + stateRelevant.add(request); // these need to be checked all together by the game instance itself + } + else { + connection.sendEvents(source, new EventBuilder(EventType.Nack).buildGameEvent(), game.getGameStateEvent()); + } } case PauseStopRequest, PauseStartRequest, EndRoundRequest, DisconnectRequest, Req -> { - processRequest(source, request); + //if game is paused only PauseStop, Disconnect and Req are available + if(!pause.isPaused()){ + processRequest(source, request); + } + else if(request.type != RequestType.PauseStartRequest && request.type != RequestType.EndRoundRequest ) { + processRequest(source, request); + } + else { + connection.sendEvents(source, new EventBuilder(EventType.Nack).buildGameEvent(), game.getGameStateEvent()); + } return; // only one of these will ever be sent at once, and if that is the case, we wont have any other state relevant events } } @@ -51,58 +73,38 @@ public class Lobby extends EventObserver { } } - private void processRequest(ParticipantType source, Request request) { + private void processRequest(Participant source, Request request) { //TODO: handle the rest of the requests switch(request.type) { case PauseStopRequest -> { - + pause.pauseEnd(); + connection.sendEvents(source, new EventBuilder(EventType.PauseStopEvent).buildGameStateEvent(), game.getGameStateEvent()); } case PauseStartRequest -> { - + pause.pauseGame(); + connection.sendEvents(source, new EventBuilder(EventType.PauseStartEvent).buildGameStateEvent(), game.getGameStateEvent()); } case EndRoundRequest -> { } case DisconnectRequest -> { - + connection.removePlayer(source); + connection.sendEvents(source, new EventBuilder(EventType.DisconnectEvent).buildGameStateEvent(), game.getGameStateEvent()); } case Req -> { - + connection.sendEvents(source, game.getGameStateEvent()); } } } - public void pause() { + public void endRound(){ + ArrayList Entitys = new ArrayList<>(); + for (EntityID id : game.state.getTurnOrder()) { + if(((Character) game.state.getEntities().findEntity(id)).hp.getValue() == 0){ - } - public void unpause(){ - - } - public void handleDisconnects(){ - - } - public void DisconnectClient(){ - - } - public void checkCheating(){ - - } - public void SendGamestate(){ - - } - public void handleConnects(){ - - } - public void handleSpectators(){ - - } - public void handlePlayers(){ - - } - public void handleLogon(){ - - } - public void handleLogoff(){ + } + } + int index = game.state.getTurnOrder().indexOf(game.state.getActiveCharacter()); } public void closeLobby(){ diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/PauseHandler.java b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/PauseHandler.java new file mode 100644 index 0000000..fe1b982 --- /dev/null +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/PauseHandler.java @@ -0,0 +1,21 @@ +package uulm.teamname.marvelous.server.Lobby; + +public class PauseHandler { + private boolean paused; + + public PauseHandler(){ + paused = false; + } + public void pauseGame(){ + if(!paused) + paused = true; + } + public void pauseEnd(){ + if(paused) + paused = false; + } + + public boolean isPaused() { + return paused; + } +} diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Pipeline.java b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Pipeline.java new file mode 100644 index 0000000..601d903 --- /dev/null +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Pipeline.java @@ -0,0 +1,23 @@ +package uulm.teamname.marvelous.server.Lobby; + +import uulm.teamname.marvelous.gamelibrary.requests.Request; + +import java.util.ArrayList; + +public class Pipeline { + + private final Segment[] segments; + + public Pipeline(Segment[] segments) { + this.segments = segments; + } + + public void processEvents(Request[] requests) { + Request[] out; + for (Segment segment: segments) { + out = segment.processEvents(requests); + } + } + + +} diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Segment.java b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Segment.java new file mode 100644 index 0000000..48d9ad2 --- /dev/null +++ b/Server/src/main/java/uulm/teamname/marvelous/server/Lobby/Segment.java @@ -0,0 +1,8 @@ +package uulm.teamname.marvelous.server.Lobby; + +import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.requests.Request; + +public interface Segment { + public Request[] processEvents(Request[] events); +}