From 9c3f3ea191e8a7bd7e46730039a37fb0c8043cb9 Mon Sep 17 00:00:00 2001 From: sk <123456@qq.com> Date: Fri, 13 Dec 2024 11:15:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=88=BF=E9=97=B4=E7=AE=A1=E7=90=86=E5=99=A8=E6=88=BF?= =?UTF-8?q?=E9=97=B4=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robot/base/{sclogin.go => action_login.go} | 0 .../{scplayerinfo.go => action_playerinfo.go} | 1 + robot/base/{scroom.go => action_room.go} | 1 + robot/base/{scserver.go => action_server.go} | 0 robot/base/clientmgr.go | 24 ++++++++++++------ robot/base/connect.go | 19 ++++++++------ robot/base/gatesessionhandler.go | 5 +--- robot/base/init.go | 4 +-- robot/base/playermgr.go | 25 ++++++++++++++++++- robot/base/scenemgr.go | 23 +++++++++++++---- worldsrv/scenemgr.go | 15 +++++------ 11 files changed, 84 insertions(+), 33 deletions(-) rename robot/base/{sclogin.go => action_login.go} (100%) rename robot/base/{scplayerinfo.go => action_playerinfo.go} (98%) rename robot/base/{scroom.go => action_room.go} (98%) rename robot/base/{scserver.go => action_server.go} (100%) diff --git a/robot/base/sclogin.go b/robot/base/action_login.go similarity index 100% rename from robot/base/sclogin.go rename to robot/base/action_login.go diff --git a/robot/base/scplayerinfo.go b/robot/base/action_playerinfo.go similarity index 98% rename from robot/base/scplayerinfo.go rename to robot/base/action_playerinfo.go index 1d9df25..7d095c4 100644 --- a/robot/base/scplayerinfo.go +++ b/robot/base/action_playerinfo.go @@ -99,6 +99,7 @@ func SCPlayerFlag(s *netlib.Session, packetid int, data interface{}) error { return nil } + // 更新房间里玩家的状态 if scene, ok := GetScene(s).(IScene); ok && scene != nil { p := scene.GetPlayerBySnid(msg.GetPlayerId()) if p != nil { diff --git a/robot/base/scroom.go b/robot/base/action_room.go similarity index 98% rename from robot/base/scroom.go rename to robot/base/action_room.go index 1d0bbf8..eae3592 100644 --- a/robot/base/scroom.go +++ b/robot/base/action_room.go @@ -52,6 +52,7 @@ func SCDestroyRoom(s *netlib.Session, packid int, pack interface{}) error { if msg.GetOpRetCode() == gamehallproto.OpResultCode_Game_OPRC_Sucess_Game { cleanRoomState(s) + SceneMgrSingleton.DelScene(msg.GetRoomId()) return nil } diff --git a/robot/base/scserver.go b/robot/base/action_server.go similarity index 100% rename from robot/base/scserver.go rename to robot/base/action_server.go diff --git a/robot/base/clientmgr.go b/robot/base/clientmgr.go index 793556b..7c286ff 100644 --- a/robot/base/clientmgr.go +++ b/robot/base/clientmgr.go @@ -30,12 +30,12 @@ import ( func init() { common.RegisterClockFunc(&common.ClockFunc{ OnHourTimerFunc: func() { - ClientMgrSingleton.HourChange() + ClientMgrSingleton.AccountReplace() - logger.Logger.Infof("client state: %+v", ClientMgrSingleton.GetState()) + logger.Logger.Info(ClientMgrSingleton.GetState()) }, OnDayTimerFunc: func() { - ClientMgrSingleton.DayChange() + ClientMgrSingleton.AccountDeletePolicy() }, }) } @@ -110,13 +110,17 @@ func (this *ClientMgr) UnRegisterSession(acc string) { delete(this.sessionPool, acc) } -func (this *ClientMgr) HourChange() { +// AccountReplace 账号替换 +func (this *ClientMgr) AccountReplace() { fileModify := false eventArr := this.CycleTimeEvent[time.Now().Hour()] for _, event := range eventArr { accChan[event.newAcc] = true //使用新的账号 - cfg := NewSessionConfig() - netlib.Connect(cfg) //创建新的连接 + + //创建新的连接 + NewSession() + + // 关闭旧的连接 if session, ok := this.sessionPool[event.oldAcc]; ok && session != nil { //删除旧有账号数据 pack := &serverproto.RWAccountInvalid{ @@ -128,6 +132,7 @@ func (this *ClientMgr) HourChange() { //关闭连接 session.Close() } + //更新本地账号数据信息 for key, value := range accPool { if value.Acc == event.oldAcc { @@ -156,7 +161,8 @@ func (this *ClientMgr) HourChange() { } } -func (this *ClientMgr) DayChange() { +// AccountDeletePolicy 账号删除策略 +func (this *ClientMgr) AccountDeletePolicy() { invalidCount := 0 //过期账号数量 updateLimit := len(accPool) * model.GameParamData.InvalidRobotAccRate / 100 //可更新的账号数量 invalidAccs := []InvalidAcc{} @@ -201,6 +207,10 @@ type ClientState struct { Event map[int]int } +func (c *ClientState) String() string { + return fmt.Sprintf("ClientMgrState 连接总数:%v, 每小时账号替换数量:%v", c.SessionNum, c.Event) +} + func (this *ClientMgr) GetState() *ClientState { ret := &ClientState{ SessionNum: len(this.sessionPool), diff --git a/robot/base/connect.go b/robot/base/connect.go index 9804c34..1b0c9fe 100644 --- a/robot/base/connect.go +++ b/robot/base/connect.go @@ -18,12 +18,19 @@ var ( WaitConnectSessions []*netlib.SessionConfig ) -func NewSessionConfig() *netlib.SessionConfig { - BenchMarkModule.idx++ +// NewSession 新建session +// id 连接id, 默认自动分配 +func NewSession(id ...int) { cfg := Config.Connects - cfg.Id = BenchMarkModule.idx + if len(id) > 0 && id[0] > 0 { + cfg.Id = id[0] + } else { + BenchMarkModule.idx++ + cfg.Id = BenchMarkModule.idx + } cfg.Init() - return &cfg + logger.Logger.Info("waite connect session id=", cfg.Id) + WaitConnectSessions = append(WaitConnectSessions, &cfg) } type BenchMark struct { @@ -37,9 +44,7 @@ func (m *BenchMark) ModuleName() string { func (m *BenchMark) Init() { m.idx = RobotSessionStartId for i := 0; i < Config.Count; i++ { - cfg := NewSessionConfig() - logger.Logger.Info("waite connect session id=", cfg.Id) - WaitConnectSessions = append(WaitConnectSessions, cfg) + NewSession() } } diff --git a/robot/base/gatesessionhandler.go b/robot/base/gatesessionhandler.go index 9d88c07..bb6e5c0 100644 --- a/robot/base/gatesessionhandler.go +++ b/robot/base/gatesessionhandler.go @@ -62,10 +62,7 @@ func (g *GateSessionHandler) OnSessionClosed(s *netlib.Session) { StopSessionPingTimer(s) if reconnect { logger.Logger.Infof("账号重连 sessionID:%v account:%v", s.Id, accIdParam) - cfg := Config.Connects - cfg.Id = s.GetSessionConfig().Id - cfg.Init() - WaitConnectSessions = append(WaitConnectSessions, &cfg) + NewSession(s.GetSessionConfig().Id) } } diff --git a/robot/base/init.go b/robot/base/init.go index 32fbd90..d8b5455 100644 --- a/robot/base/init.go +++ b/robot/base/init.go @@ -16,9 +16,9 @@ type AccountData struct { } var ( - // 待使用的账号,将要建立连接 + // 待登录的账号,将要建立连接 accChan = make(map[string]bool) - // 账号池,当前正在使用的机器人 + // 账号池,当前正在使用的机器人账号 accPool []*AccountData ) var accountFileName = "robotaccount.json" diff --git a/robot/base/playermgr.go b/robot/base/playermgr.go index cfc1c53..bfda419 100644 --- a/robot/base/playermgr.go +++ b/robot/base/playermgr.go @@ -1,12 +1,13 @@ package base import ( + "fmt" "math/rand" - "mongo.games.com/game/common" "mongo.games.com/goserver/core/logger" "mongo.games.com/goserver/core/netlib" + "mongo.games.com/game/common" "mongo.games.com/game/proto" hall_proto "mongo.games.com/game/protocol/gamehall" player_proto "mongo.games.com/game/protocol/player" @@ -20,6 +21,9 @@ func init() { OnMiniTimerFunc: func() { PlayerMgrSingleton.OnMiniTimer() }, + OnHourTimerFunc: func() { + logger.Logger.Info(PlayerMgrSingleton.GetState()) + }, }) } @@ -173,3 +177,22 @@ func (pm *PlayerMgr) OnSecondTimer() { func (pm *PlayerMgr) OnMiniTimer() { pm.ProcessCheckRobotNum() } + +type PlayerMgrState struct { + PlayerNum int + NormalSessionNum int + MatchSessionNum int +} + +func (p *PlayerMgrState) String() string { + return fmt.Sprintf("PlayerMgrState 玩家总数:%v, 普通场连接数:%v, 比赛场连接数:%v", p.PlayerNum, p.NormalSessionNum, p.MatchSessionNum) +} + +func (pm *PlayerMgr) GetState() *PlayerMgrState { + ret := &PlayerMgrState{ + PlayerNum: len(pm.playersMapSnId), + NormalSessionNum: len(pm.playersSession), + MatchSessionNum: len(pm.playersMatchSession), + } + return ret +} diff --git a/robot/base/scenemgr.go b/robot/base/scenemgr.go index a354d92..9ee47d1 100644 --- a/robot/base/scenemgr.go +++ b/robot/base/scenemgr.go @@ -1,6 +1,7 @@ package base import ( + "fmt" "time" "mongo.games.com/goserver/core/logger" @@ -16,11 +17,7 @@ func init() { common.RegisterClockFunc(&common.ClockFunc{ OnHourTimerFunc: func() { - sceneState := map[int32]int{} - for _, v := range SceneMgrSingleton.Scenes { - sceneState[v.GetGameId()]++ - } - logger.Logger.Infof("sceneState: %v", sceneState) + logger.Logger.Info(SceneMgrSingleton.GetState()) }, }) } @@ -71,6 +68,22 @@ func (sm *SceneMgr) IsFreeMode(sceneId int32) bool { return false } +type SceneMgrState struct { + Num map[int32]int +} + +func (s *SceneMgrState) String() string { + return fmt.Sprintf("SceneMgrState 每个游戏的房间数量 [游戏id:房间数量]: %v", s.Num) +} + +func (sm *SceneMgr) GetState() *SceneMgrState { + m := make(map[int32]int) + for _, v := range sm.Scenes { + m[v.GetGameId()]++ + } + return &SceneMgrState{Num: m} +} + // ////////////////////////////////////////////////////////////////// // / Module Implement [beg] // ////////////////////////////////////////////////////////////////// diff --git a/worldsrv/scenemgr.go b/worldsrv/scenemgr.go index 69b90ea..4b5b962 100644 --- a/worldsrv/scenemgr.go +++ b/worldsrv/scenemgr.go @@ -34,13 +34,14 @@ type SceneMgr struct { common.BaseClockSinker // 驱动时间事件 scenes map[int]*Scene // 房间id: Scene - privateAutoId int // 私人房房间号 - matchAutoId int // 比赛场房间号 - coinSceneAutoId int // 金币场房间号 - hundredSceneAutoId int // 百人场房间号 - password map[string]struct{} // 密码 - pushList map[int]struct{} // 已经推荐过的房间列表 - lastPushSceneId int // 最后推荐的房间id + privateAutoId int // 私人房房间号 + matchAutoId int // 比赛场房间号 + coinSceneAutoId int // 金币场房间号 + hundredSceneAutoId int // 百人场房间号 + + password map[string]struct{} // 密码 + pushList map[int]struct{} // 已经推荐过的房间列表 + lastPushSceneId int // 最后推荐的房间id } // AllocReplayCode 获取回访码