Search in sources :

Example 6 with GeneralException

use of com.gianlu.pyxreborn.Exceptions.GeneralException in project PretendYoureXyzzyReborn by devgianlu.

the class KickUserHandler method handleRequestForAdmin.

@Override
public JsonObject handleRequestForAdmin(Server server, @NotNull User user, JsonObject request, JsonObject response) throws GeneralException {
    JsonElement reason = request.get(Fields.KICK_REASON.toString());
    JsonElement nickname = request.get(Fields.NICKNAME.toString());
    if (nickname == null)
        throw new GeneralException(ErrorCodes.INVALID_REQUEST);
    User kickUser = server.users.findByNickname(nickname.getAsString());
    if (kickUser == null)
        throw new GeneralException(ErrorCodes.INVALID_NICKNAME);
    server.users.kickUser(kickUser, reason == null ? null : KickReason.parse(reason.getAsString()));
    return successful(response);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) User(com.gianlu.pyxreborn.Models.User) JsonElement(com.google.gson.JsonElement)

Example 7 with GeneralException

use of com.gianlu.pyxreborn.Exceptions.GeneralException in project PretendYoureXyzzyReborn by devgianlu.

the class ListCardsHandler method handleRequest.

@Override
public JsonObject handleRequest(Server server, JsonObject request, JsonObject response) throws GeneralException {
    JsonElement cardSetId = request.get(Fields.CARD_SET_ID.toString());
    if (cardSetId == null)
        throw new GeneralException(ErrorCodes.INVALID_REQUEST);
    CardSet set = server.cardSets.findCardSetById(cardSetId.getAsInt());
    if (set == null)
        throw new GeneralException(ErrorCodes.INVALID_CARD_SET_ID);
    response.add(Fields.CARD_SET.toString(), set.toJson());
    return response;
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonElement(com.google.gson.JsonElement) CardSet(com.gianlu.pyxreborn.Models.CardSet)

Example 8 with GeneralException

use of com.gianlu.pyxreborn.Exceptions.GeneralException in project PretendYoureXyzzyReborn by devgianlu.

the class Games method startGame.

/**
 * Starts a game. Creating the {@link GameManager} if needed.
 */
public void startGame(Game game) throws GeneralException {
    if (game.status != Game.Status.LOBBY)
        throw new GeneralException(ErrorCodes.GAME_ALREADY_STARTED);
    GameManager manager = managedGames.findGameManagerByGameId(game.gid);
    if (manager == null) {
        manager = new GameManager(server, game);
        manager.start();
        managedGames.add(manager);
    } else {
        manager.start();
    }
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) GameManager(com.gianlu.pyxreborn.server.GameManager)

Example 9 with GeneralException

use of com.gianlu.pyxreborn.Exceptions.GeneralException 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 10 with GeneralException

use of com.gianlu.pyxreborn.Exceptions.GeneralException 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)

Aggregations

GeneralException (com.gianlu.pyxreborn.Exceptions.GeneralException)24 JsonElement (com.google.gson.JsonElement)10 JsonObject (com.google.gson.JsonObject)7 User (com.gianlu.pyxreborn.Models.User)5 Player (com.gianlu.pyxreborn.Models.Player)4 Game (com.gianlu.pyxreborn.Models.Game)3 GameManager (com.gianlu.pyxreborn.server.GameManager)3 AdminOnly (com.gianlu.pyxreborn.Annotations.AdminOnly)1 CardSet (com.gianlu.pyxreborn.Models.CardSet)1 Operations (com.gianlu.pyxreborn.Operations)1 BaseHandler (com.gianlu.pyxreborn.server.Handlers.BaseHandler)1 Random (java.util.Random)1 InvalidDataException (org.java_websocket.exceptions.InvalidDataException)1 ServerHandshakeBuilder (org.java_websocket.handshake.ServerHandshakeBuilder)1 Nullable (org.jetbrains.annotations.Nullable)1