Commit eb417b0bfaf68c9548e8da87e042470b2fdec1fe
1 parent
2064f484
reactor mongo
Showing
13 changed files
with
231 additions
and
251 deletions
Show diff stats
actions/accountaction.go
@@ -10,7 +10,7 @@ import ( | @@ -10,7 +10,7 @@ import ( | ||
10 | ) | 10 | ) |
11 | 11 | ||
12 | func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, error) { | 12 | func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.PubRsp, error) { |
13 | - ok, account := models.AccountExistByPhone(s.Database, in.Phone) | 13 | + ok, account := models.AccountExistByPhone(s.DBName, in.Phone) |
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) |
@@ -24,7 +24,6 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | @@ -24,7 +24,6 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | ||
24 | return &pb.PubRsp{ | 24 | return &pb.PubRsp{ |
25 | Code: 0, | 25 | Code: 0, |
26 | }, nil | 26 | }, nil |
27 | - | ||
28 | } | 27 | } |
29 | 28 | ||
30 | 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) { |
actions/basic.go
@@ -3,27 +3,20 @@ package actions | @@ -3,27 +3,20 @@ package actions | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "net" | 5 | "net" |
6 | - "pro2d/components/db" | ||
7 | "pro2d/conf" | 6 | "pro2d/conf" |
8 | - "pro2d/utils" | ||
9 | ) | 7 | ) |
10 | 8 | ||
11 | type BasicServer struct { | 9 | type BasicServer struct { |
12 | - Database *db.Database | 10 | + DBName string |
13 | } | 11 | } |
14 | 12 | ||
15 | -func NewServer() *BasicServer { | 13 | +func NewServer(db string) *BasicServer { |
16 | return &BasicServer{ | 14 | return &BasicServer{ |
17 | - Database: new(db.Database), | 15 | + DBName: db, |
18 | } | 16 | } |
19 | } | 17 | } |
20 | 18 | ||
21 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { | 19 | func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { |
22 | - if err := b.Database.Connect(sConf.MongoConf); err !=nil { | ||
23 | - utils.Sugar.Debugf("db error: %v", err) | ||
24 | - return nil, err | ||
25 | - } | ||
26 | - | ||
27 | listing := fmt.Sprintf(":%d", sConf.Port) | 20 | listing := fmt.Sprintf(":%d", sConf.Port) |
28 | lis, err := net.Listen("tcp", listing) | 21 | lis, err := net.Listen("tcp", listing) |
29 | if err != nil { | 22 | if err != nil { |
actions/roleaction.go
@@ -43,7 +43,7 @@ func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.Token) (*pb.R | @@ -43,7 +43,7 @@ func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.Token) (*pb.R | ||
43 | if account == nil { | 43 | if account == nil { |
44 | return nil, fmt.Errorf("1") | 44 | return nil, fmt.Errorf("1") |
45 | } | 45 | } |
46 | - ok, role := models.RoleExistByUid(s.Database, account.Uid) | 46 | + ok, role := models.RoleExistByUid(account.Uid) |
47 | if !ok { | 47 | if !ok { |
48 | role.Role = &pb.Role{ | 48 | role.Role = &pb.Role{ |
49 | Level: 0, | 49 | Level: 0, |
@@ -67,13 +67,14 @@ func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRs | @@ -67,13 +67,14 @@ func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRs | ||
67 | if account == nil { | 67 | if account == nil { |
68 | return nil, fmt.Errorf("token is error") | 68 | return nil, fmt.Errorf("token is error") |
69 | } | 69 | } |
70 | - ok, role := models.RoleExistByUid(s.Database, account.Uid) | 70 | + ok, role := models.RoleExistByUid(account.Uid) |
71 | if !ok { | 71 | if !ok { |
72 | return &pb.RoleRsp{ | 72 | return &pb.RoleRsp{ |
73 | Rsp: &pb.PubRsp{ | 73 | Rsp: &pb.PubRsp{ |
74 | Code: -1, | 74 | Code: -1, |
75 | Msg: "role not exist", | 75 | Msg: "role not exist", |
76 | }, | 76 | }, |
77 | + | ||
77 | }, nil | 78 | }, nil |
78 | } | 79 | } |
79 | return &pb.RoleRsp{ | 80 | return &pb.RoleRsp{ |
@@ -82,5 +83,6 @@ func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRs | @@ -82,5 +83,6 @@ func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRs | ||
82 | Msg: "successful", | 83 | Msg: "successful", |
83 | }, | 84 | }, |
84 | Role: role.Role, | 85 | Role: role.Role, |
86 | + Hero: models.GetHeros(role.Heros), | ||
85 | }, nil | 87 | }, nil |
86 | } | 88 | } |
actions/server.go
@@ -16,7 +16,7 @@ type AccountServer struct{ | @@ -16,7 +16,7 @@ type AccountServer struct{ | ||
16 | 16 | ||
17 | func NewAccountServer() *AccountServer { | 17 | func NewAccountServer() *AccountServer { |
18 | return &AccountServer{ | 18 | return &AccountServer{ |
19 | - BasicServer: NewServer(), | 19 | + BasicServer: NewServer(conf.GlobalConf.AccountConf.DBName), |
20 | } | 20 | } |
21 | } | 21 | } |
22 | 22 | ||
@@ -60,7 +60,7 @@ type GameServer struct{ | @@ -60,7 +60,7 @@ type GameServer struct{ | ||
60 | 60 | ||
61 | func NewGameServer() *GameServer { | 61 | func NewGameServer() *GameServer { |
62 | return &GameServer{ | 62 | return &GameServer{ |
63 | - BasicServer: NewServer(), | 63 | + BasicServer: NewServer(conf.GlobalConf.GameConf.DBName), |
64 | } | 64 | } |
65 | } | 65 | } |
66 | //拦截器 | 66 | //拦截器 |
components/db/mongo.go
@@ -8,49 +8,47 @@ import ( | @@ -8,49 +8,47 @@ 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/conf" | ||
12 | "strconv" | 11 | "strconv" |
13 | "time" | 12 | "time" |
14 | ) | 13 | ) |
15 | -type Database struct { | ||
16 | - Mongo *mongo.Database | ||
17 | -} | 14 | + |
15 | +var MongoDBClient *mongo.Client | ||
18 | 16 | ||
19 | //初始化 | 17 | //初始化 |
20 | -func (db *Database)Connect(conf *conf.MongoConf) error { | 18 | +func Connect(user, password, host string,port int, MaxNum int, timeOut int) error { |
21 | var uri string | 19 | var uri string |
22 | - if conf.User!= "" { | ||
23 | - uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) | 20 | + if user!= "" { |
21 | + //uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s?w=majority", conf.User, conf.Password, conf.Host, conf.Port, conf.DBName) | ||
22 | + uri = fmt.Sprintf("mongodb://%s:%s@%s:%d/?w=majority", user, password, host, port) | ||
24 | }else { | 23 | }else { |
25 | - uri = fmt.Sprintf("mongodb://%s:%d/%s?w=majority", conf.Host, conf.Port, conf.DBName) | 24 | + //uri = fmt.Sprintf("mongodb://%s:%d/%s?w=majority", conf.Host, conf.Port, conf.DBName) |
25 | + uri = fmt.Sprintf("mongodb://%s:%d/?w=majority", host, port) | ||
26 | } | 26 | } |
27 | // 设置连接超时时间 | 27 | // 设置连接超时时间 |
28 | - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.TimeOut)) | 28 | + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeOut)) |
29 | defer cancel() | 29 | defer cancel() |
30 | // 通过传进来的uri连接相关的配置 | 30 | // 通过传进来的uri连接相关的配置 |
31 | o := options.Client().ApplyURI(uri) | 31 | o := options.Client().ApplyURI(uri) |
32 | // 设置最大连接数 - 默认是100 ,不设置就是最大 max 64 | 32 | // 设置最大连接数 - 默认是100 ,不设置就是最大 max 64 |
33 | - o.SetMaxPoolSize(uint64(conf.MaxNum)) | 33 | + o.SetMaxPoolSize(uint64(MaxNum)) |
34 | // 发起链接 | 34 | // 发起链接 |
35 | - client, err := mongo.Connect(ctx, o) | 35 | + var err error |
36 | + MongoDBClient, err = mongo.Connect(ctx, o) | ||
36 | if err != nil { | 37 | if err != nil { |
37 | - fmt.Println("ConnectToDB", err) | ||
38 | return err | 38 | return err |
39 | } | 39 | } |
40 | // 判断服务是不是可用 | 40 | // 判断服务是不是可用 |
41 | - if err = client.Ping(context.Background(), readpref.Primary()); err != nil { | ||
42 | - fmt.Println("ConnectToDB", err) | 41 | + if err = MongoDBClient.Ping(context.Background(), readpref.Primary()); err != nil { |
43 | return err | 42 | return err |
44 | } | 43 | } |
45 | - | ||
46 | - db.Mongo = client.Database(conf.DBName) | ||
47 | return nil | 44 | return nil |
48 | } | 45 | } |
49 | 46 | ||
50 | -type MgoPool struct { | ||
51 | - mgo *mongo.Database | 47 | +type MgoColl struct { |
48 | + mgo *mongo.Collection | ||
52 | 49 | ||
53 | - collection string | 50 | + pri interface{} |
51 | + schema interface{} | ||
54 | } | 52 | } |
55 | 53 | ||
56 | func GetBsonD(key string, value interface{}) interface{} { | 54 | func GetBsonD(key string, value interface{}) interface{} { |
@@ -60,58 +58,36 @@ func GetBsonM(key string, value interface{}) interface{} { | @@ -60,58 +58,36 @@ func GetBsonM(key string, value interface{}) interface{} { | ||
60 | return bson.M{key: value} | 58 | return bson.M{key: value} |
61 | } | 59 | } |
62 | 60 | ||
63 | -func NewMongoPool(mgo *Database, collection string)*MgoPool { | ||
64 | - return &MgoPool{ | ||
65 | - mgo: mgo.Mongo, | ||
66 | - collection: collection, | 61 | +func NewMongoColl(database, collection string, pri, schema interface{}) *MgoColl { |
62 | + return &MgoColl{ | ||
63 | + mgo: MongoDBClient.Database(database).Collection(collection), | ||
64 | + | ||
65 | + pri: pri, | ||
66 | + schema: schema, | ||
67 | } | 67 | } |
68 | } | 68 | } |
69 | 69 | ||
70 | -func (m *MgoPool)SetDatabase(databases string) { | 70 | +func (m *MgoColl)SetDatabase(databases string) { |
71 | //m.db = databases | 71 | //m.db = databases |
72 | } | 72 | } |
73 | -func (m *MgoPool)SetCollect(coll string) { | ||
74 | - m.collection = coll | ||
75 | -} | ||
76 | 73 | ||
77 | - | ||
78 | -//func (m *MgoPool)Insert(coll string, buffer interface{}) error { | ||
79 | -// _, err := m.mgo.Collection(coll).InsertOne(context.TODO(), buffer) | ||
80 | -// if err != nil { | ||
81 | -// fmt.Println(err) | ||
82 | -// } | ||
83 | -// return err | ||
84 | -//} | ||
85 | - | ||
86 | -//func (m *MgoPool)FindOne(coll string, filter interface{}) (interface{}, error){ | ||
87 | -// findOptions := options.FindOne() | ||
88 | -// cur := m.MongoDBClient.Collection(coll).FindOne(context.TODO(), filter, findOptions) | ||
89 | -// var role interface{} | ||
90 | -// if err := cur.Decode(role); err != nil { | ||
91 | -// return nil, err | ||
92 | -// } | ||
93 | -// return role, nil | ||
94 | -//} | ||
95 | // 查询单个 | 74 | // 查询单个 |
96 | -func (m *MgoPool) FindOneKV(key string, value interface{}) *mongo.SingleResult { | ||
97 | - collection := m.mgo.Collection(m.collection) | 75 | +func (m *MgoColl) FindOneKV(key string, value interface{}) *mongo.SingleResult { |
98 | //collection. | 76 | //collection. |
99 | filter := bson.D{ {key, value}} | 77 | filter := bson.D{ {key, value}} |
100 | - singleResult := collection.FindOne(context.TODO(), filter) | 78 | + singleResult := m.mgo.FindOne(context.TODO(), filter) |
101 | return singleResult | 79 | return singleResult |
102 | } | 80 | } |
103 | 81 | ||
104 | -func (m *MgoPool) FindOne(pri interface{}) *mongo.SingleResult { | ||
105 | - collection := m.mgo.Collection(m.collection) | 82 | +func (m *MgoColl) FindOne(pri interface{}) *mongo.SingleResult { |
106 | //collection. | 83 | //collection. |
107 | - singleResult := collection.FindOne(context.TODO(), pri) | 84 | + singleResult := m.mgo.FindOne(context.TODO(), pri) |
108 | return singleResult | 85 | return singleResult |
109 | } | 86 | } |
110 | 87 | ||
111 | //插入单个 | 88 | //插入单个 |
112 | -func (m *MgoPool) InsertOne(value interface{}) *mongo.InsertOneResult { | ||
113 | - collection := m.mgo.Collection(m.collection) | ||
114 | - insertResult, err := collection.InsertOne(context.TODO(), value) | 89 | +func (m *MgoColl) InsertOne(value interface{}) *mongo.InsertOneResult { |
90 | + insertResult, err := m.mgo.InsertOne(context.TODO(), value) | ||
115 | if err != nil { | 91 | if err != nil { |
116 | fmt.Println(err) | 92 | fmt.Println(err) |
117 | } | 93 | } |
@@ -119,28 +95,25 @@ func (m *MgoPool) InsertOne(value interface{}) *mongo.InsertOneResult { | @@ -119,28 +95,25 @@ func (m *MgoPool) InsertOne(value interface{}) *mongo.InsertOneResult { | ||
119 | } | 95 | } |
120 | 96 | ||
121 | //查询集合里有多少数据 | 97 | //查询集合里有多少数据 |
122 | -func (m *MgoPool) CollectionCount() (string, int64) { | ||
123 | - collection := m.mgo.Collection(m.collection) | ||
124 | - name := collection.Name() | ||
125 | - size, _ := collection.EstimatedDocumentCount(context.TODO()) | ||
126 | - return name, size | 98 | +func (m *MgoColl) CollectionCount() (string, int64) { |
99 | + size, _ := m.mgo.EstimatedDocumentCount(context.TODO()) | ||
100 | + return m.mgo.Name(), size | ||
127 | } | 101 | } |
128 | 102 | ||
129 | //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取 | 103 | //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取 |
130 | -func (m *MgoPool) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor { | ||
131 | - collection := m.mgo.Collection(m.collection) | 104 | +func (m *MgoColl) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor { |
132 | SORT := bson.D{ | 105 | SORT := bson.D{ |
133 | {"_id", sort}} //filter := bson.D{ {key,value}} | 106 | {"_id", sort}} //filter := bson.D{ {key,value}} |
134 | filter := bson.D{ | 107 | filter := bson.D{ |
135 | {}} | 108 | {}} |
136 | findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) | 109 | findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) |
137 | //findOptions.SetLimit(i) | 110 | //findOptions.SetLimit(i) |
138 | - temp, _ := collection.Find(context.Background(), filter, findOptions) | 111 | + temp, _ := m.mgo.Find(context.Background(), filter, findOptions) |
139 | return temp | 112 | return temp |
140 | } | 113 | } |
141 | 114 | ||
142 | //获取集合创建时间和编号 | 115 | //获取集合创建时间和编号 |
143 | -func (m *MgoPool) ParsingId(result string) (time.Time, uint64) { | 116 | +func (m *MgoColl) ParsingId(result string) (time.Time, uint64) { |
144 | temp1 := result[:8] | 117 | temp1 := result[:8] |
145 | timestamp, _ := strconv.ParseInt(temp1, 16, 64) | 118 | timestamp, _ := strconv.ParseInt(temp1, 16, 64) |
146 | dateTime := time.Unix(timestamp, 0) //这是截获情报时间 时间格式 2019-04-24 09:23:39 +0800 CST | 119 | dateTime := time.Unix(timestamp, 0) //这是截获情报时间 时间格式 2019-04-24 09:23:39 +0800 CST |
@@ -150,12 +123,11 @@ func (m *MgoPool) ParsingId(result string) (time.Time, uint64) { | @@ -150,12 +123,11 @@ func (m *MgoPool) ParsingId(result string) (time.Time, uint64) { | ||
150 | } | 123 | } |
151 | 124 | ||
152 | //删除文章和查询文章 | 125 | //删除文章和查询文章 |
153 | -func (m *MgoPool) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) { | ||
154 | - collection := m.mgo.Collection(m.collection) | 126 | +func (m *MgoColl) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) { |
155 | filter := bson.D{ | 127 | filter := bson.D{ |
156 | {key, value}} | 128 | {key, value}} |
157 | - singleResult := collection.FindOne(context.TODO(), filter) | ||
158 | - DeleteResult, err := collection.DeleteOne(context.TODO(), filter, nil) | 129 | + singleResult := m.mgo.FindOne(context.TODO(), filter) |
130 | + DeleteResult, err := m.mgo.DeleteOne(context.TODO(), filter, nil) | ||
159 | if err != nil { | 131 | if err != nil { |
160 | fmt.Println("删除时出现错误,你删不掉的~") | 132 | fmt.Println("删除时出现错误,你删不掉的~") |
161 | } | 133 | } |
@@ -163,10 +135,9 @@ func (m *MgoPool) DeleteAndFind(key string, value interface{}) (int64, *mongo.Si | @@ -163,10 +135,9 @@ func (m *MgoPool) DeleteAndFind(key string, value interface{}) (int64, *mongo.Si | ||
163 | } | 135 | } |
164 | 136 | ||
165 | //删除文章 | 137 | //删除文章 |
166 | -func (m *MgoPool) Delete(key string, value interface{}) int64 { | ||
167 | - collection := m.mgo.Collection(m.collection) | 138 | +func (m *MgoColl) Delete(key string, value interface{}) int64 { |
168 | filter := bson.D{ {key, value}} | 139 | filter := bson.D{ {key, value}} |
169 | - count, err := collection.DeleteOne(context.TODO(), filter, nil) | 140 | + count, err := m.mgo.DeleteOne(context.TODO(), filter, nil) |
170 | if err != nil { | 141 | if err != nil { |
171 | fmt.Println(err) | 142 | fmt.Println(err) |
172 | } | 143 | } |
@@ -175,11 +146,10 @@ func (m *MgoPool) Delete(key string, value interface{}) int64 { | @@ -175,11 +146,10 @@ func (m *MgoPool) Delete(key string, value interface{}) int64 { | ||
175 | } | 146 | } |
176 | 147 | ||
177 | //删除多个 | 148 | //删除多个 |
178 | -func (m *MgoPool) DeleteMany(key string, value interface{}) int64 { | ||
179 | - collection := m.mgo.Collection(m.collection) | 149 | +func (m *MgoColl) DeleteMany(key string, value interface{}) int64 { |
180 | filter := bson.D{ {key, value}} | 150 | filter := bson.D{ {key, value}} |
181 | 151 | ||
182 | - count, err := collection.DeleteMany(context.TODO(), filter) | 152 | + count, err := m.mgo.DeleteMany(context.TODO(), filter) |
183 | if err != nil { | 153 | if err != nil { |
184 | fmt.Println(err) | 154 | fmt.Println(err) |
185 | } | 155 | } |
@@ -187,9 +157,8 @@ func (m *MgoPool) DeleteMany(key string, value interface{}) int64 { | @@ -187,9 +157,8 @@ func (m *MgoPool) DeleteMany(key string, value interface{}) int64 { | ||
187 | } | 157 | } |
188 | 158 | ||
189 | //索引 | 159 | //索引 |
190 | -func (m *MgoPool) Index(key string){ | ||
191 | - collection := m.mgo.Collection(m.collection) | ||
192 | - collection.Indexes().CreateOne( | 160 | +func (m *MgoColl) Index(key string){ |
161 | + m.mgo.Indexes().CreateOne( | ||
193 | context.Background(), | 162 | context.Background(), |
194 | mongo.IndexModel{ | 163 | mongo.IndexModel{ |
195 | Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, | 164 | Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, |
@@ -199,12 +168,11 @@ func (m *MgoPool) Index(key string){ | @@ -199,12 +168,11 @@ func (m *MgoPool) Index(key string){ | ||
199 | } | 168 | } |
200 | 169 | ||
201 | //更新&保存 | 170 | //更新&保存 |
202 | -func (m *MgoPool) FindOneAndUpdate(filter interface{}, update interface{})*mongo.SingleResult { | 171 | +func (m *MgoColl) FindOneAndUpdate(filter interface{}, update interface{})*mongo.SingleResult { |
203 | //filter := bson.M{"name": "x", "array.name": "b"} | 172 | //filter := bson.M{"name": "x", "array.name": "b"} |
204 | //update := bson.M{"array.$[item].detail": "test"} | 173 | //update := bson.M{"array.$[item].detail": "test"} |
205 | 174 | ||
206 | - collection := m.mgo.Collection(m.collection) | ||
207 | - res := collection.FindOneAndUpdate(context.Background(), | 175 | + res := m.mgo.FindOneAndUpdate(context.Background(), |
208 | filter, | 176 | filter, |
209 | bson.M{"$set": update}) | 177 | bson.M{"$set": update}) |
210 | if res.Err() != nil { | 178 | if res.Err() != nil { |
@@ -213,12 +181,33 @@ func (m *MgoPool) FindOneAndUpdate(filter interface{}, update interface{})*mongo | @@ -213,12 +181,33 @@ func (m *MgoPool) FindOneAndUpdate(filter interface{}, update interface{})*mongo | ||
213 | return res | 181 | return res |
214 | } | 182 | } |
215 | 183 | ||
216 | -func (m *MgoPool) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult { | ||
217 | - collection := m.mgo.Collection(m.collection) | ||
218 | - res, err := collection.UpdateOne(context.TODO(), filter, update) | 184 | +func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult { |
185 | + res, err := m.mgo.UpdateOne(context.TODO(), filter, update) | ||
219 | if err != nil { | 186 | if err != nil { |
220 | return nil | 187 | return nil |
221 | } | 188 | } |
222 | 189 | ||
223 | return res | 190 | return res |
191 | +} | ||
192 | + | ||
193 | + | ||
194 | +func (m *MgoColl) Load() error{ | ||
195 | + r := m.FindOne(m.pri) | ||
196 | + err := r.Decode(m.schema) | ||
197 | + if err != nil { | ||
198 | + return err | ||
199 | + } | ||
200 | + return nil | ||
201 | +} | ||
202 | + | ||
203 | +func (m *MgoColl) Create() { | ||
204 | + m.InsertOne(m.schema) | ||
205 | +} | ||
206 | + | ||
207 | +func (m *MgoColl) Update(update interface{}) { | ||
208 | + m.FindOneAndUpdate(m.pri, update) | ||
209 | +} | ||
210 | + | ||
211 | +func (m *MgoColl)Save() { | ||
212 | + m.FindOneAndUpdate(m.pri, m.schema) | ||
224 | } | 213 | } |
225 | \ No newline at end of file | 214 | \ No newline at end of file |
components/db/redis.go
1 | package db | 1 | package db |
2 | - | ||
3 | -import ( | ||
4 | - "fmt" | ||
5 | - "github.com/garyburd/redigo/redis" | ||
6 | - "pro2d/conf" | ||
7 | - "pro2d/utils" | ||
8 | - "time" | ||
9 | -) | ||
10 | - | ||
11 | -type RedisPool struct { | ||
12 | - RedisPool *redis.Pool | ||
13 | -} | ||
14 | - | ||
15 | -func (rp *RedisPool)Connect(conf *conf.ServerConf) error { | ||
16 | - rp.RedisPool = &redis.Pool{ | ||
17 | - //最大活跃连接数,0代表无限 | ||
18 | - MaxActive: 888, | ||
19 | - MaxIdle: 20, | ||
20 | - //闲置连接的超时时间 | ||
21 | - IdleTimeout: time.Second * 100, | ||
22 | - //定义拨号获得连接的函数 | ||
23 | - Dial: func() (redis.Conn, error) { | ||
24 | - option := []redis.DialOption{redis.DialDatabase(conf.RedisConf.DB)} | ||
25 | - if conf.RedisConf.Auth != "" { | ||
26 | - option = append(option, redis.DialPassword(conf.RedisConf.Auth)) | ||
27 | - } | ||
28 | - return redis.Dial("tcp",conf.RedisConf.Address, option...) | ||
29 | - }, | ||
30 | - } | ||
31 | - return nil | ||
32 | -} | ||
33 | - | ||
34 | -func (rp *RedisPool)Close() { | ||
35 | - rp.RedisPool.Close() | ||
36 | -} | ||
37 | - | ||
38 | -func (rp *RedisPool) Insert() error { | ||
39 | - conn := rp.RedisPool.Get() | ||
40 | - defer conn.Close() | ||
41 | - reply, err := conn.Do("HKEYS", fmt.Sprintf("account:%s", "123123")) | ||
42 | - if err != nil { | ||
43 | - return err | ||
44 | - } | ||
45 | - | ||
46 | - utils.Sugar.Debugf("%v", reply) | ||
47 | - reply, err = conn.Do("HMSET", fmt.Sprintf("account:%s", "1231231"), "phone", "1231231", "passwd", "2131231") | ||
48 | - if err != nil { | ||
49 | - utils.Sugar.Errorf("%v", err) | ||
50 | - return err | ||
51 | - } | ||
52 | - utils.Sugar.Debugf("%v", reply) | ||
53 | - return nil | ||
54 | -} | 2 | +// |
3 | +//import ( | ||
4 | +// "fmt" | ||
5 | +// "github.com/garyburd/redigo/redis" | ||
6 | +// "pro2d/conf" | ||
7 | +// "pro2d/utils" | ||
8 | +// "time" | ||
9 | +//) | ||
10 | +// | ||
11 | +//type RedisPool struct { | ||
12 | +// RedisPool *redis.Pool | ||
13 | +//} | ||
14 | +// | ||
15 | +//func (rp *RedisPool)Connect(conf *conf.ServerConf) error { | ||
16 | +// rp.RedisPool = &redis.Pool{ | ||
17 | +// //最大活跃连接数,0代表无限 | ||
18 | +// MaxActive: 888, | ||
19 | +// MaxIdle: 20, | ||
20 | +// //闲置连接的超时时间 | ||
21 | +// IdleTimeout: time.Second * 100, | ||
22 | +// //定义拨号获得连接的函数 | ||
23 | +// Dial: func() (redis.Conn, error) { | ||
24 | +// option := []redis.DialOption{redis.DialDatabase(conf.RedisConf.DB)} | ||
25 | +// if conf.RedisConf.Auth != "" { | ||
26 | +// option = append(option, redis.DialPassword(conf.RedisConf.Auth)) | ||
27 | +// } | ||
28 | +// return redis.Dial("tcp",conf.RedisConf.Address, option...) | ||
29 | +// }, | ||
30 | +// } | ||
31 | +// return nil | ||
32 | +//} | ||
33 | +// | ||
34 | +//func (rp *RedisPool)Close() { | ||
35 | +// rp.RedisPool.Close() | ||
36 | +//} | ||
37 | +// | ||
38 | +//func (rp *RedisPool) Insert() error { | ||
39 | +// conn := rp.RedisPool.Get() | ||
40 | +// defer conn.Close() | ||
41 | +// reply, err := conn.Do("HKEYS", fmt.Sprintf("account:%s", "123123")) | ||
42 | +// if err != nil { | ||
43 | +// return err | ||
44 | +// } | ||
45 | +// | ||
46 | +// utils.Sugar.Debugf("%v", reply) | ||
47 | +// reply, err = conn.Do("HMSET", fmt.Sprintf("account:%s", "1231231"), "phone", "1231231", "passwd", "2131231") | ||
48 | +// if err != nil { | ||
49 | +// utils.Sugar.Errorf("%v", err) | ||
50 | +// return err | ||
51 | +// } | ||
52 | +// utils.Sugar.Debugf("%v", reply) | ||
53 | +// return nil | ||
54 | +//} |
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 | ||
@@ -27,7 +28,6 @@ type MongoConf struct { | @@ -27,7 +28,6 @@ type MongoConf struct { | ||
27 | Password string `yaml:"password"` | 28 | Password string `yaml:"password"` |
28 | Host string `yaml:"host"` | 29 | Host string `yaml:"host"` |
29 | Port int `yaml:"port"` | 30 | Port int `yaml:"port"` |
30 | - DBName string `yaml:"dbname"` | ||
31 | TimeOut int `yaml:"timeout"` | 31 | TimeOut int `yaml:"timeout"` |
32 | MaxNum int `yaml:"maxnum"` | 32 | MaxNum int `yaml:"maxnum"` |
33 | } | 33 | } |
@@ -36,7 +36,7 @@ type SConf struct { | @@ -36,7 +36,7 @@ type SConf struct { | ||
36 | Name string `yaml:"name"` | 36 | Name string `yaml:"name"` |
37 | IP string `yaml:"ip"` | 37 | IP string `yaml:"ip"` |
38 | Port int `yaml:"port"` | 38 | Port int `yaml:"port"` |
39 | - MongoConf *MongoConf `yaml:"mongo"` | 39 | + DBName string `yaml:"dbname"` |
40 | } | 40 | } |
41 | 41 | ||
42 | type ServerConf struct { | 42 | type ServerConf struct { |
@@ -44,6 +44,7 @@ type ServerConf struct { | @@ -44,6 +44,7 @@ type ServerConf struct { | ||
44 | Name string `yaml:"name"` | 44 | Name string `yaml:"name"` |
45 | WorkerID int64 `yaml:"workerid"` | 45 | WorkerID int64 `yaml:"workerid"` |
46 | DatacenterID int64 `yaml:"datacenterid"` | 46 | DatacenterID int64 `yaml:"datacenterid"` |
47 | + MongoConf *MongoConf `yaml:"mongo"` | ||
47 | AccountConf *SConf `yaml:"server_account"` | 48 | AccountConf *SConf `yaml:"server_account"` |
48 | GameConf *SConf `yaml:"server_game"` | 49 | GameConf *SConf `yaml:"server_game"` |
49 | RedisConf *RedisConf `yaml:"redis"` | 50 | RedisConf *RedisConf `yaml:"redis"` |
@@ -51,8 +52,10 @@ type ServerConf struct { | @@ -51,8 +52,10 @@ type ServerConf struct { | ||
51 | Etcd *Etcd `yaml:"etcd"` | 52 | Etcd *Etcd `yaml:"etcd"` |
52 | } | 53 | } |
53 | 54 | ||
54 | -var GlobalConf ServerConf | ||
55 | -var SnowFlack *utils.Snowflake | 55 | +var( |
56 | + GlobalConf ServerConf | ||
57 | + SnowFlack *utils.Snowflake | ||
58 | +) | ||
56 | func init() { | 59 | func init() { |
57 | configFile, err := ioutil.ReadFile("conf/conf.yaml") | 60 | configFile, err := ioutil.ReadFile("conf/conf.yaml") |
58 | if err != nil { | 61 | if err != nil { |
@@ -70,4 +73,10 @@ func init() { | @@ -70,4 +73,10 @@ func init() { | ||
70 | 73 | ||
71 | //初始化雪花算法 | 74 | //初始化雪花算法 |
72 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) | 75 | SnowFlack = utils.NewSnowflake(GlobalConf.WorkerID, GlobalConf.DatacenterID) |
76 | + | ||
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 | + } | ||
73 | } | 82 | } |
74 | \ No newline at end of file | 83 | \ No newline at end of file |
conf/conf.yaml
@@ -2,30 +2,26 @@ develop: true | @@ -2,30 +2,26 @@ develop: true | ||
2 | name: "Pro2DServer" | 2 | name: "Pro2DServer" |
3 | workerid: 1 | 3 | workerid: 1 |
4 | datacenterid: 1 | 4 | datacenterid: 1 |
5 | + | ||
6 | +mongo: &default-mongo | ||
7 | + host: "192.168.0.206" | ||
8 | + port: 27017 | ||
9 | + user: "" | ||
10 | + password: "" | ||
11 | + timeout: 2 | ||
12 | + maxnum: 50 | ||
13 | + | ||
5 | server_account: | 14 | server_account: |
6 | name: "account" | 15 | name: "account" |
7 | ip: "192.168.0.206" | 16 | ip: "192.168.0.206" |
8 | port: 8848 | 17 | port: 8848 |
9 | - mongo: | ||
10 | - host: "192.168.0.206" | ||
11 | - port: 27017 | ||
12 | - user: "" | ||
13 | - password: "" | ||
14 | - dbname: "account" | ||
15 | - timeout: 2 | ||
16 | - maxnum: 50 | 18 | + dbname: "account" |
19 | + | ||
17 | server_game: | 20 | server_game: |
18 | name: "game" | 21 | name: "game" |
19 | ip: "192.168.0.206" | 22 | ip: "192.168.0.206" |
20 | port: 8849 | 23 | port: 8849 |
21 | - mongo: | ||
22 | - host: "192.168.0.206" | ||
23 | - port: 27017 | ||
24 | - user: "" | ||
25 | - password: "" | ||
26 | - dbname: "game" | ||
27 | - timeout: 2 | ||
28 | - maxnum: 50 | 24 | + dbname: "game" |
29 | 25 | ||
30 | logconf: | 26 | logconf: |
31 | filename: "./pro2d.log" # ⽇志⽂件路径 | 27 | filename: "./pro2d.log" # ⽇志⽂件路径 |
models/account.go
@@ -6,27 +6,26 @@ import ( | @@ -6,27 +6,26 @@ import ( | ||
6 | ) | 6 | ) |
7 | 7 | ||
8 | type AccountModel struct { | 8 | type AccountModel struct { |
9 | - *ModelBaseMgo | 9 | + *db.MgoColl |
10 | *pb.AccountInfo | 10 | *pb.AccountInfo |
11 | } | 11 | } |
12 | 12 | ||
13 | -func AccountExistByPhone(mgo *db.Database, phone string) (bool, *AccountModel){ | ||
14 | - m := NewAccount(mgo, phone) | 13 | +func AccountExistByPhone(database, phone string) (bool, *AccountModel){ |
14 | + m := NewAccount(database, phone) | ||
15 | if err := m.Load(); err != nil { | 15 | if err := m.Load(); err != nil { |
16 | return false, m | 16 | return false, m |
17 | } | 17 | } |
18 | return true, m | 18 | return true, m |
19 | } | 19 | } |
20 | 20 | ||
21 | -func NewAccount(mgo *db.Database, phone string) *AccountModel { | 21 | +func NewAccount(database, phone string) *AccountModel { |
22 | ac := &pb.AccountInfo{ | 22 | ac := &pb.AccountInfo{ |
23 | Phone: phone, | 23 | Phone: phone, |
24 | } | 24 | } |
25 | account := &AccountModel{ | 25 | account := &AccountModel{ |
26 | - ModelBaseMgo: NewModelBaseMgo(mgo, "account", db.GetBsonM("phone", phone), ac), | 26 | + MgoColl: db.NewMongoColl(database, "account", db.GetBsonM("phone", phone), ac), |
27 | AccountInfo: ac, | 27 | AccountInfo: ac, |
28 | } | 28 | } |
29 | 29 | ||
30 | - | ||
31 | return account | 30 | return account |
32 | } | 31 | } |
33 | \ No newline at end of file | 32 | \ No newline at end of file |
models/basic.go deleted
@@ -1,46 +0,0 @@ | @@ -1,46 +0,0 @@ | ||
1 | -package models | ||
2 | - | ||
3 | -import ( | ||
4 | - "pro2d/components/db" | ||
5 | -) | ||
6 | - | ||
7 | -type ModelBaseMgo struct { | ||
8 | - MonGo *db.MgoPool | ||
9 | - | ||
10 | - pri interface{} | ||
11 | - schema interface{} | ||
12 | -} | ||
13 | - | ||
14 | - | ||
15 | -func NewModelBaseMgo(mgo *db.Database, collection string, pri interface{}, schema interface{}) *ModelBaseMgo{ | ||
16 | - return &ModelBaseMgo{ | ||
17 | - MonGo: db.NewMongoPool(mgo, collection), | ||
18 | - pri: pri, | ||
19 | - schema: schema, | ||
20 | - } | ||
21 | -} | ||
22 | - | ||
23 | -func (base *ModelBaseMgo) Load() error{ | ||
24 | - r := base.MonGo.FindOne(base.pri) | ||
25 | - err := r.Decode(base.schema) | ||
26 | - if err != nil { | ||
27 | - return err | ||
28 | - } | ||
29 | - return nil | ||
30 | -} | ||
31 | - | ||
32 | -func (base *ModelBaseMgo) Create() { | ||
33 | - base.MonGo.InsertOne(base.schema) | ||
34 | -} | ||
35 | - | ||
36 | -func (base *ModelBaseMgo) Index(key string) { | ||
37 | - base.MonGo.Index(key) | ||
38 | -} | ||
39 | - | ||
40 | -func (base *ModelBaseMgo) Update(update interface{}) { | ||
41 | - base.MonGo.FindOneAndUpdate(base.pri, update) | ||
42 | -} | ||
43 | - | ||
44 | -func (base *ModelBaseMgo) Save() { | ||
45 | - base.MonGo.FindOneAndUpdate(base.pri, base.schema) | ||
46 | -} | ||
47 | \ No newline at end of file | 0 | \ No newline at end of file |
@@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "pro2d/components/db" | ||
5 | + "pro2d/conf" | ||
6 | + "pro2d/protos/pb" | ||
7 | +) | ||
8 | + | ||
9 | +type HeroModel struct { | ||
10 | + *db.MgoColl | ||
11 | + Hero *pb.Hero | ||
12 | +} | ||
13 | +type HeroMap map[string]*HeroModel | ||
14 | + | ||
15 | +func GetHeros(hm HeroMap) map[string]*pb.Hero { | ||
16 | + h := make(map[string]*pb.Hero) | ||
17 | + for k, v := range hm { | ||
18 | + h[k] = v.Hero | ||
19 | + } | ||
20 | + return h | ||
21 | +} | ||
22 | + | ||
23 | +func NewHero(h *pb.Hero) *HeroModel { | ||
24 | + m := &HeroModel{ | ||
25 | + MgoColl: db.NewMongoColl(conf.GlobalConf.GameConf.DBName, "hero", db.GetBsonM("id", h.Id), h), | ||
26 | + Hero: h, | ||
27 | + } | ||
28 | + m.Load() | ||
29 | + return m | ||
30 | +} |
models/role.go
1 | package models | 1 | package models |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "fmt" | ||
4 | "pro2d/components/db" | 5 | "pro2d/components/db" |
6 | + "pro2d/conf" | ||
5 | "pro2d/protos/pb" | 7 | "pro2d/protos/pb" |
6 | ) | 8 | ) |
7 | 9 | ||
8 | type RoleModel struct { | 10 | type RoleModel struct { |
9 | - *ModelBaseMgo | ||
10 | - *pb.Role | 11 | + *db.MgoColl |
12 | + Role *pb.Role | ||
13 | + Heros HeroMap | ||
14 | + Teams *pb.Team | ||
15 | + Equip *pb.Equipment | ||
16 | + Prop *pb.Prop | ||
11 | } | 17 | } |
12 | 18 | ||
13 | -//创建数据 | ||
14 | -//数据加载 | ||
15 | -//数据保存 | ||
16 | - | ||
17 | -func RoleExistByUid(mgo *db.Database, uid int64) (bool, *RoleModel){ | ||
18 | - m := NewRole(mgo, uid) | 19 | +func RoleExistByUid(uid int64) (bool, *RoleModel){ |
20 | + m := NewRole(&pb.Role{Uid: uid}) | ||
19 | if err := m.Load(); err != nil { | 21 | if err := m.Load(); err != nil { |
20 | return false, m | 22 | return false, m |
21 | } | 23 | } |
22 | return true, m | 24 | return true, m |
23 | } | 25 | } |
24 | 26 | ||
25 | -func NewRole(mgo *db.Database, uid int64) *RoleModel { | ||
26 | - r := &pb.Role{ | ||
27 | - Uid: uid, | ||
28 | - } | 27 | +func NewRole(r *pb.Role) *RoleModel { |
29 | m := &RoleModel{ | 28 | m := &RoleModel{ |
30 | - ModelBaseMgo: NewModelBaseMgo(mgo, "role", db.GetBsonM("uid", uid), r), | 29 | + MgoColl: db.NewMongoColl(conf.GlobalConf.GameConf.DBName, "role", db.GetBsonM("uid", r.Uid), r), |
31 | Role: r, | 30 | Role: r, |
31 | + Heros: make(HeroMap), | ||
32 | } | 32 | } |
33 | m.Load() | 33 | m.Load() |
34 | return m | 34 | return m |
@@ -36,3 +36,12 @@ func NewRole(mgo *db.Database, uid int64) *RoleModel { | @@ -36,3 +36,12 @@ func NewRole(mgo *db.Database, uid int64) *RoleModel { | ||
36 | 36 | ||
37 | func (m *RoleModel) LoadAll() { | 37 | func (m *RoleModel) LoadAll() { |
38 | } | 38 | } |
39 | + | ||
40 | +func (m *RoleModel) LoadHero() { | ||
41 | +} | ||
42 | + | ||
43 | +func (m *RoleModel) AddHero(hero *pb.Hero) { | ||
44 | + h := NewHero(hero) | ||
45 | + h.Create() | ||
46 | + m.Heros[fmt.Sprintf("%d%d", m.Role.Id, h.Hero.Id)] = h | ||
47 | +} | ||
39 | \ No newline at end of file | 48 | \ No newline at end of file |
models/role_test.go
1 | package models | 1 | package models |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "pro2d/components/db" | ||
5 | - "pro2d/conf" | ||
6 | - "pro2d/utils" | 4 | + "pro2d/protos/pb" |
7 | "testing" | 5 | "testing" |
8 | - "time" | ||
9 | ) | 6 | ) |
10 | 7 | ||
11 | func TestNewRole(t *testing.T) { | 8 | func TestNewRole(t *testing.T) { |
12 | - db := &db.Database{} | ||
13 | - if err := db.Connect(conf.GlobalConf.GameConf.MongoConf); err != nil { | ||
14 | - utils.Sugar.Errorf("%v", err) | ||
15 | - return | ||
16 | - } | ||
17 | - | 9 | + //db.MongoDBClient.Database(conf.GlobalConf.AccountConf.DBName).Drop(context.Background()) |
10 | + //db.MongoDBClient.Database(conf.GlobalConf.GameConf.DBName).Drop(context.Background()) | ||
18 | 11 | ||
19 | - //db.Mongo.Drop(context.Background()) | ||
20 | var uid int64 = 1 | 12 | var uid int64 = 1 |
21 | var role *RoleModel | 13 | var role *RoleModel |
22 | - if ok, role := RoleExistByUid(db, uid); ok { | 14 | + if ok, role := RoleExistByUid(uid); ok { |
23 | role.Role.Device = "111111" | 15 | role.Role.Device = "111111" |
24 | - role.Role.LoginTime = time.Now().Unix() | 16 | + role.AddHero(&pb.Hero{ |
17 | + Id: 1, | ||
18 | + RoleId: role.Role.Id, | ||
19 | + Type: 0, | ||
20 | + Level: 0, | ||
21 | + ReinCount: 0, | ||
22 | + ReinPoint: 0, | ||
23 | + Equipments: "", | ||
24 | + }) | ||
25 | role.Save() | 25 | role.Save() |
26 | }else { | 26 | }else { |
27 | - role = NewRole(db, uid) | ||
28 | - role.Role.Id = "1" | 27 | + role = NewRole(&pb.Role{Uid: uid}) |
28 | + role.Role.Id = 1 | ||
29 | role.Role.Device = "222222" | 29 | role.Role.Device = "222222" |
30 | role.Role.Level = 0 | 30 | role.Role.Level = 0 |
31 | role.Create() | 31 | role.Create() |