Parse client JSON messages using JSON-B instead of JSON-P

This commit is contained in:
Andrew Guibert 2018-01-28 21:16:47 -06:00
parent f90f0ff4ea
commit cd38b43b66
3 changed files with 29 additions and 32 deletions

View File

@ -3,6 +3,8 @@
*/
package org.libertybikes.game.core;
import javax.json.bind.annotation.JsonbProperty;
/**
* @author Andrew
*
@ -17,8 +19,15 @@ public class ClientMessage {
public Player.DIRECTION direction;
public String playerjoined;
@JsonbProperty("playerjoined")
public String playerJoinedId;
@JsonbProperty("message")
public GameEvent event;
@Override
public String toString() {
return "{ direction=" + direction + ", playerjoined=" + playerJoinedId + ", event=" + event + " }";
}
}

View File

@ -64,8 +64,8 @@ public class Player {
return sb.toString();
}
public void setDirection(String dir) {
direction = DIRECTION.valueOf(dir);
public void setDirection(DIRECTION dir) {
direction = dir;
}
public DIRECTION getDrirection() {

View File

@ -3,7 +3,6 @@
*/
package org.libertybikes.game.round.service;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
@ -21,6 +20,7 @@ import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.libertybikes.game.core.ClientMessage;
import org.libertybikes.game.core.ClientMessage.GameEvent;
import org.libertybikes.game.core.GameRound;
import org.libertybikes.game.core.Player;
import org.libertybikes.game.core.PlayerFactory;
@ -52,20 +52,17 @@ public class GameRoundWebsocket {
@OnMessage
public void processMsg(@PathParam("roundId") final String roundId, String message, Session session) {
System.out.println("roundId=" + roundId + " msg=" + message);
JsonObject json = Json.createReader(new StringReader(message)).readObject();
ClientMessage msg = jsonb.fromJson(message, ClientMessage.class);
System.out.println("@AGG got jsonb msg: " + msg);
final GameRound round = gameSvc.getRound(roundId);
if (json.containsKey("message")) {
if (json.getString("message").equals("GAME_START")) {
final ClientMessage msg = jsonb.fromJson(message, ClientMessage.class);
final GameRound round = gameSvc.getRound(roundId);
System.out.println("@AGG parsed msg: " + msg);
if (msg.event != null) {
if (GameEvent.GAME_START == msg.event)
round.startGame();
}
if (json.getString("message").equals("GAME_PAUSE")) {
else if (GameEvent.GAME_PAUSE == msg.event)
round.pause();
System.out.println("Stopped the game");
}
if (json.getString("message").equals("GAME_REQUEUE")) {
else if (GameEvent.GAME_REQUEUE == msg.event) {
Player p = clients.get(session);
// TODO Game.getUnstartedGame();
JsonObject obj = Json.createObjectBuilder().add("requeue", "requeue").build();
@ -73,27 +70,18 @@ public class GameRoundWebsocket {
System.out.println("@AGG TODO: requeue not implemented");
throw new RuntimeException("Not yet implemented");
}
} else if (json.containsKey("direction")) {
System.out.println("@AGG got direction: " + json.getString("direction"));
// TODO movement not working
}
if (msg.direction != null) {
Player p = clients.get(session);
Player.DIRECTION curDir = p.getDrirection();
String noGo;
if (curDir == Player.DIRECTION.UP) {
noGo = "down";
} else if (curDir == Player.DIRECTION.RIGHT) {
noGo = "left";
} else if (curDir == Player.DIRECTION.DOWN) {
noGo = "up";
} else {
noGo = "right";
if (curDir != msg.direction) {
p.setDirection(msg.direction);
}
}
if (!noGo.equalsIgnoreCase(json.getString("direction"))) {
p.setDirection(json.getString("direction"));
}
} else if (json.containsKey("playerjoined")) {
Player p = PlayerFactory.initNextPlayer(round, session, json.getString("playerjoined"));
if (msg.playerJoinedId != null) {
Player p = PlayerFactory.initNextPlayer(round, session, msg.playerJoinedId);
clients.put(session, p);
round.addPlayer(p);
}