diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/game/GameSession.java b/Server/src/main/java/uulm/teamname/marvelous/server/game/GameSession.java index 3c541bb..652964c 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/game/GameSession.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/game/GameSession.java @@ -2,6 +2,7 @@ package uulm.teamname.marvelous.server.game; import uulm.teamname.marvelous.gamelibrary.ArrayTools; import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties; +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; @@ -17,16 +18,25 @@ import uulm.teamname.marvelous.server.net.Client; import uulm.teamname.marvelous.server.net.ClientState; import java.util.*; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; public class GameSession { public final String id = UUID.randomUUID().toString(); + private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(3); + public final HashMap characterChoices = new HashMap<>(); private final HashMap characterIndices = new HashMap<>(); private final HashMap> characterSelection = new HashMap<>(); private final HashMap badRequests = new HashMap<>(); + private ParticipantType turnWaiting; + private ScheduledFuture turnTimer; + private boolean started = false; private GameInstance instance; @@ -131,6 +141,9 @@ public class GameSession { ServerApplication.getSession().broadcast(message); started = true; + + turnWaiting = instance.state.getActiveCharacter().type == EntityType.P1 ? ParticipantType.PlayerOne : ParticipantType.PlayerTwo; + turnTimer = scheduler.schedule(this::timeout, ServerApplication.getPartyConfig().maxRoundTime, TimeUnit.SECONDS); } private void initialize() { @@ -179,7 +192,14 @@ public class GameSession { response.messages = accepted.toArray(new Event[0]); source.sendMessage(response); - badRequests.replace(source.getType(), 0); + badRequests.put(source.getType(), 0); + + if(turnTimer != null) { + turnTimer.cancel(true); + } + + turnWaiting = source.getType(); + turnTimer = scheduler.schedule(this::timeout, ServerApplication.getPartyConfig().maxRoundTime, TimeUnit.SECONDS); } private void reject(Client source) { @@ -191,7 +211,7 @@ public class GameSession { source.sendMessage(response); int bad = badRequests.getOrDefault(source.getType(), 0) + 1; - badRequests.replace(source.getType(), bad); + badRequests.put(source.getType(), bad); if(bad >= 5) { winner(source.getType() == ParticipantType.PlayerOne ? ParticipantType.PlayerTwo : ParticipantType.PlayerOne); @@ -212,6 +232,21 @@ public class GameSession { ServerApplication.getSession().reset(); } + private void timeout() { + for(Client player: ServerApplication.getSession().getPlayers()) { + if(player != null && player.getType() != turnWaiting) { + EventMessage notification = new EventMessage(); + notification.messages = new Event[] { new EventBuilder(EventType.TurnTimeoutEvent).buildGameEvent() }; + player.sendMessage(notification); + break; + } + } + + EventMessage message = new EventMessage(); + message.messages = instance.endTurn().toArray(new Event[0]); + ServerApplication.getSession().broadcast(message); + } + private GameStructureMessage getGameStructure(ParticipantType assignment) { GameStructureMessage message = new GameStructureMessage();