Example #1
0
        public void AddPlayer(TriviaPlayer player)
        {
            _players.Enqueue(player);

            if (_players.Count >= PlayersPerGame)
            {
                var readyPlayers = new List<TriviaPlayer>();

                lock (_gameStartLock)
                {
                    for (int i = 0; i < PlayersPerGame; i++)
                    {
                        _players.TryDequeue(out var readyPlayer);
                        readyPlayers.Add(readyPlayer);
                    }
                }

                var game = new TriviaGame(readyPlayers, _loggerFactory);
                Games[game.ID] = game;

                foreach (var readyPlayer in readyPlayers)
                {
                    readyPlayer.StartGame(game);
                }

                Task.Run(() => game.PlayGameAsync());
            }
        }
Example #2
0
        public override async Task <Game> StartTrivia(Player request, ServerCallContext context)
        {
            var player = new TriviaPlayer(request.Name, _loggerFactory);

            _lobby.AddPlayer(player);

            player.ScoreUpdated += ScoreUpdated;

            await player.ReadyTask;

            return(new Game {
                GameID = player.Game.ID.ToString()
            });
        }