go_dreamfactory/modules/worldtask/module.go
2023-04-01 15:20:38 +08:00

329 lines
9.9 KiB
Go

package worldtask
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
)
var _ comm.IWorldtask = (*Worldtask)(nil)
type Worldtask struct {
modules.ModuleBase
api *apiComp
service base.IRPCXService
configure *configureComp
modelWorldtask *ModelWorldtask
worldtaskConf *cfg.GameWorldTask
worldBattleConf *cfg.GameWorldBattle
}
func NewModule() core.IModule {
return &Worldtask{}
}
func (this *Worldtask) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
return
}
func (this *Worldtask) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelWorldtask = this.RegisterComp(new(ModelWorldtask)).(*ModelWorldtask)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
func (this *Worldtask) GetType() core.M_Modules {
return comm.ModuleWorldtask
}
func (this *Worldtask) Start() (err error) {
err = this.ModuleBase.Start()
if this.worldtaskConf, err = this.configure.getWorldtaskCfg(); err != nil {
return err
}
if this.worldBattleConf, err = this.configure.getWorldtaskBattleCfg(); err != nil {
return err
}
return
}
// 任务条件达成通知
func (this *Worldtask) TaskCondFinishNotify(session comm.IUserSession, condId int32) error {
uid := session.GetUserId()
// 玩家世界任务
userTask, err := this.modelWorldtask.getWorldtask(uid)
if err != nil {
this.Error("获取玩家世界任务", log.Field{Key: "uid", Value: uid}, log.Field{Key: "condId", Value: condId})
return err
}
var groupId, taskId int32
// 检索condId是否是世界任务的完成条件
finishedTaskIds := make(map[int32]int32) //达成的任务条件
for _, c := range this.worldtaskConf.GetDataList() {
for _, v := range c.Completetask {
if v == condId {
//校验任务是否是当前任务
if task, ok := userTask.CurrentTask[c.Group]; ok {
this.Debug("推送NextTask",
log.Field{Key: "group", Value: c.Group},
log.Field{Key: "Key", Value: c.Key},
log.Field{Key: "taskId", Value: task.TaskId},
log.Field{Key: "condId", Value: condId},
log.Field{Key: "npcStatus", Value: task.NpcStatus})
if task.NpcStatus == 1 {
finishedTaskIds[c.Group] = c.Key
groupId = c.Group
taskId = c.Key
}
}
break
}
}
}
if len(finishedTaskIds) == 0 {
return nil
}
if userTask.CurrentTask == nil {
userTask.CurrentTask = make(map[int32]*pb.Worldtask)
}
wt, ok := userTask.CurrentTask[groupId]
if !ok {
wt = &pb.Worldtask{}
}
if _, ok := utils.Findx(wt.CondiIds, condId); !ok {
wt.CondiIds = append(wt.CondiIds, condId)
}
userTask.CurrentTask[groupId] = wt
this.Debug("当前任务更新", log.Field{Key: "currentTask", Value: userTask.CurrentTask})
update := map[string]interface{}{
"currentTask": userTask.CurrentTask,
}
this.modelWorldtask.Change(uid, update)
session.SendMsg(string(this.GetType()), "completecondis", &pb.WorldtaskCompletecondisPush{
GroupId: groupId,
TaskId: taskId,
CondiIds: wt.CondiIds,
})
this.Debug("推送完成条件",
log.Field{Key: "condiIds", Value: wt.CondiIds},
log.Field{Key: "taskId", Value: taskId})
// 当前任务配置
curTaskConf, err := this.configure.getWorldtaskById(taskId)
if err != nil || curTaskConf == nil {
return comm.NewCustomError(pb.ErrorCode_ConfigNoFound)
}
//结束任务
if curTaskConf.DeliverNpc == 0 {
this.modelWorldtask.taskFinish(session, groupId, taskId, userTask, curTaskConf)
this.modelWorldtask.taskFinishPush(session, groupId, userTask, curTaskConf)
}
return nil
}
// 任务条件达成通知
// Deprecated
// func (this *Worldtask) TaskcondNotify(session comm.IUserSession, condId int32) error {
// uid := session.GetUserId()
// finishedTaskIds := make(map[int32]int32) //达成的任务条件
// for _, c := range this.worldtaskConf.GetDataList() {
// for _, v := range c.Completetask {
// if v == condId {
// finishedTaskIds[c.Group] = c.Key
// }
// }
// }
// if len(finishedTaskIds) == 0 {
// //this.Debug("没有匹配到任务世界任务", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "condId", Value: condId})
// return nil
// }
// this.Debug("世界任务完成通知-查找到世界任务", log.Field{Key: "uid", Value: uid}, log.Field{Key: "condId", Value: condId}, log.Field{Key: "params", Value: finishedTaskIds})
// //下一个任务ID
// var nextTaskId int32
// // 获取用户信息
// user := this.ModuleUser.GetUser(uid)
// if user == nil {
// return comm.NewCustomError(pb.ErrorCode_UserSessionNobeing)
// }
// // 玩家世界任务
// userTask, err := this.modelWorldtask.getWorldtask(uid)
// if err != nil {
// this.Error("获取玩家世界任务", log.Field{Key: "uid", Value: uid}, log.Field{Key: "condId", Value: condId})
// return err
// }
// if userTask.Uid != "" {
// //查找任务ID根据condId 可能会找出不同的任务
// for groupId, taskId := range finishedTaskIds {
// logFields := []log.Field{{Key: "uid", Value: uid}, {Key: "group", Value: groupId}, {Key: "taskId", Value: taskId}, {Key: "condId", Value: condId}}
// // 判断任务是否已完成
// if this.modelWorldtask.isFinished(taskId, userTask.TaskList) {
// this.Debug("世界任务已完成", logFields...)
// continue
// }
// taskConf, err := this.configure.getWorldtaskById(taskId)
// if err != nil {
// this.Error("world_task config not found", logFields...)
// return err
// }
// logFields = append(logFields, log.Field{Key: "id_after", Value: taskConf.IdAfter}, log.Field{Key: "des", Value: taskConf.Des})
// if taskConf != nil {
// if taskConf.Des == 2 { //只有世界任务才校验前置
// if !this.modelWorldtask.IsPreFinished(userTask, taskConf) {
// this.Debug("世界任务前置任务未完成", logFields...)
// continue
// }
// }
// nextTaskId = taskConf.IdAfter
// // 判断玩家等级要求
// if taskConf.Des == 2 {
// if user.Lv < taskConf.Lock {
// logFields = append(logFields, log.Field{Key: "当前lv", Value: user.Lv}, log.Field{Key: "期望等级", Value: taskConf.Lock})
// this.Debug("等级不满足", logFields...)
// return comm.NewCustomError(pb.ErrorCode_WorldtaskLvNotEnough)
// }
// }
// //完成任务
// if err := this.modelWorldtask.finishTask(groupId, taskId, userTask); err != nil {
// logFields = append(logFields, log.Field{Key: "err", Value: err.Error()})
// this.Error("世界任务完成", logFields...)
// return err
// }
// this.Debug("任务条件达成完成", logFields...)
// //发奖
// if code := this.DispenseRes(session, taskConf.Reword, true); code != pb.ErrorCode_Success {
// logFields = append(logFields, log.Field{Key: "reward", Value: taskConf.Reword}, log.Field{Key: "code", Value: code})
// this.Error("资源发放", logFields...)
// }
// if nextTaskId != 0 && taskConf.Des == 2 {
// if err := session.SendMsg(string(this.GetType()), "nexttask", &pb.WorldtaskNexttaskPush{
// // NextTaskId: nextTaskId,
// }); err != nil {
// logFields = append(logFields, log.Field{Key: "err", Value: err.Error()})
// log.Error("任务条件达成推送", logFields...)
// } else {
// this.Debug("推送任务", log.Field{Key: "NextTaskId", Value: nextTaskId})
// }
// } else {
// this.Debug("已经是最后一个任务了", logFields...)
// }
// }
// }
// }
// return nil
// }
// 获取我的世界任务
func (this *Worldtask) GetMyWorldtask(uid string) *pb.DBWorldtask {
wt, err := this.modelWorldtask.getWorldtask(uid)
if err != nil {
log.Errorln(err.Error())
return nil
}
return wt
}
func (this *Worldtask) BingoAllTask(session comm.IUserSession) error {
// uid := session.GetUserId()
// mytask := &pb.DBWorldtask{Uid: uid}
// tasklist := this.worldtaskConf.GetDataList()
// for _, conf := range tasklist {
// //世界任务配置
// wt := &pb.Worldtask{
// TaskId: conf.Key,
// TaskType: conf.Des,
// }
// mytask.TaskList = append(mytask.TaskList, wt)
// }
// if err := this.modelWorldtask.Add(uid, mytask); err != nil {
// return err
// }
// rsp := &pb.WorldtaskFinishIdsPush{
// TaskList: mytask.TaskList,
// }
return nil //session.SendMsg(string(this.GetType()), "finishids", rsp)
}
// bingo世界任务跳跃 支持回退
func (this *Worldtask) BingoJumpTask(session comm.IUserSession, groupId, taskId int32) error {
// uid := session.GetUserId()
// mytask, err := this.modelWorldtask.getWorldtask(uid)
// if err != nil {
// return err
// }
// mytask.Uid = uid
// // 更新数据
// update := map[string]interface{}{}
// taskConf := this.worldtaskConf.GetDataMap()[taskId]
// if taskConf == nil {
// return fmt.Errorf("taskId: %v config is nil", taskId)
// }
// //重置taskList
// mytask.TaskList = []*pb.Worldtask{}
// //遍历
// if taskConf.Ontxe != 0 && taskConf.IdAfter != 0 {
// for _, v := range this.worldtaskConf.GetDataList() {
// if v.Group == groupId && v.Key <= taskId {
// wt := &pb.Worldtask{
// TaskId: v.Key,
// TaskType: v.Des,
// }
// mytask.TaskList = append(mytask.TaskList, wt)
// }
// }
// } else {
// wt := &pb.Worldtask{
// TaskId: taskId,
// TaskType: taskConf.Des,
// }
// mytask.TaskList = append(mytask.TaskList, wt)
// }
// update = map[string]interface{}{
// "taskList": mytask.TaskList,
// }
// if err := this.modelWorldtask.Change(uid, update); err != nil {
// return err
// }
// rsp := &pb.WorldtaskFinishIdsPush{
// TaskList: mytask.TaskList,
// }
return nil //session.SendMsg(string(this.GetType()), "finishids", rsp)
}