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