Search in sources :

Example 21 with GeneralException

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

the class Games method joinGame.

/**
 * Adds a player to the game.
 */
public void joinGame(@NotNull Game game, @NotNull User user) throws GeneralException {
    if (playingIn(user) != null || spectatingIn(user) != null)
        throw new GeneralException(ErrorCodes.ALREADY_IN_GAME);
    if (game.players.size() >= game.options.maxPlayers)
        throw new GeneralException(ErrorCodes.GAME_FULL);
    Player player = new Player(user);
    JsonObject obj = Utils.event(Events.GAME_NEW_PLAYER);
    obj.add(Fields.PLAYER.toString(), player.toJson());
    // Don't broadcast this to the player itself
    server.broadcastMessageToPlayers(game, obj);
    game.players.add(player);
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonObject(com.google.gson.JsonObject)

Example 22 with GeneralException

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

the class Games method spectateGame.

/**
 * Adds a spectator to the game.
 */
public void spectateGame(@NotNull Game game, @NotNull User user) throws GeneralException {
    if (playingIn(user) != null || spectatingIn(user) != null)
        throw new GeneralException(ErrorCodes.ALREADY_IN_GAME);
    if (game.spectators.size() >= game.options.maxSpectators)
        throw new GeneralException(ErrorCodes.GAME_FULL);
    JsonObject obj = Utils.event(Events.GAME_NEW_SPECTATOR);
    obj.add(Fields.SPECTATOR.toString(), user.toJson());
    // Don't broadcast this to the user itself
    server.broadcastMessageToPlayers(game, obj);
    game.spectators.add(user);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonObject(com.google.gson.JsonObject)

Example 23 with GeneralException

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

the class Games method killGame.

/**
 * Kills the game, kicking everyone and deleting the game
 */
@AdminOnly
public void killGame(@NotNull Game game, KickReason majorReason) {
    GameManager manager = managedGames.findGameManagerByGameId(game.gid);
    if (manager != null) {
        try {
            manager.stop();
        } catch (GeneralException ignored) {
        }
    }
    if (!game.players.isEmpty()) {
        for (Player player : game.players) {
            kickPlayer(game, player, majorReason);
        }
    }
    if (!game.spectators.isEmpty()) {
        for (User spectator : game.spectators) {
            kickSpectator(game, spectator, majorReason);
        }
    }
    if (manager != null)
        managedGames.remove(manager);
    remove(game);
    server.broadcastMessage(Utils.event(Events.GAME_REMOVED));
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) User(com.gianlu.pyxreborn.Models.User) GameManager(com.gianlu.pyxreborn.server.GameManager) AdminOnly(com.gianlu.pyxreborn.Annotations.AdminOnly)

Example 24 with GeneralException

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

the class Server method onMessage.

/**
 * Picks the right {@link BaseHandler} to perform the requested {@link Operations}.
 */
@Override
@Nullable
protected JsonObject onMessage(WebSocket conn, User user, JsonObject request, JsonObject response) throws GeneralException {
    Operations op = Operations.parse(request.get(Fields.OPERATION.toString()).getAsString());
    if (op == null)
        throw new GeneralException(ErrorCodes.UNKNOWN_OPERATION);
    BaseHandler handler;
    try {
        handler = Handlers.LIST.get(op).newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new GeneralException(ErrorCodes.SERVER_ERROR, ex);
    }
    return handler.handleRequest(this, request, response);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) BaseHandler(com.gianlu.pyxreborn.server.Handlers.BaseHandler) Operations(com.gianlu.pyxreborn.Operations) Nullable(org.jetbrains.annotations.Nullable)

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