backend/src/main/java/com/coolioasjulio/influence/web/Lobby.java
/**
     * Try to add a player to this lobby.
     * Players cannot be added to a lobby once the game has started.
     * Additionally, players must have unique names.
     *
     * @param player The player to add.
     * @return True if the player was successfully added, false otherwise. If this operation failed, it's either because the lobby already started, or the name was already taken.
     */
Try to add a player to this lobby.
Players cannot be added to a lobby once the game has started.
Additionally, players must have unique names.

@param player The player to add.
@return True if the player was successfully added, false otherwise. If this operation failed, it's either because the lobby already started, or the name was already taken.
Try to add a player to this lobby . Players cannot be added to a lobby once the game has started . Additionally players must have unique names . @param player The player to add . @return True if the player was successfully added false otherwise . If this operation failed it ' s either because the lobby already started or the name was already taken .
public RejectionReason addPlayer(PlayerEndpoint player) {
        synchronized (playersLock) {
            if (!started.get()) {
                if (containsPlayer(player.getName())) {
                    return RejectionReason.NAME_TAKEN;
                } else if (players.size() == Game.MAX_PLAYERS) {
                    return RejectionReason.LOBBY_FULL;
                }
                System.out.printf("Player %s connected to lobby %s!\n", player.getName(), code);
                players.add(player);
                String list = new Gson().toJson(players.stream().map(PlayerEndpoint::getName).toArray(String[]::new));
                players.forEach(p -> p.write(list));
                return RejectionReason.NONE;
            } else {
                System.out.printf("%s is trying to reconnect to %s...", player.getName(), code);
                for (int i = 0; i < players.size(); i++) {
                    PlayerEndpoint playerEndpoint = players.get(i);
                    if (game != null && !playerEndpoint.isConnected() && playerEndpoint.getName().equals(player.getName())) {
                        players.set(i, player); 
                        game.playerReconnected(player); 
                        System.out.println("Success!");
                        return RejectionReason.NONE;
                    }
                }
                System.out.println("Failed!");
                return RejectionReason.LOBBY_STARTED;
            }
        }
    }
public RejectionReason addPlayer ( PlayerEndpoint player ) { synchronized ( playersLock ) { if ( ! started . get ( ) ) { if ( containsPlayer ( player . getName ( ) ) ) { return RejectionReason . NAME_TAKEN ; } else if ( players . size ( ) == Game . MAX_PLAYERS ) { return RejectionReason . LOBBY_FULL ; } System . out . printf ( "Player %s connected to lobby %s!\n" , player . getName ( ) , code ) ; players . add ( player ) ; String list = new Gson ( ) . toJson ( players . stream ( ) . map ( PlayerEndpoint :: getName ) . toArray ( String [ ] :: new ) ) ; players . forEach ( p -> p . write ( list ) ) ; return RejectionReason . NONE ; } else { System . out . printf ( "%s is trying to reconnect to %s..." , player . getName ( ) , code ) ; for ( int i = 0 ; i < players . size ( ) ; i ++ ) { PlayerEndpoint playerEndpoint = players . get ( i ) ; if ( game != null && ! playerEndpoint . isConnected ( ) && playerEndpoint . getName ( ) . equals ( player . getName ( ) ) ) { players . set ( i , player ) ; game . playerReconnected ( player ) ; System . out . println ( "Success!" ) ; return RejectionReason . NONE ; } } System . out . println ( "Failed!" ) ; return RejectionReason . LOBBY_STARTED ; } } }
Try to add a player to this lobby.
Try to add a player to this lobby .
// synchronize to avoid threading issues // We're joining a game that hasn't started yet // Names must be unique // This isn't a great way to handle it, since it doesn't indicate to the user exactly why it failed // Add this player to the list of players // Transmit the list of all players in the lobby to all players // Reconnecting to a game that already started // Iterate through the players and find one that matches this name // Replace the old endpoint with the new one // signal to the game that this player has reconnected