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 | 10 | ) | 
| 11 | 11 | |
| 12 | 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 | 14 | if !ok { | 
| 15 | 15 | account.Phone = in.Phone | 
| 16 | 16 | account.Password = utils.Md5V(in.Password) | 
| ... | ... | @@ -24,7 +24,6 @@ func (s *AccountServer) RegisterHandler(ctx context.Context, in *pb.Register) (* | 
| 24 | 24 | return &pb.PubRsp{ | 
| 25 | 25 | Code: 0, | 
| 26 | 26 | }, nil | 
| 27 | - | |
| 28 | 27 | } | 
| 29 | 28 | |
| 30 | 29 | func (s *AccountServer) CreateTokenHandler(ctx context.Context, in *pb.AccountInfo) (*pb.CreateTokenRsp, error) { | ... | ... | 
actions/basic.go
| ... | ... | @@ -3,27 +3,20 @@ package actions | 
| 3 | 3 | import ( | 
| 4 | 4 | "fmt" | 
| 5 | 5 | "net" | 
| 6 | - "pro2d/components/db" | |
| 7 | 6 | "pro2d/conf" | 
| 8 | - "pro2d/utils" | |
| 9 | 7 | ) | 
| 10 | 8 | |
| 11 | 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 | 14 | return &BasicServer{ | 
| 17 | - Database: new(db.Database), | |
| 15 | + DBName: db, | |
| 18 | 16 | } | 
| 19 | 17 | } | 
| 20 | 18 | |
| 21 | 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 | 20 | listing := fmt.Sprintf(":%d", sConf.Port) | 
| 28 | 21 | lis, err := net.Listen("tcp", listing) | 
| 29 | 22 | if err != nil { | ... | ... | 
actions/roleaction.go
| ... | ... | @@ -43,7 +43,7 @@ func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.Token) (*pb.R | 
| 43 | 43 | if account == nil { | 
| 44 | 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 | 47 | if !ok { | 
| 48 | 48 | role.Role = &pb.Role{ | 
| 49 | 49 | Level: 0, | 
| ... | ... | @@ -67,13 +67,14 @@ func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRs | 
| 67 | 67 | if account == nil { | 
| 68 | 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 | 71 | if !ok { | 
| 72 | 72 | return &pb.RoleRsp{ | 
| 73 | 73 | Rsp: &pb.PubRsp{ | 
| 74 | 74 | Code: -1, | 
| 75 | 75 | Msg: "role not exist", | 
| 76 | 76 | }, | 
| 77 | + | |
| 77 | 78 | }, nil | 
| 78 | 79 | } | 
| 79 | 80 | return &pb.RoleRsp{ | 
| ... | ... | @@ -82,5 +83,6 @@ func (s *GameServer) LoginHandler(ctx context.Context, in *pb.Token) (*pb.RoleRs | 
| 82 | 83 | Msg: "successful", | 
| 83 | 84 | }, | 
| 84 | 85 | Role: role.Role, | 
| 86 | + Hero: models.GetHeros(role.Heros), | |
| 85 | 87 | }, nil | 
| 86 | 88 | } | ... | ... | 
actions/server.go
| ... | ... | @@ -16,7 +16,7 @@ type AccountServer struct{ | 
| 16 | 16 | |
| 17 | 17 | func NewAccountServer() *AccountServer { | 
| 18 | 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 | 60 | |
| 61 | 61 | func NewGameServer() *GameServer { | 
| 62 | 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 | 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 | - "pro2d/conf" | |
| 12 | 11 | "strconv" | 
| 13 | 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 | 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 | 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 | 29 | defer cancel() | 
| 30 | 30 | // ้่ฟไผ ่ฟๆฅ็uri่ฟๆฅ็ธๅ ณ็้ ็ฝฎ | 
| 31 | 31 | o := options.Client().ApplyURI(uri) | 
| 32 | 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 | 37 | if err != nil { | 
| 37 | - fmt.Println("ConnectToDB", err) | |
| 38 | 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 | 42 | return err | 
| 44 | 43 | } | 
| 45 | - | |
| 46 | - db.Mongo = client.Database(conf.DBName) | |
| 47 | 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 | 54 | func GetBsonD(key string, value interface{}) interface{} { | 
| ... | ... | @@ -60,58 +58,36 @@ func GetBsonM(key string, value interface{}) interface{} { | 
| 60 | 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 | 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 | 76 | //collection. | 
| 99 | 77 | filter := bson.D{ {key, value}} | 
| 100 | - singleResult := collection.FindOne(context.TODO(), filter) | |
| 78 | + singleResult := m.mgo.FindOne(context.TODO(), filter) | |
| 101 | 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 | 83 | //collection. | 
| 107 | - singleResult := collection.FindOne(context.TODO(), pri) | |
| 84 | + singleResult := m.mgo.FindOne(context.TODO(), pri) | |
| 108 | 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 | 91 | if err != nil { | 
| 116 | 92 | fmt.Println(err) | 
| 117 | 93 | } | 
| ... | ... | @@ -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 | 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 | 105 | SORT := bson.D{ | 
| 133 | 106 | {"_id", sort}} //filter := bson.D{ {key,value}} | 
| 134 | 107 | filter := bson.D{ | 
| 135 | 108 | {}} | 
| 136 | 109 | findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip) | 
| 137 | 110 | //findOptions.SetLimit(i) | 
| 138 | - temp, _ := collection.Find(context.Background(), filter, findOptions) | |
| 111 | + temp, _ := m.mgo.Find(context.Background(), filter, findOptions) | |
| 139 | 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 | 117 | temp1 := result[:8] | 
| 145 | 118 | timestamp, _ := strconv.ParseInt(temp1, 16, 64) | 
| 146 | 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 | 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 | 127 | filter := bson.D{ | 
| 156 | 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 | 131 | if err != nil { | 
| 160 | 132 | fmt.Println("ๅ ้คๆถๅบ็ฐ้่ฏฏ๏ผไฝ ๅ ไธๆ็~") | 
| 161 | 133 | } | 
| ... | ... | @@ -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 | 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 | 141 | if err != nil { | 
| 171 | 142 | fmt.Println(err) | 
| 172 | 143 | } | 
| ... | ... | @@ -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 | 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 | 153 | if err != nil { | 
| 184 | 154 | fmt.Println(err) | 
| 185 | 155 | } | 
| ... | ... | @@ -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 | 162 | context.Background(), | 
| 194 | 163 | mongo.IndexModel{ | 
| 195 | 164 | Keys : bsonx.Doc{{key, bsonx.Int32(1)}}, | 
| ... | ... | @@ -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 | 172 | //filter := bson.M{"name": "x", "array.name": "b"} | 
| 204 | 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 | 176 | filter, | 
| 209 | 177 | bson.M{"$set": update}) | 
| 210 | 178 | if res.Err() != nil { | 
| ... | ... | @@ -213,12 +181,33 @@ func (m *MgoPool) FindOneAndUpdate(filter interface{}, update interface{})*mongo | 
| 213 | 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 | 186 | if err != nil { | 
| 220 | 187 | return nil | 
| 221 | 188 | } | 
| 222 | 189 | |
| 223 | 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 | 214 | \ No newline at end of file | ... | ... | 
components/db/redis.go
| 1 | 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 | 5 | lumberjack "gopkg.in/natefinch/lumberjack.v2" | 
| 6 | 6 | "gopkg.in/yaml.v3" | 
| 7 | 7 | "io/ioutil" | 
| 8 | + "pro2d/components/db" | |
| 8 | 9 | "pro2d/utils" | 
| 9 | 10 | ) | 
| 10 | 11 | |
| ... | ... | @@ -27,7 +28,6 @@ type MongoConf struct { | 
| 27 | 28 | Password string `yaml:"password"` | 
| 28 | 29 | Host string `yaml:"host"` | 
| 29 | 30 | Port int `yaml:"port"` | 
| 30 | - DBName string `yaml:"dbname"` | |
| 31 | 31 | TimeOut int `yaml:"timeout"` | 
| 32 | 32 | MaxNum int `yaml:"maxnum"` | 
| 33 | 33 | } | 
| ... | ... | @@ -36,7 +36,7 @@ type SConf struct { | 
| 36 | 36 | Name string `yaml:"name"` | 
| 37 | 37 | IP string `yaml:"ip"` | 
| 38 | 38 | Port int `yaml:"port"` | 
| 39 | - MongoConf *MongoConf `yaml:"mongo"` | |
| 39 | + DBName string `yaml:"dbname"` | |
| 40 | 40 | } | 
| 41 | 41 | |
| 42 | 42 | type ServerConf struct { | 
| ... | ... | @@ -44,6 +44,7 @@ type ServerConf struct { | 
| 44 | 44 | Name string `yaml:"name"` | 
| 45 | 45 | WorkerID int64 `yaml:"workerid"` | 
| 46 | 46 | DatacenterID int64 `yaml:"datacenterid"` | 
| 47 | + MongoConf *MongoConf `yaml:"mongo"` | |
| 47 | 48 | AccountConf *SConf `yaml:"server_account"` | 
| 48 | 49 | GameConf *SConf `yaml:"server_game"` | 
| 49 | 50 | RedisConf *RedisConf `yaml:"redis"` | 
| ... | ... | @@ -51,8 +52,10 @@ type ServerConf struct { | 
| 51 | 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 | 59 | func init() { | 
| 57 | 60 | configFile, err := ioutil.ReadFile("conf/conf.yaml") | 
| 58 | 61 | if err != nil { | 
| ... | ... | @@ -70,4 +73,10 @@ func init() { | 
| 70 | 73 | |
| 71 | 74 | //ๅๅงๅ้ช่ฑ็ฎๆณ | 
| 72 | 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 | 83 | \ No newline at end of file | ... | ... | 
conf/conf.yaml
| ... | ... | @@ -2,30 +2,26 @@ develop: true | 
| 2 | 2 | name: "Pro2DServer" | 
| 3 | 3 | workerid: 1 | 
| 4 | 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 | 14 | server_account: | 
| 6 | 15 | name: "account" | 
| 7 | 16 | ip: "192.168.0.206" | 
| 8 | 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 | 20 | server_game: | 
| 18 | 21 | name: "game" | 
| 19 | 22 | ip: "192.168.0.206" | 
| 20 | 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 | 26 | logconf: | 
| 31 | 27 | filename: "./pro2d.log" # โฝๅฟโฝไปถ่ทฏๅพ | ... | ... | 
models/account.go
| ... | ... | @@ -6,27 +6,26 @@ import ( | 
| 6 | 6 | ) | 
| 7 | 7 | |
| 8 | 8 | type AccountModel struct { | 
| 9 | - *ModelBaseMgo | |
| 9 | + *db.MgoColl | |
| 10 | 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 | 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(mgo *db.Database, phone string) *AccountModel { | |
| 21 | +func NewAccount(database, phone string) *AccountModel { | |
| 22 | 22 | ac := &pb.AccountInfo{ | 
| 23 | 23 | Phone: phone, | 
| 24 | 24 | } | 
| 25 | 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 | 27 | AccountInfo: ac, | 
| 28 | 28 | } | 
| 29 | 29 | |
| 30 | - | |
| 31 | 30 | return account | 
| 32 | 31 | } | 
| 33 | 32 | \ No newline at end of file | ... | ... | 
models/basic.go deleted
| ... | ... | @@ -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 | 0 | \ No newline at end of file | 
| ... | ... | @@ -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 | 1 | package models | 
| 2 | 2 | |
| 3 | 3 | import ( | 
| 4 | + "fmt" | |
| 4 | 5 | "pro2d/components/db" | 
| 6 | + "pro2d/conf" | |
| 5 | 7 | "pro2d/protos/pb" | 
| 6 | 8 | ) | 
| 7 | 9 | |
| 8 | 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 | 21 | if err := m.Load(); err != nil { | 
| 20 | 22 | return false, m | 
| 21 | 23 | } | 
| 22 | 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 | 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 | 30 | Role: r, | 
| 31 | + Heros: make(HeroMap), | |
| 32 | 32 | } | 
| 33 | 33 | m.Load() | 
| 34 | 34 | return m | 
| ... | ... | @@ -36,3 +36,12 @@ func NewRole(mgo *db.Database, uid int64) *RoleModel { | 
| 36 | 36 | |
| 37 | 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 | 48 | \ No newline at end of file | ... | ... | 
models/role_test.go
| 1 | 1 | package models | 
| 2 | 2 | |
| 3 | 3 | import ( | 
| 4 | - "pro2d/components/db" | |
| 5 | - "pro2d/conf" | |
| 6 | - "pro2d/utils" | |
| 4 | + "pro2d/protos/pb" | |
| 7 | 5 | "testing" | 
| 8 | - "time" | |
| 9 | 6 | ) | 
| 10 | 7 | |
| 11 | 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 | 12 | var uid int64 = 1 | 
| 21 | 13 | var role *RoleModel | 
| 22 | - if ok, role := RoleExistByUid(db, uid); ok { | |
| 14 | + if ok, role := RoleExistByUid(uid); ok { | |
| 23 | 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 | 25 | role.Save() | 
| 26 | 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 | 29 | role.Role.Device = "222222" | 
| 30 | 30 | role.Role.Level = 0 | 
| 31 | 31 | role.Create() | ... | ... |