backend/src/main/java/com/coolioasjulio/influence/web/Lobby.java
/**
     * Remove a player from the lobby, usually when a player disconnects.
     *
     * @param player The player to remove.
     */
Remove a player from the lobby, usually when a player disconnects.
@param player The player to remove.
Remove a player from the lobby usually when a player disconnects . @param player The player to remove .
public void removePlayer(PlayerEndpoint player) {
        synchronized (playersLock) {
            if (players.contains(player)) {
                System.out.printf("Player %s disconnected from lobby %s!\n", player.getName(), code);
                if (started.get() && game != null) {
                    game.onPlayerDisconnected(player);
                } else {
                    String list = new Gson().toJson(players.stream().map(PlayerEndpoint::getName).toArray(String[]::new));
                    players.forEach(p -> p.write(list));
                }
            }
            if (players.stream().noneMatch(PlayerEndpoint::isConnected)) {
                System.out.printf("Lobby %s terminated.\n", code);
                synchronized (lobbyMapLock) {
                    lobbyMap.remove(code);
                }
                shouldClose = true;
                if (gameThread != null) {
                    System.out.printf("Interrupting game thread for %s\n", code);
                    gameThread.interrupt();
                }
            }
        }
    }
public void removePlayer ( PlayerEndpoint player ) { synchronized ( playersLock ) { if ( players . contains ( player ) ) { System . out . printf ( "Player %s disconnected from lobby %s!\n" , player . getName ( ) , code ) ; if ( started . get ( ) && game != null ) { game . onPlayerDisconnected ( player ) ; } else { String list = new Gson ( ) . toJson ( players . stream ( ) . map ( PlayerEndpoint :: getName ) . toArray ( String [ ] :: new ) ) ; players . forEach ( p -> p . write ( list ) ) ; } } if ( players . stream ( ) . noneMatch ( PlayerEndpoint :: isConnected ) ) { System . out . printf ( "Lobby %s terminated.\n" , code ) ; synchronized ( lobbyMapLock ) { lobbyMap . remove ( code ) ; } shouldClose = true ; if ( gameThread != null ) { System . out . printf ( "Interrupting game thread for %s\n" , code ) ; gameThread . interrupt ( ) ; } } } }
Remove a player from the lobby, usually when a player disconnects.
Remove a player from the lobby usually when a player disconnects .
// synchronize to avoid threading issues // unlike addPlayer(), we won't no-op if the lobby has started. This is because disconnects can happen during the game // If the game has already started, then tell the game that this player disconnected // Otherwise, update the players in the lobby that this player disconnected // If all players have disconnected, end the lobby // Remove from the global lobby map // Terminate the game thread, if it exists