Exemple #1
0
        public int StartGame(User user, int roomId, GameParameters parameters)
        {
            var roomExists = ActiveRooms.TryGetValue(roomId, out var room);

            if (!roomExists)
            {
                throw new Exception("Room doesn't exist");
            }

            if (room.Users.Count < 2)
            {
                throw new Exception("Can only start when there are 2 or more players in a room");
            }

            ActiveRooms.TryRemove(roomId, out var _);

            var gameId        = ActiveGames.IsEmpty ? 1 : ActiveGames.Last().Key + 1;
            var numberOfWords = parameters.NumberOfRounds * room.Users.Count;
            var words         = wordsRepository.GetRandomWords(numberOfWords).ToList();

            var game = new Game(gameId, room, words, parameters.NumberOfRounds);

            ActiveGames.TryAdd(gameId, game);

            return(gameId);
        }