This commit is contained in:
殷勇 2023-08-31 11:31:23 +08:00
parent 710b28fbc0
commit c836719666
3 changed files with 27 additions and 18 deletions

View File

@ -4,6 +4,8 @@ import (
"cs" "cs"
) )
const ChatMsgMaxSize = 60
type ChatUserRec struct { type ChatUserRec struct {
HasUnreadMsg bool HasUnreadMsg bool
Dirty bool Dirty bool
@ -15,3 +17,10 @@ type ChatMsgRec struct {
LastID uint64 LastID uint64
ChatMsgList []*cs.MFChatMsg 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)
}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"cs" "cs"
"github.com/golang/protobuf/proto"
"time" "time"
) )
@ -14,9 +15,9 @@ type ChatMgr struct {
guildMsgRec map[int64]*ChatMsgRec guildMsgRec map[int64]*ChatMsgRec
privateChatUsers map[string]*ChatUserRec privateChatUsers map[string]*ChatUserRec
WorldMsgId uint64 worldMsgId uint64
GuildMsgId uint64 guildMsgId uint64
TmpMsgId uint64 tmpMsgId uint64
} }
func NewChatMgr(pm *PlayerMgr, fm *FriendsMgr, gm *GuildMgr) *ChatMgr { 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), privateChatUsers: make(map[string]*ChatUserRec),
} }
// Default values // Default values
cm.WorldMsgId = 1000 cm.worldMsgId = 1000
cm.GuildMsgId = 1000 cm.guildMsgId = 1000
cm.TmpMsgId = 1000 cm.tmpMsgId = 1000
return cm return cm
} }
@ -92,11 +93,11 @@ func (cm *ChatMgr) FillSMUpdatePrivateChatRedPointNotify(p *Player, msg *cs.SMUp
func (cm *ChatMgr) ProcWorldChat(p *Player, msg *cs.CMSendChatMsg) { func (cm *ChatMgr) ProcWorldChat(p *Player, msg *cs.CMSendChatMsg) {
accountId := p.GetAccountId() accountId := p.GetAccountId()
chatMsg := new(cs.MFChatMsg) chatMsg := new(cs.MFChatMsg)
cm.WorldMsgId++ cm.worldMsgId++
cm.FillMFChatMsg(chatMsg, &accountId, cm.WorldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) cm.FillMFChatMsg(chatMsg, &accountId, cm.worldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
cm.worldMsgRec.CurrID = cm.WorldMsgId cm.worldMsgRec.CurrID = cm.worldMsgId
cm.worldMsgRec.ChatMsgList = append(cm.worldMsgRec.ChatMsgList, chatMsg) cm.worldMsgRec.AddChatMsg(chatMsg)
// TraversePlayer // TraversePlayer
for _, p2 := range cm.pm.GetPlayers() { for _, p2 := range cm.pm.GetPlayers() {
@ -110,16 +111,16 @@ func (cm *ChatMgr) ProcGuildChat(p *Player, msg *cs.CMSendChatMsg) {
return return
} }
guildId := guild.GuildId guildId := guild.GuildId
cm.GuildMsgId++ cm.guildMsgId++
chatMsg := new(cs.MFChatMsg) 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 { if msgRec, exists := cm.guildMsgRec[guildId]; exists {
msgRec.ChatMsgList = append(msgRec.ChatMsgList, chatMsg) msgRec.AddChatMsg(chatMsg)
} else { } else {
msgRec := &ChatMsgRec{} msgRec := &ChatMsgRec{}
msgRec.CurrID = cm.GuildMsgId msgRec.CurrID = cm.guildMsgId
cm.guildMsgRec[guildId] = msgRec 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) { func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMsg *cs.MFChatMsg, lastId uint64) {
chatMsgCopy := new(cs.MFChatMsg) chatMsgCopy := new(cs.MFChatMsg)
*chatMsgCopy = *chatMsg proto.Merge(chatMsgCopy, chatMsg)
chatMsgCopy.MsgUuid = &lastId chatMsgCopy.MsgUuid = &lastId
if _, exists := cm.privateChatUsers[senderAccountId]; !exists { 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 := cm.privateChatUsers[senderAccountId].Users[receiverAccountId]
receiverUserRec.LastID = lastId receiverUserRec.LastID = lastId
receiverUserRec.ChatMsgList = append(receiverUserRec.ChatMsgList, chatMsgCopy) receiverUserRec.AddChatMsg(chatMsgCopy)
// 队列 保持最近50条
} }
func (cm *ChatMgr) GetChatUser(accountId *string) *ChatUserRec { func (cm *ChatMgr) GetChatUser(accountId *string) *ChatUserRec {

View File

@ -3,7 +3,7 @@ package main
import ( import (
"cs" "cs"
"f5" "f5"
proto "github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"q5" "q5"
) )