sdk.go 2.6 KB

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