250 lines
6.6 KiB
Go
250 lines
6.6 KiB
Go
package worldtask
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// 世界任务完成
|
|
|
|
func (this *apiComp) FinishCheck(session comm.IUserSession, req *pb.WorldtaskFinishReq) (code pb.ErrorCode) {
|
|
if req.GroupId == 0 || req.TaskId == 0 {
|
|
this.module.Error("世界任务完成参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
|
|
code = pb.ErrorCode_ReqParameterError
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) Finish(session comm.IUserSession, req *pb.WorldtaskFinishReq) (code pb.ErrorCode, data proto.Message) {
|
|
if code = this.FinishCheck(session, req); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
|
|
uid := session.GetUserId()
|
|
|
|
rsp := &pb.WorldtaskFinishResp{}
|
|
|
|
// 获取用户信息
|
|
user := this.module.ModuleUser.GetUser(uid)
|
|
if user == nil {
|
|
code = pb.ErrorCode_UserSessionNobeing
|
|
return
|
|
}
|
|
|
|
// 当前任务配置
|
|
curTaskConf, err := this.module.configure.getWorldtaskById(req.TaskId)
|
|
if err != nil || curTaskConf == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
|
|
if curTaskConf.Group != req.GroupId {
|
|
code = pb.ErrorCode_WorldtaskGroupIdNosame
|
|
return
|
|
}
|
|
|
|
// 判断玩家等级要求
|
|
if user.Lv < curTaskConf.Lock {
|
|
code = pb.ErrorCode_WorldtaskLvNotEnough
|
|
return
|
|
}
|
|
|
|
// 当前玩家世界任务
|
|
userTask, err := this.module.modelWorldtask.getWorldtask(uid)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
if userTask.Uid == "" {
|
|
userTask.Uid = uid
|
|
}
|
|
|
|
// 前置任务ID 只有世界任务才校验前置
|
|
if !this.module.modelWorldtask.IsPreFinished(req.GroupId, userTask, curTaskConf) {
|
|
this.module.Debug("前置任务未完成", log.Field{Key: "uid", Value: uid}, log.Field{Key: "preTaskId", Value: curTaskConf.Ontxe}, log.Field{Key: "taskId", Value: curTaskConf.Key})
|
|
code = pb.ErrorCode_WorldtaskLastUnFinished
|
|
return
|
|
}
|
|
|
|
updateCheckCond := func(userTask *pb.DBWorldtask, nextTaskId int32) *pb.DBWorldtask {
|
|
//检查下个任务的完成条件
|
|
nextTaskConf, err := this.module.configure.getWorldtaskById(nextTaskId)
|
|
if err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return nil
|
|
}
|
|
if nextTaskConf == nil {
|
|
return nil
|
|
}
|
|
if nextTaskConf.Des == 1 || nextTaskConf.Des == 4 {
|
|
return nil
|
|
}
|
|
|
|
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 {
|
|
for _, condiId := range nextTaskConf.Completetask {
|
|
if condiId == 0 {
|
|
continue
|
|
}
|
|
nwt, ok := userTask.CurrentTask[nextTaskConf.Group]
|
|
if ok {
|
|
nwt.TaskId = nextTaskId
|
|
nwt.TaskType = nextTaskConf.Des
|
|
} else {
|
|
nwt = &pb.Worldtask{
|
|
TaskId: nextTaskId,
|
|
TaskType: nextTaskConf.Des,
|
|
}
|
|
}
|
|
if m, err := this.module.service.GetModule(comm.ModuleRtask); err == nil {
|
|
iwt, ok := m.(comm.IRtask)
|
|
if ok {
|
|
if mc := iwt.CheckCondi(uid, condiId); mc != pb.ErrorCode_Success {
|
|
this.module.Debug("任务完成条件不满足",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "taskId", Value: nextTaskId},
|
|
log.Field{Key: "condiId", Value: condiId},
|
|
)
|
|
} else {
|
|
if ok {
|
|
nwt.CondiIds = append(nwt.CondiIds, condiId)
|
|
} else {
|
|
condiIds := []int32{condiId}
|
|
nwt.CondiIds = condiIds
|
|
}
|
|
}
|
|
}
|
|
}
|
|
userTask.CurrentTask[nextTaskConf.Group] = nwt
|
|
update["currentTask"] = userTask.CurrentTask
|
|
|
|
}
|
|
}
|
|
|
|
if len(update) > 0 {
|
|
if err := this.module.modelWorldtask.Change(uid, update); err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return userTask
|
|
}
|
|
|
|
finishRsp := func() {
|
|
nextTaskIds := this.module.modelWorldtask.findNextTasks(curTaskConf.Key)
|
|
this.module.Debug("nextTaskIds", log.Field{Key: "nextTaskIds", Value: nextTaskIds})
|
|
|
|
nextTask := make(map[int32]*pb.Worldtask)
|
|
for _, next := range nextTaskIds {
|
|
ut := updateCheckCond(userTask, next)
|
|
if ut != nil {
|
|
for k, v := range ut.CurrentTask {
|
|
nextTask[k] = &pb.Worldtask{
|
|
TaskId: v.TaskId,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if err := session.SendMsg(string(this.module.GetType()), WorldtaskSubtypeFinish, rsp); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
return
|
|
}
|
|
if curTaskConf.IdAfter != 0 {
|
|
// 任务完成推送
|
|
if err := session.SendMsg(string(this.module.GetType()), WorldtaskNexttaskPush, &pb.WorldtaskNexttaskPush{
|
|
NextTask: nextTask,
|
|
}); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
return
|
|
}
|
|
} else {
|
|
// 章节完成
|
|
if _, ok := userTask.Chapters[req.GroupId]; !ok {
|
|
if userTask.Chapters == nil {
|
|
userTask.Chapters = make(map[int32]int32)
|
|
userTask.Chapters[req.GroupId] = 1 //已解锁待领取
|
|
update := map[string]interface{}{
|
|
"chapters": userTask.Chapters,
|
|
}
|
|
this.module.modelWorldtask.Change(uid, update)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
finishCall := func() {
|
|
// 完成任务
|
|
if err := this.module.modelWorldtask.finishTask(req.GroupId, req.TaskId, userTask); err != nil {
|
|
code = pb.ErrorCode_WorldtaskFinish
|
|
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: "err", Value: err.Error()},
|
|
)
|
|
return
|
|
}
|
|
// 发奖
|
|
if code = this.module.DispenseRes(session, curTaskConf.Reword, true); code != pb.ErrorCode_Success {
|
|
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: "code", Value: code},
|
|
)
|
|
}
|
|
|
|
//判断是否配置了通知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)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//判断任务是否已完成
|
|
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},
|
|
)
|
|
finishRsp()
|
|
return
|
|
}
|
|
}
|
|
|
|
finishCall()
|
|
|
|
finishRsp()
|
|
|
|
return
|
|
}
|