From 091a094d36d8fa783aa9ae4fb5e40faac0e70563 Mon Sep 17 00:00:00 2001 From: Andrew Guibert Date: Sat, 10 Feb 2018 00:22:09 -0600 Subject: [PATCH] Automatically re-queue spectators to next round after 5s --- .../org/libertybikes/game/core/Client.java | 8 +++++++- .../org/libertybikes/game/core/GameRound.java | 14 +++++++++++-- .../round/service/GameRoundWebsocket.java | 20 +++++++++++-------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/game-service/src/main/java/org/libertybikes/game/core/Client.java b/game-service/src/main/java/org/libertybikes/game/core/Client.java index 987c2e1..dce93f6 100644 --- a/game-service/src/main/java/org/libertybikes/game/core/Client.java +++ b/game-service/src/main/java/org/libertybikes/game/core/Client.java @@ -9,20 +9,26 @@ public class Client { public final Session session; public final Player player; + public final boolean autoRequeue; /** * Create a client which is only a spectator of a game */ public Client(Session s) { - this(s, null); + this(s, null, true); } /** * Create a player who will be participating in the game */ public Client(Session s, Player p) { + this(s, p, false); + } + + private Client(Session s, Player p, boolean autoReque) { session = s; player = p; + this.autoRequeue = autoReque; } public boolean isPlayer() { diff --git a/game-service/src/main/java/org/libertybikes/game/core/GameRound.java b/game-service/src/main/java/org/libertybikes/game/core/GameRound.java index 845d753..4083cca 100644 --- a/game-service/src/main/java/org/libertybikes/game/core/GameRound.java +++ b/game-service/src/main/java/org/libertybikes/game/core/GameRound.java @@ -8,11 +8,12 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.Set; -import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; +import javax.enterprise.concurrent.ManagedScheduledExecutorService; +import javax.enterprise.inject.spi.CDI; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.naming.InitialContext; @@ -21,6 +22,8 @@ import javax.websocket.Session; import org.libertybikes.game.core.ClientMessage.GameEvent; import org.libertybikes.game.core.Player.STATUS; +import org.libertybikes.game.round.service.GameRoundService; +import org.libertybikes.game.round.service.GameRoundWebsocket; public class GameRound implements Runnable { @@ -134,6 +137,13 @@ public class GameRound implements Runnable { } runningGames.decrementAndGet(); System.out.println("Finished round: " + id); + + System.out.println("Clients flagged for auto-requeue will be redirected to the next round in 5 seconds..."); + delay(5000); + GameRoundService gameSvc = CDI.current().select(GameRoundService.class).get(); + for (Client c : clients.values()) + if (c.autoRequeue) + GameRoundWebsocket.requeueClient(gameSvc, this, c.session); } private void gameTick() { @@ -220,7 +230,7 @@ public class GameRound implements Runnable { broadcastPlayerList(); if (!gameRunning.get()) { try { - ExecutorService exec = InitialContext.doLookup("java:comp/DefaultManagedExecutorService"); + ManagedScheduledExecutorService exec = InitialContext.doLookup("java:comp/DefaultManagedScheduledExecutorService"); exec.submit(this); } catch (NamingException e) { System.out.println("Unable to start game due to: " + e); diff --git a/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java b/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java index 097f93f..377487f 100644 --- a/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java +++ b/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java @@ -57,14 +57,7 @@ public class GameRoundWebsocket { System.out.println("[onMessage] roundId=" + roundId + " msg=" + message); if (msg.event != null && GameEvent.GAME_REQUEUE == msg.event) { - GameRound nextGame = gameSvc.requeue(round); - String requeueMsg = Json.createObjectBuilder() - .add("requeue", nextGame.id) - .build() - .toString(); - sendTextToClient(session, requeueMsg); - if (round.removeClient(session) == 0) - gameSvc.deleteRound(roundId); + requeueClient(gameSvc, round, session); } else { round.handleMessage(msg, session); } @@ -73,6 +66,17 @@ public class GameRoundWebsocket { } } + public static void requeueClient(GameRoundService gameSvc, GameRound oldRound, Session s) { + GameRound nextGame = gameSvc.requeue(oldRound); + String requeueMsg = Json.createObjectBuilder() + .add("requeue", nextGame.id) + .build() + .toString(); + sendTextToClient(s, requeueMsg); + if (oldRound.removeClient(s) == 0) + gameSvc.deleteRound(oldRound.id); + } + public static void sendTextToClient(Session client, String message) { if (client != null) { try {