diff --git a/comm/imodule.go b/comm/imodule.go index 09d23c957..a4b136cf2 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -532,7 +532,7 @@ type ( //读取全局表 db层 GetGlobalData(key string, v interface{}) (err error) - //更新全局表 db层 + GetAndUpdateGlobalData(key string, v interface{}, update interface{}) (err error) UpdateGlobalData(key string, v interface{}) (err error) } diff --git a/modules/tools/modelGlobal.go b/modules/tools/modelGlobal.go index 83e1abcd2..751dfd58e 100644 --- a/modules/tools/modelGlobal.go +++ b/modules/tools/modelGlobal.go @@ -31,6 +31,14 @@ func (this *modelGlobal) GetGlobalData(key string, v interface{}) (err error) { return } +//查询更新列表 +func (this *modelGlobal) GetAndUpdateGlobalData(key string, v interface{}, update interface{}) (err error) { + if err = this.DBModel.DB.FindOneAndUpdate(core.SqlTable(this.TableName), bson.M{"_id": key}, update).Decode(v); err != nil && err != mgo.MongodbNil { + this.module.Error("GetAndUpdateGlobalData err", log.Field{Key: "key", Value: key}, log.Field{Key: "err", Value: err.Error()}) + } + return +} + // 更新全局配置 func (this *modelGlobal) UpdateGlobalData(key string, v interface{}) (err error) { if _, err = this.DBModel.DB.UpdateOne(core.SqlTable(this.TableName), bson.M{"_id": key}, bson.M{"$set": v}, options.Update().SetUpsert(true)); err != nil { diff --git a/modules/tools/module.go b/modules/tools/module.go index fc5e3e8cf..1f16c0d2c 100644 --- a/modules/tools/module.go +++ b/modules/tools/module.go @@ -111,7 +111,9 @@ func (this *Tools) GetGrormetLlame(id string) (data int32, err error) { func (this *Tools) GetGlobalData(key string, v interface{}) (err error) { return this.modelGlobal.GetGlobalData(key, v) } - +func (this *Tools) GetAndUpdateGlobalData(key string, v interface{}, update interface{}) (err error) { + return this.modelGlobal.GetAndUpdateGlobalData(key, v, update) +} func (this *Tools) UpdateGlobalData(key string, v interface{}) (err error) { return this.modelGlobal.UpdateGlobalData(key, v) } diff --git a/modules/user/api.go b/modules/user/api.go index d868cb8b8..b330c809f 100644 --- a/modules/user/api.go +++ b/modules/user/api.go @@ -39,6 +39,7 @@ type apiComp struct { modules.MCompGate service base.IRPCXService module *User + options *Options hero comm.IHero mail comm.Imail chat comm.IChat @@ -48,6 +49,7 @@ func (this *apiComp) Init(service core.IService, module core.IModule, comp core. this.MCompGate.Init(service, module, comp, options) this.service = service.(base.IRPCXService) this.module = module.(*User) + this.options = options.(*Options) return } diff --git a/modules/user/api_login.go b/modules/user/api_login.go index 88d87e99c..043169a29 100644 --- a/modules/user/api_login.go +++ b/modules/user/api_login.go @@ -9,6 +9,7 @@ import ( "go_dreamfactory/utils" "time" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) @@ -189,22 +190,25 @@ func (this *apiComp) Login(session comm.IUserSession, req *pb.UserLoginReq) (err func (this *apiComp) getGroup() (group *UserGroupData, err error) { group = &UserGroupData{ - CurrGroup: 1, - STime: configure.Now().Unix(), - ETime: configure.Now().Add(time.Hour * 24 * 7).Unix(), + CurrGroup: 1, + CurrGroupNum: 0, + STime: configure.Now().Unix(), + ETime: configure.Now().Add(time.Hour * 24 * time.Duration(this.options.GroupMaxIntervalDay)).Unix(), } - if err = this.module.ModuleTools.GetGlobalData(UserGroupDataCoonfKey, group); err != nil && err != mongo.ErrNoDocuments { + if err = this.module.ModuleTools.GetAndUpdateGlobalData(UserGroupDataCoonfKey, group, bson.M{"$inc": bson.M{"currgroupnum": 1}}); err != nil && err != mongo.ErrNoDocuments { return } err = nil - if configure.Now().After(time.Unix(group.ETime, 0)) { + if group.CurrGroupNum >= this.options.GroupMaxPlayerNum || configure.Now().After(time.Unix(group.ETime, 0)) { group.CurrGroup++ + group.CurrGroupNum = 0 group.STime = configure.Now().Unix() - group.ETime = configure.Now().Add(time.Hour * 24 * 7).Unix() + group.ETime = configure.Now().Add(time.Hour * 24 * time.Duration(this.options.GroupMaxIntervalDay)).Unix() if err = this.module.ModuleTools.UpdateGlobalData(UserGroupDataCoonfKey, map[string]interface{}{ - "currcroup": group.CurrGroup, - "stime": group.STime, - "etime": group.ETime, + "currcroup": group.CurrGroup, + "currgroupnum": group.CurrGroupNum, + "stime": group.STime, + "etime": group.ETime, }); err != nil { return } diff --git a/modules/user/core.go b/modules/user/core.go index 97dfffaa6..62461eab1 100644 --- a/modules/user/core.go +++ b/modules/user/core.go @@ -6,7 +6,8 @@ const ( //用户分组数据 type UserGroupData struct { - CurrGroup int32 `json:"currcroup" bson:"currcroup"` //刷新时间 - STime int64 `json:"stime" bson:"stime"` //开始时间 - ETime int64 `json:"etime" bson:"etime"` //结束时间 + CurrGroup int32 `json:"currcroup" bson:"currcroup"` //刷新时间 + CurrGroupNum int32 `json:"currgroupnum" bson:"currgroupnum"` //当前组人数 + STime int64 `json:"stime" bson:"stime"` //开始时间 + ETime int64 `json:"etime" bson:"etime"` //结束时间 } diff --git a/modules/user/module.go b/modules/user/module.go index eb851ef86..5a79c6e34 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -71,7 +71,9 @@ type User struct { func (this *User) GetType() core.M_Modules { return comm.ModuleUser } - +func (this *User) NewOptions() (options core.IModuleOptions) { + return new(Options) +} func (this *User) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { if err = this.ModuleBase.Init(service, module, options); err != nil { return diff --git a/modules/user/options.go b/modules/user/options.go new file mode 100644 index 000000000..0dbea3f27 --- /dev/null +++ b/modules/user/options.go @@ -0,0 +1,44 @@ +package user + +import ( + "errors" + "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/utils/mapstructure" + "go_dreamfactory/modules" +) + +type ( + IOptions interface { + modules.IOptions + } + Options struct { + modules.Options + GroupMaxPlayerNum int32 + GroupMaxIntervalDay int32 //天数 + } +) + +func (this *Options) GetDebug() bool { + return this.Debug +} + +func (this *Options) GetLog() log.ILogger { + return this.Log +} + +func (this *Options) LoadConfig(settings map[string]interface{}) (err error) { + this.GroupMaxPlayerNum = 10000 + this.GroupMaxIntervalDay = 30 + if settings != nil { + if err = mapstructure.Decode(settings, &this.Options); err != nil { + return + } + if err = mapstructure.Decode(settings, this); err != nil { + return + } + } + if this.Log = log.NewTurnlog(true, log.Clone("", 4)); this.Log == nil { + err = errors.New("log is nil") + } + return +}