예제 #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);
        }