feat: add turn timeout timer
This commit is contained in:
parent
cefee10880
commit
382b6a859d
@ -2,6 +2,7 @@ package uulm.teamname.marvelous.server.game;
|
|||||||
|
|
||||||
import uulm.teamname.marvelous.gamelibrary.ArrayTools;
|
import uulm.teamname.marvelous.gamelibrary.ArrayTools;
|
||||||
import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties;
|
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.Event;
|
||||||
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
|
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
|
||||||
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
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 uulm.teamname.marvelous.server.net.ClientState;
|
||||||
|
|
||||||
import java.util.*;
|
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 class GameSession {
|
||||||
public final String id = UUID.randomUUID().toString();
|
public final String id = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(3);
|
||||||
|
|
||||||
public final HashMap<ParticipantType, CharacterProperties[]> characterChoices = new HashMap<>();
|
public final HashMap<ParticipantType, CharacterProperties[]> characterChoices = new HashMap<>();
|
||||||
private final HashMap<ParticipantType, Integer[]> characterIndices = new HashMap<>();
|
private final HashMap<ParticipantType, Integer[]> characterIndices = new HashMap<>();
|
||||||
private final HashMap<ParticipantType, List<Integer>> characterSelection = new HashMap<>();
|
private final HashMap<ParticipantType, List<Integer>> characterSelection = new HashMap<>();
|
||||||
|
|
||||||
private final HashMap<ParticipantType, Integer> badRequests = new HashMap<>();
|
private final HashMap<ParticipantType, Integer> badRequests = new HashMap<>();
|
||||||
|
|
||||||
|
private ParticipantType turnWaiting;
|
||||||
|
private ScheduledFuture<?> turnTimer;
|
||||||
|
|
||||||
private boolean started = false;
|
private boolean started = false;
|
||||||
|
|
||||||
private GameInstance instance;
|
private GameInstance instance;
|
||||||
@ -131,6 +141,9 @@ public class GameSession {
|
|||||||
ServerApplication.getSession().broadcast(message);
|
ServerApplication.getSession().broadcast(message);
|
||||||
|
|
||||||
started = true;
|
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() {
|
private void initialize() {
|
||||||
@ -179,7 +192,14 @@ public class GameSession {
|
|||||||
response.messages = accepted.toArray(new Event[0]);
|
response.messages = accepted.toArray(new Event[0]);
|
||||||
source.sendMessage(response);
|
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) {
|
private void reject(Client source) {
|
||||||
@ -191,7 +211,7 @@ public class GameSession {
|
|||||||
source.sendMessage(response);
|
source.sendMessage(response);
|
||||||
|
|
||||||
int bad = badRequests.getOrDefault(source.getType(), 0) + 1;
|
int bad = badRequests.getOrDefault(source.getType(), 0) + 1;
|
||||||
badRequests.replace(source.getType(), bad);
|
badRequests.put(source.getType(), bad);
|
||||||
|
|
||||||
if(bad >= 5) {
|
if(bad >= 5) {
|
||||||
winner(source.getType() == ParticipantType.PlayerOne ? ParticipantType.PlayerTwo : ParticipantType.PlayerOne);
|
winner(source.getType() == ParticipantType.PlayerOne ? ParticipantType.PlayerTwo : ParticipantType.PlayerOne);
|
||||||
@ -212,6 +232,21 @@ public class GameSession {
|
|||||||
ServerApplication.getSession().reset();
|
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) {
|
private GameStructureMessage getGameStructure(ParticipantType assignment) {
|
||||||
GameStructureMessage message = new GameStructureMessage();
|
GameStructureMessage message = new GameStructureMessage();
|
||||||
|
Loading…
Reference in New Issue
Block a user