zhengtao преди 7 месеца
родител
ревизия
9eac59ed2d
променени са 2 файла, в които са добавени 65 реда и са изтрити 4 реда
  1. 22 4
      game/game_cluster/nodes/web/sdk/quick_sdk.go
  2. 43 0
      game/game_cluster/nodes/web/sdk/sdk.go

+ 22 - 4
game/game_cluster/nodes/web/sdk/quick_sdk.go

@@ -7,8 +7,10 @@ import (
 	cfacade "github.com/mhaya/facade"
 	"github.com/mhaya/game/game_cluster/internal/code"
 	"github.com/mhaya/game/game_cluster/internal/data"
-	rpcCenter "github.com/mhaya/game/game_cluster/internal/rpc/center"
+	"github.com/mhaya/game/game_cluster/internal/mdb/models"
+	"github.com/mhaya/game/game_cluster/internal/param"
 	sessionKey "github.com/mhaya/game/game_cluster/internal/session_key"
+	clog "github.com/mhaya/logger"
 )
 
 type quickSdk struct {
@@ -50,16 +52,32 @@ func (p quickSdk) Login(config *data.SdkConfigRow, params Params, callback Callb
 
 	sign, _ := params.GetString("sign")
 
-	account := rpcCenter.RegisterAccount(p.app, openid, ip, plt, channel, sign)
-	if account == nil {
+	req := &param.LoginReq{
+		OpenID:   openid,
+		IP:       ip,
+		Platform: plt,
+		Channel:  channel,
+		Sign:     sign,
+	}
+	acc := &models.Account{}
+	account, err := acc.AccountRegisterOrLogin(req)
+	if err > 0 {
 		callback(code.LoginError, nil)
 		return
 	}
+	nodeId, ok := SetNode(p.app)
+	if code.IsFail(ok) {
+		clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", openid, nodeId, err)
+		callback(code.LoginError, nil)
+		return
+	}
+
+	targetPath := cfacade.NewChildPath(nodeId, "player", account.UserName)
 
 	callback(code.OK, map[string]string{
 		sessionKey.PlayerID:   account.UserName, //返回 quick的uid做为 open id
 		sessionKey.OpenID:     openid,
-		sessionKey.TargetPath: account.TargetPath,
+		sessionKey.TargetPath: targetPath,
 	})
 }
 

+ 43 - 0
game/game_cluster/nodes/web/sdk/sdk.go

@@ -1,11 +1,17 @@
 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平台类型
@@ -70,3 +76,40 @@ func GetInvoke(sdkId int) (invoke Invoke, error error) {
 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
+}