center.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package rpcCenter
  2. import (
  3. "context"
  4. cstring "github.com/mhaya/extend/string"
  5. cfacade "github.com/mhaya/facade"
  6. "github.com/mhaya/game/game_cluster/internal/code"
  7. "github.com/mhaya/game/game_cluster/internal/constant"
  8. "github.com/mhaya/game/game_cluster/internal/mdb"
  9. "github.com/mhaya/game/game_cluster/internal/param"
  10. "github.com/mhaya/game/game_cluster/internal/pb"
  11. clog "github.com/mhaya/logger"
  12. "math"
  13. "math/rand"
  14. )
  15. // route = 节点类型.节点handler.remote函数
  16. const (
  17. centerType = "center"
  18. gameType = "game"
  19. )
  20. const (
  21. opsActor = ".ops"
  22. accountActor = ".account"
  23. )
  24. const (
  25. ping = "ping"
  26. registerAccount = "registerAccount"
  27. )
  28. const (
  29. sourcePath = ".system"
  30. )
  31. // Ping 访问center节点,确认center已启动
  32. func Ping(app cfacade.IApplication) bool {
  33. nodeId := GetCenterNodeID(app)
  34. if nodeId == "" {
  35. return false
  36. }
  37. rsp := &pb.Bool{}
  38. targetPath := nodeId + opsActor
  39. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, ping, nil, rsp)
  40. if code.IsFail(errCode) {
  41. return false
  42. }
  43. return rsp.Value
  44. }
  45. // RegisterAccount 注册帐号
  46. func RegisterAccount(app cfacade.IApplication, openID, ip, plt, channel string) *param.LoginResp {
  47. req := &param.LoginReq{
  48. OpenID: openID,
  49. IP: ip,
  50. Platform: plt,
  51. Channel: channel,
  52. }
  53. targetPath := GetTargetPath(app, accountActor)
  54. rsp := &param.LoginResp{}
  55. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, registerAccount, req, rsp)
  56. if code.IsFail(errCode) {
  57. clog.Warnf("[RegisterAccount] openID = %s, errCode = %v", openID, errCode)
  58. return nil
  59. }
  60. nodeId, ok := SetNode(app)
  61. if code.IsFail(ok) {
  62. clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", openID, nodeId, errCode)
  63. return nil
  64. }
  65. rsp.TargetPath = cfacade.NewChildPath(nodeId, "player", rsp.UserName)
  66. return rsp
  67. }
  68. func GetCenterNodeID(app cfacade.IApplication) string {
  69. list := app.Discovery().ListByType(centerType)
  70. if len(list) > 0 {
  71. return list[0].GetNodeId()
  72. }
  73. return ""
  74. }
  75. func GetTargetPath(app cfacade.IApplication, actorID string) string {
  76. nodeId := GetCenterNodeID(app)
  77. return nodeId + actorID
  78. }
  79. func SetNode(app cfacade.IApplication) (string, int32) {
  80. var nodeId string
  81. list := app.Discovery().ListByType(gameType)
  82. lNode := len(list)
  83. key := 0
  84. if lNode == 0 {
  85. return "", code.AccountBindFail
  86. } else if lNode > 1 {
  87. key = rand.Intn(lNode)
  88. }
  89. nodeId = list[key].GetNodeId()
  90. if lNode > 0 {
  91. m, err := mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
  92. if err != nil {
  93. nodeId = list[key].GetNodeId()
  94. }
  95. if len(m) == 0 {
  96. nodeId = list[key].GetNodeId()
  97. } else {
  98. num := math.MaxInt
  99. for _, v := range list {
  100. id := v.GetNodeId()
  101. total, ok := cstring.ToInt(m[id])
  102. if ok {
  103. if total < num {
  104. num = total
  105. nodeId = v.GetNodeId()
  106. }
  107. } else {
  108. mdb.RDB.HSet(context.Background(), constant.ServerLoadHKey, id, 0)
  109. }
  110. }
  111. }
  112. }
  113. return nodeId, code.OK
  114. }