go_dreamfactory/modules/hunting/api_challenge.go

141 lines
3.7 KiB
Go

package hunting
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
//参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.HuntingChallengeReq) (errdata *pb.ErrorData) {
if req.BossType <= 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.HuntingChallengeReq) (errdata *pb.ErrorData) {
var (
ps int32
)
if errdata = this.ChallengeCheck(session, req); errdata != nil {
return // 参数校验失败直接返回
}
hunting, err := this.module.modelHunting.getHuntingList(session.GetUserId())
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_PagodaNotFound,
Title: pb.ErrorCode_PagodaNotFound.ToString(),
Message: err.Error(),
}
return
}
cfgData, err := this.module.configure.GetHuntingBossConfigData(req.BossType, req.Difficulty)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
if v1, ok := hunting.Ps[req.BossType]; ok && v1 == 0 {
if errdata = this.module.ConsumeRes(session, cfgData.PsMg, true); errdata != nil { // 扣1点
return
}
} else {
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,
}
if errdata = this.module.ConsumeRes(session, []*cfg.Gameatn{psAnt}, true); errdata != nil {
return
}
hunting.Ps[req.BossType] = ps
this.module.modelHunting.modifyHuntingDataByObjId(session.GetUserId(), map[string]interface{}{
"ps": hunting.Ps,
})
}
if errdata = this.module.CheckRes(session, cfgData.PsConsume); errdata != nil {
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)
} else {
return
}
}
value, ok := hunting.Boss[req.BossType]
if !ok { // 类型校验
hunting.Boss[req.BossType] = 1
}
if value < req.Difficulty-1 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_HuntingLvErr,
Title: pb.ErrorCode_HuntingLvErr.ToString(),
Message: fmt.Sprintf("关卡难度不匹配,当前关卡:%d,请求关卡:%d", value, req.Difficulty),
}
return
}
errdata, record := this.module.battle.CreatePveBattle(session, &pb.BattlePVEReq{
Ptype: pb.PlayType_hunting,
Title: "",
Format: req.Battle,
Mformat: cfgData.Boss,
})
if errdata != nil {
return
}
session.SendMsg(string(this.module.GetType()), HuntingChallengeResp, &pb.HuntingChallengeResp{
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,
},
BossType: req.BossType,
Difficulty: req.Difficulty,
})
if user := this.module.ModuleUser.GetUser(session.GetUserId()); user != nil {
this.chat.SendSysChatToWorld(comm.ChatSystem15, nil, req.BossType, req.Difficulty, user.Name)
} else {
this.module.Errorf("no found userdata uid:%s", session.GetUserId())
}
return
}