go_dreamfactory/modules/task/module.go
2022-11-21 10:34:25 +08:00

170 lines
4.5 KiB
Go

// package
// 日/周常成就任务
// 赵长远
package task
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
event_v2 "go_dreamfactory/lego/sys/event/v2"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"time"
)
var _ comm.ITask = (*ModuleTask)(nil)
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) GetEventApp() *event_v2.App {
return this.modelTask.EventApp
}
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) {
t := configure.Now()
defer func() {
log.Debugf("初始化任务 耗时:%v", time.Since(t))
}()
this.modelTask.initTask(uid, comm.TASK_DAILY)
this.modelTask.initTask(uid, comm.TASK_WEEKLY)
this.modelTask.initTask(uid, comm.TASK_ACHIEVE)
this.modelTaskActive.initActiveReward(uid, comm.TASK_DAILY)
this.modelTaskActive.initActiveReward(uid, comm.TASK_WEEKLY)
}
// 初始化指定的任务
func (this *ModuleTask) InitTaskByTag(uid string, taskTag comm.TaskTag) {
switch taskTag {
case comm.TASK_DAILY:
this.modelTask.initTask(uid, comm.TASK_DAILY)
this.modelTaskActive.initActiveReward(uid, comm.TASK_DAILY)
case comm.TASK_WEEKLY:
this.modelTask.initTask(uid, comm.TASK_WEEKLY)
this.modelTaskActive.initActiveReward(uid, comm.TASK_WEEKLY)
case comm.TASK_ACHIEVE:
this.modelTask.initTask(uid, comm.TASK_ACHIEVE)
}
}
// 清除缓存
func (this *ModuleTask) CleanTask(session comm.IUserSession) {
this.modelTask.BatchDelLists(session.GetUserId())
this.modelTaskActive.BatchDelLists(session.GetUserId())
}
// 重置玩家活跃度
func (this *ModuleTask) resetActive(uid string, taskTag comm.TaskTag) {
update := make(map[string]interface{})
if taskTag == comm.TASK_DAILY {
update["activeday"] = 0
} else if taskTag == comm.TASK_WEEKLY {
update["activeweek"] = 0
}
if len(update) > 0 {
this.ModuleUser.ChangeUserExpand(uid, update)
}
}
// 获取玩家指定任务
func (this *ModuleTask) GetTaskById(uid string, taskId int32) *pb.DBTask {
list := []*pb.DBTask{}
if err := this.modelTask.GetList(uid, &list); err != nil {
log.Errorf("GetTaskById err %v", err)
return nil
}
for _, v := range list {
if v.TaskId == taskId {
return v
}
}
return nil
}
//重置任务
func (this *ModuleTask) ResetTask(uid string, taskTag comm.TaskTag) {
this.resetActive(uid, taskTag)
this.modelTask.clearTask(uid, taskTag)
this.modelTaskActive.clearTask(uid, taskTag)
}
// 清理任务数据
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) GetTaskFinished(uid string, taskTage comm.TaskTag) []*pb.DBTask {
return this.modelTask.getFinishTasks(uid, taskTage)
}
func (this *ModuleTask) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]bool) {
reddot = make(map[comm.ReddotType]bool)
for _, v := range rid {
switch v {
case comm.Reddot1:
if ok, _ := this.modelTask.noReceiveTask(session.GetUserId(), comm.TASK_DAILY); ok {
reddot[comm.Reddot1] = ok
break
}
case comm.Reddot2:
if ok, _ := this.modelTask.noReceiveTask(session.GetUserId(), comm.TASK_WEEKLY); ok {
reddot[comm.Reddot2] = ok
break
}
case comm.Reddot4:
if ok, _ := this.modelTask.noReceiveTask(session.GetUserId(), comm.TASK_ACHIEVE); ok {
reddot[comm.Reddot4] = ok
break
}
}
}
return
}