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

View File

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

View File

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

View File

@ -29,11 +29,23 @@ type Player interface {
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 {
ProcessCMMsg(*cs.CsNetMsgHandler, *f5.MsgHdr)
GetPlayerBySocket(f5.WspCliConn) Player
GetPlayerByAccountId(string) Player
GetOnlineStatus(string) int32
OnSocketClose(f5.WspCliConn)
GetPlayers() map[string]Player
}

View File

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

View File

@ -6,6 +6,7 @@ import (
"q5"
"strings"
"main/constant"
. "main/global"
)
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
}
accountId := q5.ToString(rows.GetByIndex(1))
onlineStatus := playerMgr.GetOnlineStatus(accountId)
onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId)
profile := &PlayerProfile{
AccountId: q5.ToString(rows.GetByIndex(1)),
Username: q5.ToString(rows.GetByIndex(2)),

View File

@ -11,6 +11,7 @@ import (
"q5"
"time"
"main/constant"
"main/common"
)
const (
@ -64,7 +65,7 @@ func (gm *GuildMgr) isNameTooLong(name string, maxNum int) bool {
}
// 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)) {
if len(guildName) <= 0 {
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())
if guild == nil || len(name) <= 0 {
cb(constant.ERR_CODE_REQUEST_PARAMS_ERROR, "params is null")