424 lines
11 KiB
Go
424 lines
11 KiB
Go
package worldtask
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/base"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"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()
|
|
event.RegisterGO(comm.EventBuriedComplete, this.TCondFinishNotify)
|
|
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) TCondFinishNotify(uid string, conids []int32) {
|
|
session, ok := this.GetUserSession(uid)
|
|
if !ok {
|
|
return
|
|
}
|
|
this.Debug("世界任务完成条件通知", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "condIds", Value: conids})
|
|
|
|
// 玩家世界任务
|
|
userTask, err := this.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
this.Error("获取玩家世界任务", log.Field{Key: "uid", Value: uid})
|
|
return
|
|
}
|
|
|
|
var groupId, taskId int32
|
|
// 检索condId是否是世界任务的完成条件
|
|
finishedTaskIds := make(map[int32]int32) //达成的任务条件
|
|
for _, c := range this.worldtaskConf.GetDataList() {
|
|
for _, v := range c.Completetask {
|
|
for _, condId := range conids {
|
|
if v == condId {
|
|
//校验任务是否是当前任务
|
|
if task, ok := userTask.CurrentTask[c.Group]; ok {
|
|
if task.NpcStatus == 1 && c.Key == task.TaskId {
|
|
finishedTaskIds[c.Group] = c.Key
|
|
groupId = c.Group
|
|
taskId = c.Key
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(finishedTaskIds) == 0 {
|
|
// this.Debug("未找到通知的世界任务",
|
|
// log.Field{Key: "uid", Value: uid},
|
|
// log.Field{Key: "finishedTaskIds", Value: finishedTaskIds})
|
|
return
|
|
}
|
|
|
|
if userTask.CurrentTask == nil {
|
|
userTask.CurrentTask = make(map[int32]*pb.Worldtask)
|
|
}
|
|
|
|
wt, ok := userTask.CurrentTask[groupId]
|
|
if !ok {
|
|
wt = &pb.Worldtask{}
|
|
}
|
|
|
|
for _, condId := range conids {
|
|
if _, ok := utils.Findx(wt.CondiIds, condId); !ok {
|
|
wt.CondiIds = append(wt.CondiIds, condId)
|
|
}
|
|
}
|
|
|
|
userTask.CurrentTask[groupId] = wt
|
|
|
|
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
|
|
}
|
|
|
|
//结束任务
|
|
if curTaskConf.DeliverNpc == 0 {
|
|
this.modelWorldtask.taskFinish(session, groupId, taskId, userTask, curTaskConf)
|
|
this.modelWorldtask.taskFinishPush(session, groupId, userTask, curTaskConf)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 任务条件达成通知
|
|
func (this *Worldtask) TaskCondFinishNotify(session comm.IUserSession, condIds []int32) error {
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
if mytask == nil {
|
|
mytask = &pb.DBWorldtask{}
|
|
mytask.Uid = uid
|
|
if err := this.modelWorldtask.Add(uid, mytask); err != nil {
|
|
this.Error("添加世界任务失败", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err})
|
|
return err
|
|
}
|
|
} else if mytask.Uid == "" {
|
|
update := map[string]interface{}{
|
|
"uid": uid,
|
|
}
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
this.Error("更新世界任务失败", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err})
|
|
return err
|
|
}
|
|
}
|
|
|
|
if _, ok := utils.Findx(mytask.TaskList, taskId); ok {
|
|
this.Error("GM 任务已完成", log.Field{Key: "taskId", Value: taskId})
|
|
return comm.NewCustomError(pb.ErrorCode_WorldtaskFinihed)
|
|
}
|
|
|
|
taskConf := this.worldtaskConf.GetDataMap()[taskId]
|
|
if taskConf == nil {
|
|
return comm.NewCustomError(pb.ErrorCode_ConfigNoFound)
|
|
}
|
|
|
|
//重置taskList
|
|
mytask.TaskList = []int32{}
|
|
|
|
//遍历
|
|
if taskConf.Ontxe != 0 && taskConf.IdAfter != 0 {
|
|
for _, v := range this.worldtaskConf.GetDataList() {
|
|
if v.Group == groupId && v.Key <= taskId && v.Des == 2 {
|
|
mytask.TaskList = append(mytask.TaskList, v.Key)
|
|
}
|
|
}
|
|
} else {
|
|
mytask.TaskList = append(mytask.TaskList, taskId)
|
|
}
|
|
|
|
update := map[string]interface{}{
|
|
"taskList": mytask.TaskList,
|
|
}
|
|
|
|
//下个任务
|
|
nextTaskIds := this.modelWorldtask.findNextTasks(taskId)
|
|
|
|
if mytask.CurrentTask == nil {
|
|
mytask.CurrentTask = make(map[int32]*pb.Worldtask)
|
|
}
|
|
|
|
if len(nextTaskIds) >= 1 {
|
|
mytask.CurrentTask[groupId] = &pb.Worldtask{
|
|
TaskId: nextTaskIds[0],
|
|
TaskType: 2, //设置主线类型
|
|
}
|
|
update["currentTask"] = mytask.CurrentTask
|
|
}
|
|
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
return err
|
|
}
|
|
|
|
rsp := &pb.WorldtaskFinishIdsPush{}
|
|
|
|
return session.SendMsg(string(this.GetType()), "finishids", rsp)
|
|
}
|
|
|
|
// 通过任务ID bingo
|
|
func (this *Worldtask) JumpTaskByTaskId(session comm.IUserSession, taskId int32) error {
|
|
uid := session.GetUserId()
|
|
//查询当前世界任务数据
|
|
mytask, err := this.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 如果是空 或Uid是空 初始一条基础数据
|
|
if mytask == nil {
|
|
mytask = &pb.DBWorldtask{}
|
|
mytask.Uid = uid
|
|
if err := this.modelWorldtask.Add(uid, mytask); err != nil {
|
|
this.Error("添加世界任务失败", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err})
|
|
return err
|
|
}
|
|
} else if mytask.Uid == "" {
|
|
update := map[string]interface{}{
|
|
"uid": uid,
|
|
}
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
this.Error("更新世界任务失败", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err})
|
|
return err
|
|
}
|
|
}
|
|
|
|
//重置taskList
|
|
mytask.TaskList = []int32{}
|
|
// 判断任务ID是否已完成
|
|
// if _, ok := utils.Findx(mytask.TaskList, taskId); ok {
|
|
// this.Error("GM 世界任务已完成", log.Field{Key: "taskId", Value: taskId})
|
|
// return comm.NewCustomError(pb.ErrorCode_WorldtaskFinihed)
|
|
// }
|
|
|
|
// 获取当前bingo的任务配置
|
|
taskConf := this.worldtaskConf.GetDataMap()[taskId]
|
|
if taskConf == nil {
|
|
return comm.NewCustomError(pb.ErrorCode_ConfigNoFound)
|
|
}
|
|
|
|
// 返回所有前置任务
|
|
mytask.TaskList = this.recursionTasks(taskId)
|
|
|
|
update := map[string]interface{}{
|
|
"taskList": mytask.TaskList,
|
|
}
|
|
|
|
if mytask.CurrentTask == nil {
|
|
mytask.CurrentTask = make(map[int32]*pb.Worldtask)
|
|
}
|
|
|
|
mytask.CurrentTask[taskConf.Group] = &pb.Worldtask{
|
|
TaskId: taskId,
|
|
TaskType: taskConf.Des,
|
|
}
|
|
update["currentTask"] = mytask.CurrentTask
|
|
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
return err
|
|
}
|
|
|
|
rsp := &pb.WorldtaskFinishIdsPush{}
|
|
|
|
return session.SendMsg(string(this.GetType()), "finishids", rsp)
|
|
}
|
|
|
|
// 返回任务ID
|
|
func (this *Worldtask) GetWorldTaskBy(session comm.IUserSession, groupId int32) (taskId int32) {
|
|
uid := session.GetUserId()
|
|
mytask, err := this.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
|
|
if gwt, err := this.configure.getWorldtaskCfg(); err == nil {
|
|
for _, v := range gwt.GetDataList() {
|
|
if v.Group == groupId && v.Des == 5 {
|
|
if _, ok := utils.Findx(mytask.TaskList, v.Key); !ok {
|
|
taskId = v.Key
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if taskId == 0 {
|
|
return
|
|
}
|
|
|
|
myWorldtask, err := this.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
this.Error("获取玩家世界任务失败", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
|
|
// 当前任务配置
|
|
curTaskConf, err := this.configure.getWorldtaskById(taskId)
|
|
if err != nil || curTaskConf == nil {
|
|
return
|
|
}
|
|
|
|
if myWorldtask.CurrentTask == nil {
|
|
myWorldtask.CurrentTask = make(map[int32]*pb.Worldtask)
|
|
}
|
|
myWorldtask.CurrentTask[curTaskConf.Group] = &pb.Worldtask{
|
|
TaskId: taskId,
|
|
TaskType: curTaskConf.Des,
|
|
NpcStatus: 1,
|
|
}
|
|
|
|
update := map[string]interface{}{
|
|
"currentTask": myWorldtask.CurrentTask,
|
|
}
|
|
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
|
|
}
|
|
|
|
//判断是否要结束任务
|
|
if ((len(curTaskConf.Completetask) == 1 && curTaskConf.Completetask[0] == 0) ||
|
|
len(curTaskConf.Completetask) == 0) &&
|
|
curTaskConf.DeliverNpc == 0 {
|
|
//结束任务
|
|
this.modelWorldtask.taskFinish(session, groupId, taskId, myWorldtask, curTaskConf)
|
|
this.modelWorldtask.taskFinishPush(session, groupId, myWorldtask, curTaskConf)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (this *Worldtask) UpdateTaskStatus(uid string, taskId int32) {
|
|
myWorldtask, err := this.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
this.Error("获取玩家世界任务失败", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
|
|
curTaskConf, err := this.configure.getWorldtaskById(taskId)
|
|
if err != nil || curTaskConf == nil {
|
|
return
|
|
}
|
|
|
|
if curTaskConf.Des != 5 {
|
|
return
|
|
}
|
|
|
|
var wt *pb.Worldtask
|
|
if curTaskConf.Ontxe != 0 {
|
|
//pre task
|
|
wt = &pb.Worldtask{
|
|
TaskId: curTaskConf.Ontxe,
|
|
TaskType: curTaskConf.Des,
|
|
NpcStatus: 1,
|
|
}
|
|
} else {
|
|
wt = &pb.Worldtask{
|
|
TaskId: taskId,
|
|
TaskType: curTaskConf.Des,
|
|
NpcStatus: 1,
|
|
}
|
|
}
|
|
|
|
myWorldtask.CurrentTask[curTaskConf.Group] = wt
|
|
update := map[string]interface{}{
|
|
"currentTask": myWorldtask.CurrentTask,
|
|
}
|
|
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
|
|
}
|
|
}
|
|
|
|
func (this *Worldtask) recursionTasks(taskId int32) (taskIds []int32) {
|
|
if taskConf, ok := this.worldtaskConf.GetDataMap()[taskId]; ok {
|
|
preId := taskConf.Ontxe
|
|
for preId > 0 {
|
|
if tc, ok := this.worldtaskConf.GetDataMap()[preId]; ok {
|
|
taskIds = append(taskIds, preId)
|
|
preId = tc.Ontxe
|
|
}
|
|
}
|
|
}
|
|
return taskIds
|
|
}
|