Esempio n. 1
0
        public async Task SendMessageToGroup(string groupName, string message, string username, string conId)
        {
            var data = new Chat
            {
                Message  = message,
                RoomName = groupName,
                Username = username
            };

            if (await _redisHandler.IsCached($"Room:StartedGame:{groupName}"))
            {
                var gameRoom = JsonConvert.DeserializeObject <StartedGame>(await _redisHandler.GetFromCache($"Room:StartedGame:{groupName}"));
                if (gameRoom.word.WordName == message)
                {
                    data.Answer = true;

                    if (await _chatRepository.CountWhere(x => x.Answer == true && x.RoomName == groupName) == 0)
                    {
                        gameRoom.userList.FirstOrDefault(x => x.Username == username).GamePoint += 10;
                        await Clients.Client(conId).SendAsync("DisableChat", true);
                    }
                    else if (await _chatRepository.CountWhere(x => x.Answer == true && x.RoomName == groupName) == 1)
                    {
                        gameRoom.userList.FirstOrDefault(x => x.Username == username).GamePoint += 8;
                        await Clients.Client(conId).SendAsync("DisableChat", true);
                    }
                    else if (await _chatRepository.CountWhere(x => x.Answer == true && x.RoomName == groupName) == 2)
                    {
                        gameRoom.userList.FirstOrDefault(x => x.Username == username).GamePoint += 5;
                        await Clients.Client(conId).SendAsync("DisableChat", true);
                    }
                    else if (await _chatRepository.CountWhere(x => x.Answer == true && x.RoomName == groupName) > 2)
                    {
                        gameRoom.userList.FirstOrDefault(x => x.Username == username).GamePoint += 3;
                        await Clients.Client(conId).SendAsync("DisableChat", true);
                    }
                    gameRoom.userList[0].GamePoint += 2;

                    await _redisHandler.RemoveFromCache($"Room:StartedGame:{groupName}");

                    await _redisHandler.AddToCache($"Room:StartedGame:{groupName}", TimeSpan.FromMinutes(10), JsonConvert.SerializeObject(gameRoom));
                }
            }

            await _chatRepository.Add(data);

            await Clients.Group(groupName).SendAsync("GroupMessage", data);
        }
Esempio n. 2
0
        public async Task <IActionResult> SaveToConnectedUser([FromBody] UserDTO user)
        {
            if (await _redisHandler.IsCached("Userlist:ConnectedUser"))
            {
                var cached        = JsonConvert.DeserializeObject <List <ConnectedUser> >(await _redisHandler.GetFromCache("Userlist:ConnectedUser"));
                var connectedUser = cached.FirstOrDefault(x => x.Username == user.Username);
                if (connectedUser != null)
                {
                    connectedUser.ConnectionId = user.ConnectionId;
                }
                else
                {
                    cached.Add(new ConnectedUser
                    {
                        Id           = Guid.NewGuid(),
                        Username     = user.Username,
                        ConnectionId = user.ConnectionId
                    });
                }

                await _redisHandler.RemoveFromCache("Userlist:ConnectedUser");

                await _redisHandler.AddToCache("Userlist:ConnectedUser", TimeSpan.FromMinutes(2), JsonConvert.SerializeObject(cached));

                return(await Task.FromResult(Ok()));
            }


            var data = await _connectedUserRepository.FirstOrDefault(x => x.Username == user.Username);

            if (data != null)
            {
                data.ConnectionId = user.ConnectionId;
                await _connectedUserRepository.Update(data);
            }
            else
            {
                await _connectedUserRepository.Add(new ConnectedUser
                {
                    Id           = Guid.NewGuid(),
                    Username     = user.Username,
                    ConnectionId = user.ConnectionId
                });;
            }
            await _redisHandler.AddToCache("Userlist:ConnectedUser", TimeSpan.FromMinutes(5), JsonConvert.SerializeObject(await _connectedUserRepository.GetAll()));

            return(await Task.FromResult(Ok()));
        }
Esempio n. 3
0
        public async Task <IActionResult> Insert([FromBody] Room room)
        {
            room.Id = Guid.NewGuid();
            List <Room> rooms = new List <Room>();

            if (await _redisHandler.IsCached("Room:Rooms"))
            {
                rooms = JsonConvert.DeserializeObject <List <Room> >(await _redisHandler.GetFromCache("Room:Rooms"));
            }

            rooms.Add(room);
            await _redisHandler.RemoveFromCache("Room:Rooms");

            await _redisHandler.AddToCache("Room:Rooms", TimeSpan.FromMinutes(2), JsonConvert.SerializeObject(rooms));

            await _hubContext.Clients.All.SendAsync("Notify", rooms);

            return(await Task.FromResult(Ok(room)));
        }