go_dreamfactory/modules/viking/api_challenge.go
2023-09-11 16:56:30 +08:00

134 lines
3.4 KiB
Go

package viking
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
// 参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.VikingChallengeReq) (errdata *pb.ErrorData) {
if req.BossId <= 0 && req.Difficulty > 0 || req.Battle == nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
}
return
}
// /挑战主线关卡
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.VikingChallengeReq) (errdata *pb.ErrorData) {
var (
ps int32
viking *pb.DBViking
err error
)
if errdata = this.ChallengeCheck(session, req); errdata != nil {
return // 参数校验失败直接返回
}
if viking, err = this.module.modelViking.getVikingList(session.GetUserId()); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
}
return
}
cfgData, err := this.module.configure.GetVikingBossConfigData(req.BossId, req.Difficulty)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
pskey := req.BossId<<8 + req.Difficulty
if v1, ok := viking.Ps[pskey]; !ok || v1 == 0 {
for _, v := range cfgData.PsConsume {
if v.A == "attr" && v.T == "ps" {
ps += v.N
}
}
for _, v := range cfgData.PsMg {
if v.A == "attr" && v.T == "ps" {
ps += v.N
}
}
psAnt := &cfg.Gameatn{ // 构建一个atn 对象
A: "attr",
T: "ps",
N: ps,
}
this.module.Debugf("开始扣除:%d", ps)
if errdata = this.module.ConsumeRes(session, []*cfg.Gameatn{psAnt}, true); errdata != nil {
return
}
viking.Ps[pskey] = ps
this.module.modelViking.modifyVikingDataByObjId(session.GetUserId(), map[string]interface{}{
"ps": viking.Ps,
})
}
if req.AutoBuy { // 不够的时候自动使用
resSell := this.module.ModuleTools.GetGlobalConf().PsItem
if errdata = this.module.CheckRes(session, []*cfg.Gameatn{resSell}); errdata != nil {
return
}
m := make(map[string]int32, 0)
m[resSell.T] = resSell.N
this.module.ModuleItems.SellItem(session, m, true)
}
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-1 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_HuntingLvErr,
Title: pb.ErrorCode_HuntingLvErr.ToString(),
}
return
}
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_HuntingLvErr,
Title: pb.ErrorCode_HuntingLvErr.ToString(),
}
return
}
errdata, record := this.module.battle.CreatePveBattle(session, &pb.BattlePVEReq{
Rulesid: cfgData.BattleReadyID,
Ptype: pb.PlayType_viking,
Title: "",
Format: req.Battle,
Mformat: cfgData.Boss,
})
if errdata != nil {
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,
Tasks: record.Tasks,
},
BossId: req.BossId,
Difficulty: req.Difficulty,
})
return
}