go_dreamfactory/modules/viking/api_challenge.go
2023-04-03 14:12:33 +08:00

92 lines
2.3 KiB
Go

package viking
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.VikingChallengeReq) (code pb.ErrorCode) {
if req.BossId <= 0 && req.Difficulty > 0 || req.Battle == nil {
code = pb.ErrorCode_ReqParameterError
return
}
return
}
///挑战主线关卡
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.VikingChallengeReq) (code pb.ErrorCode, data proto.Message) {
code = this.ChallengeCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
viking, err := this.module.modelViking.getVikingList(session.GetUserId())
if err != nil {
code = pb.ErrorCode_VikingBoosType
return
}
cfgData := this.module.configure.GetVikingBossConfigData(req.BossId, req.Difficulty)
if cfgData == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
if code = this.module.CheckRes(session, cfgData.PsConsume); code != pb.ErrorCode_Success {
if req.AutoBuy { // 不够的时候看是否能自动购买
var count int32
for _, v := range cfgData.PsConsume {
if v.N > 0 {
count += v.N
}
}
if code = this.module.ModuleItems.BuyUnifiedTicket(session, count); code != pb.ErrorCode_Success {
return
}
} else {
code = pb.ErrorCode_VikingMaxChallengeCount
return
}
}
if req.Difficulty == 1 && viking.Boss[req.BossId] == 0 { // 当前难度第一次打
viking.Boss[req.BossId] = 1
}
if value, ok := viking.Boss[req.BossId]; ok { // 类型校验
if value < req.Difficulty {
code = pb.ErrorCode_HuntingLvErr
return
}
} else {
code = pb.ErrorCode_HuntingLvErr
return
}
code, record := this.module.battle.CreatePveBattle(session, &pb.BattlePVEReq{
Ptype: pb.PlayType_viking,
Title: "",
Format: req.Battle,
Mformat: cfgData.Boss,
})
if code != pb.ErrorCode_Success {
return
}
session.SendMsg(string(this.module.GetType()), VikingChallengeResp, &pb.VikingChallengeResp{
Info: &pb.BattleInfo{
Id: record.Id, Title: record.Title,
Rulesid: cfgData.BattleReadyID,
Btype: record.Btype,
Ptype: record.Ptype,
RedCompId: record.RedCompId,
Redflist: record.Redflist,
BlueCompId: record.BlueCompId,
Buleflist: record.Buleflist,
},
BossId: req.BossId,
Difficulty: req.Difficulty,
})
return
}