go_dreamfactory/modules/rtask/model_rtask.go
2023-03-27 19:57:48 +08:00

118 lines
2.7 KiB
Go

package rtask
import (
"fmt"
"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
}
// 更新玩家的随机任务ID
func (this *ModelRtask) updateUserRtaskId(uid string, rtaskId int32) (err error) {
ex_update := map[string]interface{}{
"rtaskId": rtaskId,
}
return this.moduleRtask.ModuleUser.ChangeUserExpand(uid, ex_update)
}
// 查询用户随机任务
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
}
conf, err := this.moduleRtask.configure.getRtaskTypeById(condiId)
if err != nil || conf == nil {
errors.Errorf("conf not found condiID: %v", condiId)
return
}
if conf.Valid == 1 {
ok = true
return
}
//验证限定条件
var condi *rtaskCondi
cond, ok := this.moduleRtask.handleMap.Load(condiId)
if !ok {
errors.Errorf("condiID: %v handle no found", condiId)
return
}
if condi, ok = cond.(*rtaskCondi); !ok {
err = fmt.Errorf("condiType err")
return
}
if condi.verify == nil {
errors.Errorf("condiID: %v", condiId)
return
}
if ok, err = condi.verify(uid, conf); !ok {
err = errors.WithMessagef(err, "uid: %v do rtask [condiId:%v] condition not reach", uid, condiId)
return
}
return
}