Пример #1
0
        public async Task JoinAsync(Guid gameId, User user)
        {
            await _gameManager.JoinAsync(gameId, user, async (game, player) =>
            {
                ConnectionBinding.BindConnectionId(Context.ConnectionId, user.Id, gameId);
                await Groups.AddToGroupAsync(Context.ConnectionId, game.Id.ToString());

                await Clients.Caller.SendAsync("im_joined_game", new
                {
                    game = _mapper.Map <Game, GameDto>(game),
                    lastRoundScoreboard = game.GetScoreboard(game.PreviousRoundNumber),
                    player = _mapper.Map <Player, PlayerDto>(player),
                    round  = game.CurrentRound
                });

                await Clients.GroupExcept(game.Id.ToString(), Context.ConnectionId).SendAsync("player_joined_game", new
                {
                    player = _mapper.Map <Player, PlayerDto>(player)
                });

                if (game.State == GameState.InProgress)
                {
                    await Groups.AddConnectionToGameRoundGroupAsync(gameId, game.CurrentRoundNumber, Context.ConnectionId);
                }

                await _lobbyHubContext.Clients.All.SendAsync("player_joined_game", gameId);
            }, async (error) =>
            {
                await Clients.Caller.SendAsync("game_join_error", error);
            });
        }
Пример #2
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var(playerId, gameId) = ConnectionBinding.RemoveConnectionIdBinding(Context.ConnectionId);
            if (playerId != null)
            {
                await LeaveAsync(gameId.Value, playerId.Value);
            }

            await base.OnDisconnectedAsync(exception);
        }
Пример #3
0
        private ICollection <string> GetPlayersConnectionsIds(Guid gameId, List <Guid> playersIdsInRound)
        {
            var connectionsIds = new List <string>();

            foreach (var id in playersIdsInRound)
            {
                var connectionId = ConnectionBinding.GetPlayerConnectionId(gameId, id);
                if (!string.IsNullOrEmpty(connectionId))
                {
                    connectionsIds.Add(connectionId);
                }
            }

            return(connectionsIds);
        }