go_dreamfactory/modules/rtask/model_rtask.go
2023-05-05 16:57:38 +08:00

124 lines
2.9 KiB
Go

package rtask
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"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 *rtaskCondHandle
cond, ok := this.moduleRtask.handleMap.Load(condiId)
if !ok {
rcs := this.moduleRtask.getHandle(comm.TaskType(conf.Type))
for _, v := range rcs {
if v.condId == condiId {
cond = v
}
}
}
if condi, ok = cond.(*rtaskCondHandle); !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
}
func (this *ModelRtask) userlock(uid string) (result *redis.RedisMutex, err error) {
return this.DBModel.Redis.NewRedisMutex(fmt.Sprintf("ulocktask:%s", uid))
}