Commit 101d1cc1d6942fce43f18df8a5f27f50022c8f18

Authored by zhangqijia
1 parent 8aaf28dd

feat: 一个基于redis的自增ID功能。

cmd/gameserver/action/RoleAction.go
... ... @@ -39,14 +39,18 @@ func CreateRpc(agent components.IAgent, msg components.IMessage) (int32, interfa
39 39 req := pb.CreateReq{}
40 40 if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
41 41 logger.Error("CreateRpc err: %v", err)
42   - return 1, nil
  42 + return 1, err
43 43 }
44 44 role := models.RoleExistByUid(req.Token)
45 45 if role != nil {
46   - return 2, nil
  46 + return 2, "role exists"
  47 + }
  48 +
  49 + roleId, err := common.GetNextRoleId()
  50 + if err != nil {
  51 + return 3, err
47 52 }
48 53  
49   - roleId := common.SnowFlack.NextValStr()
50 54 role = models.NewRole(roleId)
51 55 role.Role.Uid = req.Token
52 56 role.Role.Nick = getRandomName()
... ...
cmd/gameserver/service/game.go
... ... @@ -49,7 +49,6 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) {
49 49 if err != nil {
50 50 return nil, err
51 51 }
52   - models.InitGameModels()
53 52  
54 53 //redis init
55 54 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) {
61 60 if err != nil {
62 61 return nil, err
63 62 }
64   - s.EtcdClient.PutWithLeasePrefix(common.GlobalConf.GameConf.Name, common.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", common.GlobalConf.GameConf.IP, common.GlobalConf.GameConf.Port), 5)
  63 + 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)
  64 +
  65 + //设置服务器ID & game
  66 + models.NewDBSeed(sconf.ID).InitServerDatabase(models.GameModels())
  67 + models.DBSeedS().InitAutoIncreUidTable(models.GameModels())
65 68  
66 69 return s, nil
67 70 }
68 71  
  72 +func (s *GameServer) Timeout() {
  73 + //每秒中保存一下自增ID
  74 + models.DBSeedS().SaveAutoincrementTimer(models.GameModels())
  75 +
  76 + components.TimeOut(1*time.Second, s.Timeout)
  77 +}
  78 +
69 79 func (s *GameServer) Start() error {
70 80 //设置随机种子
71 81 rand.Seed(time.Now().Unix())
72 82  
  83 + //启动定时器
  84 + s.Timeout()
  85 +
73 86 return s.IServer.Start()
74 87 }
75 88  
... ...
cmd/httpserver/AccountAction.go
... ... @@ -37,9 +37,13 @@ func (h *AccountAction) Register(c *gin.Context) (int, interface{}) {
37 37 return 3, "account exists: " + register.Phone
38 38 }
39 39  
40   - account.Uid = common.SnowFlack.NextValStr()
  40 + uid, err := common.GetNextUId()
  41 + if err != nil {
  42 + return 4, "uid get error: " + err.Error()
  43 + }
  44 + account.Uid = uid
41 45 account.Password = common.Md5V(register.Password)
42   - if err := account.Create(); err != nil {
  46 + if err = account.Create(); err != nil {
43 47 return 4, "account register err: " + err.Error()
44 48 }
45 49 account.Password = register.Password
... ...
cmd/httpserver/http.go
... ... @@ -12,11 +12,13 @@ import (
12 12 "pro2d/common/logger"
13 13 "pro2d/models"
14 14 "syscall"
  15 + "time"
15 16 )
16 17  
17 18 type AccountServer struct {
18 19 components.IHttp
19 20 EtcdClient *etcd.EtcdClient
  21 + Sconf *common.SConf
20 22 }
21 23  
22 24 func NewAccountServer(version string, port ...string) *AccountServer {
... ... @@ -24,6 +26,8 @@ func NewAccountServer(version string, port ...string) *AccountServer {
24 26 }
25 27  
26 28 func (s *AccountServer) Init(sconf *common.SConf) error {
  29 + s.Sconf = sconf
  30 +
27 31 //mgo init
28 32 err := mongoproxy.ConnectMongo(sconf.MongoConf)
29 33  
... ... @@ -37,17 +41,27 @@ func (s *AccountServer) Init(sconf *common.SConf) error {
37 41 if err != nil {
38 42 return err
39 43 }
40   - models.InitAccountModels()
  44 + models.NewDBSeed(sconf.ID).InitServerDatabase(models.AccountModels())
  45 + models.DBSeedS().InitAutoIncreUidTable(models.AccountModels())
41 46  
42   - s.EtcdClient.PutWithLeasePrefix(sconf.Name, sconf.ID, fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5)
  47 + s.EtcdClient.PutWithLeasePrefix(sconf.Name, fmt.Sprintf("%d", sconf.ID), fmt.Sprintf("%s:%d", sconf.IP, sconf.Port), 5)
43 48 return nil
44 49 }
45 50  
  51 +func (s *AccountServer) TimeOut() {
  52 + models.DBSeedS().SaveAutoincrementTimer(models.AccountModels())
  53 +
  54 + components.TimeOut(1*time.Second, s.TimeOut)
  55 +}
  56 +
46 57 func (s *AccountServer) Start() error {
47 58 if err := s.Init(common.GlobalConf.AccountConf); err != nil {
48 59 return err
49 60 }
50 61  
  62 + //开始定时器
  63 + s.TimeOut()
  64 +
51 65 return s.IHttp.Start()
52 66 }
53 67  
... ...
common/commonFunc.go 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +package common
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/garyburd/redigo/redis"
  6 + "pro2d/common/db/redisproxy"
  7 +)
  8 +
  9 +func GetNextRoleId() (string, error) {
  10 + relay, err := redisproxy.HGET(AutoIncrement, "role")
  11 + if err != nil {
  12 + return "", err
  13 + }
  14 +
  15 + relay, err = redisproxy.HINCRBY(AutoIncrement, "role", 1)
  16 + ID, err := redis.Int64(relay, err)
  17 + if err != nil {
  18 + return "", err
  19 + }
  20 + return fmt.Sprintf("%d", ID), nil
  21 +}
  22 +
  23 +func GetNextUId() (string, error) {
  24 + relay, err := redisproxy.HGET(AutoIncrement, "uid")
  25 + if err != nil {
  26 + return "", err
  27 + }
  28 +
  29 + var ID int64 = 0
  30 + if relay == nil {
  31 + ID = 90000
  32 + redisproxy.HSET(AutoIncrement, "uid", ID)
  33 + } else {
  34 + relay, err = redisproxy.HINCRBY(AutoIncrement, "uid", 1)
  35 + ID, err = redis.Int64(relay, err)
  36 + if err != nil {
  37 + return "", err
  38 + }
  39 + }
  40 + return fmt.Sprintf("%d", ID), nil
  41 +}
... ...
common/commonFunc_test.go 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +package common
  2 +
  3 +import (
  4 + "fmt"
  5 + "pro2d/common/db/redisproxy"
  6 + "pro2d/common/logger"
  7 + "testing"
  8 +)
  9 +
  10 +func TestGetNextRoleId(t *testing.T) {
  11 + if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil {
  12 + logger.Error(err)
  13 + return
  14 + }
  15 +
  16 + fmt.Println(GetNextRoleId())
  17 + fmt.Println(GetNextUId())
  18 +}
... ...
common/conf.go
... ... @@ -32,7 +32,7 @@ type MongoConf struct {
32 32 }
33 33  
34 34 type SConf struct {
35   - ID string `yaml:"id"`
  35 + ID int64 `yaml:"id"`
36 36 Name string `yaml:"name"`
37 37 IP string `yaml:"ip"`
38 38 Port int `yaml:"port"`
... ...
common/const.go
... ... @@ -12,11 +12,12 @@ const (
12 12 //保存数据时间
13 13 SaveDataInterval = 5 //s
14 14  
15   - //id相关
16   - MaxRoleNum = 1000000
17   - MaxUidNum = 1000000
18   - MaxHeroNum = 1000000
19   - MaxTeamNum = 1000000
  15 + //自增id相关
  16 + MaxCommNum = 1000000
  17 + MaxUidNum = 90000
  18 +
  19 + AutoIncrement = "pro2d_autoincrement_set"
  20 + AutoIncrementHero = "pro2d_autoincrement:hero"
20 21  
21 22 //gm参数属性
22 23 Role_ = "_role"
... ...
common/db/mongoproxy/mongoplugin.go
... ... @@ -22,15 +22,15 @@ func DB() *mongo.Database {
22 22  
23 23 func ConnectMongo(conf *common.MongoConf) error {
24 24 var uri string
25   - if conf.User!= "" {
  25 + if conf.User != "" {
26 26 //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName)
27 27 uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/?w=majority", conf.User, conf.Password, conf.Host, conf.Port)
28   - }else {
  28 + } else {
29 29 //uri = fmt.Sprintf("mongodb://%s:%d/%s?w=majority", conf.Host, conf.Port, conf.DBName)
30 30 uri = fmt.Sprintf("mongodb://%s:%d/?w=majority", conf.Host, conf.Port)
31 31 }
32 32 // 设置连接超时时间
33   - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.TimeOut) * time.Second)
  33 + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.TimeOut)*time.Second)
34 34 defer cancel()
35 35 // 通过传进来的uri连接相关的配置
36 36 o := options.Client().ApplyURI(uri)
... ... @@ -51,7 +51,7 @@ func ConnectMongo(conf *common.MongoConf) error {
51 51 return nil
52 52 }
53 53  
54   -func CloseMongo(){
  54 +func CloseMongo() {
55 55 mongoClient.Disconnect(context.TODO())
56 56 }
57 57  
... ... @@ -66,13 +66,13 @@ func CreateTable(tb string) error {
66 66 return DB().CreateCollection(context.TODO(), tb)
67 67 }
68 68  
69   -func FindOne(pri interface{}, schema interface{}) error {
70   - r := mongoDatabase.Collection(GetCollName(schema)).FindOne(context.TODO(), pri)
  69 +func FindOne(coll string, pri interface{}, schema interface{}) error {
  70 + r := mongoDatabase.Collection(coll).FindOne(context.TODO(), pri)
71 71 return r.Decode(schema)
72 72 }
73 73  
74 74 func FindMany(coll string, key string, val interface{}, schema interface{}) error {
75   - r, err := mongoDatabase.Collection(coll).Find(context.TODO(), bson.M{key:val})
  75 + r, err := mongoDatabase.Collection(coll).Find(context.TODO(), bson.M{key: val})
76 76 if err != nil {
77 77 return err
78 78 }
... ... @@ -80,14 +80,14 @@ func FindMany(coll string, key string, val interface{}, schema interface{}) erro
80 80 }
81 81  
82 82 func GetBsonD(key string, value interface{}) interface{} {
83   - return bson.D{ {key, value}}
  83 + return bson.D{{key, value}}
84 84 }
85 85  
86   -func GetBsonM(key string, value interface{}) interface{} {
  86 +func GetBsonM(key string, value interface{}) interface{} {
87 87 return bson.M{key: value}
88 88 }
89 89  
90   -func GetSchemaType(schema interface{}) reflect.Type {
  90 +func GetSchemaType(schema interface{}) reflect.Type {
91 91 s := reflect.TypeOf(schema)
92 92 if s.Kind() == reflect.Ptr {
93 93 s = reflect.TypeOf(schema).Elem()
... ... @@ -112,7 +112,7 @@ func GetPriKey(schema interface{}) string {
112 112 return pri
113 113 }
114 114  
115   -func FindIndex(schema interface{}) (string, []string){
  115 +func FindIndex(schema interface{}) (string, []string) {
116 116 s := GetSchemaType(schema)
117 117  
118 118 var index []string
... ... @@ -128,21 +128,21 @@ func FindIndex(schema interface{}) (string, []string){
128 128 return strings.ToLower(s.Name()), index
129 129 }
130 130  
131   -func SetUnique(coll, key string) (string, error){
  131 +func SetUnique(coll, key string) (string, error) {
132 132 return DB().Collection(coll).Indexes().CreateOne(
133 133 context.TODO(),
134 134 mongo.IndexModel{
135   - Keys : bsonx.Doc{{key, bsonx.Int32(1)}},
  135 + Keys: bsonx.Doc{{key, bsonx.Int32(1)}},
136 136 Options: options.Index().SetUnique(true),
137 137 },
138 138 )
139 139 }
140 140  
141   -func InitDoc(schema ...interface{}) {
  141 +func InitDoc(schema ...interface{}) {
142 142 for _, s := range schema {
143 143 coll, keys := FindIndex(s)
144 144 CreateTable(coll)
145   - for _, index := range keys {
  145 + for _, index := range keys {
146 146  
147 147 logger.Debug("InitDoc collect: %v, createIndex: %s", coll, index)
148 148 res, err := SetUnique(coll, index)
... ... @@ -153,4 +153,4 @@ func InitDoc(schema ...interface{}) {
153 153 }
154 154 }
155 155  
156   -}
157 156 \ No newline at end of file
  157 +}
... ...
common/db/redisproxy/redis.go
... ... @@ -4,6 +4,7 @@ import (
4 4 "github.com/garyburd/redigo/redis"
5 5 "time"
6 6 )
  7 +
7 8 var RedisPool *redis.Pool
8 9  
9 10 //conf *conf.ServerConf
... ... @@ -11,7 +12,7 @@ func ConnectRedis(db int, auth, address string) error {
11 12 RedisPool = &redis.Pool{
12 13 //最大活跃连接数,0代表无限
13 14 MaxActive: 888,
14   - MaxIdle: 20,
  15 + MaxIdle: 20,
15 16 //闲置连接的超时时间
16 17 IdleTimeout: time.Second * 100,
17 18 //定义拨号获得连接的函数
... ... @@ -20,35 +21,34 @@ func ConnectRedis(db int, auth, address string) error {
20 21 if auth != "" {
21 22 option = append(option, redis.DialPassword(auth))
22 23 }
23   - return redis.Dial("tcp",address, option...)
  24 + return redis.Dial("tcp", address, option...)
24 25 },
25 26 }
26 27 return nil
27 28 }
28 29  
29   -func CloseRedis() {
  30 +func CloseRedis() {
30 31 RedisPool.Close()
31 32 }
32 33  
33 34 func redisCommand(command string, args ...interface{}) (reply interface{}, err error) {
34 35 conn := RedisPool.Get()
35 36 defer conn.Close()
36   - return conn.Do(command , args...)
  37 + return conn.Do(command, args...)
37 38 }
38 39  
39   -
40 40 func ExpireKey(key interface{}, ttl interface{}) (reply interface{}, err error) {
41 41 return redisCommand("expire", key, ttl)
42 42 }
43 43  
44 44 //redis 管道操作
45   -func PipLine(f func(conn redis.Conn)) {
  45 +func PipLine(f func(conn redis.Conn)) {
46 46 conn := RedisPool.Get()
47 47 defer conn.Close()
48 48 f(conn)
49 49 }
50 50  
51   -func PipLineTest() {
  51 +func PipLineTest() {
52 52 PipLine(func(c redis.Conn) {
53 53 c.Send("SET", "foo", "bar")
54 54 c.Send("GET", "foo")
... ... @@ -79,6 +79,22 @@ func HKEYS(args ...interface{}) (reply interface{}, err error) {
79 79 return redisCommand("HKEYS", args...)
80 80 }
81 81  
82   -func HMSET(args ...interface{}) (reply interface{}, err error) {
  82 +func HMSET(args ...interface{}) (reply interface{}, err error) {
83 83 return redisCommand("HMSET", args...)
84   -}
85 84 \ No newline at end of file
  85 +}
  86 +
  87 +func HMGET(args ...interface{}) (reply interface{}, err error) {
  88 + return redisCommand("HMGET", args...)
  89 +}
  90 +
  91 +func HSET(args ...interface{}) (reply interface{}, err error) {
  92 + return redisCommand("HSET", args...)
  93 +}
  94 +
  95 +func HGET(args ...interface{}) (reply interface{}, err error) {
  96 + return redisCommand("HGET", args...)
  97 +}
  98 +
  99 +func HINCRBY(args ...interface{}) (reply interface{}, err error) {
  100 + return redisCommand("HINCRBY", args...)
  101 +}
... ...
conf/conf.yaml
... ... @@ -21,7 +21,7 @@ etcd:
21 21 - "192.168.0.206:2379"
22 22  
23 23 server_account:
24   - id: "1"
  24 + id: 1
25 25 name: "account"
26 26 ip: "192.168.0.206"
27 27 port: 8080
... ... @@ -35,7 +35,24 @@ server_account:
35 35 db: 0
36 36  
37 37 server_game:
38   - id: "2"
  38 + id: 1
  39 + name: "game"
  40 + ip: "192.168.0.206"
  41 + encipher: false
  42 + port: 8849
  43 + debugport: 6060
  44 + gm: 8880
  45 + pool_size: 1
  46 + plugin_path: "./bin/plugin.so"
  47 + mongo:
  48 + <<: *default-mongo
  49 + dbname: "game"
  50 + redis:
  51 + <<: *default-redis
  52 + db: 0
  53 +
  54 +server_game1:
  55 + id: 2
39 56 name: "game"
40 57 ip: "192.168.0.206"
41 58 encipher: false
... ... @@ -50,7 +67,6 @@ server_game:
50 67 redis:
51 68 <<: *default-redis
52 69 db: 0
53   -
54 70 test_client:
55 71 ip: "127.0.0.1"
56 72 port: 8850
... ...
models/dbseed.go 0 → 100644
... ... @@ -0,0 +1,153 @@
  1 +package models
  2 +
  3 +import (
  4 + "github.com/garyburd/redigo/redis"
  5 + "pro2d/common"
  6 + "pro2d/common/db/mongoproxy"
  7 + "pro2d/common/db/redisproxy"
  8 + "pro2d/common/logger"
  9 + "pro2d/pb"
  10 + "reflect"
  11 +)
  12 +
  13 +type STOIncrement map[interface{}]int64
  14 +
  15 +var dbSeedSingleton *DBSeed
  16 +
  17 +type DBSeed struct {
  18 + serverID int64
  19 +}
  20 +
  21 +func NewDBSeed(serverID int64) *DBSeed {
  22 + if dbSeedSingleton == nil {
  23 + dbSeedSingleton = &DBSeed{
  24 + serverID: serverID,
  25 + }
  26 + }
  27 + return dbSeedSingleton
  28 +}
  29 +
  30 +func DBSeedS() *DBSeed {
  31 + return dbSeedSingleton
  32 +}
  33 +
  34 +func AccountModels() STOIncrement {
  35 + return STOIncrement{
  36 + &pb.Account{}: 0,
  37 + "uid": common.MaxUidNum,
  38 + }
  39 +}
  40 +
  41 +func GameModels() STOIncrement {
  42 + return STOIncrement{
  43 + &pb.Increment{}: 0,
  44 + &pb.Role{}: common.MaxCommNum,
  45 + &pb.Equipment{}: 0,
  46 + &pb.Hero{}: 0,
  47 + &pb.Prop{}: 0,
  48 + &pb.Team{}: 0,
  49 + }
  50 +}
  51 +
  52 +//初始化表自增id
  53 +func (d *DBSeed) InitAutoIncreUidTable(schema STOIncrement) {
  54 + for s, b := range schema {
  55 + if b <= 0 {
  56 + continue
  57 + }
  58 + var name string
  59 + if reflect.TypeOf(s).Kind() == reflect.String {
  60 + name = s.(string)
  61 + } else {
  62 + name = mongoproxy.GetCollName(s)
  63 + }
  64 +
  65 + autoIncrement := NewIncrement(name)
  66 + var increId int64 = 0
  67 + if err := autoIncrement.Load(); err != nil {
  68 + //字段不存在 初始化 id
  69 + increId = d.serverID * b
  70 + autoIncrement.Incre.Key = name
  71 + autoIncrement.Incre.Val = increId
  72 + autoIncrement.Create()
  73 +
  74 + } else {
  75 + increId = autoIncrement.Incre.Val
  76 + }
  77 +
  78 + //设置到redis中,提供初始自增id
  79 + relay, err := redisproxy.HGET(common.AutoIncrement, name)
  80 + if err != nil {
  81 + logger.Error(err.Error())
  82 + continue
  83 + }
  84 +
  85 + var relayID int64 = 0
  86 + if relay != nil {
  87 + relayID, err = redis.Int64(relay, err)
  88 + if err != nil {
  89 + logger.Error(err.Error())
  90 + continue
  91 + }
  92 + }
  93 +
  94 + logger.Debug(relayID, common.AutoIncrement, name)
  95 + if relayID == 0 || increId > relayID {
  96 + redisproxy.HSET(common.AutoIncrement, name, increId)
  97 + }
  98 + }
  99 +}
  100 +
  101 +func (d *DBSeed) SaveAutoincrementTimer(schema STOIncrement) {
  102 + for s, b := range schema {
  103 + if b <= 0 {
  104 + continue
  105 + }
  106 + var name string
  107 + if reflect.TypeOf(s).Kind() == reflect.String {
  108 + name = s.(string)
  109 + } else {
  110 + name = mongoproxy.GetCollName(s)
  111 + }
  112 +
  113 + //获取数据库中的id 持久化数据
  114 + autoIncrement := NewIncrement(name)
  115 + var dbID int64 = 0
  116 + if err := autoIncrement.Load(); err != nil {
  117 + continue
  118 +
  119 + } else {
  120 + dbID = autoIncrement.Incre.Val
  121 + }
  122 + //获取redis中的id 内存中的数据。获取自增id
  123 + relayID, err := redis.Int64(redisproxy.HGET(common.AutoIncrement, name))
  124 + if err != nil {
  125 + logger.Error(err.Error())
  126 + continue
  127 + }
  128 + if relayID > dbID {
  129 + //持久化数据
  130 + autoIncrement.SetProperty("val", relayID)
  131 + autoIncrement.Update()
  132 + }
  133 + }
  134 +}
  135 +
  136 +//初始化服务器数据库以及服务器信息表
  137 +func (d *DBSeed) InitServerDatabase(schema STOIncrement) {
  138 + for s, _ := range schema {
  139 + if reflect.TypeOf(s).Kind() == reflect.String {
  140 + continue
  141 + }
  142 +
  143 + coll, keys := mongoproxy.FindIndex(s)
  144 + mongoproxy.CreateTable(coll)
  145 + for _, index := range keys {
  146 + res, err := mongoproxy.SetUnique(coll, index)
  147 + if err != nil {
  148 + logger.Error("InitDoc unique: %s, err: %v", res, err)
  149 + continue
  150 + }
  151 + }
  152 + }
  153 +}
... ...
models/increment.go 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +package models
  2 +
  3 +import (
  4 + "pro2d/common/components"
  5 + "pro2d/pb"
  6 +)
  7 +
  8 +type IncrementModels struct {
  9 + components.ISchema
  10 + Incre *pb.Increment
  11 +}
  12 +
  13 +func NewIncrement(key string) *IncrementModels {
  14 + data := &pb.Increment{
  15 + Key: key,
  16 + }
  17 +
  18 + r := &IncrementModels{
  19 + ISchema: NewSchema(data.Key, data),
  20 + Incre: data,
  21 + }
  22 + return r
  23 +}
... ...
models/role.go
1 1 package models
2 2  
3 3 import (
  4 + "fmt"
4 5 "github.com/golang/protobuf/proto"
5 6 "pro2d/common"
6 7 "pro2d/common/components"
... ... @@ -51,10 +52,22 @@ func NewRole(id string) *RoleModel {
51 52 return m
52 53 }
53 54  
  55 +func (m *RoleModel) IncreByKey(key string, detal int64) int64 {
  56 + v, ok := m.Role.Incres[key]
  57 + if !ok {
  58 + v = detal
  59 + } else {
  60 + v += detal
  61 + }
  62 + m.Role.Incres[key] = v
  63 + m.SetProperty("incres", m.Role.Incres)
  64 + return v + common.MaxCommNum
  65 +}
  66 +
54 67 func (m *RoleModel) InitRole() {
55 68 //init hero
56 69 h1 := pb.Hero{
57   - Id: common.SnowFlack.NextValStr(),
  70 + Id: fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1)),
58 71 RoleId: m.Role.Id,
59 72 Type: 1,
60 73 Level: 1,
... ... @@ -65,23 +78,23 @@ func (m *RoleModel) InitRole() {
65 78 m.AddHero(&h1)
66 79  
67 80 h2 := h1
68   - h2.Id = common.SnowFlack.NextValStr()
  81 + h2.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1))
69 82 h2.Type = 2
70 83 m.AddHero(&h2)
71 84  
72 85 h3 := h1
73   - h3.Id = common.SnowFlack.NextValStr()
  86 + h3.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1))
74 87 h3.Type = 3
75 88 m.AddHero(&h3)
76 89  
77 90 h4 := h1
78   - h4.Id = common.SnowFlack.NextValStr()
  91 + h4.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("hero", 1))
79 92 h4.Type = 4
80 93 m.AddHero(&h4)
81 94  
82 95 //init team
83 96 t1 := pb.Team{
84   - Id: common.SnowFlack.NextValStr(),
  97 + Id: fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1)),
85 98 RoleId: m.Role.Id,
86 99 HeroId1: h1.Id,
87 100 HeroId2: h2.Id,
... ... @@ -90,15 +103,15 @@ func (m *RoleModel) InitRole() {
90 103 m.AddTeam(&t1)
91 104  
92 105 t2 := t1
93   - t2.Id = common.SnowFlack.NextValStr()
  106 + t2.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1))
94 107 m.AddTeam(&t2)
95 108  
96 109 t3 := t1
97   - t3.Id = common.SnowFlack.NextValStr()
  110 + t3.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1))
98 111 m.AddTeam(&t3)
99 112  
100 113 t4 := t1
101   - t4.Id = common.SnowFlack.NextValStr()
  114 + t4.Id = fmt.Sprintf("%s%d", m.Role.Id, m.IncreByKey("team", 1))
102 115 m.AddTeam(&t4)
103 116 }
104 117  
... ...
models/schema.go
... ... @@ -55,6 +55,17 @@ func NewSchema(key string, schema interface{}) *Schema {
55 55 return sch
56 56 }
57 57  
  58 +func (s *Schema) getPriTag() string {
  59 + var pri string
  60 + for i := 0; i < s.reflectValue.Type().NumField(); i++ {
  61 + if s.reflectValue.Type().Field(i).Tag.Get("pri") == "1" {
  62 + pri = strings.ToLower(s.reflectValue.Type().Field(i).Name)
  63 + break
  64 + }
  65 + }
  66 + return pri
  67 +}
  68 +
58 69 func (s *Schema) FindIndex() (string, []string) {
59 70 var index []string
60 71 for i := 0; i < s.reflectValue.Type().NumField(); i++ {
... ... @@ -186,13 +197,12 @@ func (s *Schema) ParseFields(message protoreflect.Message, properties map[string
186 197 return ids
187 198 }
188 199  
189   -func (s *Schema) getPriTag() string {
190   - var pri string
191   - for i := 0; i < s.reflectValue.Type().NumField(); i++ {
192   - if s.reflectValue.Type().Field(i).Tag.Get("pri") == "1" {
193   - pri = strings.ToLower(s.reflectValue.Type().Field(i).Name)
194   - break
  200 +func (s *Schema) IncrPropertyChan(conn components.IConnection, key string, val int64) {
  201 + if conn != nil {
  202 + conn.CustomChan() <- func() {
  203 + s.IncrProperty(key, val)
195 204 }
  205 + } else {
  206 + s.IncrProperty(key, val)
196 207 }
197   - return pri
198 208 }
... ...
pb/models.pb.go
... ... @@ -391,28 +391,84 @@ func (x *Team) GetHeroId3() string {
391 391 return ""
392 392 }
393 393  
  394 +type Increment struct {
  395 + state protoimpl.MessageState
  396 + sizeCache protoimpl.SizeCache
  397 + unknownFields protoimpl.UnknownFields
  398 +
  399 + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty" index:"unique" pri:"1"` //@inject_tag: index:"unique" pri:"1"
  400 + Val int64 `protobuf:"varint,2,opt,name=val,proto3" json:"val,omitempty"`
  401 +}
  402 +
  403 +func (x *Increment) Reset() {
  404 + *x = Increment{}
  405 + if protoimpl.UnsafeEnabled {
  406 + mi := &file_models_proto_msgTypes[5]
  407 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  408 + ms.StoreMessageInfo(mi)
  409 + }
  410 +}
  411 +
  412 +func (x *Increment) String() string {
  413 + return protoimpl.X.MessageStringOf(x)
  414 +}
  415 +
  416 +func (*Increment) ProtoMessage() {}
  417 +
  418 +func (x *Increment) ProtoReflect() protoreflect.Message {
  419 + mi := &file_models_proto_msgTypes[5]
  420 + if protoimpl.UnsafeEnabled && x != nil {
  421 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  422 + if ms.LoadMessageInfo() == nil {
  423 + ms.StoreMessageInfo(mi)
  424 + }
  425 + return ms
  426 + }
  427 + return mi.MessageOf(x)
  428 +}
  429 +
  430 +// Deprecated: Use Increment.ProtoReflect.Descriptor instead.
  431 +func (*Increment) Descriptor() ([]byte, []int) {
  432 + return file_models_proto_rawDescGZIP(), []int{5}
  433 +}
  434 +
  435 +func (x *Increment) GetKey() string {
  436 + if x != nil {
  437 + return x.Key
  438 + }
  439 + return ""
  440 +}
  441 +
  442 +func (x *Increment) GetVal() int64 {
  443 + if x != nil {
  444 + return x.Val
  445 + }
  446 + return 0
  447 +}
  448 +
394 449 type Role struct {
395 450 state protoimpl.MessageState
396 451 sizeCache protoimpl.SizeCache
397 452 unknownFields protoimpl.UnknownFields
398 453  
399   - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" index:"unique" pri:"1"` // @inject_tag: index:"unique" pri:"1"
400   - Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid,omitempty" index:"unique"` // @inject_tag: index:"unique"
401   - Device string `protobuf:"bytes,3,opt,name=device,proto3" json:"device,omitempty"`
402   - Nick string `protobuf:"bytes,4,opt,name=nick,proto3" json:"nick,omitempty"`
403   - Level int32 `protobuf:"varint,5,opt,name=level,proto3" json:"level,omitempty"`
404   - Exp int64 `protobuf:"varint,6,opt,name=exp,proto3" json:"exp,omitempty"`
405   - Hp int64 `protobuf:"varint,7,opt,name=hp,proto3" json:"hp,omitempty"`
406   - HpMax int64 `protobuf:"varint,8,opt,name=hp_max,json=hpMax,proto3" json:"hp_max,omitempty"`
407   - BuyR string `protobuf:"bytes,11,opt,name=buy_r,json=buyR,proto3" json:"buy_r,omitempty"`
408   - PayR string `protobuf:"bytes,12,opt,name=pay_r,json=payR,proto3" json:"pay_r,omitempty"`
409   - Del bool `protobuf:"varint,13,opt,name=del,proto3" json:"del,omitempty"`
  454 + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" index:"unique" pri:"1"` // @inject_tag: index:"unique" pri:"1"
  455 + Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid,omitempty" index:"unique"` // @inject_tag: index:"unique"
  456 + Device string `protobuf:"bytes,3,opt,name=device,proto3" json:"device,omitempty"`
  457 + Nick string `protobuf:"bytes,4,opt,name=nick,proto3" json:"nick,omitempty"`
  458 + Level int32 `protobuf:"varint,5,opt,name=level,proto3" json:"level,omitempty"`
  459 + Exp int64 `protobuf:"varint,6,opt,name=exp,proto3" json:"exp,omitempty"`
  460 + Hp int64 `protobuf:"varint,7,opt,name=hp,proto3" json:"hp,omitempty"`
  461 + HpMax int64 `protobuf:"varint,8,opt,name=hp_max,json=hpMax,proto3" json:"hp_max,omitempty"`
  462 + BuyR string `protobuf:"bytes,11,opt,name=buy_r,json=buyR,proto3" json:"buy_r,omitempty"`
  463 + PayR string `protobuf:"bytes,12,opt,name=pay_r,json=payR,proto3" json:"pay_r,omitempty"`
  464 + Del bool `protobuf:"varint,13,opt,name=del,proto3" json:"del,omitempty"`
  465 + 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"`
410 466 }
411 467  
412 468 func (x *Role) Reset() {
413 469 *x = Role{}
414 470 if protoimpl.UnsafeEnabled {
415   - mi := &file_models_proto_msgTypes[5]
  471 + mi := &file_models_proto_msgTypes[6]
416 472 ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
417 473 ms.StoreMessageInfo(mi)
418 474 }
... ... @@ -425,7 +481,7 @@ func (x *Role) String() string {
425 481 func (*Role) ProtoMessage() {}
426 482  
427 483 func (x *Role) ProtoReflect() protoreflect.Message {
428   - mi := &file_models_proto_msgTypes[5]
  484 + mi := &file_models_proto_msgTypes[6]
429 485 if protoimpl.UnsafeEnabled && x != nil {
430 486 ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
431 487 if ms.LoadMessageInfo() == nil {
... ... @@ -438,7 +494,7 @@ func (x *Role) ProtoReflect() protoreflect.Message {
438 494  
439 495 // Deprecated: Use Role.ProtoReflect.Descriptor instead.
440 496 func (*Role) Descriptor() ([]byte, []int) {
441   - return file_models_proto_rawDescGZIP(), []int{5}
  497 + return file_models_proto_rawDescGZIP(), []int{6}
442 498 }
443 499  
444 500 func (x *Role) GetId() string {
... ... @@ -518,6 +574,13 @@ func (x *Role) GetDel() bool {
518 574 return false
519 575 }
520 576  
  577 +func (x *Role) GetIncres() map[string]int64 {
  578 + if x != nil {
  579 + return x.Incres
  580 + }
  581 + return nil
  582 +}
  583 +
521 584 var File_models_proto protoreflect.FileDescriptor
522 585  
523 586 var file_models_proto_rawDesc = []byte{
... ... @@ -558,22 +621,32 @@ var file_models_proto_rawDesc = []byte{
558 621 0x12, 0x19, 0x0a, 0x08, 0x68, 0x65, 0x72, 0x6f, 0x5f, 0x69, 0x64, 0x32, 0x18, 0x04, 0x20, 0x01,
559 622 0x28, 0x09, 0x52, 0x07, 0x68, 0x65, 0x72, 0x6f, 0x49, 0x64, 0x32, 0x12, 0x19, 0x0a, 0x08, 0x68,
560 623 0x65, 0x72, 0x6f, 0x5f, 0x69, 0x64, 0x33, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x68,
561   - 0x65, 0x72, 0x6f, 0x49, 0x64, 0x33, 0x22, 0xdf, 0x01, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x12,
562   - 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12,
563   - 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69,
564   - 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28,
565   - 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x69, 0x63,
566   - 0x6b, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x69, 0x63, 0x6b, 0x12, 0x14, 0x0a,
567   - 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65,
568   - 0x76, 0x65, 0x6c, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x78, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03,
569   - 0x52, 0x03, 0x65, 0x78, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x70, 0x18, 0x07, 0x20, 0x01, 0x28,
570   - 0x03, 0x52, 0x02, 0x68, 0x70, 0x12, 0x15, 0x0a, 0x06, 0x68, 0x70, 0x5f, 0x6d, 0x61, 0x78, 0x18,
571   - 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x68, 0x70, 0x4d, 0x61, 0x78, 0x12, 0x13, 0x0a, 0x05,
572   - 0x62, 0x75, 0x79, 0x5f, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x75, 0x79,
573   - 0x52, 0x12, 0x13, 0x0a, 0x05, 0x70, 0x61, 0x79, 0x5f, 0x72, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09,
574   - 0x52, 0x04, 0x70, 0x61, 0x79, 0x52, 0x12, 0x10, 0x0a, 0x03, 0x64, 0x65, 0x6c, 0x18, 0x0d, 0x20,
575   - 0x01, 0x28, 0x08, 0x52, 0x03, 0x64, 0x65, 0x6c, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70,
576   - 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  624 + 0x65, 0x72, 0x6f, 0x49, 0x64, 0x33, 0x22, 0x2f, 0x0a, 0x09, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x6d,
  625 + 0x65, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
  626 + 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x76, 0x61, 0x6c, 0x18, 0x02, 0x20, 0x01,
  627 + 0x28, 0x03, 0x52, 0x03, 0x76, 0x61, 0x6c, 0x22, 0xcc, 0x02, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65,
  628 + 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64,
  629 + 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75,
  630 + 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01,
  631 + 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x69,
  632 + 0x63, 0x6b, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x69, 0x63, 0x6b, 0x12, 0x14,
  633 + 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c,
  634 + 0x65, 0x76, 0x65, 0x6c, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x78, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28,
  635 + 0x03, 0x52, 0x03, 0x65, 0x78, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x68, 0x70, 0x18, 0x07, 0x20, 0x01,
  636 + 0x28, 0x03, 0x52, 0x02, 0x68, 0x70, 0x12, 0x15, 0x0a, 0x06, 0x68, 0x70, 0x5f, 0x6d, 0x61, 0x78,
  637 + 0x18, 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x68, 0x70, 0x4d, 0x61, 0x78, 0x12, 0x13, 0x0a,
  638 + 0x05, 0x62, 0x75, 0x79, 0x5f, 0x72, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x75,
  639 + 0x79, 0x52, 0x12, 0x13, 0x0a, 0x05, 0x70, 0x61, 0x79, 0x5f, 0x72, 0x18, 0x0c, 0x20, 0x01, 0x28,
  640 + 0x09, 0x52, 0x04, 0x70, 0x61, 0x79, 0x52, 0x12, 0x10, 0x0a, 0x03, 0x64, 0x65, 0x6c, 0x18, 0x0d,
  641 + 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x64, 0x65, 0x6c, 0x12, 0x30, 0x0a, 0x06, 0x69, 0x6e, 0x63,
  642 + 0x72, 0x65, 0x73, 0x18, 0x0e, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x6d, 0x6f, 0x64, 0x65,
  643 + 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x2e, 0x49, 0x6e, 0x63, 0x72, 0x65, 0x73, 0x45, 0x6e,
  644 + 0x74, 0x72, 0x79, 0x52, 0x06, 0x69, 0x6e, 0x63, 0x72, 0x65, 0x73, 0x1a, 0x39, 0x0a, 0x0b, 0x49,
  645 + 0x6e, 0x63, 0x72, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65,
  646 + 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05,
  647 + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x76, 0x61, 0x6c,
  648 + 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b,
  649 + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
577 650 }
578 651  
579 652 var (
... ... @@ -588,21 +661,24 @@ func file_models_proto_rawDescGZIP() []byte {
588 661 return file_models_proto_rawDescData
589 662 }
590 663  
591   -var file_models_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
  664 +var file_models_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
592 665 var file_models_proto_goTypes = []interface{}{
593 666 (*Account)(nil), // 0: models.Account
594 667 (*Hero)(nil), // 1: models.Hero
595 668 (*Equipment)(nil), // 2: models.Equipment
596 669 (*Prop)(nil), // 3: models.Prop
597 670 (*Team)(nil), // 4: models.Team
598   - (*Role)(nil), // 5: models.Role
  671 + (*Increment)(nil), // 5: models.Increment
  672 + (*Role)(nil), // 6: models.Role
  673 + nil, // 7: models.Role.IncresEntry
599 674 }
600 675 var file_models_proto_depIdxs = []int32{
601   - 0, // [0:0] is the sub-list for method output_type
602   - 0, // [0:0] is the sub-list for method input_type
603   - 0, // [0:0] is the sub-list for extension type_name
604   - 0, // [0:0] is the sub-list for extension extendee
605   - 0, // [0:0] is the sub-list for field type_name
  676 + 7, // 0: models.Role.incres:type_name -> models.Role.IncresEntry
  677 + 1, // [1:1] is the sub-list for method output_type
  678 + 1, // [1:1] is the sub-list for method input_type
  679 + 1, // [1:1] is the sub-list for extension type_name
  680 + 1, // [1:1] is the sub-list for extension extendee
  681 + 0, // [0:1] is the sub-list for field type_name
606 682 }
607 683  
608 684 func init() { file_models_proto_init() }
... ... @@ -672,6 +748,18 @@ func file_models_proto_init() {
672 748 }
673 749 }
674 750 file_models_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
  751 + switch v := v.(*Increment); i {
  752 + case 0:
  753 + return &v.state
  754 + case 1:
  755 + return &v.sizeCache
  756 + case 2:
  757 + return &v.unknownFields
  758 + default:
  759 + return nil
  760 + }
  761 + }
  762 + file_models_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
675 763 switch v := v.(*Role); i {
676 764 case 0:
677 765 return &v.state
... ... @@ -690,7 +778,7 @@ func file_models_proto_init() {
690 778 GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
691 779 RawDescriptor: file_models_proto_rawDesc,
692 780 NumEnums: 0,
693   - NumMessages: 6,
  781 + NumMessages: 8,
694 782 NumExtensions: 0,
695 783 NumServices: 0,
696 784 },
... ...
1   -Subproject commit c1d210bd2ef2b9ce1a23c747a814b6ee308f951d
  1 +Subproject commit f3d0853be4bc7062a327ed11b0e74ef10912b49e
... ...