use of com.gianlu.pyxreborn.Models.Player in project PretendYoureXyzzyReborn by devgianlu.
the class Games method leaveGame.
/**
* Removes a player or a spectator from the game.
*/
public void leaveGame(@NotNull Game game, @NotNull User user) {
for (Player player : new ArrayList<>(game.players)) {
if (Objects.equals(player.user, user)) {
game.players.remove(player);
JsonObject obj = Utils.event(Events.GAME_PLAYER_LEFT);
obj.addProperty(Fields.NICKNAME.toString(), user.nickname);
// Don't broadcast this to the player itself
server.broadcastMessageToPlayers(game, obj);
if (user == game.host && !game.players.isEmpty())
game.host = game.players.get(0).user;
if (game.players.isEmpty())
killGame(game, KickReason.GAME_EMPTY);
return;
}
}
for (User spectator : new ArrayList<>(game.spectators)) {
if (Objects.equals(spectator, user)) {
game.spectators.remove(spectator);
JsonObject obj = Utils.event(Events.GAME_SPECTATOR_LEFT);
obj.addProperty(Fields.NICKNAME.toString(), user.nickname);
// Don't broadcast this to the user itself
server.broadcastMessageToPlayers(game, obj);
return;
}
}
}
use of com.gianlu.pyxreborn.Models.Player 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;
}
use of com.gianlu.pyxreborn.Models.Player 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.Models.Player in project PretendYoureXyzzyReborn by devgianlu.
the class ConnectedUsers method kickUser.
/**
* Kicks an user from the server. Not allowing reconnection.
*/
@AdminOnly
public void kickUser(@NotNull User user, @Nullable KickReason reason) {
Game playingIn = server.games.playingIn(user);
if (playingIn != null) {
Player player = playingIn.findPlayerByNickname(user.nickname);
if (player != null)
server.games.kickPlayer(playingIn, player, reason);
}
WebSocket socket = server.findWebSocketByAddress(user.address);
if (socket == null)
return;
socket.close(CloseFrame.POLICY_VALIDATION, (reason == null ? KickReason.GENERAL_KICK : reason).toString());
}
use of com.gianlu.pyxreborn.Models.Player 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);
}
Aggregations