private void SetChatManager(JsonObject chat, JsonObject log) { List<Line> chatMessages = new ArrayList<Line>(); List<Line> gameHistory = new ArrayList<Line>(); // Get chat info { JsonArray lines = chat.getAsJsonArray("lines"); for (int i = 0; i < lines.size(); i++) { JsonObject line = lines.get(i).getAsJsonObject(); String message = line.getAsJsonPrimitive("message").getAsString(); String source = line.getAsJsonPrimitive("source").getAsString(); Line l = new Line(message, source); chatMessages.add(l); } } // Get log info { JsonArray lines = log.getAsJsonArray("lines"); for (int i = 0; i < lines.size(); i++) { JsonObject line = lines.get(i).getAsJsonObject(); String message = line.getAsJsonPrimitive("message").getAsString(); String source = line.getAsJsonPrimitive("source").getAsString(); Line l = new Line(message, source); gameHistory.add(l); } } this.chatManager = new ChatManager(chatMessages, gameHistory); }
/** * Parse through the JSON string received from the server and translate into GameModelJSON object * * @param json String received from the server API * @return The now parsed GameModelJSON object. * @throws GeneralPlayerException * @throws InvalidTurnStatusException * @throws TurnIndexException */ private CatanModel _deserialize(String json) throws TurnIndexException, InvalidTurnStatusException, GeneralPlayerException { JsonElement jelement = new JsonParser().parse(json); JsonObject jobject = jelement.getAsJsonObject(); JsonObject deck = jobject.getAsJsonObject("deck"); JsonObject bank = jobject.getAsJsonObject("bank"); JsonObject chat = jobject.getAsJsonObject("chat"); JsonObject log = jobject.getAsJsonObject("log"); JsonObject map = jobject.getAsJsonObject("map"); JsonArray players = jobject.getAsJsonArray("players"); JsonObject tradeOffer = jobject.getAsJsonObject("tradeOffer"); JsonObject turnTracker = jobject.getAsJsonObject("turnTracker"); int version = jobject.getAsJsonPrimitive("version").getAsInt(); int winner = jobject.getAsJsonPrimitive("winner").getAsInt(); SetResourceManager(bank, players, tradeOffer); SetDevCardManager(players, deck); SetPlayerManager(players, turnTracker); SetMapManager(map); SetChatManager(chat, log); SetTurnManager(turnTracker); return catanModel = new CatanModel( resourceManager, devCardManager, playerManager, mapManager, chatManager, version); }
private void SetMapManager(JsonObject map) { JsonArray hexes = map.getAsJsonArray("hexes"); JsonArray ports = map.getAsJsonArray("ports"); JsonArray roads = map.getAsJsonArray("roads"); JsonArray settlements = map.getAsJsonArray("settlements"); JsonArray cities = map.getAsJsonArray("cities"); JsonObject robber = map.getAsJsonObject("robber"); int radius = map.getAsJsonPrimitive("radius").getAsInt(); // Return data structures HashMap<HexLocation, Hex> hexes_r = new HashMap<>(); HashMap<VertexLocation, Settlement> settlements_r = new HashMap<VertexLocation, Settlement>(); HashMap<EdgeLocation, Port> ports_r = new HashMap<EdgeLocation, Port>(); HashMap<EdgeLocation, Road> roads_r = new HashMap<EdgeLocation, Road>(); // Get Hex info for (int i = 0; i < hexes.size(); i++) { JsonObject hex = hexes.get(i).getAsJsonObject(); JsonObject location = hex.getAsJsonObject("location"); String resource = "DESERT"; int number = 0; int x = location.getAsJsonPrimitive("x").getAsInt(); int y = location.getAsJsonPrimitive("y").getAsInt(); // if hex doesn't have resource and number it is desert hex if (hex.has("resource") && hex.has("number")) { resource = hex.getAsJsonPrimitive("resource").getAsString(); number = hex.getAsJsonPrimitive("number").getAsInt(); } // compile into Map structure Hex entry = new Hex(x, y, resource.toUpperCase(), number); hexes_r.put(entry.location, entry); } // Get Port info for (int i = 0; i < ports.size(); i++) { JsonObject port = ports.get(i).getAsJsonObject(); JsonObject location = port.getAsJsonObject("location"); String resource = "THREE"; int x = location.getAsJsonPrimitive("x").getAsInt(); int y = location.getAsJsonPrimitive("y").getAsInt(); // if port doesn't have resource than it is 3:1 and those are for any resource if (port.has("resource")) resource = port.getAsJsonPrimitive("resource").getAsString(); String direction = port.getAsJsonPrimitive("direction").getAsString(); int ratio = port.getAsJsonPrimitive("ratio").getAsInt(); // compile into Map structure HexLocation hexLocation = new HexLocation(x, y); EdgeLocation edgeLocation = new EdgeLocation(hexLocation, EdgeDirection.valueOf(Direction.shortToLong(direction))); // edgeLocation = edgeLocation.getNormalizedLocation(); Port entry = new Port(edgeLocation, PortType.valueOf(resource.toUpperCase()), ratio); ports_r.put(entry.location, entry); } // Get road info for (int i = 0; i < roads.size(); i++) { JsonObject road = roads.get(i).getAsJsonObject(); JsonObject location = road.getAsJsonObject("location"); int x = location.getAsJsonPrimitive("x").getAsInt(); int y = location.getAsJsonPrimitive("y").getAsInt(); int owner = road.getAsJsonPrimitive("owner").getAsInt(); String direction = location.getAsJsonPrimitive("direction").getAsString(); // compile into Map structure HexLocation hexLocation = new HexLocation(x, y); EdgeLocation edgeLocation = new EdgeLocation(hexLocation, EdgeDirection.valueOf(Direction.shortToLong(direction))); edgeLocation = edgeLocation.getNormalizedLocation(); Road entry = new Road(edgeLocation, owner); roads_r.put(entry.location, entry); } // Get settlement info for (int i = 0; i < settlements.size(); i++) { JsonObject settlement = settlements.get(i).getAsJsonObject(); JsonObject location = settlement.getAsJsonObject("location"); int x = location.getAsJsonPrimitive("x").getAsInt(); int y = location.getAsJsonPrimitive("y").getAsInt(); int owner = settlement.getAsJsonPrimitive("owner").getAsInt(); String direction = location.getAsJsonPrimitive("direction").getAsString(); // compile into Map structure HexLocation hexLocation = new HexLocation(x, y); VertexLocation vertexLocation = new VertexLocation( hexLocation, VertexDirection.valueOf(Direction.shortToLong(direction))); vertexLocation = vertexLocation.getNormalizedLocation(); Settlement entry = new Settlement(vertexLocation, owner); settlements_r.put(entry.location, entry); } // Get city info for (int i = 0; i < cities.size(); i++) { JsonObject city = cities.get(i).getAsJsonObject(); JsonObject location = city.getAsJsonObject("location"); int x = location.getAsJsonPrimitive("x").getAsInt(); int y = location.getAsJsonPrimitive("y").getAsInt(); int owner = city.getAsJsonPrimitive("owner").getAsInt(); String direction = location.getAsJsonPrimitive("direction").getAsString(); // compile into Map structure HexLocation hexLocation = new HexLocation(x, y); VertexLocation vertexLocation = new VertexLocation( hexLocation, VertexDirection.valueOf(Direction.shortToLong(direction))); vertexLocation = vertexLocation.getNormalizedLocation(); Settlement entry = new Settlement(vertexLocation, owner); entry.makeCity(); settlements_r.put(entry.location, entry); } int x = robber.getAsJsonPrimitive("x").getAsInt(); int y = robber.getAsJsonPrimitive("y").getAsInt(); this.mapManager = new MapManager(hexes_r, settlements_r, ports_r, roads_r, new HexLocation(x, y), radius); }