center.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, sign string) *param.LoginResp {
  47. req := &param.LoginReq{
  48. OpenID: openID,
  49. IP: ip,
  50. Platform: plt,
  51. Channel: channel,
  52. Sign: sign,
  53. }
  54. targetPath := GetTargetPath(app, accountActor)
  55. rsp := &param.LoginResp{}
  56. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, registerAccount, req, rsp)
  57. if code.IsFail(errCode) {
  58. clog.Warnf("[RegisterAccount] openID = %s, errCode = %v", openID, errCode)
  59. return nil
  60. }
  61. nodeId, ok := SetNode(app)
  62. if code.IsFail(ok) {
  63. clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", openID, nodeId, errCode)
  64. return nil
  65. }
  66. rsp.TargetPath = cfacade.NewChildPath(nodeId, "player", rsp.UserName)
  67. return rsp
  68. }
  69. func GetCenterNodeID(app cfacade.IApplication) string {
  70. list := app.Discovery().ListByType(centerType)
  71. if len(list) > 0 {
  72. return list[0].GetNodeId()
  73. }
  74. return ""
  75. }
  76. func GetTargetPath(app cfacade.IApplication, actorID string) string {
  77. nodeId := GetCenterNodeID(app)
  78. return nodeId + actorID
  79. }
  80. func SetNode(app cfacade.IApplication) (string, int32) {
  81. var nodeId string
  82. list := app.Discovery().ListByType(gameType)
  83. lNode := len(list)
  84. key := 0
  85. if lNode == 0 {
  86. return "", code.AccountBindFail
  87. } else if lNode > 1 {
  88. key = rand.Intn(lNode)
  89. }
  90. nodeId = list[key].GetNodeId()
  91. if lNode > 0 {
  92. m, err := mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
  93. if err != nil {
  94. nodeId = list[key].GetNodeId()
  95. }
  96. if len(m) == 0 {
  97. nodeId = list[key].GetNodeId()
  98. } else {
  99. num := math.MaxInt
  100. for _, v := range list {
  101. id := v.GetNodeId()
  102. total, ok := cstring.ToInt(m[id])
  103. if ok {
  104. if total < num {
  105. num = total
  106. nodeId = v.GetNodeId()
  107. }
  108. } else {
  109. mdb.RDB.HSet(context.Background(), constant.ServerLoadHKey, id, 0)
  110. }
  111. }
  112. }
  113. }
  114. return nodeId, code.OK
  115. }