common_controller.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package controller
  2. import (
  3. actorRemote "f1-game/internal/actor_remote"
  4. cherryApp "f1-game/internal/cherry_app"
  5. "f1-game/internal/code"
  6. "f1-game/internal/constant"
  7. nameActor "f1-game/internal/name/actor"
  8. nameKey "f1-game/internal/name/key"
  9. nameRemote "f1-game/internal/name/remote"
  10. "f1-game/internal/pb"
  11. "f1-game/nodes/ops/service"
  12. "fmt"
  13. cstring "github.com/cherry-game/cherry/extend/string"
  14. ctime "github.com/cherry-game/cherry/extend/time"
  15. cfacade "github.com/cherry-game/cherry/facade"
  16. cgin "github.com/cherry-game/components/gin"
  17. )
  18. type commonController struct {
  19. cgin.BaseController
  20. }
  21. func (p *commonController) Init() {
  22. p.GET("/common/db/queue/:nodeID", p.dbQueue) // 获取节点db队列
  23. p.GET("/common/ping/:nodeID", p.ping) // ping节点
  24. p.GET("/common/state/:nodeID/:state", p.state) // 更新节点状态
  25. }
  26. func (p *commonController) dbQueue(c *cgin.Context) {
  27. nodeID := c.GetString(nameKey.NodeID, "")
  28. if cstring.IsBlank(nodeID) {
  29. code.JSONResult(c, code.DataNotFound)
  30. return
  31. }
  32. nodeType, found := cherryApp.GetNodeType(nodeID)
  33. if !found {
  34. code.JSONResult(c, code.DataNotFound)
  35. return
  36. }
  37. var (
  38. list *pb.DBQueueReportList
  39. errCode int32
  40. )
  41. switch nodeType {
  42. case constant.NodeCenter:
  43. {
  44. list, errCode = actorRemote.CenterOps.GetDBReport()
  45. }
  46. case constant.NodeGame:
  47. {
  48. list, errCode = actorRemote.GameOps.GetDBReport(nodeID)
  49. }
  50. }
  51. if code.IsFail(errCode) {
  52. code.JSONResult(c, errCode)
  53. return
  54. }
  55. service.DBReportRenderHtml(c, list.List)
  56. }
  57. func (p *commonController) ping(c *cgin.Context) {
  58. nodeID := c.GetString(nameKey.NodeID, "")
  59. if cstring.IsBlank(nodeID) {
  60. code.JSONResult(c, code.DataNotFound)
  61. return
  62. }
  63. nodeType, found := cherryApp.GetNodeType(nodeID)
  64. if !found {
  65. code.JSONResult(c, code.DataNotFound)
  66. return
  67. }
  68. start := ctime.Now()
  69. switch nodeType {
  70. case constant.NodeCenter:
  71. {
  72. actorRemote.CenterOps.Ping()
  73. }
  74. }
  75. diffNanosecond := ctime.Now().ToNanosecond() - start.ToNanosecond()
  76. t := struct {
  77. Elapsed string `json:"elapsedTime"`
  78. }{
  79. Elapsed: fmt.Sprintf("%d Nanosecond", diffNanosecond),
  80. }
  81. c.RenderJSON(&t)
  82. }
  83. func (p *commonController) state(c *cgin.Context) {
  84. var (
  85. nodeID = c.GetString(nameKey.NodeID, "")
  86. state = c.GetInt32(nameKey.State, 0)
  87. )
  88. nodeType, found := cherryApp.GetNodeType(nodeID)
  89. if !found {
  90. code.JSONResult(c, code.DataNotFound)
  91. return
  92. }
  93. var (
  94. actorName string
  95. )
  96. switch nodeType {
  97. case constant.NodeGame:
  98. actorName = nameActor.GameOps
  99. case constant.NodeMap:
  100. actorName = nameActor.Map_Ops
  101. default:
  102. code.JSONResult(c, code.RequestParamsError)
  103. return
  104. }
  105. var (
  106. targetPath = cfacade.NewPath(nodeID, actorName)
  107. req = &pb.I32{
  108. Value: state,
  109. }
  110. )
  111. statusCode := cherryApp.CallWait(targetPath, nameRemote.Ops_ChangeNodeState, req, nil)
  112. code.JSONResult(c, statusCode)
  113. }