From cb1f99814adf86e69f30d9ec55f46ae0015b0627 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 1 Jun 2021 18:07:43 +0200 Subject: [PATCH] feat: implemented wins and disconnects in Lobby --- .../marvelous/server/lobby/Lobby.java | 84 +++++++++++++++---- .../server/lobbymanager/MessageRelay.java | 14 ++++ 2 files changed, 80 insertions(+), 18 deletions(-) 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 ba577c9..400d295 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 @@ -6,40 +6,48 @@ import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventType; 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.json.config.CharacterConfig; +import uulm.teamname.marvelous.gamelibrary.json.config.PartyConfig; +import uulm.teamname.marvelous.gamelibrary.json.config.ScenarioConfig; import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; -import uulm.teamname.marvelous.server.lobby.pipelining.PauseSegment; -import uulm.teamname.marvelous.server.lobby.pipelining.Pipeline; -import uulm.teamname.marvelous.server.lobby.pipelining.RequestGameStateSegment; +import uulm.teamname.marvelous.server.lobby.pipelining.*; import uulm.teamname.marvelous.server.lobbymanager.LobbyConnection; import uulm.teamname.marvelous.server.lobbymanager.Participant; import java.util.*; -public class Lobby extends EventObserver { +public class Lobby { public final String gameID; public final LobbyConnection connection; public final GameInstance game; public final Pipeline pipeline; public final PauseSegment pauseSegment; - public final RequestGameStateSegment reqSegment; private final PauseHandler pause = new PauseHandler(); - public Lobby(String gameID, IntVector2 mapSize, LobbyConnection connection) { + public Lobby( + String gameID, + LobbyConnection connection, + PartyConfig partyConfig, + CharacterConfig characterConfig, + ScenarioConfig scenarioConfig) { this.gameID = gameID; this.connection = connection; - game = new GameInstance(); //TODO: Add config objects here - game.addObserver(this); + this.game = new GameInstance( + partyConfig, + characterConfig, + scenarioConfig + ); //TODO: Add config objects here this.pipeline = new Pipeline(); + var reqSegment = new RequestGameStateSegment(this.game); this.pauseSegment = new PauseSegment(); - this.reqSegment = new RequestGameStateSegment(this); - } + var disconnectSegment = new DisconnectSegment(this); + var gameStateSegment = new GameStateSegment(this.game); + - @Override - protected void handle(Event[] events) { - connection.broadcastEvents(events); } public void receiveRequests(Participant source, Request[] requests) { @@ -69,14 +77,15 @@ public class Lobby extends EventObserver { } } } - - if(!game.checkRequestsAndApply(stateRelevant.toArray(new Request[0]))) { - // requests don't apply, tell the client that they need to update their state - reject(source); - } +// +// if(!game.checkRequestsAndApply(stateRelevant.toArray(new Request[0]))) { TODO: Fix this. +// // requests don't apply, tell the client that they need to update their state +// reject(source); +// } } private void processRequest(Participant source, Request request) { + // TODO: Implement as pipeline switch(request.type) { case PauseStopRequest -> { pause.stop(); @@ -99,4 +108,43 @@ public class Lobby extends EventObserver { private void reject(Participant source) { connection.sendEvents(source, new EventBuilder(EventType.Nack).buildGameEvent(), game.getGameStateEvent()); } + + public void soonTimeout (Participant source){ + connection.sendEvents( + source, + new EventBuilder(EventType.TimeoutWarningEvent).buildGameStateEvent()); + } + + public void Timeout(Participant source){ + connection.sendEvents( + source, + new EventBuilder(EventType.TimeoutEvent).buildGameStateEvent()); + connection.removePlayer(source); + + if(connection.hasPlayer1()){ + generateWin(connection.getPlayer1()); + } + else if(connection.hasPlayer2()) { + generateWin(connection.getPlayer2()); + } + else { + throw new IllegalStateException("Spectator was time-outed which is impossible"); + } + } + + /** + * 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 void generateWin(Participant winner){ + connection.broadcastEvents( + new EventBuilder(EventType.WinEvent) + .withPlayerWon(winner.type.equals(ParticipantType.Player1) ? 1 : 2) + .buildGameStateEvent(), + new EventBuilder(EventType.DisconnectEvent) + .buildGameStateEvent()); + connection.terminateConnection(); + } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/MessageRelay.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/MessageRelay.java index 4ad720c..45d266c 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/MessageRelay.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/MessageRelay.java @@ -42,10 +42,24 @@ public class MessageRelay { } } + /** + * Broadcasts events to all {@link Participant Participants} of a {@link LobbyConnection}. + * @param origin is the {@link LobbyConnection} that the {@link Event Events} are broadcasted from + * @param events are the {@link Event Events} to be broadcasted + */ public void broadcastEvents (LobbyConnection origin, Event[] events) { // TODO: Create JSON // TODO: send to target } + /** + * Terminates a given {@link LobbyConnection}. This is a End-of-Lifecycle method for + * {@link LobbyConnection LobbyConnections}, and they may not be reused again. + * @param origin is the LobbyConnection to be terminated + */ + public void terminate (LobbyConnection origin) { + // TODO: implement + } + }