优化&&判空
This commit is contained in:
1 parent
1d8aff9554
commit
b99549622e
4 files changed
+192
-185
No files matched your search
@@ -1,33 +1,23 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hk4e/common/mq"
|
||||
"hk4e/gdconf"
|
||||
"hk4e/gs/model"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
// 本地事件队列管理器
|
||||
|
||||
const (
|
||||
LoadLoginUserFromDbFinish = iota // 玩家登录从数据库加载完成回调
|
||||
UserLoginLoadFromDbFinish = iota // 玩家登录从数据库加载完成回调
|
||||
UserOfflineSaveToDbFinish // 玩家离线保存完成
|
||||
RunUserCopyAndSave // 执行一次在线玩家内存数据复制到数据库写入协程
|
||||
ExitRunUserCopyAndSave // 停服时执行全部玩家保存操作
|
||||
UserOfflineSaveToDbFinish // 玩家离线保存完成
|
||||
ReloadGameDataConfig // 执行热更表
|
||||
ReloadGameDataConfigFinish // 热更表完成
|
||||
)
|
||||
|
||||
const (
|
||||
UserCopyGoroutineLimit = 4
|
||||
)
|
||||
|
||||
type LocalEvent struct {
|
||||
EventId int
|
||||
Msg any
|
||||
@@ -47,144 +37,21 @@ func (l *LocalEventManager) GetLocalEventChan() chan *LocalEvent {
|
||||
return l.localEventChan
|
||||
}
|
||||
|
||||
type PlayerLastSaveTimeSortList []*model.Player
|
||||
|
||||
func (p PlayerLastSaveTimeSortList) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
func (p PlayerLastSaveTimeSortList) Less(i, j int) bool {
|
||||
return p[i].LastSaveTime < p[j].LastSaveTime
|
||||
}
|
||||
|
||||
func (p PlayerLastSaveTimeSortList) Swap(i, j int) {
|
||||
p[i], p[j] = p[j], p[i]
|
||||
}
|
||||
|
||||
func (l *LocalEventManager) LocalEventHandle(localEvent *LocalEvent) {
|
||||
switch localEvent.EventId {
|
||||
case LoadLoginUserFromDbFinish:
|
||||
case UserLoginLoadFromDbFinish:
|
||||
playerLoginInfo := localEvent.Msg.(*PlayerLoginInfo)
|
||||
GAME.OnLogin(playerLoginInfo.UserId, playerLoginInfo.ClientSeq, playerLoginInfo.GateAppId, playerLoginInfo.Player, playerLoginInfo.Req, playerLoginInfo.Ok)
|
||||
case ExitRunUserCopyAndSave:
|
||||
fallthrough
|
||||
case RunUserCopyAndSave:
|
||||
startTime := time.Now().UnixNano()
|
||||
playerList := make(PlayerLastSaveTimeSortList, 0)
|
||||
for _, player := range USER_MANAGER.GetAllOnlineUserList() {
|
||||
if player.PlayerId < PlayerBaseUid {
|
||||
continue
|
||||
}
|
||||
playerList = append(playerList, player)
|
||||
}
|
||||
sort.Stable(playerList)
|
||||
// 拷贝一份数据避免并发访问
|
||||
insertPlayerList := make([][]byte, 0)
|
||||
updatePlayerList := make([][]byte, 0)
|
||||
saveCount := 0
|
||||
times := len(playerList) / UserCopyGoroutineLimit
|
||||
if times == 0 && len(playerList) > 0 {
|
||||
times = 1
|
||||
}
|
||||
for index := 0; index < times; index++ {
|
||||
totalCostTime := time.Now().UnixNano() - startTime
|
||||
if totalCostTime > time.Millisecond.Nanoseconds()*10 {
|
||||
// 总耗时超过10ms就中止本轮保存
|
||||
logger.Info("user copy loop overtime exit, total cost time: %v ns", totalCostTime)
|
||||
break
|
||||
}
|
||||
// 分批次并发序列化玩家数据
|
||||
oncePlayerListEndIndex := 0
|
||||
if index < times-1 {
|
||||
oncePlayerListEndIndex = (index + 1) * UserCopyGoroutineLimit
|
||||
} else {
|
||||
oncePlayerListEndIndex = len(playerList)
|
||||
}
|
||||
oncePlayerList := playerList[index*UserCopyGoroutineLimit : oncePlayerListEndIndex]
|
||||
var playerDataMapLock sync.Mutex
|
||||
playerDataMap := make(map[uint32][]byte)
|
||||
var wg sync.WaitGroup
|
||||
for _, player := range oncePlayerList {
|
||||
wg.Add(1)
|
||||
go func(player *model.Player) {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
}()
|
||||
playerData, err := msgpack.Marshal(player)
|
||||
if err != nil {
|
||||
logger.Error("marshal player data error: %v", err)
|
||||
return
|
||||
}
|
||||
playerDataMapLock.Lock()
|
||||
playerDataMap[player.PlayerId] = playerData
|
||||
playerDataMapLock.Unlock()
|
||||
}(player)
|
||||
}
|
||||
wg.Wait()
|
||||
for _, player := range oncePlayerList {
|
||||
playerData, exist := playerDataMap[player.PlayerId]
|
||||
if !exist {
|
||||
continue
|
||||
}
|
||||
switch player.DbState {
|
||||
case model.DbNone:
|
||||
break
|
||||
case model.DbInsert:
|
||||
insertPlayerList = append(insertPlayerList, playerData)
|
||||
player.DbState = model.DbNormal
|
||||
player.LastSaveTime = uint32(time.Now().UnixMilli())
|
||||
saveCount++
|
||||
case model.DbDelete:
|
||||
USER_MANAGER.DeleteUser(player.PlayerId)
|
||||
case model.DbNormal:
|
||||
updatePlayerList = append(updatePlayerList, playerData)
|
||||
player.LastSaveTime = uint32(time.Now().UnixMilli())
|
||||
saveCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
saveUserData := &SaveUserData{
|
||||
insertPlayerList: insertPlayerList,
|
||||
updatePlayerList: updatePlayerList,
|
||||
exitSave: false,
|
||||
}
|
||||
if localEvent.EventId == ExitRunUserCopyAndSave {
|
||||
saveUserData.exitSave = true
|
||||
}
|
||||
USER_MANAGER.GetSaveUserChan() <- saveUserData
|
||||
endTime := time.Now().UnixNano()
|
||||
costTime := endTime - startTime
|
||||
logger.Info("run save user copy cost time: %v ns, save user count: %v", costTime, saveCount)
|
||||
if localEvent.EventId == ExitRunUserCopyAndSave {
|
||||
// 在此阻塞掉主协程 不再进行任何消息和任务的处理
|
||||
logger.Warn("game main loop block")
|
||||
select {}
|
||||
}
|
||||
case UserOfflineSaveToDbFinish:
|
||||
playerOfflineInfo := localEvent.Msg.(*PlayerOfflineInfo)
|
||||
USER_MANAGER.DeleteUser(playerOfflineInfo.Player.PlayerId)
|
||||
MESSAGE_QUEUE.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerUserOnlineStateChangeNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
UserId: playerOfflineInfo.Player.PlayerId,
|
||||
IsOnline: false,
|
||||
},
|
||||
})
|
||||
if playerOfflineInfo.ChangeGsInfo.IsChangeGs {
|
||||
gsAppId := USER_MANAGER.GetRemoteUserGsAppId(playerOfflineInfo.ChangeGsInfo.JoinHostUserId)
|
||||
MESSAGE_QUEUE.SendToGate(playerOfflineInfo.Player.GateAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerUserGsChangeNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
UserId: playerOfflineInfo.Player.PlayerId,
|
||||
GameServerAppId: gsAppId,
|
||||
JoinHostUserId: playerOfflineInfo.ChangeGsInfo.JoinHostUserId,
|
||||
},
|
||||
})
|
||||
logger.Info("user change gs notify to gate, uid: %v, gate appid: %v, gs appid: %v, host uid: %v",
|
||||
playerOfflineInfo.Player.PlayerId, playerOfflineInfo.Player.GateAppId, gsAppId, playerOfflineInfo.ChangeGsInfo.JoinHostUserId)
|
||||
}
|
||||
USER_MANAGER.OfflineUser(playerOfflineInfo.Player, playerOfflineInfo.ChangeGsInfo)
|
||||
case RunUserCopyAndSave:
|
||||
USER_MANAGER.UserCopyAndSave(false)
|
||||
case ExitRunUserCopyAndSave:
|
||||
USER_MANAGER.UserCopyAndSave(true)
|
||||
// 在此阻塞掉主协程 不再进行任何消息和任务的处理
|
||||
logger.Warn("game main loop block")
|
||||
select {}
|
||||
case ReloadGameDataConfig:
|
||||
reloadSceneLua := localEvent.Msg.(bool)
|
||||
go func() {
|
||||
|
||||
@@ -2,9 +2,12 @@ package game
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"hk4e/common/mq"
|
||||
"hk4e/gs/dao"
|
||||
"hk4e/gs/model"
|
||||
"hk4e/pkg/logger"
|
||||
@@ -99,8 +102,8 @@ type PlayerLoginInfo struct {
|
||||
Ok bool
|
||||
}
|
||||
|
||||
// OnlineUser 玩家上线
|
||||
func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32, gateAppId string, req *proto.PlayerLoginReq) {
|
||||
// UserLoginLoad 玩家登录数据库异步加载
|
||||
func (u *UserManager) UserLoginLoad(userId uint32, clientSeq uint32, gateAppId string, req *proto.PlayerLoginReq) {
|
||||
_, exist := u.playerMap[userId]
|
||||
// 正常登录
|
||||
if exist {
|
||||
@@ -113,7 +116,7 @@ func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32, gateAppId stri
|
||||
if !ok {
|
||||
logger.Error("lock redis offline player data error, uid: %v", userId)
|
||||
LOCAL_EVENT_MANAGER.GetLocalEventChan() <- &LocalEvent{
|
||||
EventId: LoadLoginUserFromDbFinish,
|
||||
EventId: UserLoginLoadFromDbFinish,
|
||||
Msg: &PlayerLoginInfo{
|
||||
UserId: userId,
|
||||
ClientSeq: clientSeq,
|
||||
@@ -127,7 +130,7 @@ func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32, gateAppId stri
|
||||
if err != nil {
|
||||
logger.Error("can not load user from db, uid: %v", userId)
|
||||
LOCAL_EVENT_MANAGER.GetLocalEventChan() <- &LocalEvent{
|
||||
EventId: LoadLoginUserFromDbFinish,
|
||||
EventId: UserLoginLoadFromDbFinish,
|
||||
Msg: &PlayerLoginInfo{
|
||||
UserId: userId,
|
||||
ClientSeq: clientSeq,
|
||||
@@ -143,11 +146,9 @@ func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32, gateAppId stri
|
||||
u.SaveUserToRedisSync(player)
|
||||
u.ChangeUserDbState(player, model.DbNormal)
|
||||
player.ChatMsgMap = u.LoadUserChatMsgFromDbSync(userId)
|
||||
} else {
|
||||
logger.Info("reg new player, uid: %v", userId)
|
||||
}
|
||||
LOCAL_EVENT_MANAGER.GetLocalEventChan() <- &LocalEvent{
|
||||
EventId: LoadLoginUserFromDbFinish,
|
||||
EventId: UserLoginLoadFromDbFinish,
|
||||
Msg: &PlayerLoginInfo{
|
||||
UserId: userId,
|
||||
Player: player,
|
||||
@@ -162,6 +163,22 @@ func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32, gateAppId stri
|
||||
}()
|
||||
}
|
||||
|
||||
// OnlineUser 玩家上线
|
||||
func (u *UserManager) OnlineUser(player *model.Player) {
|
||||
player.Online = true
|
||||
player.OnlineTime = uint32(time.Now().UnixMilli())
|
||||
USER_MANAGER.AddUser(player)
|
||||
MESSAGE_QUEUE.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerUserOnlineStateChangeNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
UserId: player.PlayerId,
|
||||
IsOnline: true,
|
||||
},
|
||||
})
|
||||
atomic.AddInt32(&ONLINE_PLAYER_NUM, 1)
|
||||
}
|
||||
|
||||
type ChangeGsInfo struct {
|
||||
IsChangeGs bool
|
||||
JoinHostUserId uint32
|
||||
@@ -172,8 +189,11 @@ type PlayerOfflineInfo struct {
|
||||
ChangeGsInfo *ChangeGsInfo
|
||||
}
|
||||
|
||||
// OfflineUser 玩家离线
|
||||
func (u *UserManager) OfflineUser(player *model.Player, changeGsInfo *ChangeGsInfo) {
|
||||
// UserOfflineSave 玩家离线数据库保存
|
||||
func (u *UserManager) UserOfflineSave(player *model.Player, changeGsInfo *ChangeGsInfo) {
|
||||
player.Online = false
|
||||
player.OfflineTime = uint32(time.Now().Unix())
|
||||
player.TotalOnlineTime += uint32(time.Now().UnixMilli()) - player.OnlineTime
|
||||
if player.OfflineNotSave {
|
||||
LOCAL_EVENT_MANAGER.GetLocalEventChan() <- &LocalEvent{
|
||||
EventId: UserOfflineSaveToDbFinish,
|
||||
@@ -218,6 +238,34 @@ func (u *UserManager) OfflineUser(player *model.Player, changeGsInfo *ChangeGsIn
|
||||
}()
|
||||
}
|
||||
|
||||
// OfflineUser 玩家离线
|
||||
func (u *UserManager) OfflineUser(player *model.Player, changeGsInfo *ChangeGsInfo) {
|
||||
USER_MANAGER.DeleteUser(player.PlayerId)
|
||||
MESSAGE_QUEUE.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerUserOnlineStateChangeNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
UserId: player.PlayerId,
|
||||
IsOnline: false,
|
||||
},
|
||||
})
|
||||
atomic.AddInt32(&ONLINE_PLAYER_NUM, -1)
|
||||
if changeGsInfo.IsChangeGs {
|
||||
gsAppId := USER_MANAGER.GetRemoteUserGsAppId(changeGsInfo.JoinHostUserId)
|
||||
MESSAGE_QUEUE.SendToGate(player.GateAppId, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerUserGsChangeNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
UserId: player.PlayerId,
|
||||
GameServerAppId: gsAppId,
|
||||
JoinHostUserId: changeGsInfo.JoinHostUserId,
|
||||
},
|
||||
})
|
||||
logger.Info("user change gs notify to gate, uid: %v, gate appid: %v, gs appid: %v, host uid: %v",
|
||||
player.PlayerId, player.GateAppId, gsAppId, changeGsInfo.JoinHostUserId)
|
||||
}
|
||||
}
|
||||
|
||||
// ChangeUserDbState 玩家存档状态机 主要用于玩家定时保存时进行分类处理
|
||||
func (u *UserManager) ChangeUserDbState(player *model.Player, state int) {
|
||||
if player == nil {
|
||||
@@ -523,6 +571,110 @@ func (u *UserManager) saveUserHandle() {
|
||||
}()
|
||||
}
|
||||
|
||||
const (
|
||||
UserCopyGoroutineLimit = 4
|
||||
)
|
||||
|
||||
type PlayerLastSaveTimeSortList []*model.Player
|
||||
|
||||
func (p PlayerLastSaveTimeSortList) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
func (p PlayerLastSaveTimeSortList) Less(i, j int) bool {
|
||||
return p[i].LastSaveTime < p[j].LastSaveTime
|
||||
}
|
||||
|
||||
func (p PlayerLastSaveTimeSortList) Swap(i, j int) {
|
||||
p[i], p[j] = p[j], p[i]
|
||||
}
|
||||
|
||||
func (u *UserManager) UserCopyAndSave(exitSave bool) {
|
||||
startTime := time.Now().UnixNano()
|
||||
playerList := make(PlayerLastSaveTimeSortList, 0)
|
||||
for _, player := range USER_MANAGER.GetAllOnlineUserList() {
|
||||
if player.PlayerId < PlayerBaseUid {
|
||||
continue
|
||||
}
|
||||
playerList = append(playerList, player)
|
||||
}
|
||||
sort.Stable(playerList)
|
||||
// 拷贝一份数据避免并发访问
|
||||
insertPlayerList := make([][]byte, 0)
|
||||
updatePlayerList := make([][]byte, 0)
|
||||
saveCount := 0
|
||||
times := len(playerList) / UserCopyGoroutineLimit
|
||||
if times == 0 && len(playerList) > 0 {
|
||||
times = 1
|
||||
}
|
||||
for index := 0; index < times; index++ {
|
||||
totalCostTime := time.Now().UnixNano() - startTime
|
||||
if totalCostTime > time.Millisecond.Nanoseconds()*10 {
|
||||
// 总耗时超过10ms就中止本轮保存
|
||||
logger.Info("user copy loop overtime exit, total cost time: %v ns", totalCostTime)
|
||||
break
|
||||
}
|
||||
// 分批次并发序列化玩家数据
|
||||
oncePlayerListEndIndex := 0
|
||||
if index < times-1 {
|
||||
oncePlayerListEndIndex = (index + 1) * UserCopyGoroutineLimit
|
||||
} else {
|
||||
oncePlayerListEndIndex = len(playerList)
|
||||
}
|
||||
oncePlayerList := playerList[index*UserCopyGoroutineLimit : oncePlayerListEndIndex]
|
||||
var playerDataMapLock sync.Mutex
|
||||
playerDataMap := make(map[uint32][]byte)
|
||||
var wg sync.WaitGroup
|
||||
for _, player := range oncePlayerList {
|
||||
wg.Add(1)
|
||||
go func(player *model.Player) {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
}()
|
||||
playerData, err := msgpack.Marshal(player)
|
||||
if err != nil {
|
||||
logger.Error("marshal player data error: %v", err)
|
||||
return
|
||||
}
|
||||
playerDataMapLock.Lock()
|
||||
playerDataMap[player.PlayerId] = playerData
|
||||
playerDataMapLock.Unlock()
|
||||
}(player)
|
||||
}
|
||||
wg.Wait()
|
||||
for _, player := range oncePlayerList {
|
||||
playerData, exist := playerDataMap[player.PlayerId]
|
||||
if !exist {
|
||||
continue
|
||||
}
|
||||
switch player.DbState {
|
||||
case model.DbNone:
|
||||
break
|
||||
case model.DbInsert:
|
||||
insertPlayerList = append(insertPlayerList, playerData)
|
||||
player.DbState = model.DbNormal
|
||||
player.LastSaveTime = uint32(time.Now().UnixMilli())
|
||||
saveCount++
|
||||
case model.DbDelete:
|
||||
USER_MANAGER.DeleteUser(player.PlayerId)
|
||||
case model.DbNormal:
|
||||
updatePlayerList = append(updatePlayerList, playerData)
|
||||
player.LastSaveTime = uint32(time.Now().UnixMilli())
|
||||
saveCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
saveUserData := &SaveUserData{
|
||||
insertPlayerList: insertPlayerList,
|
||||
updatePlayerList: updatePlayerList,
|
||||
exitSave: exitSave,
|
||||
}
|
||||
USER_MANAGER.GetSaveUserChan() <- saveUserData
|
||||
endTime := time.Now().UnixNano()
|
||||
costTime := endTime - startTime
|
||||
logger.Info("run save user copy cost time: %v ns, save user count: %v", costTime, saveCount)
|
||||
}
|
||||
|
||||
func (u *UserManager) LoadUserFromDbSync(userId uint32) (*model.Player, error) {
|
||||
player, err := u.db.QueryPlayerById(userId)
|
||||
if err != nil {
|
||||
|
||||
+13
-29
@@ -1,11 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"hk4e/common/constant"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/region"
|
||||
"hk4e/gdconf"
|
||||
"hk4e/gs/model"
|
||||
@@ -20,7 +16,7 @@ func (g *Game) PlayerLoginReq(userId uint32, clientSeq uint32, gateAppId string,
|
||||
logger.Info("player login req, uid: %v, gateAppId: %v", userId, gateAppId)
|
||||
req := payloadMsg.(*proto.PlayerLoginReq)
|
||||
logger.Debug("login data: %v", req)
|
||||
USER_MANAGER.OnlineUser(userId, clientSeq, gateAppId, req)
|
||||
USER_MANAGER.UserLoginLoad(userId, clientSeq, gateAppId, req)
|
||||
}
|
||||
|
||||
func (g *Game) SetPlayerBornDataReq(player *model.Player, payloadMsg pb.Message) {
|
||||
@@ -83,17 +79,19 @@ func (g *Game) OnLogin(userId uint32, clientSeq uint32, gateAppId string, player
|
||||
}
|
||||
|
||||
if player == nil {
|
||||
logger.Info("reg new player, uid: %v", userId)
|
||||
player = g.CreatePlayer(userId)
|
||||
USER_MANAGER.ChangeUserDbState(player, model.DbInsert)
|
||||
}
|
||||
USER_MANAGER.AddUser(player)
|
||||
USER_MANAGER.OnlineUser(player)
|
||||
|
||||
TICK_MANAGER.CreateUserGlobalTick(userId)
|
||||
TICK_MANAGER.CreateUserTimer(userId, UserTimerActionTest, 100, player.NickName)
|
||||
|
||||
player.GateAppId = gateAppId
|
||||
|
||||
SELF = player
|
||||
|
||||
player.OnlineTime = uint32(time.Now().UnixMilli())
|
||||
player.Online = true
|
||||
player.GateAppId = gateAppId
|
||||
|
||||
// 初始化
|
||||
player.InitOnlineData()
|
||||
|
||||
@@ -107,9 +105,6 @@ func (g *Game) OnLogin(userId uint32, clientSeq uint32, gateAppId string, player
|
||||
player.Rot = &model.Vector{X: 0, Y: 307, Z: 0}
|
||||
}
|
||||
|
||||
TICK_MANAGER.CreateUserGlobalTick(userId)
|
||||
TICK_MANAGER.CreateUserTimer(userId, UserTimerActionTest, 100, player.NickName)
|
||||
|
||||
if player.IsBorn {
|
||||
g.LoginNotify(userId, clientSeq, player)
|
||||
if req.TargetUid != 0 {
|
||||
@@ -151,16 +146,6 @@ func (g *Game) OnLogin(userId uint32, clientSeq uint32, gateAppId string, player
|
||||
}
|
||||
g.SendMsg(cmd.PlayerLoginRsp, userId, clientSeq, playerLoginRsp)
|
||||
|
||||
MESSAGE_QUEUE.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerUserOnlineStateChangeNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
UserId: userId,
|
||||
IsOnline: true,
|
||||
},
|
||||
})
|
||||
atomic.AddInt32(&ONLINE_PLAYER_NUM, 1)
|
||||
|
||||
SELF = nil
|
||||
}
|
||||
|
||||
@@ -229,16 +214,15 @@ func (g *Game) OnOffline(userId uint32, changeGsInfo *ChangeGsInfo) {
|
||||
logger.Error("player is nil, uid: %v", userId)
|
||||
return
|
||||
}
|
||||
TICK_MANAGER.DestroyUserGlobalTick(userId)
|
||||
|
||||
world := WORLD_MANAGER.GetWorldById(player.WorldId)
|
||||
if world != nil {
|
||||
g.WorldRemovePlayer(world, player)
|
||||
}
|
||||
player.OfflineTime = uint32(time.Now().Unix())
|
||||
player.Online = false
|
||||
player.TotalOnlineTime += uint32(time.Now().UnixMilli()) - player.OnlineTime
|
||||
USER_MANAGER.OfflineUser(player, changeGsInfo)
|
||||
atomic.AddInt32(&ONLINE_PLAYER_NUM, -1)
|
||||
|
||||
TICK_MANAGER.DestroyUserGlobalTick(userId)
|
||||
|
||||
USER_MANAGER.UserOfflineSave(player, changeGsInfo)
|
||||
}
|
||||
|
||||
func (g *Game) LoginNotify(userId uint32, clientSeq uint32, player *model.Player) {
|
||||
|
||||
@@ -162,6 +162,10 @@ func (g *Game) SceneKickPlayerReq(player *model.Player, payloadMsg pb.Message) {
|
||||
|
||||
func (g *Game) JoinOtherWorld(player *model.Player, hostPlayer *model.Player) {
|
||||
hostWorld := WORLD_MANAGER.GetWorldById(hostPlayer.WorldId)
|
||||
if hostWorld == nil {
|
||||
logger.Error("host world is nil, worldId: %v, uid: %v", hostPlayer.WorldId, player.PlayerId)
|
||||
return
|
||||
}
|
||||
if hostPlayer.SceneLoadState == model.SceneEnterDone {
|
||||
player.SceneJump = true
|
||||
player.SceneId = hostPlayer.SceneId
|
||||
@@ -246,7 +250,7 @@ func (g *Game) PlayerApplyEnterWorld(player *model.Player, targetUid uint32) {
|
||||
targetWorld := WORLD_MANAGER.GetWorldById(targetPlayer.WorldId)
|
||||
if targetWorld == nil {
|
||||
// 目标玩家世界状态异常
|
||||
logger.Error("target world is nil, target worldId: %v, uid: %v", targetPlayer.WorldId, player.PlayerId)
|
||||
logger.Error("target world is nil, worldId: %v, uid: %v", targetPlayer.WorldId, player.PlayerId)
|
||||
applyFailNotify(proto.PlayerApplyEnterMpResultNotify_PLAYER_CANNOT_ENTER_MP)
|
||||
return
|
||||
}
|
||||
@@ -338,7 +342,7 @@ func (g *Game) PlayerDealEnterWorld(hostPlayer *model.Player, otherUid uint32, a
|
||||
|
||||
func (g *Game) HostEnterMpWorld(hostPlayer *model.Player) {
|
||||
world := WORLD_MANAGER.GetWorldById(hostPlayer.WorldId)
|
||||
if world.GetMultiplayer() {
|
||||
if world == nil || world.GetMultiplayer() {
|
||||
return
|
||||
}
|
||||
world.ChangeToMultiplayer()
|
||||
@@ -655,7 +659,7 @@ func (g *Game) ServerPlayerMpReq(playerMpInfo *mq.PlayerMpInfo, gsAppId string)
|
||||
return
|
||||
}
|
||||
applyPlayerWorld := WORLD_MANAGER.GetWorldById(applyPlayer.WorldId)
|
||||
if applyPlayerWorld.GetMultiplayer() {
|
||||
if applyPlayerWorld == nil || applyPlayerWorld.GetMultiplayer() {
|
||||
playerApplyEnterMpResultNotify := &proto.PlayerApplyEnterMpResultNotify{
|
||||
TargetUid: playerMpInfo.HostUserId,
|
||||
TargetNickname: playerMpInfo.HostNickname,
|
||||
|
||||
Reference in new issue
Block a user