package rtask import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/utils" "github.com/pkg/errors" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) type ModelRtask struct { modules.MCompModel moduleRtask *ModuleRtask service core.IService } func (this *ModelRtask) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = comm.TableRtask err = this.MCompModel.Init(service, module, comp, options) this.moduleRtask = module.(*ModuleRtask) this.service = service return } //查询用户随机任务 func (this *ModelRtask) GetRtask(uid string) *pb.DBRtask { rtask := &pb.DBRtask{Uid: uid} err := this.Get(uid, rtask) if err != nil { if err == redis.RedisNil || err == mongo.ErrNoDocuments { _id := primitive.NewObjectID().Hex() rtask.Id = _id if err := this.Add(uid, rtask); err != nil { return nil } } } return rtask } // 做任务之前的校验 func (this *ModelRtask) checkHandle(uid string, frtaskIds []int32, conf *cfg.GameRdtaskData) (err error, ok bool) { //判断前置任务状态 if conf.PreTid != 0 { if _, ok = utils.Findx(frtaskIds, conf.PreTid); !ok { err = errors.New("前置任务未完成") return } } //验证限定条件 for _, v := range conf.Condition { if err, ok = this.checkCondi(uid, v); !ok { return } } return } // 确定选项前的校验 func (this *ModelRtask) checkCondi(uid string, condiId int32) (err error, ok bool) { if condiId == 0 { return nil, true } //验证限定条件 var condi *rtaskCondi if condi, ok = this.moduleRtask.handleMap[condiId]; ok { if err, ok = condi.verify(uid, condi.cfg); !ok { err = errors.WithMessagef(err, "uid: %v do rtask %v condition not reach", uid, condiId) return } return } return } // 任务记录 func (this *ModelRtask) findAndUpdate(uid string, rtypeId comm.TaskType, vals ...int32) (err error) { var ( condiId int32 condi *rtaskCondi ) // 遍历当前已注册的任务条件 for _, v := range this.moduleRtask.handleMap { // 找到任务类型 if int32(rtypeId) == v.cfg.Type { if condiId = v.find(v.cfg, vals...); condiId == 0 { continue } else { condi = v break } } } // update if condiId != 0 { err = condi.update(uid, condi.cfg, vals...) } return }