将游戏服务器uid与sdk分离
This commit is contained in:
1 parent
6d7e4867f5
commit
ddb55671b5
31 files changed
+457
-296
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
type Dao struct {
|
||||
mongo *mongo.Client
|
||||
db *mongo.Database
|
||||
}
|
||||
|
||||
func NewDao() (*Dao, error) {
|
||||
r := new(Dao)
|
||||
|
||||
clientOptions := options.Client().ApplyURI(config.GetConfig().Database.Url).SetMinPoolSize(10).SetMaxPoolSize(100)
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
logger.Error("mongo connect error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
err = client.Ping(context.TODO(), readpref.Primary())
|
||||
if err != nil {
|
||||
logger.Error("mongo ping error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
r.mongo = client
|
||||
r.db = client.Database("node_hk4e")
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (d *Dao) CloseDao() {
|
||||
err := d.mongo.Disconnect(context.TODO())
|
||||
if err != nil {
|
||||
logger.Error("mongo close error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type Region struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
RegionId uint32 `bson:"region_id"`
|
||||
Ec2bData []byte `bson:"ec2b_data"`
|
||||
NextUid uint32 `bson:"next_uid"`
|
||||
}
|
||||
|
||||
func (d *Dao) InsertRegion(region *Region) error {
|
||||
db := d.db.Collection("region")
|
||||
region.RegionId = 1
|
||||
_, err := db.InsertOne(context.TODO(), region)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) DeleteRegion() error {
|
||||
db := d.db.Collection("region")
|
||||
_, err := db.DeleteOne(context.TODO(), bson.D{{"region_id", 1}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) UpdateRegion(region *Region) error {
|
||||
db := d.db.Collection("region")
|
||||
region.RegionId = 1
|
||||
_, err := db.UpdateMany(
|
||||
context.TODO(),
|
||||
bson.D{{"region_id", 1}},
|
||||
bson.D{{"$set", region}},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) QueryRegion() (*Region, error) {
|
||||
db := d.db.Collection("region")
|
||||
result := db.FindOne(
|
||||
context.TODO(),
|
||||
bson.D{{"region_id", 1}},
|
||||
)
|
||||
region := new(Region)
|
||||
err := result.Decode(region)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return region, nil
|
||||
}
|
||||
Reference in new issue
Block a user