public async Task DealMessage(string groupNo, string account) { try { var key = CacheConst.GetGroupMsgListKey(groupNo); string cacheMsgInfo = await _database.ListLeftPopAsync(key); IMahuaApi mahuaApi = null; while (!string.IsNullOrWhiteSpace(cacheMsgInfo)) { mahuaApi = mahuaApi ?? MahuaRobotManager.Instance.CreateSession(account).MahuaApi; GroupItemRes msg = JsonConvert.DeserializeObject <GroupItemRes>(cacheMsgInfo); MahuaApiHelper.SendGroupMsg(mahuaApi, msg, groupNo, account); cacheMsgInfo = await _database.ListLeftPopAsync(key); } } catch (Exception e) { var str = e.Message; } }
//根据缓存类型获取缓存的键 internal static string GetCacheKey(CacheConst cacheConst) { var text = webName + "." + cacheConst.ToString(); int length = text.Length; return(text); }
public async Task <PrivateRes> Run(string msg, string account, Lazy <string> getLoginAccount) { var key = CacheConst.GetConfigKey(account); // 查看是否存在操作 var cache = _database.StringGet(key).ToString(); if (!string.IsNullOrWhiteSpace(cache)) { _database.KeyDelete(key); // 移除key if (CacheConst.AddFlag.Equals(cache)) { return(await AddInfo(msg)); } if (CacheConst.RemoveFlag.Equals(cache)) { await ConfigService.RemoveKeyAsync(msg); return("删除成功!"); } } return(null); }
public async Task <GroupRes> Run(string msg, string account, string groupNo, Lazy <string> getLoginAccount) { var key = CacheConst.GetMemberOptKey(account, groupNo, CacheConst.AddPet); var cache = await _database.StringGetAsync(key); if (int.TryParse(cache, out var petId)) { await _database.KeyDeleteAsync(key); var arr = msg.Split('@'); // 格式错误直接跳过 if (arr.Length != 2 || arr[1].Length != 1 || string.IsNullOrWhiteSpace(arr[0])) { return(null); } var pet = await PetService.GetAsync(petId); Gender sex; if ("男".Equals(arr[1])) { sex = Gender.MALE; } else if ("女".Equals(arr[1])) { sex = Gender.FAMALE; } else { return("性别错误!"); } if (pet == null) { return("宠物已下架!"); } // 记录流水 await BillFlowService.AddBillAsync(groupNo, account, pet.Price, pet.Price, BillTypes.Consume, "领养宠物"); // 修正账户 await MemberInfoService.ChangeAmountAsync(groupNo, account, -pet.Price); // 添加宠物 var userPet = await UserPetService.AddPetAsync(pet, groupNo, account, sex, arr[0]); return(GroupRes.GetSuccess(new GroupItemRes() { AtTa = true, Msg = $"恭喜您获得一只{userPet.Quality}品质的{pet.Name}" })); } return(null); }
public async Task<string> Alias(string contactId) { var memberPayload = await _cacheRoomMemberPayload.GetOrAddAsync( CacheConst.CacheKeyRoomMember(RoomId, contactId), async () => await _puppetService.RoomMemberRawPayload(RoomId, contactId), () => new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(CacheConst.CacheGlobalExpirationTime) }); return memberPayload?.Name; }
private async Task RoomMemberPayloadDirty(string roomId) { var contactIdList = await _puppetService.RoomMemberList(RoomPayload.Id); if (contactIdList.IsNullOrEmpty()) { return; } Logger.LogInformation($"remove room member cache, room:{roomId},contact:{string.Join(',', contactIdList)}"); foreach (var contactId in contactIdList) { var cacheKey = CacheConst.CacheKeyRoomMember(roomId, contactId); _ = _cacheRoomPayload.RemoveAsync(cacheKey); } }
public async Task <GroupRes> Run(string msg, string account, string groupNo, Lazy <string> getLoginAccount) { if ("成语接龙".Equals(msg)) { var count = await IdiomsService.GetCountAsync(); var randIndex = _random.Next(count); var info = await IdiomsService.GetInfoAsync(randIndex + 1); // 写入活动缓存 await _database.StringSetAsync(CacheConst.GetGroupActivityKey(groupNo), CacheConst.IdiomsSolitaire, RuleConst.GroupActivityExpiry); // 缓存成语id await _database.StringSetAsync(CacheConst.GetIdiomsKey(groupNo), info.Id, RuleConst.GroupActivityExpiry); // 缓存尝试次数 await _database.StringSetAsync(CacheConst.GetIdiomsTryCountKey(groupNo), 0, RuleConst.GroupActivityExpiry); // 添加活动日志 var logId = await ActivityLogService.OpenActivityAsync(groupNo, Data.Pikachu.Menu.ActivityTypes.IdiomsSolitaire, DateTime.Now.Add(RuleConst.GroupActivityExpiry)); // 缓存日志key await _database.StringSetAsync(CacheConst.GetActivityLogKey(groupNo), logId, RuleConst.GroupActivityExpiry); CreateCloseTime(logId, groupNo, getLoginAccount.Value); return($@" >>>>>>>>>全员成语接龙已开启<<<<<<<<<<<< 当前成语:{info.Word} 尾拼:{info.LastSpell} 成语解析:{info.Explanation} "); } return(null); }
public async Task DealMessage() { var list = await _groupActivityService.AutoCloseAsync(); foreach (var item in list) { var key = CacheConst.GetGroupMsgListKey(item.Group); _database.ListLeftPush(key, JsonConvert.SerializeObject(new GroupItemRes() { Msg = $@" >>>>>>>>>成语接龙已结束<<<<<<<<<<<< 成功次数:{item.SuccessCount.ToString()} 失败次数:{item.FailureCount.ToString()} 希望大家再接再厉! " })); } }
/// <summary> /// 创建一个定时关闭活动的定时器 /// </summary> private void CreateCloseTime(int logId, string groupNo, string loginQq) { // 使用线程池开启工作项替代timer ThreadPool.QueueUserWorkItem((state => { Thread.Sleep(RuleConst.GroupActivityExpiry); if (ActivityLogService.CloseActivity(logId, "活动结束,自动关闭!", out var log) > 0) { _database.ListLeftPush(CacheConst.GetGroupMsgListKey(groupNo), JsonConvert.SerializeObject(new GroupItemRes() { Msg = $@" >>>>>>>>>成语接龙已结束<<<<<<<<<<<< 成功次数:{log.SuccessCount.ToString()} 失败次数:{log.FailureCount.ToString()} 希望大家再接再厉! " })); } })); }
public async Task <GroupRes> Run(string msg, string account, string groupNo, Lazy <string> getLoginAccount) { var activityKey = CacheConst.GetGroupActivityKey(groupNo); var groupActivity = _database.StringGet(activityKey); if (CacheConst.IdiomsSolitaire.Equals(groupActivity)) { var idiomsKey = CacheConst.GetIdiomsKey(groupNo); var idiomsTryCountKey = CacheConst.GetIdiomsTryCountKey(groupNo); var logId = int.Parse(await _database.StringGetAsync(CacheConst.GetActivityLogKey(groupNo))); var activityLog = ActivityLogService.GetActivity(logId); if (activityLog.ActivityStateType == Data.Pikachu.Menu.ActivityStateTypes.Close) { // 移除活动缓存 await _database.KeyDeleteAsync(activityKey); return("成语接龙活动已关闭!"); } if ("关闭活动".Equals(msg) && await ManageService.IsManageAsync(account))// 管理员主动结束活动. { // 移除活动缓存 await _database.KeyDeleteAsync(activityKey); var log = await ActivityLogService.CloseActivityAsync(logId, "活动结束,管理员主动结束活动!"); return($@" >>>>>>>>>本次活动已结束,欢迎下次再来挑战!<<<<<<<<<<<< 本次挑战成果: 成功次数:{log.SuccessCount.ToString()} 失败次数:{log.FailureCount.ToString()} 希望大家再接再厉! "); } // 增加尝试次数 var tryCount = await _database.StringIncrementAsync(idiomsTryCountKey); var idiomId = await _database.StringGetAsync(idiomsKey); var spell = await IdiomsService.GetInfoAsync(int.Parse(idiomId)); var confrimStr = $@" >>>>>>>>>成语接龙火热进行中<<<<<<<<<<<< 当前成语:{spell.Word} 尾拼:{spell.LastSpell} 成语解析:{spell.Explanation} "; if (msg.Length != 4) { return(GroupRes.GetSuccess(new GroupItemRes() { AtTa = true, Msg = "输入格式有误!" }, await GetTryCountRes(tryCount, activityKey, logId), confrimStr)); } var info = await IdiomsService.GetByWordAsync(msg); if (info == null) { return(GroupRes.GetSuccess(new GroupItemRes() { AtTa = true, Msg = "词语输入有误!" }, await GetTryCountRes(tryCount, activityKey, logId), confrimStr)); } if (!spell.LastSpell.Equals(info.FirstSpell)) { return(GroupRes.GetSuccess(new GroupItemRes() { AtTa = true, Msg = "你输入的词语并不能接上呢!" }, await GetTryCountRes(tryCount, activityKey, logId), confrimStr)); } // 积分奖励 var amount = _random.Next(100) + 5; await ActivityLogService.AddSuccessCountAsync(logId); await BillFlowService.AddBillAsync(groupNo, account, amount, amount, Data.Pikachu.Menu.BillTypes.Reward, "成语接龙奖励"); await MemberInfoService.ChangeAmountAsync(groupNo, account, amount); // 重新缓存 await _database.StringSetAsync(idiomsKey, info.Id, RuleConst.GroupActivityExpiry); // 重新缓存 await _database.StringSetAsync(idiomsTryCountKey, 0, RuleConst.GroupActivityExpiry); return(GroupRes.GetSuccess(new GroupItemRes() { AtTa = true, Msg = $" 恭喜你获得{amount.ToString()}钻石奖励!" }, $@" >>>>>>>>>【成语接龙】下一回合<<<<<<<<<<<< 当前成语:{info.Word} 尾拼:{info.LastSpell} 成语解析:{info.Explanation} " )); } return(null); }
public async Task <GroupRes> Run(string msg, string account, string groupNo, Lazy <string> getLoginAccount) { if ("宠物系统".Equals(msg)) { return(@"当前宠物系统支持: [宠物商店] [宠物道具商店] [查看宠物] [放养宠物] "); } if ("宠物商店".Equals(msg)) { var list = await PetService.GetAll().OrderByDescending(u => u.Id).ToListAsync(); if (list.Count == 0) { return("宠物商店还没有入驻的宠物呢,请联系管理员添加吧!"); } StringBuilder builder = new StringBuilder(); builder.AppendLine(" [宠物介绍图] [宠物名称] [宠物描述] [宠物价格]"); for (int i = 0; i < list.Count; i++) { builder.AppendLine( $"{(i + 1).ToString()}. {list[i].Face} {list[i].Name} [{list[i].Description}] {list[i].Price}$"); } builder.AppendLine(); builder.AppendLine(" 试试对我说 [领养宠物] [宠物名称] 吧~"); builder.AppendLine(" 示例: 领养宠物 小白"); return(builder.ToString()); } Match match; if ((match = Regex.Match(msg, @"^领养宠物([\s|\S]*)")).Success) { if (UserPetService.IsExists(groupNo, account)) { return(GroupRes.GetSingleSuccess(new GroupItemRes() { AtTa = true, Msg = "你已经拥有宠物了,请勿重复领养!" })); } var petName = match.Groups[1].Value.Trim(); if (string.IsNullOrWhiteSpace(petName)) { return("宠物名称不能为空!"); } var pet = await PetService.GetInfoByNameAsync(petName); if (pet == null) { return($"宠物-{petName} 不存在!"); } var memberInfo = await MemberInfoService.GetInfoAsync(groupNo, account); if (pet.Price > memberInfo.Amount) { return($"宠物-{petName}的价格为{pet.Price} , 而您的余额为{memberInfo.Amount} , 不足以支付!"); } // 添加添加宠物标识 await _database.StringSetAsync(CacheConst.GetMemberOptKey(account, groupNo, CacheConst.AddPet), pet.Id, TimeSpan.FromSeconds(30)); return(GroupRes.GetSingleSuccess(new GroupItemRes() { AtTa = true, Msg = @"请输入[宠物姓名]@[宠物性别{男/女}] 示例: 小白@女" })); } return(null); }
public async Task <PrivateRes> Run(string msg, string account, Lazy <string> getLoginAccount) { var key = CacheConst.GetConfigKey(account); if ("配置管理".Equals(msg)) { return(@"当前配置管理支持内容: [查看配置] [添加配置] [删除配置] "); } if ("查看配置".Equals(msg)) { var list = await ConfigService.GetAll().OrderByDescending(u => u.UpdateTime) .ThenByDescending(u => u.CreateTime).ToListAsync(); if (list.Count == 0) { return("暂无配置"); } StringBuilder builder = new StringBuilder(); builder.AppendLine($" [配置key]|[配置value]|[配置描述]"); for (int i = 0; i < list.Count(); i++) { builder.AppendLine( $"{(i + 1).ToString()}. {list[i].Key}|{list[i].Value}|{list[i].Description}"); } builder.AppendLine(); builder.AppendLine("添加配置:[配置key]|[配置value]|[配置描述](请注意内容中不要使用'|')"); builder.AppendLine("示例: 添加配置 monster|怪兽|翻译测试 "); return(builder.ToString()); } Match match; if ((match = Regex.Match(msg, @"^添加配置([\s|\S]*)$")).Success) { var info = match.Groups[1].Value; if (string.IsNullOrWhiteSpace(info)) { // 添加标记 _database.StringSet(key, CacheConst.AddFlag, RuleConst.PrivateOptExpiry); return("请按照此格式填写你要添加的配置:[配置key]|[配置value]|[配置描述](请注意内容中不要使用'|')"); } return(await AddInfo(info)); } if ((match = Regex.Match(msg, @"^删除配置([\s|\S]*)$")).Success) { var info = match.Groups[1].Value; if (string.IsNullOrWhiteSpace(info)) { // 添加标记 _database.StringSet(key, CacheConst.RemoveFlag, RuleConst.PrivateOptExpiry); return("请输入你要删除的'配置key':"); } await ConfigService.RemoveKeyAsync(info.Trim()); return("删除成功!"); } return(null); }