Commit 1584eb4bb5401523aaed59ad2fd726baa00ef5e7
1 parent
d7ecc295
修复创建唯一索引的bug
Showing
15 changed files
with
186 additions
and
78 deletions
Show diff stats
actions/accountaction.go
| @@ -14,7 +14,7 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | @@ -14,7 +14,7 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | ||
| 14 | if !ok { | 14 | if !ok { |
| 15 | account.Phone = in.Phone | 15 | account.Phone = in.Phone |
| 16 | account.Password = utils.Md5V(in.Password) | 16 | account.Password = utils.Md5V(in.Password) |
| 17 | - account.Uid = conf.SnowFlack.NextVal() | 17 | + account.Uid = conf.SnowFlack.NextValStr() |
| 18 | account.Device = "123123" | 18 | account.Device = "123123" |
| 19 | account.Create() | 19 | account.Create() |
| 20 | }else { | 20 | }else { |
| @@ -27,7 +27,29 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | @@ -27,7 +27,29 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | ||
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | func (s *AccountServer) CreateTokenHandler(ctx context.Context, in *pb.AccountInfo) (*pb.CreateTokenRsp, error) { | 29 | func (s *AccountServer) CreateTokenHandler(ctx context.Context, in *pb.AccountInfo) (*pb.CreateTokenRsp, error) { |
| 30 | + m := models.NewAccount(in.Phone) | ||
| 31 | + if err := m.Load(); err != nil { | ||
| 32 | + return &pb.CreateTokenRsp{ | ||
| 33 | + Rsp: &pb.PubRsp{ | ||
| 34 | + Code: 1, | ||
| 35 | + Msg: err.Error(), | ||
| 36 | + }, | ||
| 37 | + }, nil | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + if m.Password != utils.Md5V(in.Password) { | ||
| 41 | + return &pb.CreateTokenRsp{ | ||
| 42 | + Rsp: &pb.PubRsp{ | ||
| 43 | + Code: 2, | ||
| 44 | + Msg: "password error", | ||
| 45 | + }, | ||
| 46 | + }, nil | ||
| 47 | + } | ||
| 48 | + | ||
| 30 | return &pb.CreateTokenRsp{ | 49 | return &pb.CreateTokenRsp{ |
| 50 | + Rsp: &pb.PubRsp{ | ||
| 51 | + Code: 0, | ||
| 52 | + }, | ||
| 31 | Token: utils.CreateToken(in), | 53 | Token: utils.CreateToken(in), |
| 32 | GameService: &pb.ServiceInfo{ | 54 | GameService: &pb.ServiceInfo{ |
| 33 | Id: "1", | 55 | Id: "1", |
actions/basic.go
| @@ -5,7 +5,6 @@ import ( | @@ -5,7 +5,6 @@ import ( | ||
| 5 | "net" | 5 | "net" |
| 6 | "pro2d/components/db" | 6 | "pro2d/components/db" |
| 7 | "pro2d/conf" | 7 | "pro2d/conf" |
| 8 | - "pro2d/utils" | ||
| 9 | ) | 8 | ) |
| 10 | 9 | ||
| 11 | type BasicServer struct { | 10 | type BasicServer struct { |
| @@ -20,10 +19,7 @@ func NewServer(db string) *BasicServer { | @@ -20,10 +19,7 @@ func NewServer(db string) *BasicServer { | ||
| 20 | 19 | ||
| 21 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { | 20 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { |
| 22 | //初始化数据库 | 21 | //初始化数据库 |
| 23 | - err := db.Connect(conf.GlobalConf.MongoConf.User, conf.GlobalConf.MongoConf.Password, conf.GlobalConf.MongoConf.Host, conf.GlobalConf.MongoConf.Port, conf.GlobalConf.MongoConf.MaxNum, conf.GlobalConf.MongoConf.TimeOut, sConf.DBName) | ||
| 24 | - if err != nil { | ||
| 25 | - utils.Sugar.Errorf("mongodb init error: %v", err) | ||
| 26 | - } | 22 | + db.MongoDatabase = db.MongoClient.Database(sConf.DBName) |
| 27 | 23 | ||
| 28 | listing := fmt.Sprintf(":%d", sConf.Port) | 24 | listing := fmt.Sprintf(":%d", sConf.Port) |
| 29 | lis, err := net.Listen("tcp", listing) | 25 | lis, err := net.Listen("tcp", listing) |
components/db/mongo.go
| @@ -8,6 +8,8 @@ import ( | @@ -8,6 +8,8 @@ import ( | ||
| 8 | "go.mongodb.org/mongo-driver/mongo/options" | 8 | "go.mongodb.org/mongo-driver/mongo/options" |
| 9 | "go.mongodb.org/mongo-driver/mongo/readpref" | 9 | "go.mongodb.org/mongo-driver/mongo/readpref" |
| 10 | "go.mongodb.org/mongo-driver/x/bsonx" | 10 | "go.mongodb.org/mongo-driver/x/bsonx" |
| 11 | + "pro2d/utils" | ||
| 12 | + "reflect" | ||
| 11 | "sort" | 13 | "sort" |
| 12 | "strconv" | 14 | "strconv" |
| 13 | "time" | 15 | "time" |
| @@ -19,7 +21,7 @@ var ( | @@ -19,7 +21,7 @@ var ( | ||
| 19 | ) | 21 | ) |
| 20 | 22 | ||
| 21 | //初始化 | 23 | //初始化 |
| 22 | -func Connect(user, password, host string,port int, MaxNum int, timeOut int, dbname string) error { | 24 | +func Connect(user, password, host string,port int, MaxNum int, timeOut int) error { |
| 23 | var uri string | 25 | var uri string |
| 24 | if user!= "" { | 26 | if user!= "" { |
| 25 | //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) | 27 | //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) |
| @@ -46,7 +48,7 @@ func Connect(user, password, host string,port int, MaxNum int, timeOut int, dbna | @@ -46,7 +48,7 @@ func Connect(user, password, host string,port int, MaxNum int, timeOut int, dbna | ||
| 46 | return err | 48 | return err |
| 47 | } | 49 | } |
| 48 | 50 | ||
| 49 | - MongoDatabase = MongoClient.Database(dbname) | 51 | + //MongoDatabase = MongoClient.Database(dbname) |
| 50 | return nil | 52 | return nil |
| 51 | } | 53 | } |
| 52 | 54 | ||
| @@ -85,17 +87,18 @@ func GetBsonM(key string, value interface{}) interface{} { | @@ -85,17 +87,18 @@ func GetBsonM(key string, value interface{}) interface{} { | ||
| 85 | return bson.M{key: value} | 87 | return bson.M{key: value} |
| 86 | } | 88 | } |
| 87 | 89 | ||
| 88 | -func NewMongoColl(collection string, pri, schema interface{}) *MgoColl { | 90 | +func NewMongoColl(key string, schema interface{}) *MgoColl { |
| 89 | return &MgoColl{ | 91 | return &MgoColl{ |
| 90 | - collection: MongoDatabase.Collection(collection), | ||
| 91 | - | ||
| 92 | - pri: pri, | 92 | + collection: MongoDatabase.Collection(utils.GetCollName(schema)), |
| 93 | + pri: GetBsonM(utils.GetPriKey(schema), key), | ||
| 93 | schema: schema, | 94 | schema: schema, |
| 94 | } | 95 | } |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | -func (m *MgoColl)SetDatabase(databases string) { | ||
| 98 | - //m.db = databases | 98 | +func FindOne(pri interface{}, schema interface{}) error { |
| 99 | + s := reflect.TypeOf(schema) | ||
| 100 | + r := MongoDatabase.Collection(s.Name()).FindOne(context.TODO(), pri) | ||
| 101 | + return r.Decode(schema) | ||
| 99 | } | 102 | } |
| 100 | 103 | ||
| 101 | // 查询单个 | 104 | // 查询单个 |
| @@ -106,21 +109,6 @@ func (m *MgoColl) FindOneKV(key string, value interface{}) *mongo.SingleResult { | @@ -106,21 +109,6 @@ func (m *MgoColl) FindOneKV(key string, value interface{}) *mongo.SingleResult { | ||
| 106 | return singleResult | 109 | return singleResult |
| 107 | } | 110 | } |
| 108 | 111 | ||
| 109 | -func (m *MgoColl) FindOne(pri interface{}) *mongo.SingleResult { | ||
| 110 | - //collection. | ||
| 111 | - singleResult := m.collection.FindOne(context.TODO(), pri) | ||
| 112 | - return singleResult | ||
| 113 | -} | ||
| 114 | - | ||
| 115 | -//插入单个 | ||
| 116 | -func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { | ||
| 117 | - insertResult, err := m.collection.InsertOne(context.TODO(), value) | ||
| 118 | - if err != nil { | ||
| 119 | - fmt.Println(err) | ||
| 120 | - } | ||
| 121 | - return insertResult | ||
| 122 | -} | ||
| 123 | - | ||
| 124 | //查询集合里有多少数据 | 112 | //查询集合里有多少数据 |
| 125 | func (m *MgoColl) CollectionCount() (string, int64) { | 113 | func (m *MgoColl) CollectionCount() (string, int64) { |
| 126 | size, _ := m.collection.EstimatedDocumentCount(context.TODO()) | 114 | size, _ := m.collection.EstimatedDocumentCount(context.TODO()) |
| @@ -219,7 +207,7 @@ func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.Update | @@ -219,7 +207,7 @@ func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.Update | ||
| 219 | 207 | ||
| 220 | 208 | ||
| 221 | func (m *MgoColl) Load() error{ | 209 | func (m *MgoColl) Load() error{ |
| 222 | - r := m.FindOne(m.pri) | 210 | + r := m.collection.FindOne(context.TODO(), m.pri) |
| 223 | err := r.Decode(m.schema) | 211 | err := r.Decode(m.schema) |
| 224 | if err != nil { | 212 | if err != nil { |
| 225 | return err | 213 | return err |
| @@ -227,8 +215,8 @@ func (m *MgoColl) Load() error{ | @@ -227,8 +215,8 @@ func (m *MgoColl) Load() error{ | ||
| 227 | return nil | 215 | return nil |
| 228 | } | 216 | } |
| 229 | 217 | ||
| 230 | -func (m *MgoColl) Create() { | ||
| 231 | - m.InsertOne(m.schema) | 218 | +func (m *MgoColl) Create() (*mongo.InsertOneResult, error){ |
| 219 | + return m.collection.InsertOne(context.TODO(), m.schema) | ||
| 232 | } | 220 | } |
| 233 | 221 | ||
| 234 | func (m *MgoColl) Update(update interface{}) { | 222 | func (m *MgoColl) Update(update interface{}) { |
conf/conf.go
| @@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
| 5 | lumberjack "gopkg.in/natefinch/lumberjack.v2" | 5 | lumberjack "gopkg.in/natefinch/lumberjack.v2" |
| 6 | "gopkg.in/yaml.v3" | 6 | "gopkg.in/yaml.v3" |
| 7 | "io/ioutil" | 7 | "io/ioutil" |
| 8 | + "pro2d/components/db" | ||
| 8 | "pro2d/utils" | 9 | "pro2d/utils" |
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| @@ -73,4 +74,8 @@ func init() { | @@ -73,4 +74,8 @@ func init() { | ||
| 73 | //初始化雪花算法 | 74 | //初始化雪花算法 |
| 74 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) | 75 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) |
| 75 | 76 | ||
| 77 | + err = db.Connect(GlobalConf.MongoConf.User, GlobalConf.MongoConf.Password, GlobalConf.MongoConf.Host, GlobalConf.MongoConf.Port, GlobalConf.MongoConf.MaxNum, GlobalConf.MongoConf.TimeOut) | ||
| 78 | + if err != nil { | ||
| 79 | + utils.Sugar.Errorf("connect db err: %v", err) | ||
| 80 | + } | ||
| 76 | } | 81 | } |
| 77 | \ No newline at end of file | 82 | \ No newline at end of file |
models/account.go
| @@ -23,7 +23,7 @@ func NewAccount(phone string) *AccountModel { | @@ -23,7 +23,7 @@ func NewAccount(phone string) *AccountModel { | ||
| 23 | Phone: phone, | 23 | Phone: phone, |
| 24 | } | 24 | } |
| 25 | account := &AccountModel{ | 25 | account := &AccountModel{ |
| 26 | - MgoColl: db.NewMongoColl( "account", db.GetBsonM("phone", phone), ac), | 26 | + MgoColl: db.NewMongoColl(phone, ac), |
| 27 | AccountInfo: ac, | 27 | AccountInfo: ac, |
| 28 | } | 28 | } |
| 29 | 29 |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +package models | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "pro2d/components/db" | ||
| 5 | + "pro2d/protos/pb" | ||
| 6 | + "strconv" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type EquipModels struct { | ||
| 10 | + *db.MgoColl | ||
| 11 | + Equip *pb.Equipment | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func NewEquip(id int64) *EquipModels{ | ||
| 15 | + data := &pb.Equipment{ | ||
| 16 | + Id: id, | ||
| 17 | + } | ||
| 18 | + m := &EquipModels { | ||
| 19 | + MgoColl: db.NewMongoColl(strconv.Itoa(int(id)), data), | ||
| 20 | + Equip: data, | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + return m | ||
| 24 | +} | ||
| 0 | \ No newline at end of file | 25 | \ No newline at end of file |
models/hero.go
| @@ -3,6 +3,7 @@ package models | @@ -3,6 +3,7 @@ package models | ||
| 3 | import ( | 3 | import ( |
| 4 | "pro2d/components/db" | 4 | "pro2d/components/db" |
| 5 | "pro2d/protos/pb" | 5 | "pro2d/protos/pb" |
| 6 | + "strconv" | ||
| 6 | ) | 7 | ) |
| 7 | 8 | ||
| 8 | type HeroModel struct { | 9 | type HeroModel struct { |
| @@ -24,7 +25,7 @@ func NewHero(id int64) *HeroModel { | @@ -24,7 +25,7 @@ func NewHero(id int64) *HeroModel { | ||
| 24 | Id: id, | 25 | Id: id, |
| 25 | } | 26 | } |
| 26 | m := &HeroModel{ | 27 | m := &HeroModel{ |
| 27 | - MgoColl: db.NewMongoColl("hero", db.GetBsonM("id", h.Id), h), | 28 | + MgoColl: db.NewMongoColl(strconv.Itoa(int(id)), h), |
| 28 | Hero: h, | 29 | Hero: h, |
| 29 | } | 30 | } |
| 30 | return m | 31 | return m |
models/init.go
| @@ -8,13 +8,12 @@ import ( | @@ -8,13 +8,12 @@ import ( | ||
| 8 | 8 | ||
| 9 | func InitDoc(schema ...interface{}) { | 9 | func InitDoc(schema ...interface{}) { |
| 10 | for _, s := range schema { | 10 | for _, s := range schema { |
| 11 | - for coll, key := range utils.FindIndex(s) { | ||
| 12 | - if err := db.CreateCollection(coll); err != nil { | ||
| 13 | - continue | ||
| 14 | - } | 11 | + coll, keys := utils.FindIndex(s) |
| 12 | + for _, index := range keys { | ||
| 13 | + db.CreateCollection(coll) | ||
| 15 | 14 | ||
| 16 | - utils.Sugar.Debugf("InitDoc collect: %v, createIndex: %s", coll, key) | ||
| 17 | - res, err := db.SetUnique(coll, key) | 15 | + utils.Sugar.Debugf("InitDoc collect: %v, createIndex: %s", coll, index) |
| 16 | + res, err := db.SetUnique(coll, index) | ||
| 18 | if err != nil { | 17 | if err != nil { |
| 19 | utils.Sugar.Errorf("InitDoc unique: %s, err: %v", res, err) | 18 | utils.Sugar.Errorf("InitDoc unique: %s, err: %v", res, err) |
| 20 | continue | 19 | continue |
models/init_test.go
| @@ -3,25 +3,18 @@ package models | @@ -3,25 +3,18 @@ package models | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | "pro2d/components/db" | 5 | "pro2d/components/db" |
| 6 | - "pro2d/conf" | ||
| 7 | _ "pro2d/conf" | 6 | _ "pro2d/conf" |
| 8 | - "pro2d/utils" | ||
| 9 | "testing" | 7 | "testing" |
| 10 | ) | 8 | ) |
| 11 | 9 | ||
| 12 | 10 | ||
| 13 | func TestInitModels(t *testing.T) { | 11 | func TestInitModels(t *testing.T) { |
| 14 | - err := db.Connect(conf.GlobalConf.MongoConf.User, conf.GlobalConf.MongoConf.Password, conf.GlobalConf.MongoConf.Host, conf.GlobalConf.MongoConf.Port, conf.GlobalConf.MongoConf.MaxNum, conf.GlobalConf.MongoConf.TimeOut,"account") | ||
| 15 | - if err != nil { | ||
| 16 | - utils.Sugar.Errorf("mongodb init error: %v", err) | ||
| 17 | - } | ||
| 18 | 12 | ||
| 13 | + db.MongoDatabase = db.MongoClient.Database("account") | ||
| 19 | InitAccountServerModels() | 14 | InitAccountServerModels() |
| 20 | db.MongoClient.Disconnect(context.TODO()) | 15 | db.MongoClient.Disconnect(context.TODO()) |
| 21 | - err = db.Connect(conf.GlobalConf.MongoConf.User, conf.GlobalConf.MongoConf.Password, conf.GlobalConf.MongoConf.Host, conf.GlobalConf.MongoConf.Port, conf.GlobalConf.MongoConf.MaxNum, conf.GlobalConf.MongoConf.TimeOut,"game") | ||
| 22 | - if err != nil { | ||
| 23 | - utils.Sugar.Errorf("mongodb init error: %v", err) | ||
| 24 | - } | 16 | + |
| 17 | + db.MongoDatabase = db.MongoClient.Database("game") | ||
| 25 | InitGameServerModels() | 18 | InitGameServerModels() |
| 26 | db.MongoClient.Disconnect(context.TODO()) | 19 | db.MongoClient.Disconnect(context.TODO()) |
| 27 | } | 20 | } |
| 28 | \ No newline at end of file | 21 | \ No newline at end of file |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +package models | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "pro2d/components/db" | ||
| 5 | + "pro2d/protos/pb" | ||
| 6 | + "strconv" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +type PropModels struct { | ||
| 10 | + *db.MgoColl | ||
| 11 | + Prop *pb.Prop | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func NewProp(id int64) *PropModels{ | ||
| 15 | + data := &pb.Prop{ | ||
| 16 | + Id: id, | ||
| 17 | + } | ||
| 18 | + m := &PropModels{ | ||
| 19 | + MgoColl: db.NewMongoColl(strconv.Itoa(int(id)), data), | ||
| 20 | + Prop: data, | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + return m | ||
| 24 | +} | ||
| 0 | \ No newline at end of file | 25 | \ No newline at end of file |
models/role.go
| @@ -4,30 +4,34 @@ import ( | @@ -4,30 +4,34 @@ import ( | ||
| 4 | "fmt" | 4 | "fmt" |
| 5 | "pro2d/components/db" | 5 | "pro2d/components/db" |
| 6 | "pro2d/protos/pb" | 6 | "pro2d/protos/pb" |
| 7 | + "strconv" | ||
| 7 | ) | 8 | ) |
| 8 | 9 | ||
| 9 | type RoleModel struct { | 10 | type RoleModel struct { |
| 10 | *db.MgoColl | 11 | *db.MgoColl |
| 11 | Role *pb.Role | 12 | Role *pb.Role |
| 12 | Heros HeroMap | 13 | Heros HeroMap |
| 13 | - Teams *pb.Team | 14 | + Teams *TeamModel |
| 14 | Equip *pb.Equipment | 15 | Equip *pb.Equipment |
| 15 | Prop *pb.Prop | 16 | Prop *pb.Prop |
| 16 | } | 17 | } |
| 17 | 18 | ||
| 18 | -func RoleExistByUid(uid int64) (bool, *RoleModel){ | ||
| 19 | - m := NewRole(uid) | ||
| 20 | - if err := m.Load(); err != nil { | ||
| 21 | - return false, m | 19 | +func RoleExistByUid(uid string) (bool, *RoleModel){ |
| 20 | + data := &pb.Role{Uid: uid} | ||
| 21 | + | ||
| 22 | + if err := db.FindOne(db.GetBsonM("uid", uid), data); err != nil { | ||
| 23 | + return false, nil | ||
| 22 | } | 24 | } |
| 25 | + m := NewRole(data.Id) | ||
| 26 | + m.Load() | ||
| 23 | return true, m | 27 | return true, m |
| 24 | } | 28 | } |
| 25 | 29 | ||
| 26 | -func NewRole(uid int64) *RoleModel { | ||
| 27 | - r := &pb.Role{Uid: uid} | 30 | +func NewRole(id int64) *RoleModel { |
| 31 | + data := &pb.Role{Id: id} | ||
| 28 | m := &RoleModel{ | 32 | m := &RoleModel{ |
| 29 | - MgoColl: db.NewMongoColl("role", db.GetBsonM("uid", r.Uid), r), | ||
| 30 | - Role: r, | 33 | + MgoColl: db.NewMongoColl(strconv.Itoa(int(id)), data), |
| 34 | + Role: data, | ||
| 31 | Heros: make(HeroMap), | 35 | Heros: make(HeroMap), |
| 32 | } | 36 | } |
| 33 | return m | 37 | return m |
models/role_test.go
| 1 | package models | 1 | package models |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "fmt" | ||
| 5 | + "pro2d/components/db" | ||
| 6 | + "pro2d/conf" | ||
| 4 | "pro2d/protos/pb" | 7 | "pro2d/protos/pb" |
| 8 | + "pro2d/utils" | ||
| 5 | "testing" | 9 | "testing" |
| 6 | ) | 10 | ) |
| 7 | 11 | ||
| 8 | func TestNewRole(t *testing.T) { | 12 | func TestNewRole(t *testing.T) { |
| 9 | //db.MongoDBClient.Database(conf.GlobalConf.AccountConf.DBName).Drop(context.Background()) | 13 | //db.MongoDBClient.Database(conf.GlobalConf.AccountConf.DBName).Drop(context.Background()) |
| 10 | //db.MongoDBClient.Database(conf.GlobalConf.GameConf.DBName).Drop(context.Background()) | 14 | //db.MongoDBClient.Database(conf.GlobalConf.GameConf.DBName).Drop(context.Background()) |
| 15 | + db.MongoDatabase = db.MongoClient.Database("game") | ||
| 11 | 16 | ||
| 12 | - var uid int64 = 1 | ||
| 13 | - var role *RoleModel | ||
| 14 | - if ok, role := RoleExistByUid(uid); ok { | ||
| 15 | - role.Role.Device = "111111" | 17 | + var uid = conf.SnowFlack.NextValStr() |
| 18 | + ok, role := RoleExistByUid(uid) | ||
| 19 | + if ok { | ||
| 20 | + //uid存在 , 更新角色 | ||
| 21 | + role.Role.Device = "222222" | ||
| 16 | role.AddHero(&pb.Hero{ | 22 | role.AddHero(&pb.Hero{ |
| 17 | Id: 1, | 23 | Id: 1, |
| 18 | RoleId: role.Role.Id, | 24 | RoleId: role.Role.Id, |
| @@ -24,12 +30,20 @@ func TestNewRole(t *testing.T) { | @@ -24,12 +30,20 @@ func TestNewRole(t *testing.T) { | ||
| 24 | }) | 30 | }) |
| 25 | role.Save() | 31 | role.Save() |
| 26 | }else { | 32 | }else { |
| 27 | - role = NewRole(uid) | ||
| 28 | - role.Role.Id = 1 | ||
| 29 | - role.Role.Device = "222222" | 33 | + //uid不存在,创建角色 |
| 34 | + role = NewRole(1) | ||
| 35 | + role.Role.Uid = uid | ||
| 36 | + role.Role.Device = "111111" | ||
| 30 | role.Role.Level = 0 | 37 | role.Role.Level = 0 |
| 31 | - role.Create() | ||
| 32 | - role.SetUnique("uid") | 38 | + i, err := role.Create() |
| 39 | + fmt.Println(i, err) | ||
| 33 | } | 40 | } |
| 34 | print(role) | 41 | print(role) |
| 42 | +} | ||
| 43 | + | ||
| 44 | +func TestRoleIndex(t *testing.T) { | ||
| 45 | + coll, keys := utils.FindIndex(pb.Role{}) | ||
| 46 | + for _, index := range keys { | ||
| 47 | + utils.Sugar.Debugf("coll: %s, key: %s", coll, index) | ||
| 48 | + } | ||
| 35 | } | 49 | } |
| 36 | \ No newline at end of file | 50 | \ No newline at end of file |
models/team.go
| @@ -3,6 +3,7 @@ package models | @@ -3,6 +3,7 @@ package models | ||
| 3 | import ( | 3 | import ( |
| 4 | "pro2d/components/db" | 4 | "pro2d/components/db" |
| 5 | "pro2d/protos/pb" | 5 | "pro2d/protos/pb" |
| 6 | + "strconv" | ||
| 6 | ) | 7 | ) |
| 7 | 8 | ||
| 8 | type TeamModel struct { | 9 | type TeamModel struct { |
| @@ -15,7 +16,7 @@ func NewTeam(id int64) *TeamModel{ | @@ -15,7 +16,7 @@ func NewTeam(id int64) *TeamModel{ | ||
| 15 | Id: id, | 16 | Id: id, |
| 16 | } | 17 | } |
| 17 | m := &TeamModel{ | 18 | m := &TeamModel{ |
| 18 | - MgoColl: db.NewMongoColl( "team", data, data), | 19 | + MgoColl: db.NewMongoColl(strconv.Itoa(int(id)), data), |
| 19 | Team: data, | 20 | Team: data, |
| 20 | } | 21 | } |
| 21 | 22 |
test/client.go
| @@ -64,16 +64,26 @@ func main() { | @@ -64,16 +64,26 @@ func main() { | ||
| 64 | } | 64 | } |
| 65 | defer conn.Close() | 65 | defer conn.Close() |
| 66 | c := pb.NewAccountClient(conn) | 66 | c := pb.NewAccountClient(conn) |
| 67 | - //err = Register(c,"17683852936", "123456") | ||
| 68 | - //if err != nil { | ||
| 69 | - // utils.Sugar.Errorf("register err: %v", err) | ||
| 70 | - // return | ||
| 71 | - //} | 67 | + err = Register(c,"17683852936", "123456") |
| 68 | + if err != nil { | ||
| 69 | + utils.Sugar.Errorf("register err: %v", err) | ||
| 70 | + return | ||
| 71 | + } | ||
| 72 | rsp, err := c.CreateTokenHandler(context.Background(), &pb.AccountInfo{ | 72 | rsp, err := c.CreateTokenHandler(context.Background(), &pb.AccountInfo{ |
| 73 | Phone: "17683852936", | 73 | Phone: "17683852936", |
| 74 | Password: "123456", | 74 | Password: "123456", |
| 75 | }) | 75 | }) |
| 76 | 76 | ||
| 77 | + if err != nil { | ||
| 78 | + utils.Sugar.Errorf("createtoken err: %v", err) | ||
| 79 | + return | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + if rsp.Rsp.Code != 0 { | ||
| 83 | + utils.Sugar.Errorf("createtoken err: %v", rsp.Rsp.Msg) | ||
| 84 | + return | ||
| 85 | + } | ||
| 86 | + | ||
| 77 | Login(rsp.GameService.Address, rsp.Token) | 87 | Login(rsp.GameService.Address, rsp.Token) |
| 78 | 88 | ||
| 79 | 89 |
utils/utils.go
| @@ -5,18 +5,45 @@ import ( | @@ -5,18 +5,45 @@ import ( | ||
| 5 | "strings" | 5 | "strings" |
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | +func GetCollName(schema interface{}) string { | ||
| 9 | + s := reflect.TypeOf(schema) | ||
| 10 | + if s.Kind() == reflect.Ptr { | ||
| 11 | + s = reflect.TypeOf(schema).Elem() | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + return strings.ToLower(s.Name()) | ||
| 15 | +} | ||
| 8 | 16 | ||
| 9 | -func FindIndex(schema interface{}) map[string]string{ | 17 | +func GetPriKey(schema interface{}) string { |
| 10 | s := reflect.TypeOf(schema) | 18 | s := reflect.TypeOf(schema) |
| 11 | - tb := make(map[string]string) | 19 | + if s.Kind() == reflect.Ptr { |
| 20 | + s = reflect.TypeOf(schema).Elem() | ||
| 21 | + } | ||
| 22 | + var pri string | ||
| 23 | + for i := 0; i < s.NumField(); i++ { | ||
| 24 | + if s.Field(i).Tag.Get("pri") == "1" { | ||
| 25 | + pri = strings.ToLower(s.Field(i).Name) | ||
| 26 | + break | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + return pri | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +func FindIndex(schema interface{}) (string, []string){ | ||
| 33 | + s := reflect.TypeOf(schema) | ||
| 34 | + if s.Kind() == reflect.Ptr { | ||
| 35 | + s = reflect.TypeOf(schema).Elem() | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + var index []string | ||
| 12 | for i := 0; i < s.NumField(); i++ { | 39 | for i := 0; i < s.NumField(); i++ { |
| 13 | if s.Field(i).Tag.Get("index") != "" { | 40 | if s.Field(i).Tag.Get("index") != "" { |
| 14 | js := strings.Split(s.Field(i).Tag.Get("json"), ",") | 41 | js := strings.Split(s.Field(i).Tag.Get("json"), ",") |
| 15 | if len(js) == 0 { | 42 | if len(js) == 0 { |
| 16 | continue | 43 | continue |
| 17 | } | 44 | } |
| 18 | - tb[strings.ToLower(s.Name())] = js[0] | 45 | + index = append(index, js[0]) |
| 19 | } | 46 | } |
| 20 | } | 47 | } |
| 21 | - return tb | 48 | + return strings.ToLower(s.Name()), index |
| 22 | } | 49 | } |
| 23 | \ No newline at end of file | 50 | \ No newline at end of file |