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