Exemplo n.º 1
0
        public async Task JoinLobby(JoinLobbyModel model)
        {
            if (CurrentPlayer.Lobby != null)
            {
                CurrentPlayer.LeaveLobby();

                await Groups.RemoveFromGroupAsync(CurrentPlayer.ConnectionId, CurrentPlayer.Lobby.Id);

                await Clients.OthersInGroup(CurrentPlayer.Lobby.Id)
                .SendAsync(Commands.PlayerLeftLobby, PlayerModel.Map(CurrentPlayer));
            }

            var(getLobbyResult, lobby) = _lobbyManager.Get(model.LobbyId);
            if (getLobbyResult.IsError)
            {
                if (getLobbyResult.ErrorType == ErrorType.NotFound)
                {
                    lobby = _lobbyManager.Create();
                }
                else
                {
                    throw new Exception(getLobbyResult.Message);
                }
            }

            CurrentPlayer.Join(lobby);

            await Groups.AddToGroupAsync(CurrentPlayer.ConnectionId, lobby.Id);

            await Clients.Client(CurrentPlayer.ConnectionId)
            .SendAsync(Commands.LobbyJoined, new LobbyJoinedModel {
                Lobby = LobbyModel.Map(lobby)
            });

            await Clients.OthersInGroup(lobby.Id).SendAsync(Commands.PlayerJoinedLobby, PlayerModel.Map(CurrentPlayer));
        }