Implement game requeue with session following

This commit is contained in:
Andrew Guibert 2018-01-28 21:55:38 -06:00
parent cd38b43b66
commit 5cadd66cfe
5 changed files with 29 additions and 18 deletions

View File

@ -38,10 +38,12 @@ subprojects {
runtimeUrl = "https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/downloads/wlp/beta/wlp-beta-2017.12.0.0.zip"
}
}
libertyDebug.dependsOn 'libertyStop'
libertyStart.dependsOn 'libertyStop'
libertyRun.dependsOn 'libertyStop'
libertyRun.dependsOn 'libertyStop'
task debug { dependsOn 'libertyDebug' }
task start { dependsOn 'libertyStart' }
task stop { dependsOn 'libertyStop' }
task stop { dependsOn 'libertyStop' }
}

View File

@ -25,6 +25,7 @@ public class GameRound implements Runnable {
}
public final String id;
public final String nextRoundId;
public Set<Player> players = new HashSet<Player>();
@ -37,11 +38,12 @@ public class GameRound implements Runnable {
AtomicBoolean paused = new AtomicBoolean(false);
public GameRound() {
id = UUID.randomUUID().toString();
this(UUID.randomUUID().toString());
}
public GameRound(String id) {
this.id = id;
nextRoundId = UUID.randomUUID().toString();
}
public void addPlayer(Player p) {

View File

@ -50,6 +50,12 @@ public class GameRoundService {
return r;
}
public GameRound requeue(GameRound oldRound) {
GameRound nextRound = new GameRound(oldRound.nextRoundId);
GameRound existingRound = allRounds.putIfAbsent(oldRound.nextRoundId, nextRound);
return existingRound == null ? nextRound : existingRound;
}
// @GET
// @Path("/{roundId}/join")
// @Produces(MediaType.APPLICATION_JSON)

View File

@ -9,7 +9,6 @@ import java.util.Map;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.websocket.OnClose;
@ -38,24 +37,22 @@ public class GameRoundWebsocket {
@OnOpen
public void onOpen(@PathParam("roundId") String roundId, Session session) {
System.out.println("@AGG opened a session for game: " + roundId + " using ws=" + this.toString());
System.out.println("Opened a session for game round: " + roundId);
}
@OnClose
public void onClose(@PathParam("roundId") String roundId, Session peer) {
System.out.println("@AGG closed a session for game: " + roundId);
System.out.println("Closed a session for game round: " + roundId);
GameRound game = gameSvc.getRound(roundId);
game.removePlayer(clients.get(peer));
clients.remove(peer);
}
@OnMessage
public void processMsg(@PathParam("roundId") final String roundId, String message, Session session) {
System.out.println("roundId=" + roundId + " msg=" + message);
public void onMessage(@PathParam("roundId") final String roundId, String message, Session session) {
final ClientMessage msg = jsonb.fromJson(message, ClientMessage.class);
final GameRound round = gameSvc.getRound(roundId);
System.out.println("@AGG parsed msg: " + msg);
System.out.println("[onMessage] roundId=" + roundId + " msg=" + msg);
if (msg.event != null) {
if (GameEvent.GAME_START == msg.event)
@ -63,12 +60,13 @@ public class GameRoundWebsocket {
else if (GameEvent.GAME_PAUSE == msg.event)
round.pause();
else if (GameEvent.GAME_REQUEUE == msg.event) {
GameRound nextGame = gameSvc.requeue(round);
String requeueMsg = Json.createObjectBuilder()
.add("requeue", nextGame.id)
.build()
.toString();
Player p = clients.get(session);
// TODO Game.getUnstartedGame();
JsonObject obj = Json.createObjectBuilder().add("requeue", "requeue").build();
p.sendTextToClient(obj.toString());
System.out.println("@AGG TODO: requeue not implemented");
throw new RuntimeException("Not yet implemented");
p.sendTextToClient(requeueMsg);
}
}

View File

@ -1,6 +1,6 @@
//var wsUri = "ws://" + document.location.hostname + ":" + document.location.port + "/websocket";
var roundId = localStorage.getItem("roundId");
var wsUri = "ws://" + document.location.hostname + ":" + document.location.port + "/round/ws/" + roundId;
var baseUri = "ws://" + document.location.hostname + ":" + document.location.port + "/round/ws/";
var wsUri = baseUri + roundId;
var websocket = new WebSocket(wsUri);
websocket.binaryType = "arraybuffer";
var output = document.getElementById("output");
@ -25,6 +25,8 @@ function onMessage(evt) {
if(json.playerlist){
updatePlayerList(json);
}else if(json.requeue) {
roundId = json.requeue;
localStorage.setItem("roundId", roundId)
location.reload();
}else {
drawImageText(evt.data);
@ -41,6 +43,7 @@ function onError(evt) {
function onConnect(evt){
var name = localStorage.getItem("username");
sendText(JSON.stringify({"playerjoined":name}));
console.log("Joined round: " + roundId);
}
function writeToScreen(message) {