255 lines
7.0 KiB
Go
255 lines
7.0 KiB
Go
package worldtask
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/utils"
|
|
)
|
|
|
|
// 世界任务完成
|
|
|
|
func (this *apiComp) FinishCheck(session comm.IUserSession, req *pb.WorldtaskFinishReq) (errdata *pb.ErrorData) {
|
|
if req.GroupId == 0 || req.TaskId == 0 {
|
|
this.module.Error("世界任务完成参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) Finish(session comm.IUserSession, req *pb.WorldtaskFinishReq) (errdata *pb.ErrorData) {
|
|
if errdata = this.FinishCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
uid := session.GetUserId()
|
|
|
|
rsp := &pb.WorldtaskFinishResp{}
|
|
|
|
// 获取用户信息
|
|
user := this.module.ModuleUser.GetUser(uid)
|
|
if user == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_UserSessionNobeing,
|
|
Title: pb.ErrorCode_UserSessionNobeing.ToString(),
|
|
Datastring: uid,
|
|
}
|
|
return
|
|
}
|
|
|
|
// 当前任务配置
|
|
curTaskConf, err := this.module.configure.getWorldtaskById(req.TaskId)
|
|
if err != nil || curTaskConf == nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ConfigNoFound,
|
|
Title: pb.ErrorCode_ConfigNoFound.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if curTaskConf.Group != req.GroupId {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_WorldtaskGroupIdNosame,
|
|
Title: pb.ErrorCode_WorldtaskGroupIdNosame.ToString(),
|
|
Message: fmt.Sprintf("组ID一致,实际:%d 期望:%d", curTaskConf.Group, req.GroupId),
|
|
}
|
|
return
|
|
}
|
|
|
|
if curTaskConf.DeliverNpc == 0 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_WorldtaskNoProcess,
|
|
Title: pb.ErrorCode_WorldtaskNoProcess.ToString(),
|
|
Message: "配置DeliverNpc字段是0",
|
|
}
|
|
return
|
|
}
|
|
|
|
// 判断玩家等级要求
|
|
if user.Lv < curTaskConf.Lock {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_WorldtaskLvNotEnough,
|
|
Title: pb.ErrorCode_WorldtaskLvNotEnough.ToString(),
|
|
Message: fmt.Sprintf("实际等级:%d 期望等级:%d", user.Lv, curTaskConf.Lock),
|
|
}
|
|
return
|
|
}
|
|
|
|
// 当前玩家世界任务
|
|
userTask, err := this.module.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
if userTask.Uid == "" {
|
|
userTask.Uid = uid
|
|
}
|
|
|
|
// 前置任务ID 只有世界任务才校验前置
|
|
if !this.module.modelWorldtask.IsPreFinished(req.GroupId, userTask, curTaskConf) {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_WorldtaskLastUnFinished,
|
|
Title: pb.ErrorCode_WorldtaskLastUnFinished.ToString(),
|
|
Message: fmt.Sprintf("任务[%v]的前置任务 [%v] 未完成", curTaskConf.Key, curTaskConf.Ontxe),
|
|
}
|
|
return
|
|
}
|
|
|
|
//判断任务是否已完成
|
|
for _, v := range userTask.TaskList {
|
|
if v == req.TaskId {
|
|
this.module.Debug("任务已完成,返回下一个",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "taskId", Value: req.TaskId},
|
|
)
|
|
this.sendMsg(session, WorldtaskSubtypeFinish, rsp)
|
|
this.module.modelWorldtask.taskFinishPush(session, req.GroupId, userTask, curTaskConf)
|
|
return
|
|
}
|
|
}
|
|
|
|
var condiFlag bool
|
|
// 检查当前任务的完成条件
|
|
for _, condId := range curTaskConf.Completetask {
|
|
if v, ok := userTask.CurrentTask[req.GroupId]; ok {
|
|
if _, ok := utils.Findx(v.CondiIds, condId); !ok {
|
|
conds, err := this.module.ModuleBuried.CheckCondition(uid, condId)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ExternalModule,
|
|
Title: pb.ErrorCode_ExternalModule.ToString(),
|
|
Message: comm.NewExternalModuleErr("Buried", "CheckCondition", uid, condId).Error(),
|
|
}
|
|
return
|
|
}
|
|
for _, cond := range conds {
|
|
if cond.Conid == condId && cond.State == pb.BuriedItemFinishState_buried_finish {
|
|
condiFlag = true
|
|
}
|
|
}
|
|
} else {
|
|
condiFlag = true
|
|
}
|
|
}
|
|
}
|
|
|
|
if !condiFlag {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_RtaskCondiNoReach,
|
|
Title: pb.ErrorCode_RtaskCondiNoReach.ToString(),
|
|
}
|
|
return
|
|
}
|
|
|
|
// 完成任务
|
|
if err := this.module.modelWorldtask.finishTask(req.GroupId, req.TaskId, userTask); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_WorldtaskFinish,
|
|
Title: pb.ErrorCode_WorldtaskFinish.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
// 发奖
|
|
if errdata = this.module.DispenseRes(session, curTaskConf.Reword, true); errdata != nil {
|
|
this.module.Error("资源发放",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "groupId", Value: req.GroupId},
|
|
log.Field{Key: "taskId", Value: req.TaskId},
|
|
log.Field{Key: "reword", Value: curTaskConf.Reword},
|
|
log.Field{Key: "errdata", Value: errdata},
|
|
)
|
|
}
|
|
|
|
//判断是否配置了通知module
|
|
for _, m := range curTaskConf.Module {
|
|
i, err := this.service.GetModule(core.M_Modules(m))
|
|
if err != nil {
|
|
this.module.Errorln(err)
|
|
continue
|
|
}
|
|
|
|
if ic, ok := i.(comm.ITaskComplete); ok {
|
|
ic.TaskComplete(session, req.TaskId)
|
|
}
|
|
}
|
|
this.sendMsg(session, WorldtaskSubtypeFinish, rsp)
|
|
this.module.modelWorldtask.taskFinishPush(session, req.GroupId, userTask, curTaskConf)
|
|
return
|
|
}
|
|
|
|
// Deprecated
|
|
func (this *apiComp) updateCheckCond(uid string, userTask *pb.DBWorldtask, nextTaskId int32) (*pb.DBWorldtask, error) {
|
|
//检查下个任务的完成条件
|
|
nextTaskConf, err := this.module.configure.getWorldtaskById(nextTaskId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if nextTaskConf.Des == 1 || nextTaskConf.Des == 4 {
|
|
return nil, comm.NewCustomError(pb.ErrorCode_WorldtaskNoProcess)
|
|
}
|
|
|
|
if userTask.CurrentTask == nil {
|
|
userTask.CurrentTask = make(map[int32]*pb.Worldtask)
|
|
}
|
|
|
|
update := make(map[string]interface{})
|
|
if (len(nextTaskConf.Completetask) == 1 && nextTaskConf.Completetask[0] == 0) ||
|
|
len(nextTaskConf.Completetask) == 0 {
|
|
wt := &pb.Worldtask{
|
|
TaskId: nextTaskId,
|
|
TaskType: nextTaskConf.Des,
|
|
CondiIds: []int32{},
|
|
}
|
|
userTask.CurrentTask[nextTaskConf.Group] = wt
|
|
update["currentTask"] = userTask.CurrentTask
|
|
} else {
|
|
nwt, ok := userTask.CurrentTask[nextTaskConf.Group]
|
|
if ok {
|
|
nwt.TaskId = nextTaskId
|
|
nwt.TaskType = nextTaskConf.Des
|
|
} else {
|
|
nwt = &pb.Worldtask{
|
|
TaskId: nextTaskId,
|
|
TaskType: nextTaskConf.Des,
|
|
}
|
|
}
|
|
|
|
conds, err := this.module.ModuleBuried.CheckCondition(uid, nextTaskConf.Completetask...)
|
|
if err != nil {
|
|
this.module.Errorf("buried err:%v", err.Error())
|
|
return nil, err
|
|
}
|
|
|
|
var condIds []int32
|
|
for _, cond := range conds {
|
|
if cond.State == pb.BuriedItemFinishState_buried_finish {
|
|
condIds = append(condIds, cond.Conid)
|
|
}
|
|
}
|
|
|
|
nwt.CondiIds = condIds
|
|
userTask.CurrentTask[nextTaskConf.Group] = nwt
|
|
update["currentTask"] = userTask.CurrentTask
|
|
}
|
|
|
|
if len(update) > 0 {
|
|
if err := this.module.modelWorldtask.Change(uid, update); err != nil {
|
|
this.module.Error("DB err", log.Field{Key: "err", Value: err.Error()})
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return userTask, nil
|
|
}
|