Example #1
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            string connectionId = Context.ConnectionId;
            string groupName    = ConnectionObserver.GetCurrentGroupName(connectionId);

            ConnectionObserver.ConnectionStates.Remove(Context.ConnectionId);
            if (groupName != null)
            {
                var playersList = ConnectionObserver.GetPlayersList(groupName);
                await Clients.Group(groupName).RefreshPlayersList(playersList);
            }
            await base.OnDisconnectedAsync(exception);
        }
Example #2
0
        public async Task ConnectToGame([GameCode] string gameCode, string nickname)
        {
            _logger.LogInformation($"Client with connection id: {Context.ConnectionId} is attempting connection using code: {gameCode} under nickname: {nickname}");
            int.TryParse(Context.Items[KeyConstants.GameCode] as string, out int gameId);
            int?userId = Context.UserIdentifier != null ? (int?)int.Parse(Context.UserIdentifier) : null;

            var playerProfile = await _profileService.CreateOrUpdatePlayerProfile(new PlayerProfileCreateRequest
            {
                GameId   = gameId,
                Nickname = nickname,
                UserId   = userId
            });

            await Clients.Caller.SendPlayerProfileId(playerProfile.Id);

            await Groups.AddToGroupAsync(Context.ConnectionId, gameCode);

            ConnectionObserver.ConnectionStates[Context.ConnectionId] = new PlayerEntry
            {
                Group           = gameCode,
                PlayerProfileId = playerProfile.Id,
                Nickname        = playerProfile.Nickname,
            };
            await Clients.GroupExcept(gameCode, Context.ConnectionId).SendMessage($"Player {playerProfile.Nickname} has joined!", MessageType.Success);

            var playersList = ConnectionObserver.GetPlayersList(gameCode);
            await Clients.Group(gameCode).RefreshPlayersList(playersList);

            var items = await _itemService.GetItemsByGameId(gameId);

            await Clients.Caller.RefreshItemList(items);

            var categories = await _categoryService.GetCategoriesByGameId(gameId);

            await Clients.Caller.RefreshCategories(categories);

            if (_gameService.IsUserGameOwner(userId, gameId))
            {
                await Clients.Caller.AllowGameControl((int)userId);

                await Groups.AddToGroupAsync(Context.ConnectionId, $"{gameCode}-master");
            }
            await Clients.Group($"{gameCode}-master").RequestCurrentItemId(gameCode);

            await Clients.Caller.SendMessage("Game joined successfully", MessageType.Success);
        }
Example #3
0
        public async Task DisconnectFromGame([GameCode] string gameCode)
        {
            int.TryParse(Context.Items[KeyConstants.GameCode] as string, out int gameId);

            _logger.LogInformation($"Connection:{Context.ConnectionId} is leaving group {gameCode}");
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, gameCode);

            await Clients.GroupExcept(gameCode, Context.ConnectionId).SendMessage($"Player {Context.User.Identity.Name} has left.", MessageType.Warning);

            string connectionId = Context.ConnectionId;
            string groupName    = ConnectionObserver.GetCurrentGroupName(connectionId);

            ConnectionObserver.ConnectionStates.Remove(Context.ConnectionId);
            if (groupName != null)
            {
                var playersList = ConnectionObserver.GetPlayersList(groupName);
                await Clients.Group(groupName).RefreshPlayersList(playersList);
            }
        }