From c83671966657f55a16011c9d774e64947c3cc07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Thu, 31 Aug 2023 11:31:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/imserver/chat.go | 9 +++++++++ server/imserver/chatmgr.go | 34 +++++++++++++++++----------------- server/imserver/player.go | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/server/imserver/chat.go b/server/imserver/chat.go index f4f8570c..b1b78087 100644 --- a/server/imserver/chat.go +++ b/server/imserver/chat.go @@ -4,6 +4,8 @@ import ( "cs" ) +const ChatMsgMaxSize = 60 + type ChatUserRec struct { HasUnreadMsg bool Dirty bool @@ -15,3 +17,10 @@ type ChatMsgRec struct { LastID uint64 ChatMsgList []*cs.MFChatMsg } + +func (c *ChatMsgRec) AddChatMsg(msg *cs.MFChatMsg) { + if len(c.ChatMsgList) >= ChatMsgMaxSize { + c.ChatMsgList = c.ChatMsgList[1:] + } + c.ChatMsgList = append(c.ChatMsgList, msg) +} diff --git a/server/imserver/chatmgr.go b/server/imserver/chatmgr.go index 32f56506..4f711aa6 100644 --- a/server/imserver/chatmgr.go +++ b/server/imserver/chatmgr.go @@ -2,6 +2,7 @@ package main import ( "cs" + "github.com/golang/protobuf/proto" "time" ) @@ -14,9 +15,9 @@ type ChatMgr struct { guildMsgRec map[int64]*ChatMsgRec privateChatUsers map[string]*ChatUserRec - WorldMsgId uint64 - GuildMsgId uint64 - TmpMsgId uint64 + worldMsgId uint64 + guildMsgId uint64 + tmpMsgId uint64 } func NewChatMgr(pm *PlayerMgr, fm *FriendsMgr, gm *GuildMgr) *ChatMgr { @@ -29,9 +30,9 @@ func NewChatMgr(pm *PlayerMgr, fm *FriendsMgr, gm *GuildMgr) *ChatMgr { privateChatUsers: make(map[string]*ChatUserRec), } // Default values - cm.WorldMsgId = 1000 - cm.GuildMsgId = 1000 - cm.TmpMsgId = 1000 + cm.worldMsgId = 1000 + cm.guildMsgId = 1000 + cm.tmpMsgId = 1000 return cm } @@ -92,11 +93,11 @@ func (cm *ChatMgr) FillSMUpdatePrivateChatRedPointNotify(p *Player, msg *cs.SMUp func (cm *ChatMgr) ProcWorldChat(p *Player, msg *cs.CMSendChatMsg) { accountId := p.GetAccountId() chatMsg := new(cs.MFChatMsg) - cm.WorldMsgId++ - cm.FillMFChatMsg(chatMsg, &accountId, cm.WorldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) + cm.worldMsgId++ + cm.FillMFChatMsg(chatMsg, &accountId, cm.worldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) - cm.worldMsgRec.CurrID = cm.WorldMsgId - cm.worldMsgRec.ChatMsgList = append(cm.worldMsgRec.ChatMsgList, chatMsg) + cm.worldMsgRec.CurrID = cm.worldMsgId + cm.worldMsgRec.AddChatMsg(chatMsg) // TraversePlayer for _, p2 := range cm.pm.GetPlayers() { @@ -110,16 +111,16 @@ func (cm *ChatMgr) ProcGuildChat(p *Player, msg *cs.CMSendChatMsg) { return } guildId := guild.GuildId - cm.GuildMsgId++ + cm.guildMsgId++ chatMsg := new(cs.MFChatMsg) - cm.FillMFChatMsg(chatMsg, &p.accountId, cm.GuildMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) + cm.FillMFChatMsg(chatMsg, &p.accountId, cm.guildMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) if msgRec, exists := cm.guildMsgRec[guildId]; exists { - msgRec.ChatMsgList = append(msgRec.ChatMsgList, chatMsg) + msgRec.AddChatMsg(chatMsg) } else { msgRec := &ChatMsgRec{} - msgRec.CurrID = cm.GuildMsgId + msgRec.CurrID = cm.guildMsgId cm.guildMsgRec[guildId] = msgRec } @@ -229,7 +230,7 @@ func (cm *ChatMgr) SyncGuildChatMsg(p *Player) { func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMsg *cs.MFChatMsg, lastId uint64) { chatMsgCopy := new(cs.MFChatMsg) - *chatMsgCopy = *chatMsg + proto.Merge(chatMsgCopy, chatMsg) chatMsgCopy.MsgUuid = &lastId if _, exists := cm.privateChatUsers[senderAccountId]; !exists { @@ -246,8 +247,7 @@ func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMs receiverUserRec := cm.privateChatUsers[senderAccountId].Users[receiverAccountId] receiverUserRec.LastID = lastId - receiverUserRec.ChatMsgList = append(receiverUserRec.ChatMsgList, chatMsgCopy) - // 队列 保持最近50条 + receiverUserRec.AddChatMsg(chatMsgCopy) } func (cm *ChatMgr) GetChatUser(accountId *string) *ChatUserRec { diff --git a/server/imserver/player.go b/server/imserver/player.go index 03a2b99e..3de17789 100644 --- a/server/imserver/player.go +++ b/server/imserver/player.go @@ -3,7 +3,7 @@ package main import ( "cs" "f5" - proto "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/proto" "q5" )