From bcf13ae137241ad6f29a0139738369766855feaf Mon Sep 17 00:00:00 2001 From: liuxiaobo <1224730913@qq.com> Date: Fri, 6 Jun 2025 00:09:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E7=99=BB=E9=99=86=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E7=94=A8=E6=88=B7=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/config/loadConfig_test.go | 13 ++-- common/constant/redis.go | 11 +++ common/model/tableOperation.go | 18 ++--- common/model/user/user.go | 14 ++-- common/model/user/userAccount.go | 10 +-- common/model/user/userResources.go | 13 ++-- common/rpcName/rpcName.go | 4 + server/chat/config/config.go | 3 +- server/chat/model/db.go | 3 +- server/client/config/config.go | 3 +- server/client/model/db.go | 27 +++---- server/db/config/config.go | 3 +- server/db/operation/db.go | 5 +- server/db/operation/operation.go | 15 ---- server/db/operation/user.go | 112 ++++++++++++++++++++++++++++ server/db/operation/userAccount.go | 2 +- server/db/operation/userResource.go | 11 +++ server/db/server/handlerUser.go | 46 +++++++----- server/db/server/processor.go | 3 + server/gate/config/config.go | 3 +- server/gate/model/db.go | 7 +- server/gate/server/service.go | 2 +- server/login/config/config.go | 3 +- server/login/model/db.go | 5 +- server/login/server/processor.go | 40 ++++++++-- server/login/server/service.go | 2 +- 26 files changed, 268 insertions(+), 110 deletions(-) create mode 100644 common/constant/redis.go create mode 100644 server/db/operation/user.go create mode 100644 server/db/operation/userResource.go diff --git a/common/config/loadConfig_test.go b/common/config/loadConfig_test.go index d848f16..544f177 100644 --- a/common/config/loadConfig_test.go +++ b/common/config/loadConfig_test.go @@ -1,16 +1,17 @@ package config import ( + "game/common/constant" "github.com/fox/fox/db" "github.com/fox/fox/log" "testing" ) const ( - redisHost = "114.132.124.145" - redisPort = "6379" - redisPassword = "fox379@@zyxi" - specialKey = "test_config" + redisHost = "114.132.124.145" + //redisPort = "6379" + //redisPassword = "fox379@@zyxi" + specialKey = "test_config" ) func initLog() { @@ -23,12 +24,12 @@ type specialConfig struct { func TestConfig(t *testing.T) { initLog() - rdb, err := db.InitRedis(redisPassword, redisHost, redisPort, 0) + rdb, err := db.InitRedis(redisPassword, redisHost, redisPort, constant.Redis0Config) if err != nil { log.Error(err.Error()) return } - comm, err := LoadCommonConfig[specialConfig](rdb) + comm, err := LoadCommonConfig[specialConfig](rdb, "", "", "") if err != nil { log.Error(err.Error()) return diff --git a/common/constant/redis.go b/common/constant/redis.go new file mode 100644 index 0000000..cddad7b --- /dev/null +++ b/common/constant/redis.go @@ -0,0 +1,11 @@ +package constant + +/* + */ +const ( + Redis0Config = 0 // redis中0号给配置信息 + Redis1User = 1 // redis中1号给玩家数据及玩家资源数据 + Redis2Account = 2 // redis中2号给玩家帐号数据 + Redis3UserBindService = 3 // redis中3号给玩家绑定的服务 + Redis4Log = 4 // redis中4号给各类需要处理的日志 +) diff --git a/common/model/tableOperation.go b/common/model/tableOperation.go index 9612c4e..8513f7f 100644 --- a/common/model/tableOperation.go +++ b/common/model/tableOperation.go @@ -22,7 +22,7 @@ type resultT[T any] struct { } type iTable interface { - GetId() uint + GetId() int64 TableName() string } @@ -43,11 +43,11 @@ func (s *TableOp[T]) tableName() string { return result.ret.TableName() } -func (s *TableOp[T]) redisKey(id uint) string { +func (s *TableOp[T]) redisKey(id int64) string { return fmt.Sprintf("%s:%d", s.tableName(), id) } -func (s *TableOp[T]) findByRedis(id uint) *T { +func (s *TableOp[T]) findByRedis(id int64) *T { if s.rds == nil { return nil } @@ -68,7 +68,7 @@ func (s *TableOp[T]) findByRedis(id uint) *T { return us } -func (s *TableOp[T]) writeRedis(id uint, t *T) { +func (s *TableOp[T]) writeRedis(id int64, t *T) { if s.rds == nil { return } @@ -79,7 +79,7 @@ func (s *TableOp[T]) writeRedis(id uint, t *T) { s.updateRedis(id, maps) } -func (s *TableOp[T]) updateRedis(id uint, maps map[string]any) { +func (s *TableOp[T]) updateRedis(id int64, maps map[string]any) { if s.rds == nil { return } @@ -89,7 +89,7 @@ func (s *TableOp[T]) updateRedis(id uint, maps map[string]any) { _ = s.rds.Expire(context.Background(), s.redisKey(id), TableExpire).Err() } -func (s *TableOp[T]) deleteRedis(id uint) { +func (s *TableOp[T]) deleteRedis(id int64) { if s.rds == nil { return } @@ -105,7 +105,7 @@ func (s *TableOp[T]) Create(t *T) (*T, pb.ErrCode) { return t, pb.ErrCode_OK } -func (s *TableOp[T]) Find(id uint) (*T, pb.ErrCode) { +func (s *TableOp[T]) Find(id int64) (*T, pb.ErrCode) { // 先从redis中查询,redis中没有则从mysql中查询 if table := s.findByRedis(id); table != nil { return table, pb.ErrCode_OK @@ -135,7 +135,7 @@ func (s *TableOp[T]) FindCondition(condition map[string]any) (*T, error) { return &result.ret, nil } -func (s *TableOp[T]) Update(id uint, updates map[string]any) (*T, pb.ErrCode) { +func (s *TableOp[T]) Update(id int64, updates map[string]any) (*T, pb.ErrCode) { var result resultT[T] err := s.db.Model(&result.ret).Where("id = ?", id).Updates(updates).Error if err != nil { @@ -146,7 +146,7 @@ func (s *TableOp[T]) Update(id uint, updates map[string]any) (*T, pb.ErrCode) { return &result.ret, pb.ErrCode_OK } -func (s *TableOp[T]) Delete(id uint) (*T, pb.ErrCode) { +func (s *TableOp[T]) Delete(id int64) (*T, pb.ErrCode) { var result resultT[T] err := s.db.Delete(&result.ret, id).Error if err != nil { diff --git a/common/model/user/user.go b/common/model/user/user.go index 136b366..428732b 100644 --- a/common/model/user/user.go +++ b/common/model/user/user.go @@ -2,15 +2,15 @@ package user // 玩家账户表 type User struct { - ID uint `gorm:"primarykey;autoIncrement" json:"id"` - accountId uint `gorm:"type:bigint;uniqueIndex;not null"` // 帐号id - Nickname string `gorm:"type:varchar(32);uniqueIndex;not null"` // 昵称 - AvatarUrl string `gorm:"type:varchar(255)"` // 头像 - AvatarFrame string `gorm:"type:varchar(255)"` // 头像框 - VipExp int32 `gorm:"type:int"` // vip经验值 + ID int64 `gorm:"primarykey;autoIncrement" json:"id"` + AccountId int64 `gorm:"uniqueIndex;not null" json:"account_id"` // 帐号id + Nickname string `gorm:"type:varchar(32);uniqueIndex;not null" json:"nickname"` // 昵称 + AvatarUrl string `gorm:"type:varchar(255)" json:"avatar_url"` // 头像 + AvatarFrame string `gorm:"type:varchar(255)" json:"avatar_frame"` // 头像框 + VipExp int32 `gorm:"type:int" json:"vip_exp"` // vip经验值 } -func (u User) GetId() uint { +func (u User) GetId() int64 { return u.ID } diff --git a/common/model/user/userAccount.go b/common/model/user/userAccount.go index ffc071b..803884b 100644 --- a/common/model/user/userAccount.go +++ b/common/model/user/userAccount.go @@ -12,7 +12,7 @@ const ( // 玩家账户表 type UserAccount struct { - ID uint `gorm:"primarykey;autoIncrement" json:"id"` + ID int64 `gorm:"primarykey;autoIncrement" json:"id"` Username string `gorm:"type:varchar(32);uniqueIndex;not null" json:"username"` // 用户名 Password string `gorm:"type:varchar(255);not null" json:"password"` // 密码哈希 Email string `gorm:"type:varchar(100)" json:"email"` // 邮箱(可选) @@ -25,7 +25,7 @@ type UserAccount struct { RegisterTime time.Time `gorm:"type:TIMESTAMP;default:CURRENT_TIMESTAMP" json:"register_time"` // 注册时间 } -func (u UserAccount) GetId() uint { +func (u UserAccount) GetId() int64 { return u.ID } @@ -35,8 +35,8 @@ func (u UserAccount) TableName() string { // 玩家登录记录表 type UserLoginLog struct { - ID uint `gorm:"primarykey;autoIncrement" json:"id"` - UID uint `gorm:"index" json:"uid"` // 关联玩家ID + ID int64 `gorm:"primarykey;autoIncrement" json:"id"` + AccountID int64 `gorm:"index" json:"account_id"` // 关联帐号ID LoginIP string `gorm:"type:varchar(45);not null" json:"login_ip"` // 登录IP LoginTime time.Time `gorm:"type:TIMESTAMP;default:CURRENT_TIMESTAMP" json:"login_time"` // 登录时间 DeviceInfo string `gorm:"type:varchar(255)" json:"device_info"` // 设备信息(JSON格式) @@ -44,7 +44,7 @@ type UserLoginLog struct { FailReason string `gorm:"type:varchar(100)" json:"fail_reason"` // 失败原因 } -func (u UserLoginLog) GetId() uint { +func (u UserLoginLog) GetId() int64 { return u.ID } diff --git a/common/model/user/userResources.go b/common/model/user/userResources.go index fbc86f0..88e88fc 100644 --- a/common/model/user/userResources.go +++ b/common/model/user/userResources.go @@ -2,16 +2,13 @@ package user // 玩家账户表 type UserResources struct { - ID uint `gorm:"primarykey;autoIncrement" json:"id"` - accountId uint `gorm:"type:bigint;uniqueIndex;not null"` // 帐号id - Nickname string `gorm:"type:varchar(32);uniqueIndex;not null"` // 昵称 - AvatarUrl string `gorm:"type:varchar(255)"` // 头像 - AvatarFrame string `gorm:"type:varchar(255)"` // 头像框 - VipExp int32 `gorm:"type:int"` // vip经验值 + UID int64 `gorm:"uniqueIndex" json:"id"` + Gold int64 `gorm:"default:0" json:"gold"` // 金币 + Diamond int64 `gorm:"default:0" json:"diamond"` // 钻石 } -func (u UserResources) GetId() uint { - return u.ID +func (u UserResources) GetId() int64 { + return u.UID } func (u UserResources) TableName() string { diff --git a/common/rpcName/rpcName.go b/common/rpcName/rpcName.go index 7d2029a..9f8f649 100644 --- a/common/rpcName/rpcName.go +++ b/common/rpcName/rpcName.go @@ -1,8 +1,12 @@ package rpcName const ( + // 用户、用户资源及帐号相关操作 GetUserAccount = "get.user.account.rpc" CreateUserAccount = "create.user.account.rpc" UpdateUserPassword = "update.user.password.rpc" LogUserAccountLogin = "user.login.rpc" + GetUserByUid = "get.user.uid.rpc" + GetUserByAccountId = "get.user.account.id.rpc" + GetUserResources = "get.user.resources.rpc" ) diff --git a/server/chat/config/config.go b/server/chat/config/config.go index 7e9c86a..3c60fcb 100644 --- a/server/chat/config/config.go +++ b/server/chat/config/config.go @@ -2,6 +2,7 @@ package config import ( "game/common/config" + "game/common/constant" "github.com/fox/fox/db" "github.com/fox/fox/log" ) @@ -22,7 +23,7 @@ func InitLog() { func LoadConfig(GitCommit, GitBranch, BuildDate string) { Command = config.ParseCommand() - rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0) + rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config) if err != nil { log.Error(err.Error()) return diff --git a/server/chat/model/db.go b/server/chat/model/db.go index 5bcba4b..10b0868 100644 --- a/server/chat/model/db.go +++ b/server/chat/model/db.go @@ -1,6 +1,7 @@ package model import ( + "game/common/constant" "game/server/chat/config" "github.com/fox/fox/db" "github.com/fox/fox/log" @@ -11,7 +12,7 @@ var UserRedis *redis.Client var err error func InitRedis() { - UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, 0) + UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, constant.Redis1User) if err != nil { log.Fatal(err.Error()) return diff --git a/server/client/config/config.go b/server/client/config/config.go index 1b5642f..d6233d3 100644 --- a/server/client/config/config.go +++ b/server/client/config/config.go @@ -2,6 +2,7 @@ package config import ( "game/common/config" + "game/common/constant" "github.com/fox/fox/db" "github.com/fox/fox/log" ) @@ -25,7 +26,7 @@ func initLog() { func LoadConfig(GitCommit, GitBranch, BuildDate string) { Command = config.ParseCommand() initLog() - rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0) + rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config) if err != nil { log.Error(err.Error()) return diff --git a/server/client/model/db.go b/server/client/model/db.go index 2992a1d..62b48d9 100644 --- a/server/client/model/db.go +++ b/server/client/model/db.go @@ -1,19 +1,12 @@ package model -import ( - "game/server/client/config" - "github.com/fox/fox/db" - "github.com/fox/fox/log" - "github.com/go-redis/redis/v8" -) - -var UserRedis *redis.Client -var err error - -func InitRedis() { - UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, 0) - if err != nil { - log.Fatal(err.Error()) - return - } -} +//var UserRedis *redis.Client +//var err error +// +//func InitRedis() { +// UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, 0) +// if err != nil { +// log.Fatal(err.Error()) +// return +// } +//} diff --git a/server/db/config/config.go b/server/db/config/config.go index ab93548..923d3a0 100644 --- a/server/db/config/config.go +++ b/server/db/config/config.go @@ -2,6 +2,7 @@ package config import ( "game/common/config" + "game/common/constant" "github.com/fox/fox/db" "github.com/fox/fox/log" ) @@ -22,7 +23,7 @@ func InitLog() { func LoadConfig(GitCommit, GitBranch, BuildDate string) { Command = config.ParseCommand() - rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0) + rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config) if err != nil { log.Error(err.Error()) return diff --git a/server/db/operation/db.go b/server/db/operation/db.go index 404324e..3b62d04 100644 --- a/server/db/operation/db.go +++ b/server/db/operation/db.go @@ -1,6 +1,7 @@ package operation import ( + "game/common/constant" "game/common/model/user" "game/common/utils" "game/server/db/config" @@ -21,13 +22,13 @@ func InitRedis() { log.Debug("init redis") var err error cfg := &config.Cfg.Redis - UserRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, 0) + UserRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, constant.Redis1User) if err != nil { log.Fatal(err.Error()) return } utils.AutoSetRedisPool(UserRedis) - AccountRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, 1) + AccountRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, constant.Redis2Account) if err != nil { log.Fatal(err.Error()) return diff --git a/server/db/operation/operation.go b/server/db/operation/operation.go index 9aa9638..d820bc3 100644 --- a/server/db/operation/operation.go +++ b/server/db/operation/operation.go @@ -1,16 +1 @@ package operation - -import ( - "game/common/model" - "game/common/model/user" -) - -// 玩家表 -func NewUserOp() *model.TableOp[user.User] { - return model.NewTableOp[user.User](UserDB, UserRedis) -} - -// 玩家资源表 -func NewUserResourcesOp() *model.TableOp[user.UserResources] { - return model.NewTableOp[user.UserResources](UserDB, UserRedis) -} diff --git a/server/db/operation/user.go b/server/db/operation/user.go new file mode 100644 index 0000000..a448b69 --- /dev/null +++ b/server/db/operation/user.go @@ -0,0 +1,112 @@ +package operation + +import ( + "context" + "errors" + "fmt" + "game/common/model" + "game/common/model/user" + "game/common/proto/pb" + "game/common/serialization" + "game/common/utils" + "github.com/fox/fox/log" + "github.com/fox/fox/xrand" + "github.com/go-redis/redis/v8" + "github.com/google/uuid" + "gorm.io/gorm" + "strconv" +) + +type UserOp struct { + db *gorm.DB + userRedis *redis.Client + *model.TableOp[user.User] +} + +// 玩家表 +func NewUserOp() *UserOp { + return &UserOp{ + db: UserDB, + userRedis: UserRedis, + TableOp: model.NewTableOp[user.User](UserDB, UserRedis), + } +} + +func (s *UserOp) redisKey(accountId int64) string { + return fmt.Sprintf("account_id:%v", accountId) +} + +func (s *UserOp) GetUserByAccountId(accountId int64) (*user.User, pb.ErrCode) { + sUid, err := s.userRedis.Get(context.Background(), s.redisKey(accountId)).Result() + if err != nil { + if errors.Is(err, redis.Nil) { + us := &user.User{} + err = s.db.Where("account_id = ?", accountId).First(us).Error + if err != nil { + log.DebugF("find user by account id:%v err:%v", accountId, err) + return nil, pb.ErrCode_SystemErr + } + // 从db中查到后写入redis,并建立索引 + if us.ID > 0 { + _, _ = s.Update(us.ID, serialization.StructToMap(us)) + _ = s.userRedis.Set(context.Background(), s.redisKey(accountId), us.ID, model.TableExpire).Err() + } else { + // db中没有则创建玩家 + return s.createUserAccountId(accountId) + } + return us, pb.ErrCode_OK + } else { + log.ErrorF("find user by account id:%v err:%v", accountId, err) + return nil, pb.ErrCode_SystemErr + } + } + uid, _ := strconv.ParseInt(sUid, 10, 64) + if uid < 0 { + log.ErrorF("get user failed, account id:%d, uid:%v", accountId, sUid) + return nil, pb.ErrCode_SystemErr + } + return s.Find(uid) +} + +func (s *UserOp) generateUniqueNickname(db *gorm.DB) (string, error) { + // 基础昵称模板 + baseNick := "用户_" + uuid.New().String()[:8] + nickname := baseNick + + // 检查是否唯一,不唯一则追加随机字符 + for i := 0; i < 5; i++ { // 最多尝试5次 + var count int64 + err := db.Model(&user.User{}).Where("nickname = ?", nickname).Count(&count).Error + if err != nil { + return "", err + } + if count == 0 { + return nickname, nil + } + nickname = baseNick + xrand.RandomString(2) // 追加2位随机字符 + } + + return "", fmt.Errorf("无法生成唯一昵称") +} + +// 创建用户 +func (s *UserOp) createUserAccountId(accountId int64) (*user.User, pb.ErrCode) { + nickname, err := s.generateUniqueNickname(s.db) + if err != nil { + log.ErrorF("generate unique nickname err:%v", err) + return nil, pb.ErrCode_SystemErr + } + us := &user.User{ + AccountId: accountId, + Nickname: nickname, + } + var code pb.ErrCode + us, code = s.Create(us) + log.DebugF("create user:%v", utils.JsonMarshal(us)) + if code != pb.ErrCode_OK { + return nil, code + } + // 建立索引 + _ = s.userRedis.Set(context.Background(), s.redisKey(us.AccountId), us.ID, model.TableExpire).Err() + return us, pb.ErrCode_OK +} diff --git a/server/db/operation/userAccount.go b/server/db/operation/userAccount.go index 04a21eb..28ff0ef 100644 --- a/server/db/operation/userAccount.go +++ b/server/db/operation/userAccount.go @@ -61,7 +61,7 @@ func (s *UserAccountOp) GetUserAccount(username string) (*user.UserAccount, pb.E log.ErrorF("get user account:%v failed, uid is: %d", username, uid) return nil, pb.ErrCode_SystemErr } - return s.accountOp.Find(uint(uid)) + return s.accountOp.Find(uid) } // 创建用户 diff --git a/server/db/operation/userResource.go b/server/db/operation/userResource.go new file mode 100644 index 0000000..515fc63 --- /dev/null +++ b/server/db/operation/userResource.go @@ -0,0 +1,11 @@ +package operation + +import ( + "game/common/model" + "game/common/model/user" +) + +// 玩家资源表 +func NewUserResourcesOp() *model.TableOp[user.UserResources] { + return model.NewTableOp[user.UserResources](UserDB, UserRedis) +} diff --git a/server/db/server/handlerUser.go b/server/db/server/handlerUser.go index 5a44191..a13511f 100644 --- a/server/db/server/handlerUser.go +++ b/server/db/server/handlerUser.go @@ -63,22 +63,30 @@ func (s *DbService) onLogUserAccountLogin(iMsg *ipb.InternalMsg) *ipb.InternalMs return iMsg } -//func (s *DbService) operation(iMsg *ipb.InternalMsg, operation func(us *user.UserAccount) (*user.UserAccount, pb.ErrCode)) { -// us := &user.UserAccount{} -// err := json.Unmarshal(iMsg.Msg, us) -// if err != nil { -// log.ErrorF("error unmarshalling user account %v", err) -// return -// } -// var code pb.ErrCode -// us, code = operation(us) -// if code != pb.ErrCode_OK { -// return -// } -// iMsg.Msg, err = json.Marshal(us) -// if err != nil { -// log.ErrorF("error marshalling user account %v", err) -// return -// } -// return -//} +// 获取用户数据,没有则创建 +func (s *DbService) onGetUserByAccountId(iMsg *ipb.InternalMsg) *ipb.InternalMsg { + operationDb[user.User](iMsg, func(us *user.User) (*user.User, pb.ErrCode) { + return operation.NewUserOp().GetUserByAccountId(us.AccountId) + }) + return iMsg +} + +// 获取用户数据,没有则创建 +func (s *DbService) onGetUserByUid(iMsg *ipb.InternalMsg) *ipb.InternalMsg { + operationDb[user.User](iMsg, func(us *user.User) (*user.User, pb.ErrCode) { + return operation.NewUserOp().Find(us.ID) + }) + return iMsg +} + +// 获取用户数据,没有则创建 +func (s *DbService) onGetUserResources(iMsg *ipb.InternalMsg) *ipb.InternalMsg { + operationDb[user.UserResources](iMsg, func(res *user.UserResources) (*user.UserResources, pb.ErrCode) { + if us1, code := operation.NewUserResourcesOp().Find(res.UID); code != pb.ErrCode_OK { + return operation.NewUserResourcesOp().Create(res) + } else { + return us1, code + } + }) + return iMsg +} diff --git a/server/db/server/processor.go b/server/db/server/processor.go index 4952786..cecb539 100644 --- a/server/db/server/processor.go +++ b/server/db/server/processor.go @@ -11,5 +11,8 @@ func (s *DbService) initRpcProcessor() { rpcName.GetUserAccount: s.onGetUserAccount, rpcName.UpdateUserPassword: s.onUpdateUserAccount, rpcName.LogUserAccountLogin: s.onLogUserAccountLogin, + rpcName.GetUserByUid: s.onGetUserByUid, + rpcName.GetUserByAccountId: s.onGetUserByAccountId, + rpcName.GetUserResources: s.onGetUserResources, }) } diff --git a/server/gate/config/config.go b/server/gate/config/config.go index ef23eca..e6a4f2f 100644 --- a/server/gate/config/config.go +++ b/server/gate/config/config.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "game/common/config" + "game/common/constant" "github.com/fox/fox/db" "github.com/fox/fox/log" ) @@ -31,7 +32,7 @@ func InitLog() { func LoadConfig(GitCommit, GitBranch, BuildDate string) { Command = config.ParseCommand() - rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0) + rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config) if err != nil { log.Error(err.Error()) return diff --git a/server/gate/model/db.go b/server/gate/model/db.go index 8ba7029..524cacf 100644 --- a/server/gate/model/db.go +++ b/server/gate/model/db.go @@ -1,6 +1,7 @@ package model import ( + "game/common/constant" "game/common/utils" "game/server/gate/config" "github.com/fox/fox/db" @@ -8,14 +9,14 @@ import ( "github.com/go-redis/redis/v8" ) -var UserRedis *redis.Client +var UserBindServiceRedis *redis.Client var err error func InitRedis() { - UserRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, 0) + UserBindServiceRedis, err = db.InitRedis(config.Cfg.Redis.Password, config.Cfg.Redis.Host, config.Cfg.Redis.Port, constant.Redis3UserBindService) if err != nil { log.Fatal(err.Error()) return } - utils.AutoSetRedisPool(UserRedis) + utils.AutoSetRedisPool(UserBindServiceRedis) } diff --git a/server/gate/server/service.go b/server/gate/server/service.go index 01a08a9..7998c96 100644 --- a/server/gate/server/service.go +++ b/server/gate/server/service.go @@ -71,7 +71,7 @@ func newGateService(serviceId int) *GateService { } wsAddress := config.Cfg.Special.Address[addressPos] s.wss = ws.NewWsServer(wsAddress, s.WsOnMessage, s.WsOnDisconnect) - s.bindService = userBindService.NewUserBindService(model.UserRedis, s.ServiceEtcd()) + s.bindService = userBindService.NewUserBindService(model.UserBindServiceRedis, s.ServiceEtcd()) s.processor = processor.NewProcessor() s.initProcessor() diff --git a/server/login/config/config.go b/server/login/config/config.go index 7b60738..d0837dd 100644 --- a/server/login/config/config.go +++ b/server/login/config/config.go @@ -2,6 +2,7 @@ package config import ( "game/common/config" + "game/common/constant" "github.com/fox/fox/db" "github.com/fox/fox/log" ) @@ -22,7 +23,7 @@ func InitLog() { func LoadConfig(GitCommit, GitBranch, BuildDate string) { Command = config.ParseCommand() - rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, 0) + rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config) if err != nil { log.Error(err.Error()) return diff --git a/server/login/model/db.go b/server/login/model/db.go index 98ab41a..fbaf4b1 100644 --- a/server/login/model/db.go +++ b/server/login/model/db.go @@ -1,6 +1,7 @@ package model import ( + "game/common/constant" "game/server/login/config" "github.com/fox/fox/db" "github.com/fox/fox/log" @@ -8,7 +9,7 @@ import ( ) var ( - UserRedis *redis.Client + UserBindServiceRedis *redis.Client //UserDB *gorm.DB //LogDB *gorm.DB ) @@ -17,7 +18,7 @@ func InitRedis() { log.Debug("init redis") var err error cfg := &config.Cfg.Redis - UserRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, 0) + UserBindServiceRedis, err = db.InitRedis(cfg.Password, cfg.Host, cfg.Port, constant.Redis3UserBindService) if err != nil { log.Fatal(err.Error()) return diff --git a/server/login/server/processor.go b/server/login/server/processor.go index c677960..f8ff444 100644 --- a/server/login/server/processor.go +++ b/server/login/server/processor.go @@ -88,7 +88,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User } // 生成JWT令牌(简化版) -func generateToken(userID uint, username string) (string, error) { +func generateToken(userID int64, username string) (string, error) { _ = userID _ = username // 这里应该使用JWT库生成实际令牌 @@ -96,22 +96,46 @@ func generateToken(userID uint, username string) (string, error) { return "generated-token-placeholder", nil } +// 获取用户数据,如果没有则创建 +func (s *LoginService) getUser(accountId int64, tName string) (*user.User, pb.ErrCode) { + us := &user.User{ + AccountId: accountId, + } + rpcMsg := ipb.MakeRpcMsg[user.User](rpcName.GetUserByAccountId, 0, us) + rsp, err := s.Call(service.RpcTopicEx(tName), timeout, rpcMsg) + if err != nil { + log.ErrorF(s.Log("call rpc:%v err:%s", rpcMsg.RpcMsgId, err.Error())) + return nil, pb.ErrCode_SystemErr + } + _ = json.Unmarshal(rsp.Msg, us) + if us.ID == 0 { + log.ErrorF(s.Log("call rpc:%v return:%v", rpcMsg.RpcMsgId, string(rsp.Msg))) + return us, pb.ErrCode_SystemErr + } + return us, pb.ErrCode_OK +} + // 登录或注册 func (s *LoginService) onLoginOrRegister(iMsg *ipb.InternalMsg, req *pb.C2SUserLogin) { ksync.GoSafe(func() { - us, code, node := s.checkLoginOrRegister(req) + account, code, node := s.checkLoginOrRegister(req) userId := int64(0) rsp := &pb.S2CUserLogin{Code: code} - if us != nil && code == pb.ErrCode_OK { - rsp.UserId = int64(us.ID) - rsp.Token, _ = generateToken(us.ID, us.Username) - userId = rsp.UserId + if account != nil && code == pb.ErrCode_OK { + // 拉取用户数据 + us := &user.User{} + us, code = s.getUser(userId, req.Username) + if code == pb.ErrCode_OK { + rsp.UserId = us.ID + rsp.Token, _ = generateToken(account.ID, account.Username) + userId = rsp.UserId + } } s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, userId, int32(pb.MsgId_S2CUserLoginId), rsp) - if us != nil && us.ID > 0 { + if account != nil && account.ID > 0 { loginLog := &user.UserLoginLog{ - UID: us.ID, + AccountID: account.ID, LoginIP: req.Ip, LoginTime: time.Now(), DeviceInfo: req.DeviceId, diff --git a/server/login/server/service.go b/server/login/server/service.go index 34badc4..8e131fd 100644 --- a/server/login/server/service.go +++ b/server/login/server/service.go @@ -62,7 +62,7 @@ func newLoginService(serviceId int) *LoginService { return nil } - s.bindService = userBindService.NewUserBindService(model.UserRedis, s.ServiceEtcd()) + s.bindService = userBindService.NewUserBindService(model.UserBindServiceRedis, s.ServiceEtcd()) s.processor = processor.NewProcessor() s.initProcessor() s.OnInit()