优化用户分组方式

This commit is contained in:
liwei1dao 2024-01-17 14:57:41 +08:00
parent 4c3ea94ff8
commit fc6ce6cbaf
8 changed files with 78 additions and 15 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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
}

View File

@ -9,6 +9,7 @@ import (
"go_dreamfactory/utils"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
@ -190,19 +191,22 @@ func (this *apiComp) Login(session comm.IUserSession, req *pb.UserLoginReq) (err
func (this *apiComp) getGroup() (group *UserGroupData, err error) {
group = &UserGroupData{
CurrGroup: 1,
CurrGroupNum: 0,
STime: configure.Now().Unix(),
ETime: configure.Now().Add(time.Hour * 24 * 7).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,
"currgroupnum": group.CurrGroupNum,
"stime": group.STime,
"etime": group.ETime,
}); err != nil {

View File

@ -7,6 +7,7 @@ const (
//用户分组数据
type UserGroupData struct {
CurrGroup int32 `json:"currcroup" bson:"currcroup"` //刷新时间
CurrGroupNum int32 `json:"currgroupnum" bson:"currgroupnum"` //当前组人数
STime int64 `json:"stime" bson:"stime"` //开始时间
ETime int64 `json:"etime" bson:"etime"` //结束时间
}

View File

@ -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

44
modules/user/options.go Normal file
View File

@ -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
}