Esempio n. 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);
            });
        }