package task import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "time" ) type ModuleTask struct { modules.ModuleBase modelTask *ModelTask modelTaskActive *ModelTaskActive api *apiComp configure *configureComp lastDayTime int64 //上次日任务执行时间戳 lastWeekTime int64 //上次周任务执行时间戳 } func NewModule() core.IModule { return &ModuleTask{} } 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) go func() { tickC := time.NewTicker(time.Second * 1) for { select { case tc := <-tickC.C: this.update(tc) } } }() 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() return } func (this *ModuleTask) update(t time.Time) { this.taskEveryDay(t) this.taskWeekDay(t) } //日任务 func (this *ModuleTask) taskEveryDay(t time.Time) { n := time.Now().Unix() if t.Hour() == 4 && t.Minute() == 0 { this.lastDayTime = n } else { this.lastDayTime = 0 } } //周任务 func (this *ModuleTask) taskWeekDay(t time.Time) { n := time.Now().Unix() if int(t.Weekday()) == 1 { //周一 if t.Hour() == 0 && t.Minute() == 0 { this.lastWeekTime = n } else { this.lastWeekTime = 0 } } } //初始化任务 func (this *ModuleTask) InitTask(uid string, taskTag comm.TaskTag) { if err := this.modelTask.initTaskByTag(uid, taskTag); err != nil { log.Errorf("uid:%v tag:%v initTask err: %v", uid, taskTag, err) } } //初始化日常、周常、成就 func (this *ModuleTask) InitTaskAll(uid string) { this.InitTask(uid, comm.TASK_DAILY) this.InitTask(uid, comm.TASK_WEEKLY) this.InitTask(uid, comm.TASK_ACHIEVE) this.modelTaskActive.initActiveRewardByTag(uid, comm.TASK_DAILY) this.modelTaskActive.initActiveRewardByTag(uid, comm.TASK_WEEKLY) } //重置任务 func (this *ModuleTask) ResetTask(uid string, taskTag comm.TaskTag) { if err := this.modelTask.clearTask(uid, taskTag); err != nil { log.Errorf("uid:%v tag:%v ResetTask err:%v", uid, taskTag, err) return } this.InitTask(uid, taskTag) if err := this.modelTaskActive.clearTask(uid, taskTag); err != nil { log.Errorf("uid:%v tag:%v ResetTaskActive err:%v", uid, taskTag, err) return } this.modelTaskActive.initActiveRewardByTag(uid, taskTag) } //任务处理 func (this *ModuleTask) SendToTask(uid string, taskType comm.TaskType, taskPram *pb.TaskParam) (code pb.ErrorCode) { if err := this.modelTask.taskHandle(uid, taskType, taskPram); err != nil { code = pb.ErrorCode_TaskHandle } return }