go_dreamfactory/modules/rtask/module.go
2022-10-08 11:03:53 +08:00

266 lines
8.0 KiB
Go

// package 随机任务
package rtask
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"github.com/pkg/errors"
)
var _ comm.IRtask = (*ModuleRtask)(nil)
// 限定条件
type rtaskCondi struct {
cfg *cfg.GameRdtaskCondiData
verify verifyHandle //校验任务条件
find condiFindHandle //检索任务条件
update updateDataHandle //更新任务数据
}
type verifyHandle func(uid string, cfg *cfg.GameRdtaskCondiData) (bool, error)
type condiFindHandle func(cfg *cfg.GameRdtaskCondiData, vals ...int32) (int32, error)
type updateDataHandle func(uid string, cfg *cfg.GameRdtaskCondiData, vals ...int32) error
type ModuleRtask struct {
modules.ModuleBase
modelRtask *ModelRtask
modelRtaskRecord *ModelRtaskRecord
api *apiComp
configure *configureComp
handleMap map[int32]*rtaskCondi //任务校验处理器
}
func NewModule() core.IModule {
return &ModuleRtask{
handleMap: make(map[int32]*rtaskCondi),
}
}
func (this *ModuleRtask) GetType() core.M_Modules {
return comm.ModuleRtask
}
func (this *ModuleRtask) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.initRtaskVerifyHandle()
return
}
func (this *ModuleRtask) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelRtask = this.RegisterComp(new(ModelRtask)).(*ModelRtask)
this.modelRtaskRecord = this.RegisterComp(new(ModelRtaskRecord)).(*ModelRtaskRecord)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
func (this *ModuleRtask) registerVerifyHandle(condiId int32, condi *rtaskCondi) {
if _, ok := this.handleMap[condiId]; !ok {
this.handleMap[condiId] = condi
}
}
// 条件校验初始
func (this *ModuleRtask) initRtaskVerifyHandle() {
conf, err := this.configure.getRtaskCondiCfg()
if err != nil {
return
}
for _, v := range conf.GetDataList() {
if typeCfg, err := this.configure.getRtaskTypeById(v.Id); err == nil {
if typeCfg != nil {
switch comm.TaskType(typeCfg.Type) {
case comm.Rtype1:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verfiyRtype1,
})
case comm.Rtype2:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype2,
})
case comm.Rtype3:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype3,
})
case comm.Rtype4:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype4,
})
case comm.Rtype5:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype5,
})
case comm.Rtype6:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype6,
})
// case comm.Rtype7:
// this.registerVerifyHandle(v.Id, &rtaskCondi{
// find: this.modelRtaskRecord.lessEqualFirstParam,
// cfg: typeCfg,
// verify: this.modelRtask.verfiyRtype7,
// update: this.modelRtaskRecord.addUpdate,
// })
case comm.Rtype8:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verfiyRtype8,
})
case comm.Rtype9:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verfiyRtype9,
})
case comm.Rtype10:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verfiyRtype10,
})
case comm.Rtype11:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
find: this.modelRtaskRecord.lessEqualFirstParam,
verify: this.modelRtaskRecord.verifyFirstGreatEqualParam,
update: this.modelRtaskRecord.overrideUpdate,
})
case comm.Rtype7, comm.Rtype12, comm.Rtype13, comm.Rtype14, comm.Rtype15,
comm.Rtype18, comm.Rtype19, comm.Rtype21, comm.Rtype24,
comm.Rtype26, comm.Rtype27, comm.Rtype28, comm.Rtype38,
comm.Rtype39, comm.Rtype50, comm.Rtype51, comm.Rtype53,
comm.Rtype54, comm.Rtype57, comm.Rtype58, comm.Rtype60,
comm.Rtype62, comm.Rtype64, comm.Rtype69:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
find: this.modelRtaskRecord.lessEqualFirstParam,
verify: this.modelRtaskRecord.verifyFirstGreatEqualParam,
update: this.modelRtaskRecord.addUpdate,
})
case comm.Rtype20:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype20,
})
case comm.Rtype22:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
find: this.modelRtaskRecord.equalFirstParam,
verify: this.modelRtaskRecord.verifyFirstEqualParam,
update: this.modelRtaskRecord.overrideUpdate,
})
case comm.Rtype63:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
verify: this.modelRtask.verifyRtype63,
})
case comm.Rtype16, comm.Rtype17,
comm.Rtype35, comm.Rtype44,
comm.Rtype59, comm.Rtype61, comm.Rtype70:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
find: this.modelRtaskRecord.equalParams,
verify: this.modelRtaskRecord.verifyFromDb,
update: this.modelRtaskRecord.overrideUpdate,
})
case comm.Rtype23, comm.Rtype25, comm.Rtype29, comm.Rtype30, comm.Rtype31,
comm.Rtype32, comm.Rtype33, comm.Rtype34, comm.Rtype36,
comm.Rtype37, comm.Rtype40, comm.Rtype41,
comm.Rtype42, comm.Rtype43, comm.Rtype45,
comm.Rtype46, comm.Rtype47, comm.Rtype48, comm.Rtype49,
comm.Rtype52, comm.Rtype55, comm.Rtype56,
comm.Rtype65, comm.Rtype66, comm.Rtype67, comm.Rtype68:
this.registerVerifyHandle(v.Id, &rtaskCondi{
cfg: typeCfg,
find: this.modelRtaskRecord.lessThanParams,
verify: this.modelRtaskRecord.verifyFromDb,
update: this.modelRtaskRecord.addUpdate,
})
default:
log.Warnf("rtaskType[%v] not register", typeCfg.Type)
}
}
}
}
}
func (this *ModuleRtask) SendToRtask(session comm.IUserSession, rtaskType comm.TaskType, params ...int32) (code pb.ErrorCode) {
this.Debug("任务事件触发", log.Field{"uid", session.GetUserId()}, log.Field{"taskType", rtaskType}, log.Field{"params", params})
var (
err error
condiId int32
condis []*rtaskCondi
)
user := this.ModuleUser.GetUser(session.GetUserId())
if user == nil {
code = pb.ErrorCode_UserSessionNobeing
return
}
for _, codi := range this.configure.getRtaskCondis(int32(rtaskType)) {
v, ok := this.handleMap[codi.Id]
if !ok {
this.Warn("未注册事件处理器", log.Field{"uid", session.GetUserId()}, log.Field{"condiId", codi.Id})
code = pb.ErrorCode_RtaskCondiNoFound
return
}
if v.find == nil {
return
}
if condiId, err = v.find(v.cfg, params...); condiId == 0 {
if err != nil {
this.Warn(errors.WithMessage(err, session.GetUserId()).Error())
}
} else {
condis = append(condis, v)
}
}
// update
for _, v := range condis {
if v.update != nil {
//条件未达成才更新数据
// if code = this.CheckCondi(user.Id, v.cfg.Id); code != pb.ErrorCode_Success {
if err := v.update(session.GetUserId(), v.cfg, params...); err != nil {
code = pb.ErrorCode_DBError
}
// }
}
}
return
}
// 任务条件校验
func (this *ModuleRtask) CheckCondi(uid string, condiId int32) (code pb.ErrorCode) {
if _, ok := this.modelRtask.checkCondi(uid, condiId); !ok {
code = pb.ErrorCode_RtaskCondiNoReach
}
return
}
// 初始化任务条件数据
func (this *ModuleRtask) InitCondiData(uid string) error {
return this.modelRtaskRecord.initCondiData(uid)
}
// 获取玩家任务条件记录
func (this *ModuleRtask) GetCondiData(uid string) *pb.DBRtaskRecord {
return this.modelRtaskRecord.getRecord(uid)
}