Search in sources :

Example 11 with GeneralException

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

the class PyxServerAdapter method onWebsocketHandshakeReceivedAsServer.

/**
 * This method takes care of validating the client handshake.
 * The handshake must contains a nickname. Optionally, an admin code can be sent to login as admin.
 * <p>
 * To resume a previous session a session ID can be sent. See {@link ConnectedUsers#removeUser(User, boolean)}.
 */
@Override
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException {
    ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer(conn, draft, request);
    if (request.hasFieldValue(Fields.NICKNAME.toString())) {
        boolean admin = false;
        if (request.hasFieldValue(Fields.ADMIN_CODE.toString())) {
            String code = request.getFieldValue(Fields.ADMIN_CODE.toString());
            if (Objects.equals(code, currentAdminCode)) {
                admin = true;
            } else {
                throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, ErrorCodes.INVALID_ADMIN_CODE.toString());
            }
        }
        try {
            User user = users.checkAndAdd(request.getFieldValue(Fields.NICKNAME.toString()), request.getFieldValue(Fields.SESSION_ID.toString()), conn.getRemoteSocketAddress(), admin);
            builder.put(Fields.SESSION_ID.toString(), user.sessionId);
        } catch (GeneralException ex) {
            throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, ex.code.toString());
        }
    } else {
        throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, ErrorCodes.INVALID_REQUEST.toString());
    }
    return builder;
}
Also used : User(com.gianlu.pyxreborn.Models.User) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) InvalidDataException(org.java_websocket.exceptions.InvalidDataException) ServerHandshakeBuilder(org.java_websocket.handshake.ServerHandshakeBuilder)

Example 12 with GeneralException

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

the class GameManager method stop.

/**
 * Stops the game.
 */
public void stop() throws GeneralException {
    if (game.status == Game.Status.LOBBY)
        throw new GeneralException(ErrorCodes.GAME_NOT_STARTED);
    round = null;
    whiteCards.clear();
    blackCards.clear();
    cardSets.clear();
    game.status = Game.Status.LOBBY;
    server.broadcastMessageToPlayers(game, Utils.event(Events.GAME_STOPPED));
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException)

Example 13 with GeneralException

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

the class GameManager method playCard.

/**
 * Plays a card.
 */
public void playCard(@NotNull Player player, int whiteCardId) throws GeneralException {
    if (game.status == Game.Status.LOBBY)
        throw new GeneralException(ErrorCodes.GAME_NOT_STARTED);
    if (round == null)
        throw new GeneralException(ErrorCodes.GAME_NOT_YOUR_TURN);
    WhiteCard card = player.hand.findCardById(whiteCardId);
    if (card == null)
        throw new GeneralException(ErrorCodes.GAME_CARD_NOT_IN_YOUR_HAND);
    round.playCard(player, card);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException)

Example 14 with GeneralException

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

the class GameManager method judge.

/**
 * Decides the winning card.
 */
public void judge(@NotNull Player player, int whiteCardId) throws GeneralException {
    if (game.status == Game.Status.LOBBY)
        throw new GeneralException(ErrorCodes.GAME_NOT_STARTED);
    if (round == null)
        throw new GeneralException(ErrorCodes.GAME_NOT_YOUR_TURN);
    WhiteCard card = round.playedCards.findCardById(whiteCardId);
    if (card == null)
        throw new GeneralException(ErrorCodes.GAME_CARD_NOT_PLAYED);
    round.judge(player, card);
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException)

Example 15 with GeneralException

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

the class GameManager method loadCards.

/**
 * Load the needed cards.
 *
 * @throws GeneralException if the game can't be started with the current options.
 */
private void loadCards() throws GeneralException {
    cardSets.clear();
    whiteCards.clear();
    blackCards.clear();
    List<Integer> cardSetIds = game.options.cardSetIds;
    if (cardSetIds.isEmpty())
        throw new GeneralException(ErrorCodes.GAME_NOT_ENOUGH_CARDS);
    for (int id : cardSetIds) {
        CardSet set = server.cardSets.findCardSetById(id);
        if (set != null)
            cardSets.add(set);
    }
    int whiteCards = 0;
    int blackCards = 0;
    for (CardSet set : cardSets) {
        whiteCards += set.whiteCards.size();
        blackCards += set.blackCards.size();
    }
    if (whiteCards <= game.players.size() * CARDS_PER_HAND)
        throw new GeneralException(ErrorCodes.GAME_NOT_ENOUGH_CARDS);
    if (blackCards <= game.players.size())
        throw new GeneralException(ErrorCodes.GAME_NOT_ENOUGH_CARDS);
    for (CardSet set : cardSets) {
        this.whiteCards.addAll(set.whiteCards);
        this.blackCards.addAll(set.blackCards);
    }
}
Also used : GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException)

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