108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package mainline
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/pb"
|
|
"sort"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.MainlineChallengeReq) (code pb.ErrorCode) {
|
|
if req.ChapterId == 0 || req.MainlineId == 0 {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
///挑战主线关卡
|
|
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.MainlineChallengeReq) (code pb.ErrorCode, data proto.Message) {
|
|
var (
|
|
curChapter *pb.DBMainline // 当前章节信息
|
|
bBranch bool // 当前挑战关卡是不是分支
|
|
)
|
|
bBranch = false
|
|
code = this.ChallengeCheck(session, req)
|
|
if code != pb.ErrorCode_Success {
|
|
return // 参数校验失败直接返回
|
|
}
|
|
list, err := this.module.modelMainline.getMainlineList(session.GetUserId())
|
|
if err != nil && err != redis.RedisNil {
|
|
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] // 取第一条(第一条肯定是最新的)
|
|
}
|
|
|
|
// 先校验是不是分支
|
|
chaptConfig := this.module.configure.GetMainlineChapter(int32(req.ChapterId)) // 根据配置文件找
|
|
if chaptConfig == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
if curChapter == nil {
|
|
if len(chaptConfig.Episode) <= 0 {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
if int32(req.ChapterId) != 1 {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
_data := &pb.DBMainline{}
|
|
_data.Id = primitive.NewObjectID().Hex()
|
|
_data.ChapterId = int32(req.ChapterId)
|
|
_mData := make(map[string]interface{}, 0)
|
|
_data.Uid = session.GetUserId()
|
|
_mData[_data.Id] = _data
|
|
|
|
this.module.modelMainline.addNewChapter(session.GetUserId(), _mData)
|
|
curChapter = _data
|
|
//curChapter.MainlineId = chaptConfig.Fubendata[0] // 第一次挑战
|
|
}
|
|
// 根据难度找对应的配置文件
|
|
if chaptConfig.Intensity == "1" { // 这里按临时配置读取 后面会修改
|
|
con := this.module.configure.GetMainlineEasyChapter(int32(req.MainlineId)) // 根据配置文件找
|
|
if con == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
if con.Route == 2 { //分支
|
|
bBranch = true
|
|
// 只需要校验小关ID 是不是大于当前ID就可以
|
|
if curChapter.MainlineId < int32(req.MainlineId) { //必须大于前置关卡才可以挑战
|
|
code = pb.ErrorCode_MainlineIDFailed
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// TODO 调用战斗逻辑
|
|
// 挑战成功
|
|
curChapter.MainlineId += 1 // 临时数据 后面配置表完善查找
|
|
if bBranch {
|
|
curChapter.BranchID = append(curChapter.BranchID, int32(req.ChapterId)) // 记录分支关卡
|
|
}
|
|
if curChapter.ChapterId == int32(req.ChapterId) && curChapter.MainlineId == int32(req.MainlineId) {
|
|
update := map[string]interface{}{
|
|
"mainlineId": req.MainlineId,
|
|
"ChapterId": req.ChapterId,
|
|
"branchID": curChapter.BranchID,
|
|
}
|
|
err = this.module.modelMainline.modifyMainlineData(session.GetUserId(), curChapter.Id, update)
|
|
} else {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
// 发奖 (奖励数据还没配置,后续补充)
|
|
session.SendMsg(string(this.module.GetType()), MainlineChallengeResp, &pb.MainlineChallengeResp{Data: curChapter})
|
|
return
|
|
}
|