From bea91fbf73ffaca1d588e9deb2f8e208690add84 Mon Sep 17 00:00:00 2001 From: flswld Date: Mon, 23 Oct 2023 22:59:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96GM=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/mq/net_msg.go | 3 ++ dispatch/app/app.go | 2 +- dispatch/controller/controller.go | 8 ++++ gate/app/app.go | 2 +- gm/app/app.go | 7 +++- gm/controller/controller.go | 54 ++++++++++++++++++++++--- gm/controller/gm_controller.go | 65 +++++++++++++++++++++--------- gm/controller/server_controller.go | 12 ++++++ gs/app/app.go | 3 +- gs/game/game_plugin_pubg.go | 11 ++++- gs/game/game_route_manager.go | 7 ++++ node/api/server_type.go | 15 ++++--- 12 files changed, 152 insertions(+), 37 deletions(-) diff --git a/common/mq/net_msg.go b/common/mq/net_msg.go index 2d8dad49..228ca234 100644 --- a/common/mq/net_msg.go +++ b/common/mq/net_msg.go @@ -60,6 +60,7 @@ const ( ServerForwardDispatchInfoNotify // 转发模式区服信息通知 ServerStopNotify // 停服通知 ServerDispatchCancelNotify // 服务器取消调度通知 + ServerGmCmdNotify // 服务器GM指令执行通知 ) type ServerMsg struct { @@ -73,6 +74,8 @@ type ServerMsg struct { AddFriendInfo *AddFriendInfo ForwardDispatchInfo *ForwardDispatchInfo AppVersion string + GmCmdFuncName string + GmCmdParamList []string } type OriginInfo struct { diff --git a/dispatch/app/app.go b/dispatch/app/app.go index e5cc26e9..1d3f230c 100644 --- a/dispatch/app/app.go +++ b/dispatch/app/app.go @@ -64,7 +64,7 @@ func Run(ctx context.Context, configFile string) error { logger.CloseLogger() }() - messageQueue := mq.NewMessageQueue(api.DISPATCH, APPID, discoveryClient) + messageQueue := mq.NewMessageQueue(api.DISPATCH, APPID, nil) defer messageQueue.Close() db, err := dao.NewDao() diff --git a/dispatch/controller/controller.go b/dispatch/controller/controller.go index feac0916..acdbd4bd 100644 --- a/dispatch/controller/controller.go +++ b/dispatch/controller/controller.go @@ -50,6 +50,14 @@ func NewController(db *dao.Dao, discovery *rpc.DiscoveryClient, messageQueue *mq } r.ec2b = ec2b r.messageQueue = messageQueue + go func() { + for { + _, ok := <-r.messageQueue.GetNetMsg() + if !ok { + return + } + } + }() r.gateServerMap = new(sync.Map) r.stopServerInfo = nil r.whiteList = nil diff --git a/gate/app/app.go b/gate/app/app.go index ddb6b17c..25d43152 100644 --- a/gate/app/app.go +++ b/gate/app/app.go @@ -74,7 +74,7 @@ func Run(ctx context.Context, configFile string) error { logger.CloseLogger() }() - messageQueue := mq.NewMessageQueue(api.GATE, APPID, discoveryClient) + messageQueue := mq.NewMessageQueue(api.GATE, APPID, nil) defer messageQueue.Close() db, err := dao.NewDao() diff --git a/gm/app/app.go b/gm/app/app.go index 460063a6..221e9e52 100644 --- a/gm/app/app.go +++ b/gm/app/app.go @@ -7,8 +7,10 @@ import ( "syscall" "hk4e/common/config" + "hk4e/common/mq" "hk4e/common/rpc" "hk4e/gm/controller" + "hk4e/node/api" "hk4e/pkg/logger" ) @@ -27,7 +29,10 @@ func Run(ctx context.Context, configFile string) error { return err } - _ = controller.NewController(discoveryClient) + messageQueue := mq.NewMessageQueue(api.GM, "gm", nil) + defer messageQueue.Close() + + _ = controller.NewController(discoveryClient, messageQueue) c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) diff --git a/gm/controller/controller.go b/gm/controller/controller.go index f297391d..39e53cf8 100644 --- a/gm/controller/controller.go +++ b/gm/controller/controller.go @@ -1,11 +1,14 @@ package controller import ( + "context" "net/http" "strconv" "sync" + "time" "hk4e/common/config" + "hk4e/common/mq" "hk4e/common/rpc" "hk4e/pkg/logger" @@ -13,22 +16,62 @@ import ( ) type Controller struct { - gmClientMap map[uint32]*rpc.GMClient - gmClientMapLock sync.RWMutex - discoveryClient *rpc.DiscoveryClient + gmClientMap map[uint32]*rpc.GMClient + gmClientMapLock sync.RWMutex + discoveryClient *rpc.DiscoveryClient + messageQueue *mq.MessageQueue + globalGsOnlineMap map[uint32]string // 全服玩家在线表 + globalGsOnlineMapLock sync.RWMutex } -func NewController(discoveryClient *rpc.DiscoveryClient) (r *Controller) { +func NewController(discoveryClient *rpc.DiscoveryClient, messageQueue *mq.MessageQueue) (r *Controller) { r = new(Controller) r.gmClientMap = make(map[uint32]*rpc.GMClient) r.discoveryClient = discoveryClient + r.messageQueue = messageQueue + go func() { + for { + _, ok := <-r.messageQueue.GetNetMsg() + if !ok { + return + } + } + }() + r.globalGsOnlineMap = make(map[uint32]string) + r.syncGlobalGsOnlineMap() + go r.autoSyncGlobalGsOnlineMap() go r.registerRouter() return r } +func (c *Controller) autoSyncGlobalGsOnlineMap() { + ticker := time.NewTicker(time.Second * 60) + for { + <-ticker.C + c.syncGlobalGsOnlineMap() + } +} + +func (c *Controller) syncGlobalGsOnlineMap() { + rsp, err := c.discoveryClient.GetGlobalGsOnlineMap(context.TODO(), nil) + if err != nil { + logger.Error("get global gs online map error: %v", err) + return + } + copyMap := make(map[uint32]string) + for k, v := range rsp.OnlineMap { + copyMap[k] = v + } + copyMapLen := len(copyMap) + c.globalGsOnlineMapLock.Lock() + c.globalGsOnlineMap = copyMap + c.globalGsOnlineMapLock.Unlock() + logger.Info("sync global gs online map finish, len: %v", copyMapLen) +} + func (c *Controller) authorize() gin.HandlerFunc { return func(context *gin.Context) { - if true { + if context.GetHeader("Auth") == "flswld" { // 验证通过 context.Next() return @@ -55,6 +98,7 @@ func (c *Controller) registerRouter() { gin.SetMode(gin.ReleaseMode) } engine := gin.Default() + engine.GET("/server/online/stats", c.serverOnlineStats) engine.Use(c.authorize()) engine.POST("/gm/cmd", c.gmCmd) engine.GET("/server/stop/state", c.serverStopState) diff --git a/gm/controller/gm_controller.go b/gm/controller/gm_controller.go index 38f8a47b..b1a2f9f3 100644 --- a/gm/controller/gm_controller.go +++ b/gm/controller/gm_controller.go @@ -3,6 +3,7 @@ package controller import ( "net/http" + "hk4e/common/mq" "hk4e/common/rpc" "hk4e/gs/api" "hk4e/pkg/logger" @@ -14,6 +15,7 @@ type GmCmdReq struct { FuncName string `json:"func_name"` ParamList []string `json:"param_list"` GsId uint32 `json:"gs_id"` + GsAppid string `json:"gs_appid"` } func (c *Controller) gmCmd(ctx *gin.Context) { @@ -25,28 +27,53 @@ func (c *Controller) gmCmd(ctx *gin.Context) { return } logger.Info("GmCmdReq: %v", gmCmdReq) - c.gmClientMapLock.RLock() - gmClient, exist := c.gmClientMap[gmCmdReq.GsId] - c.gmClientMapLock.RUnlock() - if !exist { - var err error = nil - gmClient, err = rpc.NewGMClient(gmCmdReq.GsId) + if gmCmdReq.GsId != 0 { + // 指定gsid + c.gmClientMapLock.RLock() + gmClient, exist := c.gmClientMap[gmCmdReq.GsId] + c.gmClientMapLock.RUnlock() + if !exist { + var err error = nil + gmClient, err = rpc.NewGMClient(gmCmdReq.GsId) + if err != nil { + logger.Error("new gm client error: %v", err) + ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err}) + return + } + c.gmClientMapLock.Lock() + c.gmClientMap[gmCmdReq.GsId] = gmClient + c.gmClientMapLock.Unlock() + } + rsp, err := gmClient.Cmd(ctx.Request.Context(), &api.CmdRequest{ + FuncName: gmCmdReq.FuncName, + ParamList: gmCmdReq.ParamList, + }) if err != nil { - logger.Error("new gm client error: %v", err) ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err}) return } - c.gmClientMapLock.Lock() - c.gmClientMap[gmCmdReq.GsId] = gmClient - c.gmClientMapLock.Unlock() + ctx.JSON(http.StatusOK, rsp) + } else if gmCmdReq.GsAppid != "" { + // 指定gsappid + c.messageQueue.SendToGs(gmCmdReq.GsAppid, &mq.NetMsg{ + MsgType: mq.MsgTypeServer, + EventId: mq.ServerGmCmdNotify, + ServerMsg: &mq.ServerMsg{ + GmCmdFuncName: gmCmdReq.FuncName, + GmCmdParamList: gmCmdReq.ParamList, + }, + }) + ctx.JSON(http.StatusOK, nil) + } else { + // 全服 + c.messageQueue.SendToAll(&mq.NetMsg{ + MsgType: mq.MsgTypeServer, + EventId: mq.ServerGmCmdNotify, + ServerMsg: &mq.ServerMsg{ + GmCmdFuncName: gmCmdReq.FuncName, + GmCmdParamList: gmCmdReq.ParamList, + }, + }) + ctx.JSON(http.StatusOK, nil) } - rep, err := gmClient.Cmd(ctx.Request.Context(), &api.CmdRequest{ - FuncName: gmCmdReq.FuncName, - ParamList: gmCmdReq.ParamList, - }) - if err != nil { - ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err}) - return - } - ctx.JSON(http.StatusOK, rep) } diff --git a/gm/controller/server_controller.go b/gm/controller/server_controller.go index 085cc085..c0b07f19 100644 --- a/gm/controller/server_controller.go +++ b/gm/controller/server_controller.go @@ -122,3 +122,15 @@ func (c *Controller) serverDispatchCancel(ctx *gin.Context) { } ctx.JSON(http.StatusOK, &CommonRsp{Code: 0, Msg: "", Data: nil}) } + +type ServerOnlineStats struct { + TotalOnlinePlayerNum uint32 `json:"total_online_player_num"` +} + +func (c *Controller) serverOnlineStats(ctx *gin.Context) { + num := 0 + c.globalGsOnlineMapLock.RLock() + num = len(c.globalGsOnlineMap) + c.globalGsOnlineMapLock.RUnlock() + ctx.JSON(http.StatusOK, &CommonRsp{Code: 0, Msg: "", Data: &ServerOnlineStats{TotalOnlinePlayerNum: uint32(num)}}) +} diff --git a/gs/app/app.go b/gs/app/app.go index 039cebe5..c688a54c 100644 --- a/gs/app/app.go +++ b/gs/app/app.go @@ -5,6 +5,7 @@ import ( _ "net/http/pprof" "os" "os/signal" + "strconv" "sync/atomic" "syscall" "time" @@ -66,7 +67,7 @@ func Run(ctx context.Context, configFile string) error { }) }() - logger.InitLogger("gs_" + APPID) + logger.InitLogger("gs_" + strconv.Itoa(int(GSID)) + "_" + APPID) logger.Warn("gs start, appid: %v, gsid: %v", APPID, GSID) defer func() { logger.CloseLogger() diff --git a/gs/game/game_plugin_pubg.go b/gs/game/game_plugin_pubg.go index cbf10816..037741a5 100644 --- a/gs/game/game_plugin_pubg.go +++ b/gs/game/game_plugin_pubg.go @@ -249,7 +249,8 @@ func (p *PluginPubg) GlobalTickPubg() { // GlobalTickMinuteChange 定时开启pubg游戏 func (p *PluginPubg) GlobalTickMinuteChange() { minute := time.Now().Minute() - startMinute := GAME.GetGsId() % 6 * 10 + roomNumber := GAME.GetGsId() - 1 + startMinute := roomNumber % 6 * 10 if uint32(minute) == startMinute { p.StartPubg() } @@ -268,6 +269,10 @@ func (p *PluginPubg) UserTimerPubgEnd(player *model.Player, data []any) { newWorld := WORLD_MANAGER.CreateWorld(player) GAME.WorldAddPlayer(newWorld, player) WORLD_MANAGER.InitAiWorld(player) + roomNumber := GAME.GetGsId() - 1 + startMinute := roomNumber % 6 * 10 + info := fmt.Sprintf("下一次游戏开启时间:%02d:%02d。", time.Now().Add(time.Hour).Hour(), startMinute) + GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}}) } // UserTimerPubgUpdateArea 更新游戏区域 @@ -375,6 +380,8 @@ func (p *PluginPubg) StartPubg() { }) p.playerHitTimeMap[player.PlayerId] = 0 } + info := "游戏开始。" + GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}}) } // StopPubg 结束pubg游戏 @@ -397,6 +404,8 @@ func (p *PluginPubg) StopPubg() { p.entityIdWorldGadgetIdMap = make(map[uint32]int32) p.playerHitTimeMap = make(map[uint32]int64) p.CreateUserTimer(world.GetOwner().PlayerId, 60, p.UserTimerPubgEnd) + info := "游戏结束。" + GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}}) } // IsStartPubg pubg游戏是否开启 diff --git a/gs/game/game_route_manager.go b/gs/game/game_route_manager.go index 6b513200..5f9dc7a8 100644 --- a/gs/game/game_route_manager.go +++ b/gs/game/game_route_manager.go @@ -212,6 +212,13 @@ func (r *RouteManager) RouteHandle(netMsg *mq.NetMsg) { GAME.ServerStopNotify() case mq.ServerDispatchCancelNotify: GAME.ServerDispatchCancelNotify(serverMsg.AppVersion) + case mq.ServerGmCmdNotify: + commandTextInput := COMMAND_MANAGER.GetCommandMessageInput() + commandTextInput <- &CommandMessage{ + GMType: SystemFuncGM, + FuncName: serverMsg.GmCmdFuncName, + ParamList: serverMsg.GmCmdParamList, + } } } } diff --git a/node/api/server_type.go b/node/api/server_type.go index ac369f43..4024225c 100644 --- a/node/api/server_type.go +++ b/node/api/server_type.go @@ -1,12 +1,11 @@ package api const ( - NODE = "NODE" - DISPATCH = "DISPATCH" - GATE = "GATE" - GS = "GS" - ANTICHEAT = "ANTICHEAT" - PATHFINDING = "PATHFINDING" - MULTI = "MULTI" - ROBOT = "ROBOT" + NODE = "NODE" + DISPATCH = "DISPATCH" + GATE = "GATE" + GS = "GS" + MULTI = "MULTI" + GM = "GM" + ROBOT = "ROBOT" )