This commit is contained in:
aozhiwei 2024-03-22 17:29:56 +08:00
parent dc001de7e1
commit 254aa958b1
5 changed files with 10 additions and 382 deletions

View File

@ -18,43 +18,15 @@ func (this *CacheMgr) UnInit() {
}
func (this *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
var wg sync.WaitGroup
var mu sync.Mutex
successCount := 0
for _, accountId := range accountIds {
wg.Add(1)
go func(accountId string) {
defer wg.Done()
this.lock.Lock()
/*
cp, exists := this.cachePlayerProfiles[accountId]
this.cacheMutex.Unlock()
if exists && cp.Version == this.getCacheVersion() {
mu.Lock()
successCount++
mu.Unlock()
return
}
this.loadUserProfile(accountId)
*/
mu.Lock()
successCount++
mu.Unlock()
}(accountId)
}
wg.Wait()
if successCount == len(accountIds) {
cb(true)
} else {
cb(false)
}
}
func (this *CacheMgr) GetPlayerProfile(accountId string) common.UserProfile {
func (this *CacheMgr) GetUserProfile(accountId string) common.UserProfile {
if user, ok := this.userHash[accountId]; ok {
return user
}
return nil
}
func (this *CacheMgr) PreLoad(accountIds []string) {
}

View File

@ -1,26 +0,0 @@
package chat
import (
"cs"
)
const ChatMsgMaxSize = 60
type ChatUserRec struct {
HasUnreadMsg bool
Dirty bool
Users map[string]*ChatMsgRec
}
type ChatMsgRec struct {
CurrID int64
LastID int64
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

@ -1,293 +1,19 @@
package chat
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
return new(ChatMgr)
}
func (cm *ChatMgr) init() {}
func (this *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)
}
}
}
func (this *ChatMgr) UnInit() {
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
}

View File

@ -43,23 +43,8 @@ type App interface {
type Player interface {
GetAccountId() string
GetSessionId() string
GetName() string
GetAvatarUrl() string
GetHeroId() string
GetHeadFrame() string
GetPing() int32
SendMsg(proto.Message)
IsOnline() bool
FillMFChatUser() *cs.MFChatUser
GetWorldChannelLastId() int64
GetGuildChannelLastId() int64
GetChatChannel() int32
SyncPrivateChatRedPoint()
IncrPrivateChatLastId() int64
SetWorldChannelLastId(int64)
SetGuildChannelLastId(int64)
MarkNewMsg()
GetPrivateTargetAccountId() string
}
type PlayerMgr interface {
@ -111,21 +96,12 @@ type GuildMgr interface {
}
type CacheMgr interface {
GetPlayerProfile(accountId string) UserProfile
LoadUserProfile(string)
AsyncGetUsers(accountIds []string, cb func(bool))
PreLoadUser([]string)
AsyncGetUsers([]string, func(bool))
GetUserProfile(string) UserProfile
}
type ChatMgr interface {
ProcWorldChat(p Player, msg *cs.CMSendChatMsg)
ProcGuildChat(p Player, msg *cs.CMSendChatMsg)
ProcTeamChat(p Player, msg *cs.CMSendChatMsg)
ProcPrivateChat(p Player, msg *cs.CMSendChatMsg)
FillSMUpdateChatRedPointNotify(p Player, msg *cs.SMUpdateChatRedPointNotify)
FillSMUpdatePrivateChatRedPointNotify(p Player, msg *cs.SMUpdatePrivateChatRedPointNotify)
SyncWorldChatMsg(p Player)
SyncGuildChatMsg(p Player)
SyncPrivateChatMsg(p Player)
}
type UserProfile interface {

View File

@ -35,26 +35,6 @@ func (this *player) GetSessionId() string {
return this.sessionId
}
func (this *player) GetAvatarUrl() string {
return ""
}
func (this *player) GetName() string {
return ""
}
func (this *player) GetHeroId() string {
return ""
}
func (this *player) GetPing() int32 {
return 0
}
func (this *player) GetHeadFrame() string {
return ""
}
func (this *player) GetPrivateTargetAccountId() string {
return ""
}