private void createGame(HttpExchange exchange) throws IOException { InputStream headers = exchange.getRequestBody(); String gameCreateInfo = HandlerUtils.inputStreamToString(headers); Map<String, String> gameCreateInfoMap = HandlerUtils.decodeQueryString(gameCreateInfo); String name = gameCreateInfoMap.get("name"); Boolean randomNumbers = Boolean.parseBoolean(gameCreateInfoMap.get("randomNumbers")); Boolean randomTiles = Boolean.parseBoolean(gameCreateInfoMap.get("randomTiles")); Boolean randomPorts = Boolean.parseBoolean(gameCreateInfoMap.get("randomPorts")); GameModel model = new GameModel(); model.initGame(name, randomNumbers, randomTiles, randomPorts); Games catanGames = Games.get(); catanGames.addGame(model); Gson gson = new Gson(); ArrayList<GameListPlayer> glp = new ArrayList<GameListPlayer>(); for (int i = 0; i < 4; i++) { glp.add(new GameListPlayer()); } GameListGames g = new GameListGames(name, catanGames.getGames().size(), glp); String jsonString = gson.toJson(g); Server.println(" /games/create"); HandlerUtils.sendStringAsJSON(exchange, HttpURLConnection.HTTP_OK, jsonString); }
private void listGames(HttpExchange exchange) throws IOException { Server.println("\n" + this.getClass().getSimpleName() + ":"); Games games = Games.get(); Map<Integer, GameModel> gameList = games.getGames(); String jsonString = "["; int gameNum = 1; for (Map.Entry<Integer, GameModel> e : gameList.entrySet()) { GameModel game = e.getValue(); jsonString += game.getGamesListJSON(e.getKey()); if (gameNum < gameList.size()) { jsonString += ","; } gameNum++; } jsonString += "]"; Server.println(" /games/list"); HandlerUtils.sendStringAsJSON(exchange, HttpURLConnection.HTTP_OK, jsonString); }