예제 #1
0
        public async Task <JoinGameResponse> JoinNewPlayerToGameAsync(string playerName, string joinCode)
        {
            var gameSession = await _gameSessionRepository.GetByJoinCodeAsync(joinCode);

            if (gameSession == null)
            {
                throw new ArgumentException($"Game for join code {joinCode} does not exist.");
            }

            if (gameSession.Players.Any(x => x.Name == playerName))
            {
                throw new ArgumentException($"Invalid name {playerName}. Player already exists in game {gameSession.Name}.");
            }

            var player = new Player
            {
                Name = playerName
            };

            gameSession.Players.Add(player);

            await _gameSessionRepository.UpdateAsync(gameSession);

            return(new JoinGameResponse
            {
                PlayerJWT = _playerService.GeneratePlayerJWT(player)
            });
        }