go_dreamfactory/modules/mainline/api_challenge.go
2023-07-26 20:11:09 +08:00

105 lines
2.7 KiB
Go

package mainline
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
// 参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.MainlineChallengeReq) (errdata *pb.ErrorData) {
if req.Level == 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
// /挑战主线关卡
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.MainlineChallengeReq) (errdata *pb.ErrorData) {
var (
info *pb.DBMainline // 当前章节信息
ps int32 // 消耗的体力
psAnt *cfg.Gameatn
conf *cfg.GameMainStageData // 当前章节数据
err error
)
if errdata = this.ChallengeCheck(session, req); errdata != nil {
return // 参数校验失败直接返回
}
if conf, err = this.module.configure.GetMainStageConf(req.Level); err != nil { // 配置文件校验
errdata = &pb.ErrorData{
Code: pb.ErrorCode_MainlineNotFindChapter,
Title: pb.ErrorCode_MainlineNotFindChapter.ToString(),
Message: err.Error(),
}
return
}
if info, err = this.module.modelMline.getMainlineData(session.GetUserId()); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
}
if v1, ok := info.Ps[req.Level]; !ok || v1 == 0 {
for _, v := range conf.PsConsume {
if v.A == "attr" && v.T == "ps" {
ps += v.N
}
}
for _, v := range conf.PsMg {
if v.A == "attr" && v.T == "ps" {
ps += v.N
}
}
psAnt = &cfg.Gameatn{ // 构建一个atn 对象
A: "attr",
T: "ps",
N: ps,
}
if errdata = this.module.ConsumeRes(session, []*cfg.Gameatn{psAnt}, true); errdata != nil {
return
}
info.Ps[req.Level] = ps
if err = this.module.modelMline.updateMainlineData(session.GetUserId(), info); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
}
errdata, record := this.module.battle.CreatePveBattle(session, &pb.BattlePVEReq{
Ptype: pb.PlayType_mainline,
Title: "",
Format: req.Battle,
Mformat: conf.FormatList,
})
if errdata != nil {
return
}
session.SendMsg(string(this.module.GetType()), "challenge", &pb.MainlineChallengeResp{
Info: &pb.BattleInfo{
Id: record.Id,
Title: record.Title,
Rulesid: conf.BattleReadyID,
Btype: record.Btype,
Ptype: record.Ptype,
RedCompId: record.RedCompId,
Redflist: record.Redflist,
BlueCompId: record.BlueCompId,
Buleflist: record.Buleflist,
},
Level: req.Level,
})
return
}