Search in sources :

Example 16 with GeneralException

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

the class BaseHandlerWithGameManager 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);
    GameManager manager = server.games.getGameManagerFor(gid.getAsInt());
    if (manager == null)
        throw new GeneralException(ErrorCodes.GAME_NOT_STARTED);
    Player player = manager.game.findPlayerByNickname(user.nickname);
    if (player == null)
        throw new GeneralException(ErrorCodes.NOT_IN_THIS_GAME);
    return handleRequest(server, player, manager, request, response);
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonElement(com.google.gson.JsonElement) GameManager(com.gianlu.pyxreborn.server.GameManager)

Example 17 with GeneralException

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

the class ChangeGameOptionsHandler method handleRequest.

@Override
public JsonObject handleRequest(Server server, @NotNull User user, @NotNull Game game, JsonObject request, JsonObject response) throws GeneralException {
    if (user != game.host)
        throw new GeneralException(ErrorCodes.NOT_GAME_HOST);
    JsonElement options = request.get(Fields.OPTIONS.toString());
    if (options == null)
        throw new GeneralException(ErrorCodes.INVALID_REQUEST);
    server.games.changeGameOptions(game, options.getAsJsonObject());
    return successful(response);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonElement(com.google.gson.JsonElement)

Example 18 with GeneralException

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

the class PlayCardHandler method handleRequest.

@Override
public JsonObject handleRequest(Server server, @NotNull Player player, @NotNull GameManager manager, JsonObject request, JsonObject response) throws GeneralException {
    JsonElement cardId = request.get(Fields.CARD_ID.toString());
    if (cardId == null)
        throw new GeneralException(ErrorCodes.INVALID_REQUEST);
    manager.playCard(player, cardId.getAsInt());
    return successful(response);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonElement(com.google.gson.JsonElement)

Example 19 with GeneralException

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

the class JudgeHandler method handleRequest.

@Override
public JsonObject handleRequest(Server server, @NotNull Player player, @NotNull GameManager manager, JsonObject request, JsonObject response) throws GeneralException {
    JsonElement cardId = request.get(Fields.CARD_ID.toString());
    if (cardId == null)
        throw new GeneralException(ErrorCodes.INVALID_REQUEST);
    manager.judge(player, cardId.getAsInt());
    return successful(response);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonElement(com.google.gson.JsonElement)

Example 20 with GeneralException

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

the class ConnectedUsers method checkAndAdd.

/**
 * Adds an user to the server.
 *
 * @param admin if true the user must connect at every cost, even by kicking other players
 */
public User checkAndAdd(String nickname, @Nullable String sid, InetSocketAddress address, boolean admin) throws GeneralException {
    if (size() >= maxUsers) {
        if (admin)
            kickUser(get(new Random().nextInt(size())), KickReason.ADMIN_LOGGED);
        else
            throw new GeneralException(ErrorCodes.TOO_MANY_USERS);
    }
    User alreadyConnectedUser = findByNickname(nickname);
    if (admin && alreadyConnectedUser != null)
        kickUser(alreadyConnectedUser, KickReason.ADMIN_LOGGED);
    if (alreadyConnectedUser != null) {
        if (alreadyConnectedUser.isDisconnected()) {
            if (Objects.equals(alreadyConnectedUser.sessionId, sid)) {
                alreadyConnectedUser.disconnectedAt = -1;
                JsonObject obj = new JsonObject();
                obj.addProperty(Fields.SESSION_ID.toString(), alreadyConnectedUser.sessionId);
                server.sendMessage(alreadyConnectedUser, obj);
                return alreadyConnectedUser;
            } else {
                throw new GeneralException(ErrorCodes.INVALID_SID);
            }
        } else {
            throw new GeneralException(ErrorCodes.NICK_ALREADY_IN_USE);
        }
    }
    if (nickname.isEmpty() || nickname.length() < 5 || (!admin && nickname.equals("xyzzy")))
        throw new GeneralException(ErrorCodes.INVALID_NICKNAME);
    User user = new User(nickname, null, address, admin);
    JsonObject obj = Utils.event(Events.NEW_USER);
    obj.addProperty(Fields.NICKNAME.toString(), nickname);
    // This way we don't send the broadcast to the user itself
    server.broadcastMessage(obj);
    add(user);
    JsonObject obj1 = new JsonObject();
    obj1.addProperty(Fields.SESSION_ID.toString(), user.sessionId);
    server.sendMessage(user, obj1);
    return user;
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) User(com.gianlu.pyxreborn.Models.User) 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