Automatically re-queue spectators to next round after 5s

This commit is contained in:
Andrew Guibert 2018-02-10 00:22:09 -06:00
parent f150f69568
commit 091a094d36
3 changed files with 31 additions and 11 deletions

View File

@ -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() {

View File

@ -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);

View File

@ -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 {