feat: add turn timeout timer

This commit is contained in:
punchready 2021-08-11 01:46:38 +02:00
parent cefee10880
commit 382b6a859d
1 changed files with 37 additions and 2 deletions

View File

@ -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<ParticipantType, CharacterProperties[]> characterChoices = new HashMap<>();
private final HashMap<ParticipantType, Integer[]> characterIndices = new HashMap<>();
private final HashMap<ParticipantType, List<Integer>> characterSelection = new HashMap<>();
private final HashMap<ParticipantType, Integer> 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();