218 lines
5.9 KiB
Go
218 lines
5.9 KiB
Go
package worldtask
|
|
|
|
import (
|
|
"fmt"
|
|
"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"
|
|
)
|
|
|
|
var _ comm.IWorldtask = (*Worldtask)(nil)
|
|
|
|
type Worldtask struct {
|
|
modules.ModuleBase
|
|
api *apiComp
|
|
service base.IRPCXService
|
|
configure *configureComp
|
|
modelWorldtask *ModelWorldtask
|
|
worldtaskConf *cfg.GameWorldTask
|
|
}
|
|
|
|
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
|
|
}
|
|
return
|
|
}
|
|
|
|
// 任务条件达成通知
|
|
func (this *Worldtask) TaskcondNotify(session comm.IUserSession, condId int32) error {
|
|
uid := session.GetUserId()
|
|
|
|
finishedTaskIds := make(map[int32]int32) //达成的任务条件
|
|
for _, c := range this.worldtaskConf.GetDataList() {
|
|
if c.Completetask != 0 && c.Completetask == condId {
|
|
finishedTaskIds[c.Group] = c.Key
|
|
}
|
|
}
|
|
|
|
if len(finishedTaskIds) == 0 {
|
|
this.Debug("没有匹配到任务世界任务", log.Fields{"uid": uid, "condId": condId})
|
|
return nil
|
|
}
|
|
|
|
log.Debug("世界任务完成通知-查找到世界任务", log.Fields{"uid": uid, "condId": condId, "params": 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.Fields{"uid": uid, "condId": condId})
|
|
return err
|
|
}
|
|
|
|
if userTask.Uid != "" {
|
|
//查找任务ID根据condId 可能会找出不同的任务
|
|
for groupId, taskId := range finishedTaskIds {
|
|
logFields := log.Fields{"uid": uid, "group": groupId, "taskId": taskId, "condId": 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
|
|
}
|
|
if taskConf != nil {
|
|
if taskConf.Des == 2 { //只有世界任务才校验前置
|
|
if !this.modelWorldtask.IsPreFinished(userTask, taskConf) {
|
|
this.Debug("世界任务前置任务未完成", logFields)
|
|
continue
|
|
}
|
|
}
|
|
nextTaskId = taskConf.IdAfter
|
|
|
|
// 判断玩家等级要求
|
|
if user.Lv < taskConf.Lock {
|
|
logFields["当前lv"] = user.Lv
|
|
logFields["期望等级"] = taskConf.Lock
|
|
this.Debug("等级不满足", logFields)
|
|
return comm.NewCustomError(pb.ErrorCode_WorldtaskLvNotEnough)
|
|
}
|
|
|
|
//完成任务
|
|
if err := this.modelWorldtask.finishTask(groupId, taskId, userTask); err != nil {
|
|
logFields["err"] = err.Error()
|
|
this.Error("世界任务完成", logFields)
|
|
return err
|
|
}
|
|
this.Debug("任务条件达成完成", logFields)
|
|
//发奖
|
|
if code := this.DispenseRes(session, taskConf.Reword, true); code != pb.ErrorCode_Success {
|
|
logFields["reward"] = taskConf.Reword
|
|
logFields["code"] = 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["err"] = err.Error()
|
|
log.Error("任务条件达成推送", logFields)
|
|
}
|
|
} 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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
if mytask.LastTaskIds == nil {
|
|
mytask.LastTaskIds = make(map[int32]*pb.Worldtask)
|
|
}
|
|
//重置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.LastTaskIds[groupId] = wt
|
|
mytask.TaskList = append(mytask.TaskList, wt)
|
|
}
|
|
|
|
}
|
|
} else {
|
|
wt := &pb.Worldtask{
|
|
TaskId: taskId,
|
|
TaskType: taskConf.Des,
|
|
}
|
|
mytask.LastTaskIds[groupId] = wt
|
|
mytask.TaskList = append(mytask.TaskList, wt)
|
|
}
|
|
update = map[string]interface{}{
|
|
"lastTaskIds": mytask.LastTaskIds,
|
|
"taskList": mytask.TaskList,
|
|
}
|
|
|
|
if err := this.modelWorldtask.Change(uid, update); err != nil {
|
|
return err
|
|
}
|
|
|
|
rsp := &pb.WorldtaskFinishIdsPush{
|
|
TaskList: mytask.TaskList,
|
|
}
|
|
|
|
return session.SendMsg(string(this.GetType()), "finishids", rsp)
|
|
}
|