示例#1
0
        public IActionResult ConnectLobbyGuest(ConnectGuestToLobbyRequest connectGuestToLobbyRequest)
        {
            //TODO: Add fluent validation
            if (string.IsNullOrEmpty(connectGuestToLobbyRequest.Username) ||
                string.IsNullOrEmpty(connectGuestToLobbyRequest.LobbyCode))
            {
                return(BadRequest());
            }

            var lobby = _lobbyHandler.GetLobbyDto(connectGuestToLobbyRequest.LobbyCode);

            if (lobby == null)
            {
                var errorDictionary = new Dictionary <string, string[]>
                {
                    { nameof(Lobby), new[] { LobbyDoesNotExist(connectGuestToLobbyRequest.Username) } }
                };
                return(BadRequest(new ValidationProblemDetails(errorDictionary)));
            }

            if (lobby.Players.FirstOrDefault(player => player.Username == connectGuestToLobbyRequest.Username) != null)
            {
                var errorDictionary = new Dictionary <string, string[]>
                {
                    { nameof(Lobby), new[] { PlayerWithUsernamePresentInLobby(connectGuestToLobbyRequest.Username) } }
                };
                return(BadRequest(new ValidationProblemDetails(errorDictionary)));
            }

            var lobbyPlayer = new LobbyPlayer()
            {
                Id       = Guid.NewGuid(),
                Username = connectGuestToLobbyRequest.Username
            };

            var lobbyDto = _lobbyHandler.ConnectToLobby(lobbyPlayer, connectGuestToLobbyRequest.LobbyCode);
            var jwtToken = _playerService.Enter(lobbyPlayer.Id, lobbyDto.Code);

            return(Ok(new
            {
                Lobby = lobbyDto,
                AccessToken = jwtToken
            }));
        }