This commit is contained in:
aozhiwei 2024-02-17 19:58:58 +08:00
parent c118e749c5
commit 7e2b19c64b
7 changed files with 70 additions and 57 deletions

View File

@ -6,6 +6,7 @@ import (
"q5" "q5"
"time" "time"
"main/constant" "main/constant"
. "main/global"
) )
func (cm *CacheMgr) LoadFromDB() { func (cm *CacheMgr) LoadFromDB() {
@ -100,7 +101,7 @@ func (cm *CacheMgr) loadUserProfile(accountId string) {
} }
for rows.Next() { for rows.Next() {
aId := q5.ToString(rows.GetByName("account_id")) aId := q5.ToString(rows.GetByName("account_id"))
onlineStatus := playerMgr.GetOnlineStatus(accountId) onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId)
profile := &PlayerProfile{ profile := &PlayerProfile{
AccountId: aId, AccountId: aId,
Username: q5.ToString(rows.GetByName("name")), Username: q5.ToString(rows.GetByName("name")),

View File

@ -13,8 +13,8 @@ type ChatUserRec struct {
} }
type ChatMsgRec struct { type ChatMsgRec struct {
CurrID uint64 CurrID int64
LastID uint64 LastID int64
ChatMsgList []*cs.MFChatMsg ChatMsgList []*cs.MFChatMsg
} }

View File

@ -5,10 +5,11 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"time" "time"
"main/constant" "main/constant"
"main/common"
. "main/global"
) )
type ChatMgr struct { type ChatMgr struct {
pm *PlayerMgr
fm *FriendsMgr fm *FriendsMgr
gm *GuildMgr gm *GuildMgr
@ -16,16 +17,14 @@ type ChatMgr struct {
guildMsgRec map[int64]*ChatMsgRec guildMsgRec map[int64]*ChatMsgRec
privateChatUsers map[string]*ChatUserRec privateChatUsers map[string]*ChatUserRec
worldMsgId uint64 worldMsgId int64
guildMsgId uint64 guildMsgId int64
teamMsgId uint64 teamMsgId int64
//tmpMsgId uint64 //tmpMsgId uint64
} }
func NewChatMgr(pm *PlayerMgr, fm *FriendsMgr, gm *GuildMgr) *ChatMgr { func NewChatMgr(fm *FriendsMgr, gm *GuildMgr) *ChatMgr {
cm := &ChatMgr{ cm := &ChatMgr{
pm: pm,
fm: fm,
gm: gm, gm: gm,
worldMsgRec: &ChatMsgRec{}, worldMsgRec: &ChatMsgRec{},
guildMsgRec: make(map[int64]*ChatMsgRec), guildMsgRec: make(map[int64]*ChatMsgRec),
@ -41,10 +40,10 @@ func NewChatMgr(pm *PlayerMgr, fm *FriendsMgr, gm *GuildMgr) *ChatMgr {
func (cm *ChatMgr) init() {} func (cm *ChatMgr) init() {}
func (cm *ChatMgr) FillMFChatMsg(msg *cs.MFChatMsg, sender *Player, func (cm *ChatMgr) FillMFChatMsg(msg *cs.MFChatMsg, sender common.Player,
msgAutoId uint64, chatChannel int32, msgType int32, msgBody *string) { msgAutoId int64, chatChannel int32, msgType int32, msgBody *string) {
msg.MsgUuid = &msgAutoId msg.MsgUuid = proto.Uint64(uint64(msgAutoId))
msg.Sender = sender.FillMFChatUser() msg.Sender = sender.FillMFChatUser()
msg.ChatChannel = &chatChannel msg.ChatChannel = &chatChannel
msg.MsgType = &msgType msg.MsgType = &msgType
@ -54,21 +53,21 @@ func (cm *ChatMgr) FillMFChatMsg(msg *cs.MFChatMsg, sender *Player,
msg.SendTime = &unixSec msg.SendTime = &unixSec
} }
func (cm *ChatMgr) FillSMUpdateChatRedPointNotify(p *Player, msg *cs.SMUpdateChatRedPointNotify) { func (cm *ChatMgr) FillSMUpdateChatRedPointNotify(p common.Player, msg *cs.SMUpdateChatRedPointNotify) {
// New messages flag // New messages flag
if cm.worldMsgRec.CurrID > p.worldChannelLastId { if cm.worldMsgRec.CurrID > p.GetWorldChannelLastId() {
msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCWorld) msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCWorld)
} }
guildId := cm.gm.GetGuildIdByAccountId(p.accountId) guildId := cm.gm.GetGuildIdByAccountId(p.GetAccountId())
if guildId > 0 { if guildId > 0 {
if msgRec, exists := cm.guildMsgRec[guildId]; exists { if msgRec, exists := cm.guildMsgRec[guildId]; exists {
if msgRec.CurrID > p.guildChannelLastId { if msgRec.CurrID > p.GetGuildChannelLastId() {
msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCGuild) msg.HasUnreadMsgChannels = append(msg.HasUnreadMsgChannels, constant.CCGuild)
} }
} }
} }
userRec := cm.GetChatUser(&p.accountId) userRec := cm.GetChatUser(p.GetAccountId())
if userRec != nil { if userRec != nil {
if userRec.Dirty { if userRec.Dirty {
userRec.HasUnreadMsg = false userRec.HasUnreadMsg = false
@ -80,8 +79,8 @@ func (cm *ChatMgr) FillSMUpdateChatRedPointNotify(p *Player, msg *cs.SMUpdateCha
} }
} }
func (cm *ChatMgr) FillSMUpdatePrivateChatRedPointNotify(p *Player, msg *cs.SMUpdatePrivateChatRedPointNotify) { func (cm *ChatMgr) FillSMUpdatePrivateChatRedPointNotify(p common.Player, msg *cs.SMUpdatePrivateChatRedPointNotify) {
userRec := cm.GetChatUser(&p.accountId) userRec := cm.GetChatUser(p.GetAccountId())
if userRec == nil { if userRec == nil {
return return
} }
@ -92,7 +91,7 @@ func (cm *ChatMgr) FillSMUpdatePrivateChatRedPointNotify(p *Player, msg *cs.SMUp
} }
} }
func (cm *ChatMgr) ProcWorldChat(p *Player, msg *cs.CMSendChatMsg) { func (cm *ChatMgr) ProcWorldChat(p common.Player, msg *cs.CMSendChatMsg) {
chatMsg := new(cs.MFChatMsg) chatMsg := new(cs.MFChatMsg)
cm.worldMsgId++ cm.worldMsgId++
cm.FillMFChatMsg(chatMsg, p, cm.worldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) cm.FillMFChatMsg(chatMsg, p, cm.worldMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
@ -101,13 +100,13 @@ func (cm *ChatMgr) ProcWorldChat(p *Player, msg *cs.CMSendChatMsg) {
cm.worldMsgRec.AddChatMsg(chatMsg) cm.worldMsgRec.AddChatMsg(chatMsg)
// TraversePlayer // TraversePlayer
for _, p2 := range cm.pm.GetPlayers() { for _, p2 := range GetPlayerMgr().GetPlayers() {
cm.SyncWorldChatMsg(p2) cm.SyncWorldChatMsg(p2)
} }
} }
func (cm *ChatMgr) ProcGuildChat(p *Player, msg *cs.CMSendChatMsg) { func (cm *ChatMgr) ProcGuildChat(p common.Player, msg *cs.CMSendChatMsg) {
guild := cm.gm.GetGuildByAccountId(p.accountId) guild := cm.gm.GetGuildByAccountId(p.GetAccountId())
if guild == nil { if guild == nil {
return return
} }
@ -128,14 +127,14 @@ func (cm *ChatMgr) ProcGuildChat(p *Player, msg *cs.CMSendChatMsg) {
// TraverseMember // TraverseMember
for _, member := range guild.Members { for _, member := range guild.Members {
guildMember := cm.pm.GetPlayerByAccountId(member.GetAccountId()) guildMember := GetPlayerMgr().GetPlayerByAccountId(member.GetAccountId())
if guildMember != nil { if guildMember != nil {
cm.SyncGuildChatMsg(guildMember) cm.SyncGuildChatMsg(guildMember)
} }
} }
} }
func (cm *ChatMgr) ProcTeamChat(p *Player, msg *cs.CMSendChatMsg) { func (cm *ChatMgr) ProcTeamChat(p common.Player, msg *cs.CMSendChatMsg) {
chatMsg := new(cs.MFChatMsg) chatMsg := new(cs.MFChatMsg)
cm.teamMsgId++ cm.teamMsgId++
cm.FillMFChatMsg(chatMsg, p, cm.teamMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) cm.FillMFChatMsg(chatMsg, p, cm.teamMsgId, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
@ -145,35 +144,35 @@ func (cm *ChatMgr) ProcTeamChat(p *Player, msg *cs.CMSendChatMsg) {
// Traverse msg members // Traverse msg members
for _, accountId := range msg.GetMembers() { for _, accountId := range msg.GetMembers() {
p2 := cm.pm.GetPlayerByAccountId(accountId) p2 := GetPlayerMgr().GetPlayerByAccountId(accountId)
if p2 != nil { if p2 != nil {
p2.SendMsg(notifyMsg) p2.SendMsg(notifyMsg)
if p2.chatChannel == constant.CCTeam { if p2.GetChatChannel() == constant.CCTeam {
p2.SyncPrivateChatRedPoint() p2.SyncPrivateChatRedPoint()
} }
} }
} }
} }
func (cm *ChatMgr) ProcPrivateChat(p *Player, msg *cs.CMSendChatMsg) { func (cm *ChatMgr) ProcPrivateChat(p common.Player, msg *cs.CMSendChatMsg) {
targetAccountId := msg.GetTargetAccountId() targetAccountId := msg.GetTargetAccountId()
if p.accountId == targetAccountId { if p.GetAccountId() == targetAccountId {
return return
} }
// 确定是否好友 // 确定是否好友
targetAccount := cm.fm.GetFriendByAccountId(p.accountId, targetAccountId) targetAccount := cm.fm.GetFriendByAccountId(p.GetAccountId(), targetAccountId)
if targetAccount == nil { if targetAccount == nil {
return return
} }
chatMsg := new(cs.MFChatMsg) chatMsg := new(cs.MFChatMsg)
cm.FillMFChatMsg(chatMsg, p, 0, *msg.ChatChannel, *msg.MsgType, msg.MsgBody) cm.FillMFChatMsg(chatMsg, p, 0, *msg.ChatChannel, *msg.MsgType, msg.MsgBody)
cm.AddChatUser(p.accountId, msg.GetTargetAccountId(), chatMsg, p.IncrPrivateChatLastId()) cm.AddChatUser(p.GetAccountId(), msg.GetTargetAccountId(), chatMsg, p.IncrPrivateChatLastId())
cm.SyncPrivateChatMsg(p) cm.SyncPrivateChatMsg(p)
// 聊天好友在线 // 聊天好友在线
targetPlayer := cm.pm.GetPlayerByAccountId(targetAccountId) targetPlayer := GetPlayerMgr().GetPlayerByAccountId(targetAccountId)
if targetPlayer != nil { if targetPlayer != nil {
cm.SyncPrivateChatMsg(targetPlayer) cm.SyncPrivateChatMsg(targetPlayer)
} else { } else {
@ -181,13 +180,13 @@ func (cm *ChatMgr) ProcPrivateChat(p *Player, msg *cs.CMSendChatMsg) {
} }
} }
func (cm *ChatMgr) SyncWorldChatMsg(p *Player) { func (cm *ChatMgr) SyncWorldChatMsg(p common.Player) {
if p.chatChannel == constant.CCWorld { if p.GetChatChannel() == constant.CCWorld {
notifyMsg := &cs.SMChatMsgNotify{} notifyMsg := &cs.SMChatMsgNotify{}
for _, chatMsg := range cm.worldMsgRec.ChatMsgList { for _, chatMsg := range cm.worldMsgRec.ChatMsgList {
if chatMsg.GetMsgUuid() > p.worldChannelLastId { if int64(chatMsg.GetMsgUuid()) > p.GetWorldChannelLastId() {
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg) notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
p.worldChannelLastId = chatMsg.GetMsgUuid() p.SetWorldChannelLastId(int64(chatMsg.GetMsgUuid()))
} }
} }
if len(notifyMsg.MsgList) > 0 { if len(notifyMsg.MsgList) > 0 {
@ -198,20 +197,20 @@ func (cm *ChatMgr) SyncWorldChatMsg(p *Player) {
p.MarkNewMsg() p.MarkNewMsg()
} }
func (cm *ChatMgr) SyncPrivateChatMsg(p *Player) { func (cm *ChatMgr) SyncPrivateChatMsg(p common.Player) {
if p.chatChannel == constant.CCPrivate { if p.GetChatChannel() == constant.CCPrivate {
chatUser := cm.GetChatUser(&p.accountId) chatUser := cm.GetChatUser(p.GetAccountId())
if chatUser == nil { if chatUser == nil {
return return
} }
notifyMsg := &cs.SMChatMsgNotify{} notifyMsg := &cs.SMChatMsgNotify{}
for accountId, chatMsgRec := range chatUser.Users { for accountId, chatMsgRec := range chatUser.Users {
if accountId == p.privateTargetAccountId { if accountId == p.GetPrivateTargetAccountId() {
for _, chatMsg := range chatMsgRec.ChatMsgList { for _, chatMsg := range chatMsgRec.ChatMsgList {
if *chatMsg.MsgUuid > chatMsgRec.CurrID { if int64(*chatMsg.MsgUuid) > chatMsgRec.CurrID {
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg) notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
chatMsgRec.CurrID = *chatMsg.MsgUuid chatMsgRec.CurrID = int64(*chatMsg.MsgUuid)
chatUser.Dirty = true chatUser.Dirty = true
} }
} }
@ -224,8 +223,8 @@ func (cm *ChatMgr) SyncPrivateChatMsg(p *Player) {
p.MarkNewMsg() p.MarkNewMsg()
} }
func (cm *ChatMgr) SyncGuildChatMsg(p *Player) { func (cm *ChatMgr) SyncGuildChatMsg(p common.Player) {
guildId := cm.gm.GetGuildIdByAccountId(p.accountId) guildId := cm.gm.GetGuildIdByAccountId(p.GetAccountId())
if guildId <= 0 { if guildId <= 0 {
return return
} }
@ -234,12 +233,12 @@ func (cm *ChatMgr) SyncGuildChatMsg(p *Player) {
return return
} }
if p.chatChannel == constant.CCGuild { if p.GetChatChannel() == constant.CCGuild {
notifyMsg := &cs.SMChatMsgNotify{} notifyMsg := &cs.SMChatMsgNotify{}
for _, chatMsg := range msgRec.ChatMsgList { for _, chatMsg := range msgRec.ChatMsgList {
if chatMsg.GetMsgUuid() > p.guildChannelLastId { if int64(chatMsg.GetMsgUuid()) > p.GetGuildChannelLastId() {
notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg) notifyMsg.MsgList = append(notifyMsg.MsgList, chatMsg)
p.guildChannelLastId = chatMsg.GetMsgUuid() p.SetGuildChannelLastId(int64(chatMsg.GetMsgUuid()))
} }
} }
if len(notifyMsg.MsgList) > 0 { if len(notifyMsg.MsgList) > 0 {
@ -251,10 +250,10 @@ func (cm *ChatMgr) SyncGuildChatMsg(p *Player) {
// player.SyncRedPoint() // player.SyncRedPoint()
} }
func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMsg *cs.MFChatMsg, lastId uint64) { func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMsg *cs.MFChatMsg, lastId int64) {
chatMsgCopy := new(cs.MFChatMsg) chatMsgCopy := new(cs.MFChatMsg)
proto.Merge(chatMsgCopy, chatMsg) proto.Merge(chatMsgCopy, chatMsg)
chatMsgCopy.MsgUuid = &lastId chatMsgCopy.MsgUuid = proto.Uint64(uint64(lastId))
if _, exists := cm.privateChatUsers[senderAccountId]; !exists { if _, exists := cm.privateChatUsers[senderAccountId]; !exists {
cm.privateChatUsers[senderAccountId] = &ChatUserRec{} cm.privateChatUsers[senderAccountId] = &ChatUserRec{}
@ -273,8 +272,8 @@ func (cm *ChatMgr) AddChatUser(senderAccountId, receiverAccountId string, chatMs
receiverUserRec.AddChatMsg(chatMsgCopy) receiverUserRec.AddChatMsg(chatMsgCopy)
} }
func (cm *ChatMgr) GetChatUser(accountId *string) *ChatUserRec { func (cm *ChatMgr) GetChatUser(accountId string) *ChatUserRec {
if userRec, exists := cm.privateChatUsers[*accountId]; exists { if userRec, exists := cm.privateChatUsers[accountId]; exists {
return userRec return userRec
} }
return nil return nil

View File

@ -29,11 +29,23 @@ type Player interface {
GetPing() int32 GetPing() int32
SendMsg(proto.Message) SendMsg(proto.Message)
IsOnline() bool IsOnline() bool
FillMFChatUser() *cs.MFChatUser
GetWorldChannelLastId() int64
GetGuildChannelLastId() int64
GetChatChannel() int32
SyncPrivateChatRedPoint()
IncrPrivateChatLastId() int64
SetWorldChannelLastId(int64)
SetGuildChannelLastId(int64)
MarkNewMsg()
GetPrivateTargetAccountId() string
} }
type PlayerMgr interface { type PlayerMgr interface {
ProcessCMMsg(*cs.CsNetMsgHandler, *f5.MsgHdr) ProcessCMMsg(*cs.CsNetMsgHandler, *f5.MsgHdr)
GetPlayerBySocket(f5.WspCliConn) Player GetPlayerBySocket(f5.WspCliConn) Player
GetPlayerByAccountId(string) Player GetPlayerByAccountId(string) Player
GetOnlineStatus(string) int32
OnSocketClose(f5.WspCliConn) OnSocketClose(f5.WspCliConn)
GetPlayers() map[string]Player
} }

View File

@ -1,10 +1,9 @@
package main package main
var playerMgr = new(PlayerMgr)
var handlerMgr = new(HandlerMgr) var handlerMgr = new(HandlerMgr)
var friendMgr = new(FriendsMgr) var friendMgr = new(FriendsMgr)
var cacheMgr = new(CacheMgr) var cacheMgr = new(CacheMgr)
// var guildMgr = new(GuildMgr) // var guildMgr = new(GuildMgr)
var guildMgr = NewGuildMgr() var guildMgr = NewGuildMgr()
var chatMgr = NewChatMgr(playerMgr, friendMgr, guildMgr) var chatMgr = NewChatMgr(friendMgr, guildMgr)

View File

@ -6,6 +6,7 @@ import (
"q5" "q5"
"strings" "strings"
"main/constant" "main/constant"
. "main/global"
) )
func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isFriendship int, requestTime int64, cb func(error)) { func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isFriendship int, requestTime int64, cb func(error)) {
@ -112,7 +113,7 @@ func (fm *FriendsMgr) findUsersByUsername(username string, sinceId int64, cb fun
lastId = autoId lastId = autoId
} }
accountId := q5.ToString(rows.GetByIndex(1)) accountId := q5.ToString(rows.GetByIndex(1))
onlineStatus := playerMgr.GetOnlineStatus(accountId) onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId)
profile := &PlayerProfile{ profile := &PlayerProfile{
AccountId: q5.ToString(rows.GetByIndex(1)), AccountId: q5.ToString(rows.GetByIndex(1)),
Username: q5.ToString(rows.GetByIndex(2)), Username: q5.ToString(rows.GetByIndex(2)),

View File

@ -11,6 +11,7 @@ import (
"q5" "q5"
"time" "time"
"main/constant" "main/constant"
"main/common"
) )
const ( const (
@ -64,7 +65,7 @@ func (gm *GuildMgr) isNameTooLong(name string, maxNum int) bool {
} }
// CreateGuild 创建公会 // CreateGuild 创建公会
func (gm *GuildMgr) CreateGuild(p *Player, avatar int32, guildName string, leaderId string, func (gm *GuildMgr) CreateGuild(p common.Player, avatar int32, guildName string, leaderId string,
cb func(errCode int32, errMsg string, guild *Guild)) { cb func(errCode int32, errMsg string, guild *Guild)) {
if len(guildName) <= 0 { if len(guildName) <= 0 {
cb(constant.ERR_CODE_REQUEST_PARAMS_ERROR, "params is null", nil) cb(constant.ERR_CODE_REQUEST_PARAMS_ERROR, "params is null", nil)
@ -752,7 +753,7 @@ func (gm *GuildMgr) SetAvatar(operatorAccountId string, avatar int32, cb func(er
}) })
} }
func (gm *GuildMgr) SetName(player *Player, name string, itemId, itemNum int32, cb func(errCode int32, errMsg string)) { func (gm *GuildMgr) SetName(player common.Player, name string, itemId, itemNum int32, cb func(errCode int32, errMsg string)) {
guild := gm.GetGuildByPermission(player.GetAccountId()) guild := gm.GetGuildByPermission(player.GetAccountId())
if guild == nil || len(name) <= 0 { if guild == nil || len(name) <= 0 {
cb(constant.ERR_CODE_REQUEST_PARAMS_ERROR, "params is null") cb(constant.ERR_CODE_REQUEST_PARAMS_ERROR, "params is null")