Search in sources :

Example 1 with Game

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);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) Game(com.gianlu.pyxreborn.Models.Game) JsonElement(com.google.gson.JsonElement)

Example 2 with Game

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);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) Game(com.gianlu.pyxreborn.Models.Game) JsonObject(com.google.gson.JsonObject)

Example 3 with Game

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;
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) Game(com.gianlu.pyxreborn.Models.Game) Random(java.util.Random) JsonObject(com.google.gson.JsonObject)

Example 4 with 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;
}
Also used : Game(com.gianlu.pyxreborn.Models.Game)

Example 5 with Game

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;
}
Also used : JsonArray(com.google.gson.JsonArray) Game(com.gianlu.pyxreborn.Models.Game)

Aggregations

Game (com.gianlu.pyxreborn.Models.Game)7 GeneralException (com.gianlu.pyxreborn.Exceptions.GeneralException)3 Player (com.gianlu.pyxreborn.Models.Player)2 JsonObject (com.google.gson.JsonObject)2 AdminOnly (com.gianlu.pyxreborn.Annotations.AdminOnly)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 Random (java.util.Random)1 WebSocket (org.java_websocket.WebSocket)1