go_dreamfactory/modules/task/module.go
2022-07-26 16:52:53 +08:00

128 lines
3.6 KiB
Go

package task
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
)
type ModuleTask struct {
modules.ModuleBase
modelTask *ModelTask
modelTaskActive *ModelTaskActive
api *apiComp
configure *configureComp
taskHandleMap map[int32]taskHandle //任务处理器
}
func NewModule() core.IModule {
return &ModuleTask{
taskHandleMap: make(map[int32]taskHandle),
}
}
func (this *ModuleTask) GetType() core.M_Modules {
return comm.ModuleTask
}
func (this *ModuleTask) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.initTaskHandle()
return
}
func (this *ModuleTask) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelTask = this.RegisterComp(new(ModelTask)).(*ModelTask)
this.modelTaskActive = this.RegisterComp(new(ModelTaskActive)).(*ModelTaskActive)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
//模块启动接口
func (this *ModuleTask) Start() (err error) {
err = this.ModuleBase.Start()
event.RegisterGO(comm.EventUserOffline, this.CleanTask)
return
}
//初始化日常、周常、成就
func (this *ModuleTask) InitTaskAll(uid string) {
this.modelTask.initTask(uid)
this.modelTaskActive.initActiveReward(uid)
}
// 清除缓存
func (this *ModuleTask) CleanTask(session comm.IUserSession) {
this.modelTask.BatchDelLists(session.GetUserId())
this.modelTaskActive.BatchDelLists(session.GetUserId())
}
//重置任务
func (this *ModuleTask) ResetTask(uid string, taskTag comm.TaskTag) {
this.modelTask.clearTask(uid, taskTag)
this.modelTaskActive.clearTask(uid, taskTag)
this.InitTaskAll(uid)
}
//任务处理
func (this *ModuleTask) SendToTask(session comm.IUserSession, taskType comm.TaskType, taskPram *pb.TaskParam) (code pb.ErrorCode) {
if tasks, err := this.modelTask.doTaskHandle(session.GetUserId(), taskType, taskPram); err != nil {
code = pb.ErrorCode_TaskHandle
} else {
for _, t := range tasks {
if err := session.SendMsg(string(comm.ModuleTask), TaskSubTypeFinishedPush, &pb.TaskFinishedPush{TaskId: t.TaskId}); err != nil {
this.modelTask.moduleTask.Errorf("SendToTask sendmsg err:%v", err)
}
}
}
return
}
//创建玩家攻略任务
func (this *ModuleTask) CreateTaskForStrategy(uid string, heroCfgId int32) {
//
ids := make(map[string]string)
ids, _ = this.modelTask.Redis.HGetAllToMapString(fmt.Sprintf("task:%v_%v", uid, comm.TASK_STRATEGY))
fmt.Println(ids)
//
}
// 清理任务数据
func (this *ModuleTask) CleanData(uid string) {
this.modelTask.clearTask(uid)
this.modelTaskActive.clearTask(uid)
}
//任务处理器注册
type taskHandle func(uid string, taskId int32, tp *pb.TaskParam) *pb.DBTask
func (this *ModuleTask) register(taskType comm.TaskType, fn taskHandle) {
if _, ok := this.taskHandleMap[int32(taskType)]; !ok {
this.taskHandleMap[int32(taskType)] = fn
}
}
func (this *ModuleTask) initTaskHandle() {
if data, err := this.configure.getTaskList(); err == nil {
for _, v := range data {
switch v.TypeId {
case int32(comm.TaskTypeUpEquip):
this.register(comm.TaskTypeUpEquip, this.modelTask.UpEquip)
case int32(comm.TaskTypeUpHeroStar):
this.register(comm.TaskTypeUpHeroStar, this.modelTask.UpHeroStar)
case int32(comm.TaskTypeUpHeroLevel):
this.register(comm.TaskTypeUpHeroLevel, this.modelTask.UpHeroLevel)
default:
log.Warnf("%v task type not supported", v.TypeId)
}
}
}
}