go_dreamfactory/modules/story/api_challenge.go

87 lines
2.5 KiB
Go

package story
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"sort"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.StoryChallengeReq) (code pb.ErrorCode) {
if req.ChapterId == 0 || req.StoryId == 0 {
code = pb.ErrorCode_ReqParameterError
return
}
return
}
///挑战主线关卡
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.StoryChallengeReq) (code pb.ErrorCode, data proto.Message) {
var (
curChapter *pb.DBStory // 当前章节信息
bBranch bool // 当前挑战关卡是不是分支
)
bBranch = false
defer func() {
session.SendMsg(string(this.module.GetType()), StoryChallengeResp, &pb.StoryChallengeResp{Data: curChapter})
}()
code = this.ChallengeCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
list, err := this.module.modelStory.getStoryList(session.GetUserId())
if err != nil {
code = pb.ErrorCode_DBError
return
}
sort.SliceStable(list, func(i, j int) bool { // 倒序排序
return list[i].ChapterId > list[j].ChapterId
})
if len(list) > 0 {
curChapter = list[0] // 取第一条(第一条肯定是最新的)
}
if curChapter == nil {
code = pb.ErrorCode_StoryNotFindChapter // 没有找到主线关卡信息
return
}
// 先校验是不是分支
chaptConfig := this.module.configure.GetStoryChapter(int32(req.ChapterId)) // 根据配置文件找
if chaptConfig == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
// 根据难度找对应的配置文件
if chaptConfig.Intensity == "1" { // 这里安临时配置读取 后面会修改
con := this.module.configure.GetStoryEasyChapter(int32(req.StoryId)) // 根据配置文件找
if con != nil {
code = pb.ErrorCode_ConfigNoFound
return
}
if con.Area == 2 { //分支
bBranch = true
// 只需要校验小关ID 是不是大于当前ID就可以
if curChapter.StoryId < int32(req.StoryId) { //必须大于前置关卡才可以挑战
code = pb.ErrorCode_StoryIDFailed
return
}
}
}
// TODO 调用战斗逻辑
if bBranch {
curChapter.BranchID = append(curChapter.BranchID, int32(req.ChapterId)) // 记录分支关卡
}
if curChapter.ChapterId == int32(req.ChapterId) && curChapter.StoryId == int32(req.StoryId) {
update := map[string]interface{}{
"storyId": req.StoryId,
"branchID": curChapter.BranchID,
}
err = this.module.modelStory.modifyStoryData(session.GetUserId(), curChapter.Id, update)
}
// 发奖 (奖励数据还没配置,后续补充)
return
}