use of com.gianlu.pyxreborn.Models.Game in project PretendYoureXyzzyReborn by devgianlu.
the class BaseHandlerWithGame method handleRequest.
@Override
public final JsonObject handleRequest(Server server, @NotNull User user, JsonObject request, JsonObject response) throws GeneralException {
JsonElement gid = request.get(Fields.GID.toString());
if (gid == null)
throw new GeneralException(ErrorCodes.INVALID_REQUEST);
Game game = server.games.findGameById(gid.getAsInt());
if (game == null)
throw new GeneralException(ErrorCodes.GAME_DOESNT_EXIST);
return handleRequest(server, user, game, request, response);
}
use of com.gianlu.pyxreborn.Models.Game in project PretendYoureXyzzyReborn by devgianlu.
the class Games method changeGameOptions.
/**
* Changes the game options. Partial updates are supported.
*/
public void changeGameOptions(@NotNull Game game, @NotNull JsonObject options) throws GeneralException {
if (game.status != Game.Status.LOBBY)
throw new GeneralException(ErrorCodes.GAME_ALREADY_STARTED);
game.options = new Game.Options(options.has(Fields.MAX_PLAYERS.toString()) ? options.get(Fields.MAX_PLAYERS.toString()).getAsInt() : game.options.maxPlayers, options.has(Fields.MAX_SPECTATORS.toString()) ? options.get(Fields.MAX_SPECTATORS.toString()).getAsInt() : game.options.maxSpectators, options.has(Fields.CARD_SET_ID.toString()) ? Utils.toIntegersList(options.get(Fields.CARD_SET_ID.toString()).getAsJsonArray()) : game.options.cardSetIds);
JsonObject obj = Utils.event(Events.GAME_OPTIONS_CHANGED);
obj.add(Fields.OPTIONS.toString(), game.options.toJson());
server.broadcastMessageToPlayers(game, obj);
}
use of com.gianlu.pyxreborn.Models.Game in project PretendYoureXyzzyReborn by devgianlu.
the class Games method createAndAdd.
/**
* Creates a new game.
*/
public Game createAndAdd(@NotNull User host) throws GeneralException {
if (size() >= maxGames)
throw new GeneralException(ErrorCodes.TOO_MANY_GAMES);
if (playingIn(host) != null || spectatingIn(host) != null)
throw new GeneralException(ErrorCodes.ALREADY_IN_GAME);
Game game = new Game(new Random().nextInt(), host);
game.players.add(new Player(host));
add(game);
JsonObject obj = Utils.event(Events.NEW_GAME);
obj.addProperty(Fields.GID.toString(), game.gid);
server.broadcastMessage(obj);
return game;
}
use of com.gianlu.pyxreborn.Models.Game in project PretendYoureXyzzyReborn by devgianlu.
the class CreateGameHandler method handleRequest.
@Override
public JsonObject handleRequest(Server server, @NotNull User user, JsonObject request, JsonObject response) throws GeneralException {
Game game = server.games.createAndAdd(user);
response.add(Fields.GAME.toString(), game.toJson());
return response;
}
use of com.gianlu.pyxreborn.Models.Game in project PretendYoureXyzzyReborn by devgianlu.
the class ListGamesHandler method handleRequest.
@Override
public JsonObject handleRequest(Server server, JsonObject request, JsonObject response) throws GeneralException {
JsonArray list = new JsonArray();
for (Game game : server.games) list.add(game.toJson());
response.add(Fields.GAMES_LIST.toString(), list);
response.addProperty(Fields.MAX_GAMES.toString(), server.games.getMax());
return response;
}
Aggregations