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;
}
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));
}
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);
}
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);
}
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);
}
}
Aggregations