go_dreamfactory/modules/arena/api_challenge.go
2024-02-21 16:14:10 +08:00

175 lines
4.6 KiB
Go

package arena
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
// 参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.ArenaChallengeReq) (errdata *pb.ErrorData) {
if (!req.Isai && req.Playerid == "") || (req.Isai && req.MformatId == 0) || req.Battle == nil || req.Battle.Format == nil || len(req.Battle.Format) != 5 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
for _, v := range req.Battle.Format {
if v != "" {
return
}
}
errdata = &pb.ErrorData{ //没有英雄
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
Message: "no hero",
}
return
}
// /挑战
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.ArenaChallengeReq) (errdata *pb.ErrorData) {
var (
red *pb.DBArenaUser
bule *pb.ArenaPlayer
heros []*pb.DBHero
record *pb.DBBattleRecord
// change bool
isopen bool
ok bool
err error
)
if errdata = this.ChallengeCheck(session, req); errdata != nil {
return
}
if isopen = this.module.modelArena.isInMaintenance(); isopen {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ArenaInMaintenance,
Message: "InMaintenance!",
}
return
}
if red, err = this.module.modelArena.queryPlayerInfo(session.GetUserId()); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
this.module.modelArena.recoverTicket(session, red)
if errdata = this.module.ConsumeRes(session, []*cfg.Gameatn{this.module.ModuleTools.GetGlobalConf().ArenaTicketCos}, true); errdata != nil {
return
}
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
this.module.WriteUserLog(session.GetUserId(), req, comm.GMResDelType, "ArenaChallengeReq", this.module.ModuleTools.GetGlobalConf().ArenaTicketCos) // 消耗资源
})
// if red.Defend == nil || change {
if heros, err = this.module.modelArena.queryUserHeros(session.GetUserId(), req.Battle.Format); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
ok = false
for _, v := range heros {
if v != nil && v.Id != "" {
ok = true
}
}
if !ok {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: "no found any hero",
}
return
}
if red.Defend == nil {
red.Defend = &pb.DBPlayerBattleFormt{
Leadpos: req.Battle.Leadpos,
Formt: heros,
}
red.Isdef = true
}
// if change {
red.Attack = &pb.DBPlayerBattleFormt{
Leadpos: req.Battle.Leadpos,
Formt: heros,
}
// }
// }
if !req.Isai {
if bule, err = this.module.modelArena.queryArenaPlayer(req.Playerid); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
} else {
if bule, err = this.module.modelArena.getAI(req.MformatId); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
bule.Integral = req.Aiintegral
bule.Dan = red.Dan
}
redplay := &pb.ArenaPlayer{
Uinfo: red.Uinfo,
Integral: red.Integral,
Dan: red.Dan,
}
this.module.modelArena.integralCompute(redplay, &pb.ArenaPlayer{
Uinfo: bule.Uinfo,
Integral: bule.Integral,
Dan: bule.Dan,
}, false)
red.Integral = redplay.Integral
red.Prededuction = -1 * redplay.Changeintegral
if err = this.module.modelArena.updateArenaUserInfo(red); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
if errdata, record = this.module.battle.CreatePvpBattle(session, &pb.BattlePVPReq{
Rulesid: 105,
Ptype: pb.PlayType_arena,
Redformat: &pb.PVPFormation{Uid: red.Uinfo.Uid, Leadpos: red.Attack.Leadpos, Format: red.Attack.Formt},
Buleformat: &pb.PVPFormation{Uid: bule.Uinfo.Uid, Leadpos: bule.Defend.Leadpos, Format: bule.Defend.Formt},
}); errdata != nil {
return
}
session.SendMsg(string(this.module.GetType()), "challenge", &pb.ArenaChallengeResp{Info: &pb.BattleInfo{
Id: record.Id,
Title: record.Title,
Rulesid: 105,
Btype: record.Btype,
Ptype: record.Ptype,
RedCompId: record.RedCompId,
Redflist: record.Redflist,
BlueCompId: record.BlueCompId,
Buleflist: record.Buleflist,
Tasks: record.Tasks,
}})
return
}