123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package sdk
- import (
- "context"
- mhayaGin "github.com/mhaya/components/gin"
- mhayaError "github.com/mhaya/error"
- mhayaString "github.com/mhaya/extend/string"
- cfacade "github.com/mhaya/facade"
- "github.com/mhaya/game/game_cluster/internal/code"
- "github.com/mhaya/game/game_cluster/internal/constant"
- "github.com/mhaya/game/game_cluster/internal/data"
- "github.com/mhaya/game/game_cluster/internal/mdb"
- "math"
- "math/rand"
- )
- // sdk平台类型
- const (
- DevMode int = 1 // 开发模式,注册开发帐号登陆(开发时使用)
- QuickSDK int = 2 // quick sdk
- )
- var (
- invokeMaps = make(map[int]Invoke)
- )
- type (
- Invoke interface {
- SdkId() int // sdk id
- Login(config *data.SdkConfigRow, params Params, callback Callback) // Login 登录验证接口
- PayCallback(config *data.SdkConfigRow, c *mhayaGin.Context) // PayCallback 支付回调接口
- }
- Params map[string]string
- Callback func(code int32, result Params, error ...error)
- )
- func (p Params) GetInt(key string, defaultValue ...int) int {
- defVal := 0
- if len(defaultValue) > 0 {
- defVal = defaultValue[0]
- }
- val, found := p[key]
- if !found {
- return defVal
- }
- intVal, ok := mhayaString.ToInt(val)
- if ok {
- return intVal
- }
- return defVal
- }
- func (p Params) GetString(key string) (string, bool) {
- v, ok := p[key]
- return v, ok
- }
- func register(invoke Invoke) {
- invokeMaps[invoke.SdkId()] = invoke
- }
- func GetInvoke(sdkId int) (invoke Invoke, error error) {
- invoke, found := invokeMaps[sdkId]
- if found == false {
- return nil, mhayaError.Errorf("[sdkId = %d] not found.", sdkId)
- }
- return invoke, nil
- }
- func Init(app cfacade.IApplication) {
- register(quickSdk{app})
- }
- func SetNode(app cfacade.IApplication) (string, int32) {
- var nodeId string
- list := app.Discovery().ListByType("game")
- lNode := len(list)
- key := 0
- if lNode == 0 {
- return "", code.AccountBindFail
- } else if lNode > 1 {
- key = rand.Intn(lNode)
- }
- nodeId = list[key].GetNodeId()
- if lNode > 0 {
- m, err := mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
- if err != nil {
- nodeId = list[key].GetNodeId()
- }
- if len(m) == 0 {
- nodeId = list[key].GetNodeId()
- } else {
- num := math.MaxInt
- for _, v := range list {
- id := v.GetNodeId()
- total, ok := mhayaString.ToInt(m[id])
- if ok {
- if total < num {
- num = total
- nodeId = v.GetNodeId()
- }
- } else {
- mdb.RDB.HSet(context.Background(), constant.ServerLoadHKey, id, 0)
- }
- }
- }
- }
- return nodeId, code.OK
- }
|