backend/src/main/java/com/coolioasjulio/influence/web/Lobby.java
/**
     * Start the lobby, playing games on repeat forever, until the thread is interrupted.
     *
     * @param checkPreconditions If true, check for the the number of players and if the game has started before continuing. If false, assume the conditions are correct.
     */
Start the lobby, playing games on repeat forever, until the thread is interrupted.
@param checkPreconditions If true, check for the the number of players and if the game has started before continuing. If false, assume the conditions are correct.
Start the lobby playing games on repeat forever until the thread is interrupted . @param checkPreconditions If true check for the the number of players and if the game has started before continuing . If false assume the conditions are correct .
public void startGame(boolean checkPreconditions) {
        if (checkPreconditions) {
            synchronized (playersLock) {
                if (players.size() < Game.MIN_PLAYERS || started.getAndSet(true)) return;
            }
        }
        System.out.println("Starting lobby: " + code);
        String message = new Gson().toJson("Start");
        synchronized (playersLock) {
            players.forEach(p -> p.write(message));
        }
        while (!Thread.interrupted()) {
            PlayerEndpoint[] players = this.players.toArray(new PlayerEndpoint[0]);
            for (int i = 0; i < players.length; i++) {
                int index = ThreadLocalRandom.current().nextInt(players.length);
                PlayerEndpoint temp = players[i];
                players[i] = players[index];
                players[index] = temp;
            }
            game = new WebGame(players);
            game.playGame();
        }
        for (PlayerEndpoint player : players) {
            try {
                player.close("The game has been terminated!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        synchronized (lobbyMapLock) {
            lobbyMap.remove(code); 
        }
        System.out.printf("Thread for lobby %s terminated!\n", code);
    }
public void startGame ( boolean checkPreconditions ) { if ( checkPreconditions ) { synchronized ( playersLock ) { if ( players . size ( ) < Game . MIN_PLAYERS || started . getAndSet ( true ) ) return ; } } System . out . println ( "Starting lobby: " + code ) ; String message = new Gson ( ) . toJson ( "Start" ) ; synchronized ( playersLock ) { players . forEach ( p -> p . write ( message ) ) ; } while ( ! Thread . interrupted ( ) ) { PlayerEndpoint [ ] players = this . players . toArray ( new PlayerEndpoint [ 0 ] ) ; for ( int i = 0 ; i < players . length ; i ++ ) { int index = ThreadLocalRandom . current ( ) . nextInt ( players . length ) ; PlayerEndpoint temp = players [ i ] ; players [ i ] = players [ index ] ; players [ index ] = temp ; } game = new WebGame ( players ) ; game . playGame ( ) ; } for ( PlayerEndpoint player : players ) { try { player . close ( "The game has been terminated!" ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } synchronized ( lobbyMapLock ) { lobbyMap . remove ( code ) ; } System . out . printf ( "Thread for lobby %s terminated!\n" , code ) ; }
Start the lobby, playing games on repeat forever, until the thread is interrupted.
Start the lobby playing games on repeat forever until the thread is interrupted .
// Only check the preconditions if we're supposed to. // This is because when we're using startGameAsync(), the checks are performed before starting this thread // Write to each player, telling them the game has started // Play forever until the thread is interrupted // shuffle the players to randomize turn order // Play a new game // Close all websockets. This will no-op for already closed sockets. // Remove the lobby from the map