sdk.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package sdk
  2. import (
  3. "context"
  4. mhayaGin "github.com/mhaya/components/gin"
  5. mhayaError "github.com/mhaya/error"
  6. mhayaString "github.com/mhaya/extend/string"
  7. cfacade "github.com/mhaya/facade"
  8. "github.com/mhaya/game/game_cluster/internal/code"
  9. "github.com/mhaya/game/game_cluster/internal/constant"
  10. "github.com/mhaya/game/game_cluster/internal/data"
  11. "github.com/mhaya/game/game_cluster/internal/mdb"
  12. "github.com/mhaya/game/game_cluster/internal/token"
  13. mhayaLogger "github.com/mhaya/logger"
  14. "math/rand"
  15. )
  16. // sdk平台类型
  17. const (
  18. DevMode int = 1 // 开发模式,注册开发帐号登陆(开发时使用)
  19. QuickSDK int = 2 // quick sdk
  20. )
  21. var (
  22. invokeMaps = make(map[int]Invoke)
  23. )
  24. type (
  25. Invoke interface {
  26. SdkId() int // sdk id
  27. Reconnect(token *token.Token) (int32, *token.Token) //重连
  28. Login(config *data.SdkConfigRow, params Params, callback Callback) // Login 登录验证接口
  29. PayCallback(config *data.SdkConfigRow, c *mhayaGin.Context) // PayCallback 支付回调接口
  30. }
  31. Params map[string]string
  32. Result map[string]interface{}
  33. Callback func(code int32, result Result, error ...error)
  34. )
  35. func (p Params) GetInt(key string, defaultValue ...int) int {
  36. defVal := 0
  37. if len(defaultValue) > 0 {
  38. defVal = defaultValue[0]
  39. }
  40. val, found := p[key]
  41. if !found {
  42. return defVal
  43. }
  44. intVal, ok := mhayaString.ToInt(val)
  45. if ok {
  46. return intVal
  47. }
  48. return defVal
  49. }
  50. func (p Params) GetString(key string) (string, bool) {
  51. v, ok := p[key]
  52. return v, ok
  53. }
  54. func register(invoke Invoke) {
  55. invokeMaps[invoke.SdkId()] = invoke
  56. }
  57. func GetInvoke(sdkId int) (invoke Invoke, error error) {
  58. invoke, found := invokeMaps[sdkId]
  59. if found == false {
  60. return nil, mhayaError.Errorf("[sdkId = %d] not found.", sdkId)
  61. }
  62. return invoke, nil
  63. }
  64. func Init(app cfacade.IApplication) {
  65. register(quickSdk{app})
  66. }
  67. func SetNode(app cfacade.IApplication) (string, int32) {
  68. var nodeId string
  69. list := app.Discovery().ListByType("game")
  70. lNode := len(list)
  71. key := 0
  72. if lNode == 0 {
  73. return "", code.AccountBindFail
  74. } else if lNode > 1 {
  75. key = rand.Intn(lNode)
  76. }
  77. if lNode == 1 {
  78. return list[0].GetNodeId(), code.OK
  79. }
  80. nodeId = list[key].GetNodeId()
  81. if lNode > 0 {
  82. m, err := mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
  83. if err != nil {
  84. mhayaLogger.Warnf("[SetNode] Load too high. err=%v", err)
  85. }
  86. var maps = make(map[string]int, 10)
  87. for _, v := range list {
  88. if d, ok := m[v.GetNodeId()]; ok {
  89. p, ok := mhayaString.ToInt(d)
  90. if !ok {
  91. nodeId = list[key].GetNodeId()
  92. }
  93. maps[v.GetNodeId()] = p
  94. } else {
  95. maps[v.GetNodeId()] = 0
  96. }
  97. }
  98. wwr := NewWeightedRoundRobin(maps)
  99. info := wwr.Pick()
  100. if info == nil {
  101. mhayaLogger.Warnf("[SetNode] Load too high. data=%v", maps)
  102. return "", code.Error
  103. }
  104. nodeId = info.NodeId
  105. }
  106. return nodeId, code.OK
  107. }