server/services/gameService.js
// If player count is fulfilled, starts the game immediately
If player count is fulfilled, starts the game immediately
If player count is fulfilled starts the game immediately
createGame(playerName, playerCount, cpuPlayerCount, gameCount, randomizePlayerOrder, autoDisconnect,
    shuffleDeck = true) {
    if (!authService.validateUsername(playerName)) {
      return null;
    }
    if (playerCount < GameValidation.minPlayerCount || playerCount > GameValidation.maxPlayerCount ||
      cpuPlayerCount < 0 || cpuPlayerCount > playerCount - 1) {
      return null;
    }
    if (gameCount < GameValidation.minGameCount || gameCount > GameValidation.maxGameCount) {
      return null;
    }
    let game = new Game(playerCount, gameCount, randomizePlayerOrder, autoDisconnect, shuffleDeck);
    game.eventEmitter.on('playerInactive', (game, player) => {
      if (player.socket) {
        socketService.emitToClient(player.socket, 'exception', { message: 'Too long inactivity' });
      }
      this.removePlayerFromGame(game, player);
    });
    game.eventEmitter.on('playerInactiveWarning', (game, player, remainingTime) => {
      socketService.sendChatMessage(game.id, null,
        `Wake up! You have ${remainingTime} seconds until you'll be removed from the game...`, player);
    });
    game.eventEmitter.on('gameEnded', (game) => {
      socketService.sendChatMessage(game.id, null, 'The game ended.');
    });
    let human = new HumanPlayer(playerName);
    game.addPlayer(human);
    for (let i = 0; i < cpuPlayerCount; ++i) {
      game.addPlayer(new CpuPlayer(`Computer ${i + 1}`));
    }
    this._games.set(game.id, game);
    if (game.isFull()) {
      this.startNewGame(game);
    }
    else {
      socketService.emitToRoom('playRoom', 'playRoom', 'openGamesChanged', this.getOpenGames());
    }
    return { game: game, player: human };
  }
createGame ( playerName , playerCount , cpuPlayerCount , gameCount , randomizePlayerOrder , autoDisconnect , shuffleDeck = true ) { if ( ! authService . validateUsername ( playerName ) ) { return null ; } if ( playerCount < GameValidation . minPlayerCount || playerCount > GameValidation . maxPlayerCount || cpuPlayerCount < 0 || cpuPlayerCount > playerCount - 1 ) { return null ; } if ( gameCount < GameValidation . minGameCount || gameCount > GameValidation . maxGameCount ) { return null ; } let game = new Game ( playerCount , gameCount , randomizePlayerOrder , autoDisconnect , shuffleDeck ) ; game . eventEmitter . on ( 'playerInactive' , ( game , player ) => { if ( player . socket ) { socketService . emitToClient ( player . socket , 'exception' , { message : 'Too long inactivity' } ) ; } this . removePlayerFromGame ( game , player ) ; } ) ; game . eventEmitter . on ( 'playerInactiveWarning' , ( game , player , remainingTime ) => { socketService . sendChatMessage ( game . id , null , ` ${ remainingTime } ` , player ) ; } ) ; game . eventEmitter . on ( 'gameEnded' , ( game ) => { socketService . sendChatMessage ( game . id , null , 'The game ended.' ) ; } ) ; let human = new HumanPlayer ( playerName ) ; game . addPlayer ( human ) ; for ( let i = 0 ; i < cpuPlayerCount ; ++ i ) { game . addPlayer ( new CpuPlayer ( ` ${ i + 1 } ` ) ) ; } this . _games . set ( game . id , game ) ; if ( game . isFull ( ) ) { this . startNewGame ( game ) ; } else { socketService . emitToRoom ( 'playRoom' , 'playRoom' , 'openGamesChanged' , this . getOpenGames ( ) ) ; } return { game : game , player : human } ; }
If player count is fulfilled, starts the game immediately
If player count is fulfilled starts the game immediately
// Create game and players