将游戏服务器uid与sdk分离
This commit is contained in:
1 parent
6d7e4867f5
commit
ddb55671b5
31 files changed
+457
-296
No files matched your search
+4
-1
@@ -65,7 +65,10 @@ func Run(ctx context.Context, configFile string) error {
|
||||
messageQueue := mq.NewMessageQueue(api.DISPATCH, APPID, discoveryClient)
|
||||
defer messageQueue.Close()
|
||||
|
||||
db := dao.NewDao()
|
||||
db, err := dao.NewDao()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.CloseDao()
|
||||
|
||||
_ = controller.NewController(db, discoveryClient, messageQueue)
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
dao *dao.Dao
|
||||
db *dao.Dao
|
||||
discovery *rpc.DiscoveryClient
|
||||
signRsaKey []byte
|
||||
encRsaKeyMap map[string][]byte
|
||||
@@ -28,9 +28,9 @@ type Controller struct {
|
||||
messageQueue *mq.MessageQueue
|
||||
}
|
||||
|
||||
func NewController(dao *dao.Dao, discovery *rpc.DiscoveryClient, messageQueue *mq.MessageQueue) (r *Controller) {
|
||||
func NewController(db *dao.Dao, discovery *rpc.DiscoveryClient, messageQueue *mq.MessageQueue) (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.dao = dao
|
||||
r.db = db
|
||||
r.discovery = discovery
|
||||
r.signRsaKey, r.encRsaKeyMap, r.pwdRsaKey = region.LoadRegionRsaKey()
|
||||
rsp, err := r.discovery.GetRegionEc2B(context.TODO(), &api.NullMsg{})
|
||||
@@ -168,7 +168,6 @@ func (c *Controller) registerRouter() {
|
||||
}
|
||||
engine.Use(c.authorize())
|
||||
engine.POST("/gate/token/verify", c.gateTokenVerify)
|
||||
engine.POST("/gate/player/info", c.gatePlayerInfo)
|
||||
port := config.GetConfig().HttpPort
|
||||
addr := ":" + strconv.Itoa(int(port))
|
||||
err := engine.Run(addr)
|
||||
|
||||
@@ -77,7 +77,7 @@ func GetRegionList(ec2b *random.Ec2b) *proto.QueryRegionListHttpRsp {
|
||||
RegionConfig: "pm|fk|add",
|
||||
DownloadMode: 0,
|
||||
DebugMenu: true,
|
||||
DebugLogSwitch: []int32{0, 1, 2},
|
||||
DebugLogSwitch: []int32{0},
|
||||
DebugLog: true,
|
||||
})
|
||||
endec.Xor(clientCustomConfig, dispatchXorKey)
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/dispatch/model"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -65,7 +64,7 @@ func (c *Controller) gateTokenVerify(ctx *gin.Context) {
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
account, err := c.dao.QueryAccountByField("AccountID", uint64(accountId))
|
||||
account, err := c.db.QueryAccountByField("account_id", uint64(accountId))
|
||||
if err != nil || account == nil {
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
@@ -100,86 +99,3 @@ func (c *Controller) gateTokenVerify(ctx *gin.Context) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type PlayerInfoReq struct {
|
||||
AccountId string `json:"accountId"`
|
||||
}
|
||||
|
||||
type PlayerInfoRsp struct {
|
||||
RetCode int32 `json:"retcode"`
|
||||
Message string `json:"message"`
|
||||
Data struct{} `json:"data"`
|
||||
Forbid bool `json:"forbid"`
|
||||
ForbidEndTime uint32 `json:"forbidEndTime"`
|
||||
PlayerID uint32 `json:"playerID"`
|
||||
}
|
||||
|
||||
func (c *Controller) gatePlayerInfo(ctx *gin.Context) {
|
||||
playerInfoReq := new(PlayerInfoReq)
|
||||
err := ctx.ShouldBindJSON(playerInfoReq)
|
||||
if err != nil {
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
logger.Info("gate player info, req: %v", playerInfoReq)
|
||||
accountId, err := strconv.Atoi(playerInfoReq.AccountId)
|
||||
if err != nil {
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
account, err := c.dao.QueryAccountByField("AccountID", uint64(accountId))
|
||||
if err != nil {
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
if account == nil {
|
||||
// 外部sdk登录
|
||||
redisAccountId, err := c.dao.GetAccountId()
|
||||
if err != nil {
|
||||
logger.Error("get account id error: %v", err)
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
if uint32(accountId) > redisAccountId {
|
||||
// 临时修正自增id
|
||||
err := c.dao.SetAccountId(uint32(accountId))
|
||||
if err != nil {
|
||||
logger.Error("set account id error: %v", err)
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 补账号记录
|
||||
playerID, err := c.dao.GetNextYuanShenUid()
|
||||
if err != nil {
|
||||
logger.Error("get next player id error: %v", err)
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
regAccount := &model.Account{
|
||||
AccountID: uint32(accountId),
|
||||
Username: "",
|
||||
Password: "",
|
||||
PlayerID: playerID,
|
||||
Token: "",
|
||||
ComboToken: "",
|
||||
Forbid: false,
|
||||
ForbidEndTime: 0,
|
||||
}
|
||||
_, err = c.dao.InsertAccount(regAccount)
|
||||
if err != nil {
|
||||
logger.Error("insert account error: %v", err)
|
||||
c.gateReqErrorRsp(ctx)
|
||||
return
|
||||
}
|
||||
account = regAccount
|
||||
}
|
||||
ctx.JSON(http.StatusOK, &PlayerInfoRsp{
|
||||
RetCode: 0,
|
||||
Message: "OK",
|
||||
Data: struct{}{},
|
||||
Forbid: account.Forbid,
|
||||
ForbidEndTime: account.ForbidEndTime,
|
||||
PlayerID: account.PlayerID,
|
||||
})
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (c *Controller) log8888(ctx *gin.Context) {
|
||||
logger.Error("parse client log error: %v", err)
|
||||
return
|
||||
}
|
||||
_, err = c.dao.InsertClientLog(clientLog)
|
||||
_, err = c.db.InsertClientLog(clientLog)
|
||||
if err != nil {
|
||||
logger.Error("insert client log error: %v", err)
|
||||
return
|
||||
|
||||
@@ -92,14 +92,14 @@ func (c *Controller) apiLogin(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
account, err := c.dao.QueryAccountByField("Username", username)
|
||||
account, err := c.db.QueryAccountByField("username", username)
|
||||
if err != nil {
|
||||
logger.Error("query account from db error: %v", err)
|
||||
return
|
||||
}
|
||||
if account == nil {
|
||||
// 自动注册
|
||||
accountId, err := c.dao.GetNextAccountId()
|
||||
accountId, err := c.db.GetNextAccountId()
|
||||
if err != nil {
|
||||
logger.Error("get next account id error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
@@ -107,29 +107,18 @@ func (c *Controller) apiLogin(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
playerID, err := c.dao.GetNextYuanShenUid()
|
||||
if err != nil {
|
||||
logger.Error("get next player id error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-2"
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
regAccount := &model.Account{
|
||||
AccountID: accountId,
|
||||
Username: username,
|
||||
Password: endec.Md5Str(password),
|
||||
PlayerID: playerID,
|
||||
Token: "",
|
||||
ComboToken: "",
|
||||
Forbid: false,
|
||||
ForbidEndTime: 0,
|
||||
AccountId: accountId,
|
||||
Username: username,
|
||||
Password: endec.Md5Str(password),
|
||||
Token: "",
|
||||
ComboToken: "",
|
||||
}
|
||||
_, err = c.dao.InsertAccount(regAccount)
|
||||
_, err = c.db.InsertAccount(regAccount)
|
||||
if err != nil {
|
||||
logger.Error("insert account error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-3"
|
||||
responseData.Message = "服务器内部错误:-2"
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
@@ -143,24 +132,24 @@ func (c *Controller) apiLogin(ctx *gin.Context) {
|
||||
}
|
||||
// 生成新的token
|
||||
account.Token = base64.StdEncoding.EncodeToString(random.GetRandomByte(24))
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "Token", account.Token)
|
||||
_, err = c.db.UpdateAccountFieldByFieldName("account_id", account.AccountId, "token", account.Token)
|
||||
if err != nil {
|
||||
logger.Error("update account token error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-3"
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
_, err = c.db.UpdateAccountFieldByFieldName("account_id", account.AccountId, "token_create_time", time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
logger.Error("update account token time error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-4"
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "TokenCreateTime", time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
logger.Error("update account token time error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-5"
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
responseData.Message = "OK"
|
||||
responseData.Data.Account.Uid = strconv.FormatInt(int64(account.AccountID), 10)
|
||||
responseData.Data.Account.Uid = strconv.FormatInt(int64(account.AccountId), 10)
|
||||
responseData.Data.Account.Token = account.Token
|
||||
responseData.Data.Account.Email = account.Username
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
@@ -178,7 +167,7 @@ func (c *Controller) apiVerify(ctx *gin.Context) {
|
||||
logger.Error("parse uid error: %v", err)
|
||||
return
|
||||
}
|
||||
account, err := c.dao.QueryAccountByField("AccountID", uid)
|
||||
account, err := c.db.QueryAccountByField("account_id", uid)
|
||||
if err != nil {
|
||||
logger.Error("query account from db error: %v", err)
|
||||
return
|
||||
@@ -227,7 +216,7 @@ func (c *Controller) v2Login(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
responseData := api.NewComboTokenRsp()
|
||||
account, err := c.dao.QueryAccountByField("AccountID", uid)
|
||||
account, err := c.db.QueryAccountByField("account_id", uid)
|
||||
if account == nil || account.Token != loginData.Token {
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "token错误"
|
||||
@@ -236,7 +225,7 @@ func (c *Controller) v2Login(ctx *gin.Context) {
|
||||
}
|
||||
// 生成新的comboToken
|
||||
account.ComboToken = random.GetRandomByteHexStr(20)
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "ComboToken", account.ComboToken)
|
||||
_, err = c.db.UpdateAccountFieldByFieldName("account_id", account.AccountId, "combo_token", account.ComboToken)
|
||||
if err != nil {
|
||||
logger.Error("update combo token error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
@@ -244,7 +233,7 @@ func (c *Controller) v2Login(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "ComboTokenCreateTime", time.Now().UnixMilli())
|
||||
_, err = c.db.UpdateAccountFieldByFieldName("account_id", account.AccountId, "combo_token_create_time", time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
logger.Error("update combo token time error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
|
||||
@@ -8,10 +8,8 @@ import (
|
||||
const RedisPlayerKeyPrefix = "HK4E"
|
||||
|
||||
const (
|
||||
AccountIdRedisKey = "AccountId"
|
||||
AccountIdBegin uint32 = 10000
|
||||
YuanShenUidRedisKey = "YuanShenUid"
|
||||
YuanShenUidBegin uint32 = 100000000
|
||||
AccountIdRedisKey = "AccountId"
|
||||
AccountIdBegin uint32 = 10000
|
||||
)
|
||||
|
||||
func (d *Dao) GetNextAccountId() (uint32, error) {
|
||||
@@ -26,10 +24,6 @@ func (d *Dao) SetAccountId(accountId uint32) error {
|
||||
return d.redisSet(RedisPlayerKeyPrefix+":"+AccountIdRedisKey, accountId)
|
||||
}
|
||||
|
||||
func (d *Dao) GetNextYuanShenUid() (uint32, error) {
|
||||
return d.redisInc(RedisPlayerKeyPrefix + ":" + YuanShenUidRedisKey)
|
||||
}
|
||||
|
||||
func (d *Dao) redisInc(keyName string) (uint32, error) {
|
||||
var exist int64 = 0
|
||||
var err error = nil
|
||||
@@ -42,17 +36,11 @@ func (d *Dao) redisInc(keyName string) (uint32, error) {
|
||||
return 0, err
|
||||
}
|
||||
if exist == 0 {
|
||||
var value uint32 = 0
|
||||
if keyName == RedisPlayerKeyPrefix+":"+AccountIdRedisKey {
|
||||
value = AccountIdBegin
|
||||
} else if keyName == RedisPlayerKeyPrefix+":"+YuanShenUidRedisKey {
|
||||
value = YuanShenUidBegin
|
||||
}
|
||||
var err error = nil
|
||||
if d.redisCluster != nil {
|
||||
err = d.redisCluster.Set(context.TODO(), keyName, value, 0).Err()
|
||||
err = d.redisCluster.Set(context.TODO(), keyName, AccountIdBegin, 0).Err()
|
||||
} else {
|
||||
err = d.redis.Set(context.TODO(), keyName, value, 0).Err()
|
||||
err = d.redis.Set(context.TODO(), keyName, AccountIdBegin, 0).Err()
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
||||
+6
-6
@@ -20,19 +20,19 @@ type Dao struct {
|
||||
redisCluster *redis.ClusterClient
|
||||
}
|
||||
|
||||
func NewDao() (r *Dao) {
|
||||
r = new(Dao)
|
||||
func NewDao() (*Dao, error) {
|
||||
r := new(Dao)
|
||||
|
||||
clientOptions := options.Client().ApplyURI(config.GetConfig().Database.Url).SetMinPoolSize(10).SetMaxPoolSize(100)
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
logger.Error("mongo connect error: %v", err)
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
err = client.Ping(context.TODO(), readpref.Primary())
|
||||
if err != nil {
|
||||
logger.Error("mongo ping error: %v", err)
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
r.mongo = client
|
||||
r.db = client.Database("dispatch_hk4e")
|
||||
@@ -64,10 +64,10 @@ func NewDao() (r *Dao) {
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis ping error: %v", err)
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (d *Dao) CloseDao() {
|
||||
|
||||
@@ -4,14 +4,11 @@ import "go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
type Account struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
AccountID uint32 `bson:"AccountID"`
|
||||
PlayerID uint32 `bson:"PlayerID"`
|
||||
Username string `bson:"Username"`
|
||||
Password string `bson:"Password"`
|
||||
Token string `bson:"Token"`
|
||||
TokenCreateTime uint64 `bson:"TokenCreateTime"` // 毫秒时间戳
|
||||
ComboToken string `bson:"ComboToken"`
|
||||
ComboTokenCreateTime uint64 `bson:"ComboTokenCreateTime"` // 毫秒时间戳
|
||||
Forbid bool `bson:"Forbid"`
|
||||
ForbidEndTime uint32 `bson:"ForbidEndTime"` // 秒时间戳
|
||||
AccountId uint32 `bson:"account_id"` // 账号id
|
||||
Username string `bson:"username"` // 用户名
|
||||
Password string `bson:"password"` // 密码
|
||||
Token string `bson:"token"` // 账号token
|
||||
TokenCreateTime uint64 `bson:"token_create_time"` // 毫秒时间戳
|
||||
ComboToken string `bson:"combo_token"` // 游戏服务器token
|
||||
ComboTokenCreateTime uint64 `bson:"combo_token_create_time"` // 毫秒时间戳
|
||||
}
|
||||
@@ -9,37 +9,12 @@ type Service struct {
|
||||
}
|
||||
|
||||
// UserPasswordChange 用户密码改变
|
||||
func (s *Service) UserPasswordChange(uid uint32) bool {
|
||||
func (s *Service) UserPasswordChange(accountId uint32) bool {
|
||||
// http登录态失效
|
||||
_, err := s.dao.UpdateAccountFieldByFieldName("PlayerID", uid, "TokenCreateTime", 0)
|
||||
_, err := s.dao.UpdateAccountFieldByFieldName("account_id", accountId, "token_create_time", 0)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
// TODO 游戏内登录态失效
|
||||
return true
|
||||
}
|
||||
|
||||
// ForbidUser 封号
|
||||
func (s *Service) ForbidUser(uid uint32, forbidEndTime uint64) bool {
|
||||
// 写入账号封禁信息
|
||||
_, err := s.dao.UpdateAccountFieldByFieldName("PlayerID", uid, "Forbid", true)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, err = s.dao.UpdateAccountFieldByFieldName("PlayerID", uid, "ForbidEndTime", forbidEndTime)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
// TODO 游戏强制下线
|
||||
return true
|
||||
}
|
||||
|
||||
// UnForbidUser 解封
|
||||
func (s *Service) UnForbidUser(uid uint32) bool {
|
||||
// 解除账号封禁
|
||||
_, err := s.dao.UpdateAccountFieldByFieldName("PlayerID", uid, "Forbid", false)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user