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