186 lines
5.2 KiB
Go
186 lines
5.2 KiB
Go
package task
|
|
|
|
import (
|
|
"fmt"
|
|
"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) SendToTask(session comm.IUserSession, condiId int32, params ...int32) (code pb.ErrorCode) {
|
|
// tl := new(TaskListen)
|
|
// tl.Uid = session.GetUserId()
|
|
// tl.TaskType = comm.TaskType(condiId)
|
|
// this.modelTask.EventApp.Dispatch(comm.EventTaskChanged, tl)
|
|
// 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)
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
func (this *ModuleTask) GetTaskFinished(uid string, taskTage comm.TaskTag) []*pb.DBTask {
|
|
return this.modelTask.getFinishTasks(uid, taskTage)
|
|
}
|