go_dreamfactory/modules/rtask/module.go
2022-08-19 18:13:13 +08:00

134 lines
3.3 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"
)
// 随机任务
type rtask struct {
rtaskId int32
handlers []*rtaskCondi
}
// 限定条件
type rtaskCondi struct {
cfg *cfg.GameRdtaskCondiData
fn rtaskHandle
}
// 设定返回值
type rtaskHandle func(cfg *cfg.GameRdtaskCondiData, tp *pb.RtaskParam) (ok bool)
type ModuleRtask struct {
modules.ModuleBase
modelRtask *ModelRtask
api *apiComp
configure *configureComp
rtaskHandleMap map[int32]*rtask // 任务处理器
}
func NewModule() core.IModule {
return &ModuleRtask{
rtaskHandleMap: make(map[int32]*rtask),
}
}
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.initRtaskHandle()
return
}
func (this *ModuleRtask) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelRtask = this.RegisterComp(new(ModelRtask)).(*ModelRtask)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
func (this *ModuleRtask) register(rtaskId int32, rtask *rtask) {
if _, ok := this.rtaskHandleMap[rtaskId]; !ok {
this.rtaskHandleMap[rtaskId] = rtask
}
}
func (this *ModuleRtask) initRtaskHandle() {
if data, err := this.configure.getRtaskList(); err == nil {
for _, v := range data {
var handlers []*rtaskCondi
rtask := &rtask{
rtaskId: v.Id,
handlers: handlers,
}
//遍历任务的限定条件
for _, rtaskTypeId := range v.Condition {
// 获取每个限定条件配置
if typeCfg, err2 := this.configure.getRtaskTypeById(rtaskTypeId); err2 == nil {
if typeCfg != nil {
switch comm.TaskType(typeCfg.GetTypeId()) {
case comm.RtaskTypeHeroTarget:
handlers = append(handlers, &rtaskCondi{
cfg: typeCfg,
fn: this.modelRtask.HeroTarget,
})
case comm.RtaskTypeHeroLvTarget:
handlers = append(handlers, &rtaskCondi{
cfg: typeCfg,
fn: this.modelRtask.HeroLvTarget,
})
case comm.RtaskTypeEquipNum:
handlers = append(handlers, &rtaskCondi{
cfg: typeCfg,
fn: this.modelRtask.EquipNum,
})
case comm.RtaskTypePoltId:
handlers = append(handlers, &rtaskCondi{
cfg: typeCfg,
fn: this.modelRtask.PoltId,
})
case comm.RtaskTypeTaskDay:
handlers = append(handlers, &rtaskCondi{
cfg: typeCfg,
fn: this.modelRtask.TaskDay,
})
default:
log.Warnf("%v rtask type not configure", typeCfg.GetTypeId())
}
}
}
}
this.register(v.Id, rtask)
}
}
}
// 通知随机任务
func (this *ModuleRtask) SendToRtask(session comm.IUserSession, param *pb.RtaskParam) (code pb.ErrorCode) {
if taskId, err := this.modelRtask.doRtaskHandle(session.GetUserId(), param); err != nil {
code = pb.ErrorCode_TaskHandle
} else {
if err := session.SendMsg(string(comm.ModuleRtask), "", &pb.RtaskFinishPush{
RtaskId: taskId,
}); err != nil {
code = pb.ErrorCode_SystemError
}
}
return
}