From f5c12418603f9747803f4a4bd41dad1d7df2748f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=99=BD=E7=8C=BF?= <1083092844@qq.com> Date: Mon, 10 Apr 2023 20:39:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BC=E7=89=A9=E6=95=B0=E9=87=8F=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E8=BF=9E=E7=BB=AD=E6=8E=A8=E9=80=81=EF=BC=8C=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8F=96=E6=AF=8F=E6=AC=A1=E5=A2=9E=E9=87=8F=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E9=99=A4GroupCount=E6=94=B9=E4=B8=BAGiftCount?= =?UTF-8?q?=E5=8D=B3=E5=90=8C=E4=B8=80=E7=BB=84=E7=A4=BC=E7=89=A9=E4=B8=8B?= =?UTF-8?q?=E6=9C=AC=E6=AC=A1=E7=9B=B8=E8=BE=83=E4=B8=8A=E6=AC=A1=E7=9A=84?= =?UTF-8?q?=E7=A4=BC=E7=89=A9=E7=9A=84=E5=A2=9E=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BarrageGrab/JsonEntity/BarrageMessages.cs | 11 ++- BarrageGrab/WsBarrageService.cs | 93 ++++++++++++----------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/BarrageGrab/JsonEntity/BarrageMessages.cs b/BarrageGrab/JsonEntity/BarrageMessages.cs index 298dc7d..f6f8c79 100644 --- a/BarrageGrab/JsonEntity/BarrageMessages.cs +++ b/BarrageGrab/JsonEntity/BarrageMessages.cs @@ -177,17 +177,16 @@ /// public string GiftName { get; set; } + /// + /// 本次(增量)礼物数量 + /// + public long GiftCount { get; set; } + /// /// 礼物数量(连续的) /// public long RepeatCount { get; set; } - /// - /// 礼物数量(本次) - /// - public long GroupCount { get; set; } - - /// /// 抖币价格 /// diff --git a/BarrageGrab/WsBarrageService.cs b/BarrageGrab/WsBarrageService.cs index e2132d2..024a99c 100644 --- a/BarrageGrab/WsBarrageService.cs +++ b/BarrageGrab/WsBarrageService.cs @@ -61,7 +61,7 @@ namespace BarrageGrab private void GiftCountTimer_Elapsed(object sender, ElapsedEventArgs e) { var now = DateTime.Now; - var timeOutKeys = giftCountCache.Where(w => w.Value.Item2 < now.AddSeconds(-10)||w.Value==null).Select(s => s.Key).ToList(); + var timeOutKeys = giftCountCache.Where(w => w.Value.Item2 < now.AddSeconds(-10) || w.Value == null).Select(s => s.Key).ToList(); //淘汰过期的礼物计数缓存 lock (giftCountCache) @@ -69,7 +69,7 @@ namespace BarrageGrab timeOutKeys.ForEach(key => { giftCountCache.Remove(key); - + }); } } @@ -89,7 +89,7 @@ namespace BarrageGrab Id = data.Id, Level = data.Level, Nickname = data.Nickname, - HeadImgUrl = data.avatarThumb.urlLists.FirstOrDefault()??"", + HeadImgUrl = data.avatarThumb.urlLists.FirstOrDefault() ?? "", SecUid = data.sec_uid }; user.FansClub = new FansClubInfo() @@ -98,7 +98,7 @@ namespace BarrageGrab Level = 0 }; - if (data.fansClub!= null && data.fansClub.Data != null) + if (data.fansClub != null && data.fansClub.Data != null) { user.FansClub.ClubName = data.fansClub.Data.clubName; user.Level = data.fansClub.Data.Level; @@ -145,75 +145,78 @@ namespace BarrageGrab var json = JsonConvert.SerializeObject(pack); this.Broadcast(json); } - + //礼物 private void Grab_OnGiftMessage(object sender, ProtoEntity.GiftMessage e) { if (!CheckRoomId(e.Common.roomId)) return; - - var key = e.giftId+"-"+e.groupId.ToString(); - + + var key = e.giftId + "-" + e.groupId.ToString(); + //判断礼物重复 if (e.repeatEnd == 1) - { + { //清除缓存中的key - if (e.groupId>0&& giftCountCache.ContainsKey(key)) + if (e.groupId > 0 && giftCountCache.ContainsKey(key)) { lock (giftCountCache) { giftCountCache.Remove(key); - } + } } return; } - + int lastCount = 0; int currCount = (int)e.repeatCount; + var backward = currCount <= lastCount; + if (currCount <= 0) currCount = 1; if (giftCountCache.ContainsKey(key)) { lastCount = giftCountCache[key].Item1; - if (currCount > lastCount) + backward = currCount <= lastCount; + if (!backward) { lock (giftCountCache) { giftCountCache[key] = Tuple.Create(currCount, DateTime.Now); } - } + } } else { - if (e.groupId > 0) + if (e.groupId > 0 && !backward) { lock (giftCountCache) { giftCountCache.Add(key, Tuple.Create(currCount, DateTime.Now)); - } - } + } + } } - //比上次小,则说明先后顺序出了问题,直接丢掉,因为上次已经填充发送过了 - if (currCount < lastCount) return; + //比上次小,则说明先后顺序出了问题,直接丢掉,应为比它大的消息已经处理过了 + if (backward) return; + - for (int repeat = lastCount+1; repeat <= currCount; repeat++) + var count = currCount - lastCount; + + var enty = new GiftMsg() { - var enty = new GiftMsg() - { - MsgId = e.Common.msgId+repeat, - RoomId = e.Common.roomId, - Content = $"{e.User.Nickname} 送出 {e.Gift.Name} x {repeat} 个", - DiamondCount = e.Gift.diamondCount, - RepeatCount = repeat, - GroupCount = e.groupCount, - GiftId = e.giftId, - GiftName = e.Gift.Name, - User = GetUser(e.User) - }; + MsgId = e.Common.msgId, + RoomId = e.Common.roomId, + Content = $"{e.User.Nickname} 送出 {e.Gift.Name} x {currCount} 个,增量{count}个", + DiamondCount = e.Gift.diamondCount, + RepeatCount = currCount, + GiftCount = count, + GiftId = e.giftId, + GiftName = e.Gift.Name, + User = GetUser(e.User) + }; - Print($"{enty.User.GenderToString()} {enty.Content}", ConsoleColor.Red, BarrageMsgType.礼物消息); - var pack = new BarrageMsgPack(JsonConvert.SerializeObject(enty), BarrageMsgType.礼物消息); - var json = JsonConvert.SerializeObject(pack); - this.Broadcast(json); - } + Print($"{enty.User.GenderToString()} {enty.Content}", ConsoleColor.Red, BarrageMsgType.礼物消息); + var pack = new BarrageMsgPack(JsonConvert.SerializeObject(enty), BarrageMsgType.礼物消息); + var json = JsonConvert.SerializeObject(pack); + this.Broadcast(json); } //关注 @@ -227,8 +230,8 @@ namespace BarrageGrab Content = $"{e.User.Nickname} 关注了主播", RoomId = e.Common.roomId, User = GetUser(e.User) - }; - + }; + Print($"{enty.User.GenderToString()} {enty.Content}", ConsoleColor.Yellow, BarrageMsgType.关注消息); var pack = new BarrageMsgPack(JsonConvert.SerializeObject(enty), BarrageMsgType.关注消息); var json = JsonConvert.SerializeObject(pack); @@ -265,7 +268,7 @@ namespace BarrageGrab private void Grab_OnMemberMessage(object sender, ProtoEntity.MemberMessage e) { if (!CheckRoomId(e.Common.roomId)) return; - + var enty = new JsonEntity.MemberMessage() { MsgId = e.Common.msgId, @@ -284,7 +287,7 @@ namespace BarrageGrab private void Grab_OnLikeMessage(object sender, ProtoEntity.LikeMessage e) { if (!CheckRoomId(e.Common.roomId)) return; - + var enty = new LikeMsg() { MsgId = e.Common.msgId, @@ -304,7 +307,7 @@ namespace BarrageGrab private void Grab_OnChatMessage(object sender, ProtoEntity.ChatMessage e) { if (!CheckRoomId(e.Common.roomId)) return; - + var enty = new Msg() { MsgId = e.Common.msgId, @@ -336,12 +339,12 @@ namespace BarrageGrab Print($"直播已结束", ConsoleColor.DarkCyan, BarrageMsgType.下播); pack = new BarrageMsgPack(JsonConvert.SerializeObject(enty), BarrageMsgType.下播); } - - if(pack!=null) + + if (pack != null) { var json = JsonConvert.SerializeObject(pack); this.Broadcast(json); - } + } } private void Print(string msg, ConsoleColor color, BarrageMsgType bartype)