aozhiwei 0354503367 1
2024-02-17 20:16:38 +08:00

294 lines
7.7 KiB
Go

package main
import (
"cs"
"github.com/golang/protobuf/proto"
"time"
"main/constant"
"main/common"
. "main/global"
)
type ChatMgr struct {
worldMsgRec *ChatMsgRec
guildMsgRec map[int64]*ChatMsgRec
privateChatUsers map[string]*ChatUserRec
worldMsgId int64
guildMsgId int64
teamMsgId int64
//tmpMsgId uint64
}
func NewChatMgr() *ChatMgr {
cm := &ChatMgr{
worldMsgRec: &ChatMsgRec{},
guildMsgRec: make(map[int64]*ChatMsgRec),
privateChatUsers: make(map[string]*ChatUserRec),
}
// Default values
cm.worldMsgId = 1000
cm.guildMsgId = 1000
cm.teamMsgId = 1000
return cm
}
func (cm *ChatMgr) init() {}
func (cm *ChatMgr) FillMFChatMsg(msg *cs.MFChatMsg, sender common.Player,
msgAutoId int64, chatChannel int32, msgType int32, msgBody *string) {
msg.MsgUuid = proto.Uint64(uint64(msgAutoId))
msg.Sender = sender.FillMFChatUser()
msg.ChatChannel = &chatChannel
msg.MsgType = &msgType
msg.MsgBody = msgBody
unixSec := time.Now().Unix()
msg.SendTime = &unixSec
}
func (cm *ChatMgr) FillSMUpdateChatRedPointNotify(p common.Player, msg *cs.SMUpdateChatRedPointNotify) {
// New messages flag
if cm.worldMsgRec.CurrID > p.GetWorldChannelLastId() {
msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCWorld)
}
guildId := GetGuildMgr().GetGuildIdByAccountId(p.GetAccountId())
if guildId > 0 {
if msgRec, exists := cm.guildMsgRec[guildId]; exists {
if msgRec.CurrID > p.GetGuildChannelLastId() {
msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCGuild)
}
}
}
userRec := cm.GetChatUser(p.GetAccountId())
if userRec != nil {
if userRec.Dirty {
userRec.HasUnreadMsg = false
userRec.Dirty = false
}
if userRec.HasUnreadMsg {
msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCPrivate)
}
}
}
func (cm *ChatMgr) FillSMUpdatePrivateChatRedPointNotify(p common.Player, msg *cs.SMUpdatePrivateChatRedPointNotify) {
userRec := cm.GetChatUser(p.GetAccountId())
if userRec == nil {
return
}
for accountId, msgRec := range userRec.Users {
if msgRec.CurrID < msgRec.LastID {
msg.HasUnreadMsgAccounts = append(msg.HasUnreadMsgAccounts, accountId)
}
}
}
func (cm *ChatMgr) ProcWorldChat(p common.Player, msg *cs.CMSendChatMsg) {
chatMsg := new(cs.MFChatMsg)
cm.worldMsgId++
cm.FillMFChatMsg(chatMsg, p, cm.worldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
cm.worldMsgRec.CurrID = cm.worldMsgId
cm.worldMsgRec.AddChatMsg(chatMsg)
// TraversePlayer
for _, p2 := range GetPlayerMgr().GetPlayers() {
cm.SyncWorldChatMsg(p2)
}
}
func (cm *ChatMgr) ProcGuildChat(p common.Player, msg *cs.CMSendChatMsg) {
guild := GetGuildMgr().GetGuildByAccountId(p.GetAccountId())
if guild == nil {
return
}
guildId := guild.GetGuildId()
cm.guildMsgId++
chatMsg := new(cs.MFChatMsg)
cm.FillMFChatMsg(chatMsg, p, cm.guildMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
if msgRec, exists := cm.guildMsgRec[guildId]; exists {
msgRec.AddChatMsg(chatMsg)
} else {
newMsgRec := &ChatMsgRec{}
newMsgRec.CurrID = cm.guildMsgId
newMsgRec.AddChatMsg(chatMsg)
cm.guildMsgRec[guildId] = newMsgRec
}
// TraverseMember
for _, member := range guild.GetMembers() {
guildMember := GetPlayerMgr().GetPlayerByAccountId(member.GetAccountId())
if guildMember != nil {
cm.SyncGuildChatMsg(guildMember)
}
}
}
func (cm *ChatMgr) ProcTeamChat(p common.Player, msg *cs.CMSendChatMsg) {
chatMsg := new(cs.MFChatMsg)
cm.teamMsgId++
cm.FillMFChatMsg(chatMsg, p, cm.teamMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
notifyMsg := &cs.SMChatMsgNotify{}
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
// Traverse msg members
for _, accountId := range msg.GetMembers() {
p2 := GetPlayerMgr().GetPlayerByAccountId(accountId)
if p2 != nil {
p2.SendMsg(notifyMsg)
if p2.GetChatChannel() == constant.CCTeam {
p2.SyncPrivateChatRedPoint()
}
}
}
}
func (cm *ChatMgr) ProcPrivateChat(p common.Player, msg *cs.CMSendChatMsg) {
targetAccountId := msg.GetTargetAccountId()
if p.GetAccountId() == targetAccountId {
return
}
// 确定是否好友
targetAccount := GetFriendMgr().GetFriendByAccountId(p.GetAccountId(), targetAccountId)
if targetAccount == nil {
return
}
chatMsg := new(cs.MFChatMsg)
cm.FillMFChatMsg(chatMsg, p, 0, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
cm.AddChatUser(p.GetAccountId(), msg.GetTargetAccountId(), chatMsg, p.IncrPrivateChatLastId())
cm.SyncPrivateChatMsg(p)
// 聊天好友在线
targetPlayer := GetPlayerMgr().GetPlayerByAccountId(targetAccountId)
if targetPlayer != nil {
cm.SyncPrivateChatMsg(targetPlayer)
} else {
cm.AddChatUser(targetAccountId, p.GetAccountId(), chatMsg, 0)
}
}
func (cm *ChatMgr) SyncWorldChatMsg(p common.Player) {
if p.GetChatChannel() == constant.CCWorld {
notifyMsg := &cs.SMChatMsgNotify{}
for _, chatMsg := range cm.worldMsgRec.ChatMsgList {
if int64(chatMsg.GetMsgUuid()) > p.GetWorldChannelLastId() {
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
p.SetWorldChannelLastId(int64(chatMsg.GetMsgUuid()))
}
}
if len(notifyMsg.MsgList) > 0 {
p.SendMsg(notifyMsg)
}
return
}
p.MarkNewMsg()
}
func (cm *ChatMgr) SyncPrivateChatMsg(p common.Player) {
if p.GetChatChannel() == constant.CCPrivate {
chatUser := cm.GetChatUser(p.GetAccountId())
if chatUser == nil {
return
}
notifyMsg := &cs.SMChatMsgNotify{}
for accountId, chatMsgRec := range chatUser.Users {
if accountId == p.GetPrivateTargetAccountId() {
for _, chatMsg := range chatMsgRec.ChatMsgList {
if int64(*chatMsg.MsgUuid) > chatMsgRec.CurrID {
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
chatMsgRec.CurrID = int64(*chatMsg.MsgUuid)
chatUser.Dirty = true
}
}
}
}
if len(notifyMsg.MsgList) > 0 {
p.SendMsg(notifyMsg)
}
}
p.MarkNewMsg()
}
func (cm *ChatMgr) SyncGuildChatMsg(p common.Player) {
guildId := GetGuildMgr().GetGuildIdByAccountId(p.GetAccountId())
if guildId <= 0 {
return
}
msgRec, exists := cm.guildMsgRec[guildId]
if !exists {
return
}
if p.GetChatChannel() == constant.CCGuild {
notifyMsg := &cs.SMChatMsgNotify{}
for _, chatMsg := range msgRec.ChatMsgList {
if int64(chatMsg.GetMsgUuid()) > p.GetGuildChannelLastId() {
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
p.SetGuildChannelLastId(int64(chatMsg.GetMsgUuid()))
}
}
if len(notifyMsg.MsgList) > 0 {
p.SendMsg(notifyMsg)
}
return
}
p.MarkNewMsg()
// player.SyncRedPoint()
}
func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMsg *cs.MFChatMsg, lastId int64) {
chatMsgCopy := new(cs.MFChatMsg)
proto.Merge(chatMsgCopy, chatMsg)
chatMsgCopy.MsgUuid = proto.Uint64(uint64(lastId))
if _, exists := cm.privateChatUsers[senderAccountId]; !exists {
cm.privateChatUsers[senderAccountId] = &ChatUserRec{}
}
userRec := cm.privateChatUsers[senderAccountId]
userRec.Dirty = true
userRec.HasUnreadMsg = true
if _, exists := cm.privateChatUsers[senderAccountId].Users[receiverAccountId]; !exists {
cm.privateChatUsers[senderAccountId].Users[receiverAccountId] = &ChatMsgRec{}
}
receiverUserRec := cm.privateChatUsers[senderAccountId].Users[receiverAccountId]
receiverUserRec.LastID = lastId
receiverUserRec.AddChatMsg(chatMsgCopy)
}
func (cm *ChatMgr) GetChatUser(accountId string) *ChatUserRec {
if userRec, exists := cm.privateChatUsers[accountId]; exists {
return userRec
}
return nil
}
func (cm *ChatMgr) RemoveChatUser(accountId *string) {
delete(cm.privateChatUsers, *accountId)
}
func (cm *ChatMgr) markMessagesAsRead(sender, receiver string) {
userRec, exists := cm.privateChatUsers[receiver]
if !exists {
return
}
msgRec, exists := userRec.Users[sender]
if !exists {
return
}
msgRec.LastID = msgRec.CurrID
}