go_dreamfactory/modules/plunder/api_challenge.go
2024-01-19 10:58:21 +08:00

108 lines
3.0 KiB
Go

package plunder
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
// 参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.PlunderChallengeReq) (errdata *pb.ErrorData) {
if req.Battle == nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
// 船准备出发
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.PlunderChallengeReq) (errdata *pb.ErrorData) {
var (
conf *cfg.GamePlunderData
err error
list *pb.DBPlunder
battleConf *cfg.GamePlunderBattleData
)
if errdata = this.ChallengeCheck(session, req); errdata != nil {
return // 参数校验失败直接返回
}
if list, err = this.module.modelPlunder.getPlunderData(session.GetUserId()); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Message: err.Error(),
}
return
}
if len(list.Source) < int(req.Index) { // 数组长度校验
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
Message: fmt.Sprintf("list.Source len:%d,req.index:%d", len(list.Source), req.Index),
}
return
}
for k := range list.Setout {
if req.Index == int32(k) {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
Message: fmt.Sprintf("list.Setout %d,", req.Pos),
}
return
}
}
if req.Pos >= int32(len(list.Line)) {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
}
if list.Line[req.Pos].Closetime == -1 { // 队列没解锁
errdata = &pb.ErrorData{
Code: pb.ErrorCode_PlunderLineUnlock,
Title: pb.ErrorCode_PlunderLineUnlock.ToString(),
}
return
}
// 配置校验
if conf, err = this.module.configure.getGamePlunderDataById(list.Source[req.Index]); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
if battleConf, err = this.module.configure.getGamePlunderBattleById(conf.Battleid); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
errdata, record := this.module.battle.CreatePveBattle(session, &pb.BattlePVEReq{
Rulesid: battleConf.BattleReadyID,
Ptype: pb.PlayType_plunder,
Title: "",
Format: req.Battle,
Mformat: battleConf.FormatList,
})
if errdata != nil {
return
}
session.SendMsg(string(this.module.GetType()), "challenge", &pb.PlunderChallengeResp{
Info: &pb.BattleInfo{Id: record.Id, Title: record.Title, Rulesid: battleConf.BattleReadyID, Btype: record.Btype, Ptype: record.Ptype, RedCompId: record.RedCompId, Redflist: record.Redflist, BlueCompId: record.BlueCompId, Buleflist: record.Buleflist, Tasks: record.Tasks},
})
return
}