From 101d1cc1d6942fce43f18df8a5f27f50022c8f18 Mon Sep 17 00:00:00 2001 From: zqj <582132116@qq.com> Date: Mon, 11 Apr 2022 15:43:25 +0800 Subject: [PATCH] feat: 一个基于redis的自增ID功能。 --- cmd/gameserver/action/RoleAction.go | 10 +++++++--- cmd/gameserver/service/game.go | 17 +++++++++++++++-- cmd/httpserver/AccountAction.go | 8 ++++++-- cmd/httpserver/http.go | 18 ++++++++++++++++-- common/commonFunc.go | 41 +++++++++++++++++++++++++++++++++++++++++ common/commonFunc_test.go | 18 ++++++++++++++++++ common/conf.go | 2 +- common/const.go | 11 ++++++----- common/db/mongoproxy/mongoplugin.go | 32 ++++++++++++++++---------------- common/db/redisproxy/redis.go | 34 +++++++++++++++++++++++++--------- conf/conf.yaml | 22 +++++++++++++++++++--- models/dbseed.go | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/increment.go | 23 +++++++++++++++++++++++ models/role.go | 29 +++++++++++++++++++++-------- models/schema.go | 24 +++++++++++++++++------- pb/models.pb.go | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------- protos | 2 +- 17 files changed, 511 insertions(+), 97 deletions(-) create mode 100644 common/commonFunc.go create mode 100644 common/commonFunc_test.go create mode 100644 models/dbseed.go create mode 100644 models/increment.go diff --git a/cmd/gameserver/action/RoleAction.go b/cmd/gameserver/action/RoleAction.go index 4cb248a..af85c20 100644 --- a/cmd/gameserver/action/RoleAction.go +++ b/cmd/gameserver/action/RoleAction.go @@ -39,14 +39,18 @@ func CreateRpc(agent components.IAgent, msg components.IMessage) (int32, interfa req := pb.CreateReq{} if err := proto.Unmarshal(msg.GetData(), &req); err != nil { logger.Error("CreateRpc err: %v", err) - return 1, nil + return 1, err } role := models.RoleExistByUid(req.Token) if role != nil { - return 2, nil + return 2, "role exists" + } + + roleId, err := common.GetNextRoleId() + if err != nil { + return 3, err } - roleId := common.SnowFlack.NextValStr() role = models.NewRole(roleId) role.Role.Uid = req.Token role.Role.Nick = getRandomName() diff --git a/cmd/gameserver/service/game.go b/cmd/gameserver/service/game.go index c059d35..6fa80e2 100644 --- a/cmd/gameserver/service/game.go +++ b/cmd/gameserver/service/game.go @@ -49,7 +49,6 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { if err != nil { return nil, err } - models.InitGameModels() //redis init if err = redisproxy.ConnectRedis(sconf.RedisConf.DB, sconf.RedisConf.Auth, sconf.RedisConf.Address); err != nil { @@ -61,15 +60,29 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { if err != nil { return nil, err } - s.EtcdClient.PutWithLeasePrefix(common.GlobalConf.GameConf.Name, common.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", common.GlobalConf.GameConf.IP, common.GlobalConf.GameConf.Port), 5) + s.EtcdClient.PutWithLeasePrefix(common.GlobalConf.GameConf.Name, fmt.Sprintf("%d", common.GlobalConf.GameConf.ID), fmt.Sprintf("%s:%d", common.GlobalConf.GameConf.IP, common.GlobalConf.GameConf.Port), 5) + + //设置服务器ID & game + models.NewDBSeed(sconf.ID).InitServerDatabase(models.GameModels()) + models.DBSeedS().InitAutoIncreUidTable(models.GameModels()) return s, nil } +func (s *GameServer) Timeout() { + //每秒中保存一下自增ID + models.DBSeedS().SaveAutoincrementTimer(models.GameModels()) + + components.TimeOut(1*time.Second, s.Timeout) +} + func (s *GameServer) Start() error { //设置随机种子 rand.Seed(time.Now().Unix()) + //启动定时器 + s.Timeout() + return s.IServer.Start() } diff --git a/cmd/httpserver/AccountAction.go b/cmd/httpserver/AccountAction.go index b8aabe6..106c8ef 100644 --- a/cmd/httpserver/AccountAction.go +++ b/cmd/httpserver/AccountAction.go @@ -37,9 +37,13 @@ func (h *AccountAction) Register(c *gin.Context) (int, interface{}) { return 3, "account exists: " + register.Phone } - account.Uid = common.SnowFlack.NextValStr() + uid, err := common.GetNextUId() + if err != nil { + return 4, "uid get error: " + err.Error() + } + account.Uid = uid account.Password = common.Md5V(register.Password) - if err := account.Create(); err != nil { + if err = account.Create(); err != nil { return 4, "account register err: " + err.Error() } account.Password = register.Password diff --git a/cmd/httpserver/http.go b/cmd/httpserver/http.go index 1589c34..872f5c0 100644 --- a/cmd/httpserver/http.go +++ b/cmd/httpserver/http.go @@ -12,11 +12,13 @@ import ( "pro2d/common/logger" "pro2d/models" "syscall" + "time" ) type AccountServer struct { components.IHttp EtcdClient *etcd.EtcdClient + Sconf *common.SConf } func NewAccountServer(version string, port ...string) *AccountServer { @@ -24,6 +26,8 @@ func NewAccountServer(version string, port ...string) *AccountServer { } func (s *AccountServer) Init(sconf *common.SConf) error { + s.Sconf = sconf + //mgo init err := mongoproxy.ConnectMongo(sconf.MongoConf) @@ -37,17 +41,27 @@ func (s *AccountServer) Init(sconf *common.SConf) error { if err != nil { return err } - models.InitAccountModels() + models.NewDBSeed(sconf.ID).InitServerDatabase(models.AccountModels()) + models.DBSeedS().InitAutoIncreUidTable(models.AccountModels()) - s.EtcdClient.PutWithLeasePrefix(sconf.Name, sconf.ID, fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) + s.EtcdClient.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5) return nil } +func (s *AccountServer) TimeOut() { + models.DBSeedS().SaveAutoincrementTimer(models.AccountModels()) + + components.TimeOut(1*time.Second, s.TimeOut) +} + func (s *AccountServer) Start() error { if err := s.Init(common.GlobalConf.AccountConf); err != nil { return err } + //开始定时器 + s.TimeOut() + return s.IHttp.Start() } diff --git a/common/commonFunc.go b/common/commonFunc.go new file mode 100644 index 0000000..f62220c --- /dev/null +++ b/common/commonFunc.go @@ -0,0 +1,41 @@ +package common + +import ( + "fmt" + "github.com/garyburd/redigo/redis" + "pro2d/common/db/redisproxy" +) + +func GetNextRoleId() (string, error) { + relay, err := redisproxy.HGET(AutoIncrement, "role") + if err != nil { + return "", err + } + + relay, err = redisproxy.HINCRBY(AutoIncrement, "role", 1) + ID, err := redis.Int64(relay, err) + if err != nil { + return "", err + } + return fmt.Sprintf("%d", ID), nil +} + +func GetNextUId() (string, error) { + relay, err := redisproxy.HGET(AutoIncrement, "uid") + if err != nil { + return "", err + } + + var ID int64 = 0 + if relay == nil { + ID = 90000 + redisproxy.HSET(AutoIncrement, "uid", ID) + } else { + relay, err = redisproxy.HINCRBY(AutoIncrement, "uid", 1) + ID, err = redis.Int64(relay, err) + if err != nil { + return "", err + } + } + return fmt.Sprintf("%d", ID), nil +} diff --git a/common/commonFunc_test.go b/common/commonFunc_test.go new file mode 100644 index 0000000..1c7bee6 --- /dev/null +++ b/common/commonFunc_test.go @@ -0,0 +1,18 @@ +package common + +import ( + "fmt" + "pro2d/common/db/redisproxy" + "pro2d/common/logger" + "testing" +) + +func TestGetNextRoleId(t *testing.T) { + if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil { + logger.Error(err) + return + } + + fmt.Println(GetNextRoleId()) + fmt.Println(GetNextUId()) +} diff --git a/common/conf.go b/common/conf.go index 1fe4277..c490848 100644 --- a/common/conf.go +++ b/common/conf.go @@ -32,7 +32,7 @@ type MongoConf struct { } type SConf struct { - ID string `yaml:"id"` + ID int64 `yaml:"id"` Name string `yaml:"name"` IP string `yaml:"ip"` Port int `yaml:"port"` diff --git a/common/const.go b/common/const.go index edf464e..c40f118 100644 --- a/common/const.go +++ b/common/const.go @@ -12,11 +12,12 @@ const ( //保存数据时间 SaveDataInterval = 5 //s - //id相关 - MaxRoleNum = 1000000 - MaxUidNum = 1000000 - MaxHeroNum = 1000000 - MaxTeamNum = 1000000 + //自增id相关 + MaxCommNum = 1000000 + MaxUidNum = 90000 + + AutoIncrement = "pro2d_autoincrement_set" + AutoIncrementHero = "pro2d_autoincrement:hero" //gm参数属性 Role_ = "_role" diff --git a/common/db/mongoproxy/mongoplugin.go b/common/db/mongoproxy/mongoplugin.go index edecc5b..e146258 100644 --- a/common/db/mongoproxy/mongoplugin.go +++ b/common/db/mongoproxy/mongoplugin.go @@ -22,15 +22,15 @@ func DB() *mongo.Database { func ConnectMongo(conf *common.MongoConf) error { var uri string - if conf.User!= "" { + if conf.User != "" { //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/?w=majority", conf.User, conf.Password, conf.Host, conf.Port) - }else { + } else { //uri = fmt.Sprintf("mongodb://%s:%d/%s?w=majority", conf.Host, conf.Port, conf.DBName) uri = fmt.Sprintf("mongodb://%s:%d/?w=majority", conf.Host, conf.Port) } // 设置连接超时时间 - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.TimeOut) * time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.TimeOut)*time.Second) defer cancel() // 通过传进来的uri连接相关的配置 o := options.Client().ApplyURI(uri) @@ -51,7 +51,7 @@ func ConnectMongo(conf *common.MongoConf) error { return nil } -func CloseMongo(){ +func CloseMongo() { mongoClient.Disconnect(context.TODO()) } @@ -66,13 +66,13 @@ func CreateTable(tb string) error { return DB().CreateCollection(context.TODO(), tb) } -func FindOne(pri interface{}, schema interface{}) error { - r := mongoDatabase.Collection(GetCollName(schema)).FindOne(context.TODO(), pri) +func FindOne(coll string, pri interface{}, schema interface{}) error { + r := mongoDatabase.Collection(coll).FindOne(context.TODO(), pri) return r.Decode(schema) } func FindMany(coll string, key string, val interface{}, schema interface{}) error { - r, err := mongoDatabase.Collection(coll).Find(context.TODO(), bson.M{key:val}) + r, err := mongoDatabase.Collection(coll).Find(context.TODO(), bson.M{key: val}) if err != nil { return err } @@ -80,14 +80,14 @@ func FindMany(coll string, key string, val interface{}, schema interface{}) erro } func GetBsonD(key string, value interface{}) interface{} { - return bson.D{ {key, value}} + return bson.D{{key, value}} } -func GetBsonM(key string, value interface{}) interface{} { +func GetBsonM(key string, value interface{}) interface{} { return bson.M{key: value} } -func GetSchemaType(schema interface{}) reflect.Type { +func GetSchemaType(schema interface{}) reflect.Type { s := reflect.TypeOf(schema) if s.Kind() == reflect.Ptr { s = reflect.TypeOf(schema).Elem() @@ -112,7 +112,7 @@ func GetPriKey(schema interface{}) string { return pri } -func FindIndex(schema interface{}) (string, []string){ +func FindIndex(schema interface{}) (string, []string) { s := GetSchemaType(schema) var index []string @@ -128,21 +128,21 @@ func FindIndex(schema interface{}) (string, []string){ return strings.ToLower(s.Name()), index } -func SetUnique(coll, key string) (string, error){ +func SetUnique(coll, key string) (string, error) { return DB().Collection(coll).Indexes().CreateOne( context.TODO(), mongo.IndexModel{ - Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, + Keys: bsonx.Doc{{key, bsonx.Int32(1)}}, Options: options.Index().SetUnique(true), }, ) } -func InitDoc(schema ...interface{}) { +func InitDoc(schema ...interface{}) { for _, s := range schema { coll, keys := FindIndex(s) CreateTable(coll) - for _, index := range keys { + for _, index := range keys { logger.Debug("InitDoc collect: %v, createIndex: %s", coll, index) res, err := SetUnique(coll, index) @@ -153,4 +153,4 @@ func InitDoc(schema ...interface{}) { } } -} \ No newline at end of file +} diff --git a/common/db/redisproxy/redis.go b/common/db/redisproxy/redis.go index 666d811..f53a142 100644 --- a/common/db/redisproxy/redis.go +++ b/common/db/redisproxy/redis.go @@ -4,6 +4,7 @@ import ( "github.com/garyburd/redigo/redis" "time" ) + var RedisPool *redis.Pool //conf *conf.ServerConf @@ -11,7 +12,7 @@ func ConnectRedis(db int, auth, address string) error { RedisPool = &redis.Pool{ //最大活跃连接数,0代表无限 MaxActive: 888, - MaxIdle: 20, + MaxIdle: 20, //闲置连接的超时时间 IdleTimeout: time.Second * 100, //定义拨号获得连接的函数 @@ -20,35 +21,34 @@ func ConnectRedis(db int, auth, address string) error { if auth != "" { option = append(option, redis.DialPassword(auth)) } - return redis.Dial("tcp",address, option...) + return redis.Dial("tcp", address, option...) }, } return nil } -func CloseRedis() { +func CloseRedis() { RedisPool.Close() } func redisCommand(command string, args ...interface{}) (reply interface{}, err error) { conn := RedisPool.Get() defer conn.Close() - return conn.Do(command , args...) + return conn.Do(command, args...) } - func ExpireKey(key interface{}, ttl interface{}) (reply interface{}, err error) { return redisCommand("expire", key, ttl) } //redis 管道操作 -func PipLine(f func(conn redis.Conn)) { +func PipLine(f func(conn redis.Conn)) { conn := RedisPool.Get() defer conn.Close() f(conn) } -func PipLineTest() { +func PipLineTest() { PipLine(func(c redis.Conn) { c.Send("SET", "foo", "bar") c.Send("GET", "foo") @@ -79,6 +79,22 @@ func HKEYS(args ...interface{}) (reply interface{}, err error) { return redisCommand("HKEYS", args...) } -func HMSET(args ...interface{}) (reply interface{}, err error) { +func HMSET(args ...interface{}) (reply interface{}, err error) { return redisCommand("HMSET", args...) -} \ No newline at end of file +} + +func HMGET(args ...interface{}) (reply interface{}, err error) { + return redisCommand("HMGET", args...) +} + +func HSET(args ...interface{}) (reply interface{}, err error) { + return redisCommand("HSET", args...) +} + +func HGET(args ...interface{}) (reply interface{}, err error) { + return redisCommand("HGET", args...) +} + +func HINCRBY(args ...interface{}) (reply interface{}, err error) { + return redisCommand("HINCRBY", args...) +} diff --git a/conf/conf.yaml b/conf/conf.yaml index 9f040dc..d27f2e9 100644 --- a/conf/conf.yaml +++ b/conf/conf.yaml @@ -21,7 +21,7 @@ etcd: - "192.168.0.206:2379" server_account: - id: "1" + id: 1 name: "account" ip: "192.168.0.206" port: 8080 @@ -35,7 +35,24 @@ server_account: db: 0 server_game: - id: "2" + id: 1 + name: "game" + ip: "192.168.0.206" + encipher: false + port: 8849 + debugport: 6060 + gm: 8880 + pool_size: 1 + plugin_path: "./bin/plugin.so" + mongo: + <<: *default-mongo + dbname: "game" + redis: + <<: *default-redis + db: 0 + +server_game1: + id: 2 name: "game" ip: "192.168.0.206" encipher: false @@ -50,7 +67,6 @@ server_game: redis: <<: *default-redis db: 0 - test_client: ip: "127.0.0.1" port: 8850 diff --git a/models/dbseed.go b/models/dbseed.go new file mode 100644 index 0000000..13150dc --- /dev/null +++ b/models/dbseed.go @@ -0,0 +1,153 @@ +package models + +import ( + "github.com/garyburd/redigo/redis" + "pro2d/common" + "pro2d/common/db/mongoproxy" + "pro2d/common/db/redisproxy" + "pro2d/common/logger" + "pro2d/pb" + "reflect" +) + +type STOIncrement map[interface{}]int64 + +var dbSeedSingleton *DBSeed + +type DBSeed struct { + serverID int64 +} + +func NewDBSeed(serverID int64) *DBSeed { + if dbSeedSingleton == nil { + dbSeedSingleton = &DBSeed{ + serverID: serverID, + } + } + return dbSeedSingleton +} + +func DBSeedS() *DBSeed { + return dbSeedSingleton +} + +func AccountModels() STOIncrement { + return STOIncrement{ + &pb.Account{}: 0, + "uid": common.MaxUidNum, + } +} + +func GameModels() STOIncrement { + return STOIncrement{ + &pb.Increment{}: 0, + &pb.Role{}: common.MaxCommNum, + &pb.Equipment{}: 0, + &pb.Hero{}: 0, + &pb.Prop{}: 0, + &pb.Team{}: 0, + } +} + +//初始化表自增id +func (d *DBSeed) InitAutoIncreUidTable(schema STOIncrement) { + for s, b := range schema { + if b <= 0 { + continue + } + var name string + if reflect.TypeOf(s).Kind() == reflect.String { + name = s.(string) + } else { + name = mongoproxy.GetCollName(s) + } + + autoIncrement := NewIncrement(name) + var increId int64 = 0 + if err := autoIncrement.Load(); err != nil { + //字段不存在 初始化 id + increId = d.serverID * b + autoIncrement.Incre.Key = name + autoIncrement.Incre.Val = increId + autoIncrement.Create() + + } else { + increId = autoIncrement.Incre.Val + } + + //设置到redis中,提供初始自增id + relay, err := redisproxy.HGET(common.AutoIncrement, name) + if err != nil { + logger.Error(err.Error()) + continue + } + + var relayID int64 = 0 + if relay != nil { + relayID, err = redis.Int64(relay, err) + if err != nil { + logger.Error(err.Error()) + continue + } + } + + logger.Debug(relayID, common.AutoIncrement, name) + if relayID == 0 || increId > relayID { + redisproxy.HSET(common.AutoIncrement, name, increId) + } + } +} + +func (d *DBSeed) SaveAutoincrementTimer(schema STOIncrement) { + for s, b := range schema { + if b <= 0 { + continue + } + var name string + if reflect.TypeOf(s).Kind() == reflect.String { + name = s.(string) + } else { + name = mongoproxy.GetCollName(s) + } + + //获取数据库中的id 持久化数据 + autoIncrement := NewIncrement(name) + var dbID int64 = 0 + if err := autoIncrement.Load(); err != nil { + continue + + } else { + dbID = autoIncrement.Incre.Val + } + //获取redis中的id 内存中的数据。获取自增id + relayID, err := redis.Int64(redisproxy.HGET(common.AutoIncrement, name)) + if err != nil { + logger.Error(err.Error()) + continue + } + if relayID > dbID { + //持久化数据 + autoIncrement.SetProperty("val", relayID) + autoIncrement.Update() + } + } +} + +//初始化服务器数据库以及服务器信息表 +func (d *DBSeed) InitServerDatabase(schema STOIncrement) { + for s, _ := range schema { + if reflect.TypeOf(s).Kind() == reflect.String { + continue + } + + coll, keys := mongoproxy.FindIndex(s) + mongoproxy.CreateTable(coll) + for _, index := range keys { + res, err := mongoproxy.SetUnique(coll, index) + if err != nil { + logger.Error("InitDoc unique: %s, err: %v", res, err) + continue + } + } + } +} diff --git a/models/increment.go b/models/increment.go new file mode 100644 index 0000000..38c5b59 --- /dev/null +++ b/models/increment.go @@ -0,0 +1,23 @@ +package models + +import ( + "pro2d/common/components" + "pro2d/pb" +) + +type IncrementModels struct { + components.ISchema + Incre *pb.Increment +} + +func NewIncrement(key string) *IncrementModels { + data := &pb.Increment{ + Key: key, + } + + r := &IncrementModels{ + ISchema: NewSchema(data.Key, data), + Incre: data, + } + return r +} diff --git a/models/role.go b/models/role.go index 555ae09..674e85b 100644 --- a/models/role.go +++ b/models/role.go @@ -1,6 +1,7 @@ package models import ( + "fmt" "github.com/golang/protobuf/proto" "pro2d/common" "pro2d/common/components" @@ -51,10 +52,22 @@ func NewRole(id string) *RoleModel { return m } +func (m *RoleModel) IncreByKey(key string, detal int64) int64 { + v, ok := m.Role.Incres[key] + if !ok { + v = detal + } else { + v += detal + } + m.Role.Incres[key] = v + m.SetProperty("incres", m.Role.Incres) + return v + common.MaxCommNum +} + func (m *RoleModel) InitRole() { //init hero h1 := pb.Hero{ - Id: common.SnowFlack.NextValStr(), + Id: fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1)), RoleId: m.Role.Id, Type: 1, Level: 1, @@ -65,23 +78,23 @@ func (m *RoleModel) InitRole() { m.AddHero(&h1) h2 := h1 - h2.Id = common.SnowFlack.NextValStr() + h2.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1)) h2.Type = 2 m.AddHero(&h2) h3 := h1 - h3.Id = common.SnowFlack.NextValStr() + h3.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1)) h3.Type = 3 m.AddHero(&h3) h4 := h1 - h4.Id = common.SnowFlack.NextValStr() + h4.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1)) h4.Type = 4 m.AddHero(&h4) //init team t1 := pb.Team{ - Id: common.SnowFlack.NextValStr(), + Id: fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1)), RoleId: m.Role.Id, HeroId1: h1.Id, HeroId2: h2.Id, @@ -90,15 +103,15 @@ func (m *RoleModel) InitRole() { m.AddTeam(&t1) t2 := t1 - t2.Id = common.SnowFlack.NextValStr() + t2.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1)) m.AddTeam(&t2) t3 := t1 - t3.Id = common.SnowFlack.NextValStr() + t3.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1)) m.AddTeam(&t3) t4 := t1 - t4.Id = common.SnowFlack.NextValStr() + t4.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1)) m.AddTeam(&t4) } diff --git a/models/schema.go b/models/schema.go index 1600667..3fe1e7b 100644 --- a/models/schema.go +++ b/models/schema.go @@ -55,6 +55,17 @@ func NewSchema(key string, schema interface{}) *Schema { return sch } +func (s *Schema) getPriTag() string { + var pri string + for i := 0; i < s.reflectValue.Type().NumField(); i++ { + if s.reflectValue.Type().Field(i).Tag.Get("pri") == "1" { + pri = strings.ToLower(s.reflectValue.Type().Field(i).Name) + break + } + } + return pri +} + func (s *Schema) FindIndex() (string, []string) { var index []string for i := 0; i < s.reflectValue.Type().NumField(); i++ { @@ -186,13 +197,12 @@ func (s *Schema) ParseFields(message protoreflect.Message, properties map[string return ids } -func (s *Schema) getPriTag() string { - var pri string - for i := 0; i < s.reflectValue.Type().NumField(); i++ { - if s.reflectValue.Type().Field(i).Tag.Get("pri") == "1" { - pri = strings.ToLower(s.reflectValue.Type().Field(i).Name) - break +func (s *Schema) IncrPropertyChan(conn components.IConnection, key string, val int64) { + if conn != nil { + conn.CustomChan() <- func() { + s.IncrProperty(key, val) } + } else { + s.IncrProperty(key, val) } - return pri } diff --git a/pb/models.pb.go b/pb/models.pb.go index b2b9137..f75498c 100644 --- a/pb/models.pb.go +++ b/pb/models.pb.go @@ -391,28 +391,84 @@ func (x *Team) GetHeroId3() string { return "" } +type Increment struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty" index:"unique" pri:"1"` //@inject_tag: index:"unique" pri:"1" + Val int64 `protobuf:"varint,2,opt,name=val,proto3" json:"val,omitempty"` +} + +func (x *Increment) Reset() { + *x = Increment{} + if protoimpl.UnsafeEnabled { + mi := &file_models_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Increment) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Increment) ProtoMessage() {} + +func (x *Increment) ProtoReflect() protoreflect.Message { + mi := &file_models_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Increment.ProtoReflect.Descriptor instead. +func (*Increment) Descriptor() ([]byte, []int) { + return file_models_proto_rawDescGZIP(), []int{5} +} + +func (x *Increment) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +func (x *Increment) GetVal() int64 { + if x != nil { + return x.Val + } + return 0 +} + type Role struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" index:"unique" pri:"1"` // @inject_tag: index:"unique" pri:"1" - Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid,omitempty" index:"unique"` // @inject_tag: index:"unique" - Device string `protobuf:"bytes,3,opt,name=device,proto3" json:"device,omitempty"` - Nick string `protobuf:"bytes,4,opt,name=nick,proto3" json:"nick,omitempty"` - Level int32 `protobuf:"varint,5,opt,name=level,proto3" json:"level,omitempty"` - Exp int64 `protobuf:"varint,6,opt,name=exp,proto3" json:"exp,omitempty"` - Hp int64 `protobuf:"varint,7,opt,name=hp,proto3" json:"hp,omitempty"` - HpMax int64 `protobuf:"varint,8,opt,name=hp_max,json=hpMax,proto3" json:"hp_max,omitempty"` - BuyR string `protobuf:"bytes,11,opt,name=buy_r,json=buyR,proto3" json:"buy_r,omitempty"` - PayR string `protobuf:"bytes,12,opt,name=pay_r,json=payR,proto3" json:"pay_r,omitempty"` - Del bool `protobuf:"varint,13,opt,name=del,proto3" json:"del,omitempty"` + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" index:"unique" pri:"1"` // @inject_tag: index:"unique" pri:"1" + Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid,omitempty" index:"unique"` // @inject_tag: index:"unique" + Device string `protobuf:"bytes,3,opt,name=device,proto3" json:"device,omitempty"` + Nick string `protobuf:"bytes,4,opt,name=nick,proto3" json:"nick,omitempty"` + Level int32 `protobuf:"varint,5,opt,name=level,proto3" json:"level,omitempty"` + Exp int64 `protobuf:"varint,6,opt,name=exp,proto3" json:"exp,omitempty"` + Hp int64 `protobuf:"varint,7,opt,name=hp,proto3" json:"hp,omitempty"` + HpMax int64 `protobuf:"varint,8,opt,name=hp_max,json=hpMax,proto3" json:"hp_max,omitempty"` + BuyR string `protobuf:"bytes,11,opt,name=buy_r,json=buyR,proto3" json:"buy_r,omitempty"` + PayR string `protobuf:"bytes,12,opt,name=pay_r,json=payR,proto3" json:"pay_r,omitempty"` + Del bool `protobuf:"varint,13,opt,name=del,proto3" json:"del,omitempty"` + Incres map[string]int64 `protobuf:"bytes,14,rep,name=incres,proto3" json:"incres,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` } func (x *Role) Reset() { *x = Role{} if protoimpl.UnsafeEnabled { - mi := &file_models_proto_msgTypes[5] + mi := &file_models_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -425,7 +481,7 @@ func (x *Role) String() string { func (*Role) ProtoMessage() {} func (x *Role) ProtoReflect() protoreflect.Message { - mi := &file_models_proto_msgTypes[5] + mi := &file_models_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -438,7 +494,7 @@ func (x *Role) ProtoReflect() protoreflect.Message { // Deprecated: Use Role.ProtoReflect.Descriptor instead. func (*Role) Descriptor() ([]byte, []int) { - return file_models_proto_rawDescGZIP(), []int{5} + return file_models_proto_rawDescGZIP(), []int{6} } func (x *Role) GetId() string { @@ -518,6 +574,13 @@ func (x *Role) GetDel() bool { return false } +func (x *Role) GetIncres() map[string]int64 { + if x != nil { + return x.Incres + } + return nil +} + var File_models_proto protoreflect.FileDescriptor var file_models_proto_rawDesc = []byte{ @@ -558,22 +621,32 @@ var file_models_proto_rawDesc = []byte{ 0x12, 0x19, 0x0a, 0x08, 0x68, 0x65, 0x72, 0x6f, 0x5f, 0x69, 0x64, 0x32, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x68, 0x65, 0x72, 0x6f, 0x49, 0x64, 0x32, 0x12, 0x19, 0x0a, 0x08, 0x68, 0x65, 0x72, 0x6f, 0x5f, 0x69, 0x64, 0x33, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x68, - 0x65, 0x72, 0x6f, 0x49, 0x64, 0x33, 0x22, 0xdf, 0x01, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x12, - 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, - 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, - 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x69, 0x63, - 0x6b, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x69, 0x63, 0x6b, 0x12, 0x14, 0x0a, - 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, - 0x76, 0x65, 0x6c, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x78, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, - 0x52, 0x03, 0x65, 0x78, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x70, 0x18, 0x07, 0x20, 0x01, 0x28, - 0x03, 0x52, 0x02, 0x68, 0x70, 0x12, 0x15, 0x0a, 0x06, 0x68, 0x70, 0x5f, 0x6d, 0x61, 0x78, 0x18, - 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x68, 0x70, 0x4d, 0x61, 0x78, 0x12, 0x13, 0x0a, 0x05, - 0x62, 0x75, 0x79, 0x5f, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x75, 0x79, - 0x52, 0x12, 0x13, 0x0a, 0x05, 0x70, 0x61, 0x79, 0x5f, 0x72, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x04, 0x70, 0x61, 0x79, 0x52, 0x12, 0x10, 0x0a, 0x03, 0x64, 0x65, 0x6c, 0x18, 0x0d, 0x20, - 0x01, 0x28, 0x08, 0x52, 0x03, 0x64, 0x65, 0x6c, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, - 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x72, 0x6f, 0x49, 0x64, 0x33, 0x22, 0x2f, 0x0a, 0x09, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d, + 0x65, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x22, 0xcc, 0x02, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, + 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, + 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, + 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x69, + 0x63, 0x6b, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x69, 0x63, 0x6b, 0x12, 0x14, + 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, + 0x65, 0x76, 0x65, 0x6c, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x78, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x03, 0x65, 0x78, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x70, 0x18, 0x07, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x02, 0x68, 0x70, 0x12, 0x15, 0x0a, 0x06, 0x68, 0x70, 0x5f, 0x6d, 0x61, 0x78, + 0x18, 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x68, 0x70, 0x4d, 0x61, 0x78, 0x12, 0x13, 0x0a, + 0x05, 0x62, 0x75, 0x79, 0x5f, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x75, + 0x79, 0x52, 0x12, 0x13, 0x0a, 0x05, 0x70, 0x61, 0x79, 0x5f, 0x72, 0x18, 0x0c, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x04, 0x70, 0x61, 0x79, 0x52, 0x12, 0x10, 0x0a, 0x03, 0x64, 0x65, 0x6c, 0x18, 0x0d, + 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x64, 0x65, 0x6c, 0x12, 0x30, 0x0a, 0x06, 0x69, 0x6e, 0x63, + 0x72, 0x65, 0x73, 0x18, 0x0e, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x6d, 0x6f, 0x64, 0x65, + 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x2e, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x73, 0x45, 0x6e, + 0x74, 0x72, 0x79, 0x52, 0x06, 0x69, 0x6e, 0x63, 0x72, 0x65, 0x73, 0x1a, 0x39, 0x0a, 0x0b, 0x49, + 0x6e, 0x63, 0x72, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, + 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -588,21 +661,24 @@ func file_models_proto_rawDescGZIP() []byte { return file_models_proto_rawDescData } -var file_models_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_models_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_models_proto_goTypes = []interface{}{ (*Account)(nil), // 0: models.Account (*Hero)(nil), // 1: models.Hero (*Equipment)(nil), // 2: models.Equipment (*Prop)(nil), // 3: models.Prop (*Team)(nil), // 4: models.Team - (*Role)(nil), // 5: models.Role + (*Increment)(nil), // 5: models.Increment + (*Role)(nil), // 6: models.Role + nil, // 7: models.Role.IncresEntry } var file_models_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name + 7, // 0: models.Role.incres:type_name -> models.Role.IncresEntry + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name } func init() { file_models_proto_init() } @@ -672,6 +748,18 @@ func file_models_proto_init() { } } file_models_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Increment); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_models_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Role); i { case 0: return &v.state @@ -690,7 +778,7 @@ func file_models_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_models_proto_rawDesc, NumEnums: 0, - NumMessages: 6, + NumMessages: 8, NumExtensions: 0, NumServices: 0, }, diff --git a/protos b/protos index c1d210b..f3d0853 160000 --- a/protos +++ b/protos @@ -1 +1 @@ -Subproject commit c1d210bd2ef2b9ce1a23c747a814b6ee308f951d +Subproject commit f3d0853be4bc7062a327ed11b0e74ef10912b49e -- libgit2 0.21.2