Commit 3592dfd3747ba9d086918986319e82dac1745af3
1 parent
eb417b0b
重构models, 索引唯一索引
Showing
20 changed files
with
239 additions
and
49 deletions
Show diff stats
Makefile
README.md
actions/accountaction.go
... | ... | @@ -10,7 +10,7 @@ import ( |
10 | 10 | ) |
11 | 11 | |
12 | 12 | func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, error) { |
13 | - ok, account := models.AccountExistByPhone(s.DBName, in.Phone) | |
13 | + ok, account := models.AccountExistByPhone(in.Phone) | |
14 | 14 | if !ok { |
15 | 15 | account.Phone = in.Phone |
16 | 16 | account.Password = utils.Md5V(in.Password) | ... | ... |
actions/basic.go
... | ... | @@ -3,7 +3,9 @@ package actions |
3 | 3 | import ( |
4 | 4 | "fmt" |
5 | 5 | "net" |
6 | + "pro2d/components/db" | |
6 | 7 | "pro2d/conf" |
8 | + "pro2d/utils" | |
7 | 9 | ) |
8 | 10 | |
9 | 11 | type BasicServer struct { |
... | ... | @@ -17,6 +19,12 @@ func NewServer(db string) *BasicServer { |
17 | 19 | } |
18 | 20 | |
19 | 21 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { |
22 | + //初始化数据库 | |
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 | + } | |
27 | + | |
20 | 28 | listing := fmt.Sprintf(":%d", sConf.Port) |
21 | 29 | lis, err := net.Listen("tcp", listing) |
22 | 30 | if err != nil { | ... | ... |
actions/server.go
... | ... | @@ -5,6 +5,7 @@ import ( |
5 | 5 | "google.golang.org/grpc" |
6 | 6 | "google.golang.org/grpc/reflection" |
7 | 7 | "pro2d/conf" |
8 | + "pro2d/models" | |
8 | 9 | "pro2d/protos/pb" |
9 | 10 | "pro2d/utils" |
10 | 11 | ) |
... | ... | @@ -35,6 +36,8 @@ func (s *AccountServer)Start() error { |
35 | 36 | return err |
36 | 37 | } |
37 | 38 | |
39 | + models.InitAccountServerModels() | |
40 | + | |
38 | 41 | //new一个grpc |
39 | 42 | gs := grpc.NewServer(grpc.UnaryInterceptor(AccountServerInterceptor)) |
40 | 43 | |
... | ... | @@ -52,7 +55,6 @@ func (s *AccountServer)Stop() { |
52 | 55 | s.BasicServer.Close() |
53 | 56 | } |
54 | 57 | |
55 | - | |
56 | 58 | type GameServer struct{ |
57 | 59 | pb.UnimplementedGameServer |
58 | 60 | *BasicServer |
... | ... | @@ -78,6 +80,8 @@ func (s *GameServer)Start() error { |
78 | 80 | return err |
79 | 81 | } |
80 | 82 | |
83 | + models.InitGameServerModels() | |
84 | + | |
81 | 85 | //new一个grpc |
82 | 86 | gs := grpc.NewServer(grpc.UnaryInterceptor(GameServerInterceptor)) |
83 | 87 | ... | ... |
... | ... | @@ -0,0 +1,17 @@ |
1 | +package main | |
2 | + | |
3 | +import ( | |
4 | + "pro2d/actions" | |
5 | + "pro2d/utils" | |
6 | +) | |
7 | + | |
8 | +func main() { | |
9 | + err := make(chan error) | |
10 | + server := actions.NewAccountServer() | |
11 | + go func() { | |
12 | + defer server.Stop() | |
13 | + err <- server.Start() | |
14 | + }() | |
15 | + | |
16 | + utils.Sugar.Errorf("server error: %v", <- err) | |
17 | +} | ... | ... |
... | ... | @@ -0,0 +1,19 @@ |
1 | +package main | |
2 | + | |
3 | +import ( | |
4 | + "pro2d/actions" | |
5 | + "pro2d/utils" | |
6 | +) | |
7 | + | |
8 | +func main() { | |
9 | + err := make(chan error) | |
10 | + | |
11 | + | |
12 | + server := actions.NewGameServer() | |
13 | + go func() { | |
14 | + defer server.Stop() | |
15 | + err <- server.Start() | |
16 | + }() | |
17 | + | |
18 | + utils.Sugar.Errorf("server error: %v", <- err) | |
19 | +} | ... | ... |
components/db/mongo.go
... | ... | @@ -8,14 +8,18 @@ import ( |
8 | 8 | "go.mongodb.org/mongo-driver/mongo/options" |
9 | 9 | "go.mongodb.org/mongo-driver/mongo/readpref" |
10 | 10 | "go.mongodb.org/mongo-driver/x/bsonx" |
11 | + "sort" | |
11 | 12 | "strconv" |
12 | 13 | "time" |
13 | 14 | ) |
14 | 15 | |
15 | -var MongoDBClient *mongo.Client | |
16 | +var ( | |
17 | + MongoClient *mongo.Client | |
18 | + MongoDatabase *mongo.Database | |
19 | +) | |
16 | 20 | |
17 | 21 | //初始化 |
18 | -func Connect(user, password, host string,port int, MaxNum int, timeOut int) error { | |
22 | +func Connect(user, password, host string,port int, MaxNum int, timeOut int, dbname string) error { | |
19 | 23 | var uri string |
20 | 24 | if user!= "" { |
21 | 25 | //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) |
... | ... | @@ -33,19 +37,42 @@ func Connect(user, password, host string,port int, MaxNum int, timeOut int) erro |
33 | 37 | o.SetMaxPoolSize(uint64(MaxNum)) |
34 | 38 | // 发起链接 |
35 | 39 | var err error |
36 | - MongoDBClient, err = mongo.Connect(ctx, o) | |
40 | + MongoClient , err = mongo.Connect(ctx, o) | |
37 | 41 | if err != nil { |
38 | 42 | return err |
39 | 43 | } |
40 | 44 | // 判断服务是不是可用 |
41 | - if err = MongoDBClient.Ping(context.Background(), readpref.Primary()); err != nil { | |
45 | + if err = MongoClient.Ping(context.Background(), readpref.Primary()); err != nil { | |
42 | 46 | return err |
43 | 47 | } |
48 | + | |
49 | + MongoDatabase = MongoClient.Database(dbname) | |
44 | 50 | return nil |
45 | 51 | } |
46 | 52 | |
53 | +func CreateCollection(collection string) error { | |
54 | + colls, _ := MongoDatabase.ListCollectionNames(context.TODO(), bson.D{}) | |
55 | + pos := sort.SearchStrings(colls, collection) | |
56 | + if pos != len(colls) { | |
57 | + if collection == colls[pos] { | |
58 | + return MongoDatabase.CreateCollection(context.TODO(), collection) | |
59 | + } | |
60 | + } | |
61 | + return MongoDatabase.CreateCollection(context.TODO(), collection) | |
62 | +} | |
63 | + | |
64 | +func SetUnique(collection string, key string) (string, error) { | |
65 | + return MongoDatabase.Collection(collection).Indexes().CreateOne( | |
66 | + context.TODO(), | |
67 | + mongo.IndexModel{ | |
68 | + Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, | |
69 | + Options: options.Index().SetUnique(true), | |
70 | + }, | |
71 | + ) | |
72 | +} | |
73 | + | |
47 | 74 | type MgoColl struct { |
48 | - mgo *mongo.Collection | |
75 | + collection *mongo.Collection | |
49 | 76 | |
50 | 77 | pri interface{} |
51 | 78 | schema interface{} |
... | ... | @@ -58,9 +85,9 @@ func GetBsonM(key string, value interface{}) interface{} { |
58 | 85 | return bson.M{key: value} |
59 | 86 | } |
60 | 87 | |
61 | -func NewMongoColl(database, collection string, pri, schema interface{}) *MgoColl { | |
88 | +func NewMongoColl(collection string, pri, schema interface{}) *MgoColl { | |
62 | 89 | return &MgoColl{ |
63 | - mgo: MongoDBClient.Database(database).Collection(collection), | |
90 | + collection: MongoDatabase.Collection(collection), | |
64 | 91 | |
65 | 92 | pri: pri, |
66 | 93 | schema: schema, |
... | ... | @@ -75,19 +102,19 @@ func (m *MgoColl)SetDatabase(databases string) { |
75 | 102 | func (m *MgoColl) FindOneKV(key string, value interface{}) *mongo.SingleResult { |
76 | 103 | //collection. |
77 | 104 | filter := bson.D{ {key, value}} |
78 | - singleResult := m.mgo.FindOne(context.TODO(), filter) | |
105 | + singleResult := m.collection.FindOne(context.TODO(), filter) | |
79 | 106 | return singleResult |
80 | 107 | } |
81 | 108 | |
82 | 109 | func (m *MgoColl) FindOne(pri interface{}) *mongo.SingleResult { |
83 | 110 | //collection. |
84 | - singleResult := m.mgo.FindOne(context.TODO(), pri) | |
111 | + singleResult := m.collection.FindOne(context.TODO(), pri) | |
85 | 112 | return singleResult |
86 | 113 | } |
87 | 114 | |
88 | 115 | //插入单个 |
89 | 116 | func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { |
90 | - insertResult, err := m.mgo.InsertOne(context.TODO(), value) | |
117 | + insertResult, err := m.collection.InsertOne(context.TODO(), value) | |
91 | 118 | if err != nil { |
92 | 119 | fmt.Println(err) |
93 | 120 | } |
... | ... | @@ -96,8 +123,8 @@ func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { |
96 | 123 | |
97 | 124 | //查询集合里有多少数据 |
98 | 125 | func (m *MgoColl) CollectionCount() (string, int64) { |
99 | - size, _ := m.mgo.EstimatedDocumentCount(context.TODO()) | |
100 | - return m.mgo.Name(), size | |
126 | + size, _ := m.collection.EstimatedDocumentCount(context.TODO()) | |
127 | + return m.collection.Name(), size | |
101 | 128 | } |
102 | 129 | |
103 | 130 | //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取 |
... | ... | @@ -108,7 +135,7 @@ func (m *MgoColl) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor |
108 | 135 | {}} |
109 | 136 | findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) |
110 | 137 | //findOptions.SetLimit(i) |
111 | - temp, _ := m.mgo.Find(context.Background(), filter, findOptions) | |
138 | + temp, _ := m.collection.Find(context.Background(), filter, findOptions) | |
112 | 139 | return temp |
113 | 140 | } |
114 | 141 | |
... | ... | @@ -126,8 +153,8 @@ func (m *MgoColl) ParsingId(result string) (time.Time, uint64) { |
126 | 153 | func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) { |
127 | 154 | filter := bson.D{ |
128 | 155 | {key, value}} |
129 | - singleResult := m.mgo.FindOne(context.TODO(), filter) | |
130 | - DeleteResult, err := m.mgo.DeleteOne(context.TODO(), filter, nil) | |
156 | + singleResult := m.collection.FindOne(context.TODO(), filter) | |
157 | + DeleteResult, err := m.collection.DeleteOne(context.TODO(), filter, nil) | |
131 | 158 | if err != nil { |
132 | 159 | fmt.Println("删除时出现错误,你删不掉的~") |
133 | 160 | } |
... | ... | @@ -137,7 +164,7 @@ func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.Si |
137 | 164 | //删除文章 |
138 | 165 | func (m *MgoColl) Delete(key string, value interface{}) int64 { |
139 | 166 | filter := bson.D{ {key, value}} |
140 | - count, err := m.mgo.DeleteOne(context.TODO(), filter, nil) | |
167 | + count, err := m.collection.DeleteOne(context.TODO(), filter, nil) | |
141 | 168 | if err != nil { |
142 | 169 | fmt.Println(err) |
143 | 170 | } |
... | ... | @@ -149,7 +176,7 @@ func (m *MgoColl) Delete(key string, value interface{}) int64 { |
149 | 176 | func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { |
150 | 177 | filter := bson.D{ {key, value}} |
151 | 178 | |
152 | - count, err := m.mgo.DeleteMany(context.TODO(), filter) | |
179 | + count, err := m.collection.DeleteMany(context.TODO(), filter) | |
153 | 180 | if err != nil { |
154 | 181 | fmt.Println(err) |
155 | 182 | } |
... | ... | @@ -157,8 +184,8 @@ func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { |
157 | 184 | } |
158 | 185 | |
159 | 186 | //索引 |
160 | -func (m *MgoColl) Index(key string){ | |
161 | - m.mgo.Indexes().CreateOne( | |
187 | +func (m *MgoColl) SetUnique(key string){ | |
188 | + m.collection.Indexes().CreateOne( | |
162 | 189 | context.Background(), |
163 | 190 | mongo.IndexModel{ |
164 | 191 | Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, |
... | ... | @@ -172,7 +199,7 @@ func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo |
172 | 199 | //filter := bson.M{"name": "x", "array.name": "b"} |
173 | 200 | //update := bson.M{"array.$[item].detail": "test"} |
174 | 201 | |
175 | - res := m.mgo.FindOneAndUpdate(context.Background(), | |
202 | + res := m.collection.FindOneAndUpdate(context.Background(), | |
176 | 203 | filter, |
177 | 204 | bson.M{"$set": update}) |
178 | 205 | if res.Err() != nil { |
... | ... | @@ -182,7 +209,7 @@ func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo |
182 | 209 | } |
183 | 210 | |
184 | 211 | func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult { |
185 | - res, err := m.mgo.UpdateOne(context.TODO(), filter, update) | |
212 | + res, err := m.collection.UpdateOne(context.TODO(), filter, update) | |
186 | 213 | if err != nil { |
187 | 214 | return nil |
188 | 215 | } | ... | ... |
conf/conf.go
... | ... | @@ -5,7 +5,6 @@ import ( |
5 | 5 | lumberjack "gopkg.in/natefinch/lumberjack.v2" |
6 | 6 | "gopkg.in/yaml.v3" |
7 | 7 | "io/ioutil" |
8 | - "pro2d/components/db" | |
9 | 8 | "pro2d/utils" |
10 | 9 | ) |
11 | 10 | |
... | ... | @@ -74,9 +73,4 @@ func init() { |
74 | 73 | //初始化雪花算法 |
75 | 74 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) |
76 | 75 | |
77 | - //初始化数据库 | |
78 | - err = db.Connect(GlobalConf.MongoConf.User, GlobalConf.MongoConf.Password, GlobalConf.MongoConf.Host, GlobalConf.MongoConf.Port, GlobalConf.MongoConf.MaxNum, GlobalConf.MongoConf.TimeOut) | |
79 | - if err != nil { | |
80 | - utils.Sugar.Errorf("mongodb init error: %v", err) | |
81 | - } | |
82 | 76 | } |
83 | 77 | \ No newline at end of file | ... | ... |
... | ... | @@ -4,9 +4,7 @@ go 1.17 |
4 | 4 | |
5 | 5 | require ( |
6 | 6 | github.com/dgrijalva/jwt-go v3.2.0+incompatible |
7 | - github.com/garyburd/redigo v1.6.3 | |
8 | 7 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b |
9 | - github.com/golang/protobuf v1.5.2 | |
10 | 8 | go.mongodb.org/mongo-driver v1.8.3 |
11 | 9 | go.uber.org/zap v1.17.0 |
12 | 10 | google.golang.org/grpc v1.38.0 |
... | ... | @@ -16,7 +14,10 @@ require ( |
16 | 14 | ) |
17 | 15 | |
18 | 16 | require ( |
17 | + github.com/favadi/protoc-go-inject-tag v1.3.0 // indirect | |
18 | + github.com/garyburd/redigo v1.6.3 // indirect | |
19 | 19 | github.com/go-stack/stack v1.8.0 // indirect |
20 | + github.com/golang/protobuf v1.5.2 // indirect | |
20 | 21 | github.com/golang/snappy v0.0.1 // indirect |
21 | 22 | github.com/klauspost/compress v1.13.6 // indirect |
22 | 23 | github.com/pkg/errors v0.9.1 // indirect | ... | ... |
... | ... | @@ -13,6 +13,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF |
13 | 13 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= |
14 | 14 | github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= |
15 | 15 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= |
16 | +github.com/favadi/protoc-go-inject-tag v1.3.0 h1:JPrmsmc/uBShG85uY5xGZIa5WJ0IaNZn6LZhQR9tIQE= | |
17 | +github.com/favadi/protoc-go-inject-tag v1.3.0/go.mod h1:SSkUBgfqw2IJ2p7NPNKWk0Idwxt/qIt2LQgFPUgRGtc= | |
16 | 18 | github.com/garyburd/redigo v1.6.3 h1:HCeeRluvAgMusMomi1+6Y5dmFOdYV/JzoRrrbFlkGIc= |
17 | 19 | github.com/garyburd/redigo v1.6.3/go.mod h1:rTb6epsqigu3kYKBnaF028A7Tf/Aw5s0cqA47doKKqw= |
18 | 20 | github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= | ... | ... |
models/account.go
... | ... | @@ -10,20 +10,20 @@ type AccountModel struct { |
10 | 10 | *pb.AccountInfo |
11 | 11 | } |
12 | 12 | |
13 | -func AccountExistByPhone(database, phone string) (bool, *AccountModel){ | |
14 | - m := NewAccount(database, phone) | |
13 | +func AccountExistByPhone(phone string) (bool, *AccountModel){ | |
14 | + m := NewAccount(phone) | |
15 | 15 | if err := m.Load(); err != nil { |
16 | 16 | return false, m |
17 | 17 | } |
18 | 18 | return true, m |
19 | 19 | } |
20 | 20 | |
21 | -func NewAccount(database, phone string) *AccountModel { | |
21 | +func NewAccount(phone string) *AccountModel { | |
22 | 22 | ac := &pb.AccountInfo{ |
23 | 23 | Phone: phone, |
24 | 24 | } |
25 | 25 | account := &AccountModel{ |
26 | - MgoColl: db.NewMongoColl(database, "account", db.GetBsonM("phone", phone), ac), | |
26 | + MgoColl: db.NewMongoColl( "account", db.GetBsonM("phone", phone), ac), | |
27 | 27 | AccountInfo: ac, |
28 | 28 | } |
29 | 29 | ... | ... |
models/hero.go
... | ... | @@ -2,7 +2,6 @@ package models |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "pro2d/components/db" |
5 | - "pro2d/conf" | |
6 | 5 | "pro2d/protos/pb" |
7 | 6 | ) |
8 | 7 | |
... | ... | @@ -20,11 +19,13 @@ func GetHeros(hm HeroMap) map[string]*pb.Hero { |
20 | 19 | return h |
21 | 20 | } |
22 | 21 | |
23 | -func NewHero(h *pb.Hero) *HeroModel { | |
22 | +func NewHero(id int64) *HeroModel { | |
23 | + h := &pb.Hero{ | |
24 | + Id: id, | |
25 | + } | |
24 | 26 | m := &HeroModel{ |
25 | - MgoColl: db.NewMongoColl(conf.GlobalConf.GameConf.DBName, "hero", db.GetBsonM("id", h.Id), h), | |
27 | + MgoColl: db.NewMongoColl("hero", db.GetBsonM("id", h.Id), h), | |
26 | 28 | Hero: h, |
27 | 29 | } |
28 | - m.Load() | |
29 | 30 | return m |
30 | 31 | } | ... | ... |
... | ... | @@ -0,0 +1,40 @@ |
1 | +package models | |
2 | + | |
3 | +import ( | |
4 | + "pro2d/components/db" | |
5 | + "pro2d/protos/pb" | |
6 | + "pro2d/utils" | |
7 | +) | |
8 | + | |
9 | +func InitDoc(schema ...interface{}) { | |
10 | + for _, s := range schema { | |
11 | + for coll, key := range utils.FindIndex(s) { | |
12 | + if err := db.CreateCollection(coll); err != nil { | |
13 | + utils.Sugar.Errorf("InitDoc err: %v", err) | |
14 | + continue | |
15 | + } | |
16 | + | |
17 | + res, err := db.SetUnique(coll, key) | |
18 | + if err != nil { | |
19 | + utils.Sugar.Errorf("InitDoc unique: %s, err: %v", res, err) | |
20 | + continue | |
21 | + } | |
22 | + } | |
23 | + } | |
24 | +} | |
25 | + | |
26 | +func InitAccountServerModels() { | |
27 | + var schema []interface{} = []interface{}{ | |
28 | + pb.AccountInfo{}, | |
29 | + } | |
30 | + InitDoc(schema...) | |
31 | +} | |
32 | + | |
33 | +func InitGameServerModels() { | |
34 | + var schema []interface{} = []interface{}{ | |
35 | + pb.Hero{}, | |
36 | + pb.Role{}, | |
37 | + pb.Team{}, | |
38 | + } | |
39 | + InitDoc(schema...) | |
40 | +} | |
0 | 41 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,27 @@ |
1 | +package models | |
2 | + | |
3 | +import ( | |
4 | + "context" | |
5 | + "pro2d/components/db" | |
6 | + "pro2d/conf" | |
7 | + _ "pro2d/conf" | |
8 | + "pro2d/utils" | |
9 | + "testing" | |
10 | +) | |
11 | + | |
12 | + | |
13 | +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 | + | |
19 | + InitAccountServerModels() | |
20 | + 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 | + } | |
25 | + InitGameServerModels() | |
26 | + db.MongoClient.Disconnect(context.TODO()) | |
27 | +} | |
0 | 28 | \ No newline at end of file | ... | ... |
models/role.go
... | ... | @@ -3,7 +3,6 @@ package models |
3 | 3 | import ( |
4 | 4 | "fmt" |
5 | 5 | "pro2d/components/db" |
6 | - "pro2d/conf" | |
7 | 6 | "pro2d/protos/pb" |
8 | 7 | ) |
9 | 8 | |
... | ... | @@ -17,20 +16,20 @@ type RoleModel struct { |
17 | 16 | } |
18 | 17 | |
19 | 18 | func RoleExistByUid(uid int64) (bool, *RoleModel){ |
20 | - m := NewRole(&pb.Role{Uid: uid}) | |
19 | + m := NewRole(uid) | |
21 | 20 | if err := m.Load(); err != nil { |
22 | 21 | return false, m |
23 | 22 | } |
24 | 23 | return true, m |
25 | 24 | } |
26 | 25 | |
27 | -func NewRole(r *pb.Role) *RoleModel { | |
26 | +func NewRole(uid int64) *RoleModel { | |
27 | + r := &pb.Role{Uid: uid} | |
28 | 28 | m := &RoleModel{ |
29 | - MgoColl: db.NewMongoColl(conf.GlobalConf.GameConf.DBName, "role", db.GetBsonM("uid", r.Uid), r), | |
29 | + MgoColl: db.NewMongoColl("role", db.GetBsonM("uid", r.Uid), r), | |
30 | 30 | Role: r, |
31 | 31 | Heros: make(HeroMap), |
32 | 32 | } |
33 | - m.Load() | |
34 | 33 | return m |
35 | 34 | } |
36 | 35 | |
... | ... | @@ -41,7 +40,8 @@ func (m *RoleModel) LoadHero() { |
41 | 40 | } |
42 | 41 | |
43 | 42 | func (m *RoleModel) AddHero(hero *pb.Hero) { |
44 | - h := NewHero(hero) | |
43 | + h := NewHero(hero.Id) | |
44 | + h.Hero = hero | |
45 | 45 | h.Create() |
46 | 46 | m.Heros[fmt.Sprintf("%d%d", m.Role.Id, h.Hero.Id)] = h |
47 | 47 | } |
48 | 48 | \ No newline at end of file | ... | ... |
models/role_test.go
... | ... | @@ -24,12 +24,12 @@ func TestNewRole(t *testing.T) { |
24 | 24 | }) |
25 | 25 | role.Save() |
26 | 26 | }else { |
27 | - role = NewRole(&pb.Role{Uid: uid}) | |
27 | + role = NewRole(uid) | |
28 | 28 | role.Role.Id = 1 |
29 | 29 | role.Role.Device = "222222" |
30 | 30 | role.Role.Level = 0 |
31 | 31 | role.Create() |
32 | - role.Index("uid") | |
32 | + role.SetUnique("uid") | |
33 | 33 | } |
34 | 34 | print(role) |
35 | 35 | } |
36 | 36 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,23 @@ |
1 | +package models | |
2 | + | |
3 | +import ( | |
4 | + "pro2d/components/db" | |
5 | + "pro2d/protos/pb" | |
6 | +) | |
7 | + | |
8 | +type TeamModel struct { | |
9 | + *db.MgoColl | |
10 | + Team *pb.Team | |
11 | +} | |
12 | + | |
13 | +func NewTeam(id int64) *TeamModel{ | |
14 | + data := &pb.Team{ | |
15 | + Id: id, | |
16 | + } | |
17 | + m := &TeamModel{ | |
18 | + MgoColl: db.NewMongoColl( "team", data, data), | |
19 | + Team: data, | |
20 | + } | |
21 | + | |
22 | + return m | |
23 | +} | |
0 | 24 | \ No newline at end of file | ... | ... |
utils/utils.go
1 | -package utils | |
2 | 1 | \ No newline at end of file |
2 | +package utils | |
3 | + | |
4 | +import ( | |
5 | + "reflect" | |
6 | + "strings" | |
7 | +) | |
8 | + | |
9 | + | |
10 | +func FindIndex(schema interface{}) map[string]string{ | |
11 | + s := reflect.TypeOf(schema) | |
12 | + tb := make(map[string]string) | |
13 | + for i := 0; i < s.NumField(); i++ { | |
14 | + if s.Field(i).Tag.Get("index") != "" { | |
15 | + js := strings.Split(s.Field(i).Tag.Get("json"), ",") | |
16 | + if len(js) == 0 { | |
17 | + continue | |
18 | + } | |
19 | + tb[strings.ToLower(s.Name())] = js[0] | |
20 | + } | |
21 | + } | |
22 | + return tb | |
23 | +} | |
3 | 24 | \ No newline at end of file | ... | ... |