Search in sources :

Example 1 with Player

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;
        }
    }
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) User(com.gianlu.pyxreborn.Models.User) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject)

Example 2 with Player

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;
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) Game(com.gianlu.pyxreborn.Models.Game) Random(java.util.Random) JsonObject(com.google.gson.JsonObject)

Example 3 with Player

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);
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonElement(com.google.gson.JsonElement) GameManager(com.gianlu.pyxreborn.server.GameManager)

Example 4 with Player

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());
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) Game(com.gianlu.pyxreborn.Models.Game) WebSocket(org.java_websocket.WebSocket) AdminOnly(com.gianlu.pyxreborn.Annotations.AdminOnly)

Example 5 with Player

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);
}
Also used : Player(com.gianlu.pyxreborn.Models.Player) GeneralException(com.gianlu.pyxreborn.Exceptions.GeneralException) JsonObject(com.google.gson.JsonObject)

Aggregations

Player (com.gianlu.pyxreborn.Models.Player)7 GeneralException (com.gianlu.pyxreborn.Exceptions.GeneralException)4 JsonObject (com.google.gson.JsonObject)4 AdminOnly (com.gianlu.pyxreborn.Annotations.AdminOnly)2 Game (com.gianlu.pyxreborn.Models.Game)2 User (com.gianlu.pyxreborn.Models.User)2 GameManager (com.gianlu.pyxreborn.server.GameManager)2 JsonElement (com.google.gson.JsonElement)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 WebSocket (org.java_websocket.WebSocket)1