go_dreamfactory/modules/arena/api_challenge.go
2023-03-27 15:47:17 +08:00

131 lines
3.7 KiB
Go

package arena
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) ChallengeCheck(session comm.IUserSession, req *pb.ArenaChallengeReq) (code pb.ErrorCode) {
if (!req.Isai && req.Playerid == "") || (req.Isai && req.MformatId == 0) || req.Battle.Format == nil || len(req.Battle.Format) != 5 {
code = pb.ErrorCode_ReqParameterError
}
return
}
///挑战
func (this *apiComp) Challenge(session comm.IUserSession, req *pb.ArenaChallengeReq) (code pb.ErrorCode, data proto.Message) {
var (
red *pb.DBArenaUser
bule *pb.ArenaPlayer
heros []*pb.DBHero
record *pb.DBBattleRecord
cd pb.ErrorCode
change bool
err error
)
defer func() {
if cd == pb.ErrorCode_Success {
session.SendMsg(string(this.module.GetType()), "challenge", &pb.ArenaChallengeResp{Code: cd, Info: &pb.BattleInfo{
Id: record.Id,
Title: record.Title,
Btype: record.Btype,
Ptype: record.Ptype,
RedCompId: record.RedCompId,
Redflist: record.Redflist,
BlueCompId: record.BlueCompId,
Buleflist: record.Buleflist,
Tasks: record.Tasks,
}})
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype130, 1)
go this.module.ModuleRtask.TriggerTask(session.GetUserId(), comm.GettaskParam(comm.Rtype130, 1))
} else {
session.SendMsg(string(this.module.GetType()), "challenge", &pb.ArenaChallengeResp{Code: cd, Info: nil})
}
}()
if cd = this.ChallengeCheck(session, req); cd != pb.ErrorCode_Success {
return
}
if red, err = this.module.modelArena.queryPlayerInfo(session.GetUserId()); err != nil {
cd = pb.ErrorCode_DBError
return
}
this.module.modelArena.recoverTicket(session, red)
if cd = this.module.ConsumeRes(session, []*cfg.Gameatn{this.module.configure.GetGlobalConf().ArenaTicketCos}, true); cd != pb.ErrorCode_Success {
return
}
// if red.Ticket > this.module.configure.GetGlobalConf().ArenaTicketCos {
// red.Ticket -= this.module.configure.GetGlobalConf().ArenaTicketCos
// } else {
// code = pb.ErrorCode_ArenaTicketNotEnough
// return
// }
if red.Attack != nil {
for i, v := range req.Battle.Format {
if red.Attack.Formt[i].Id != v {
change = true
}
}
} else {
change = true
}
if red.Defend == nil || change {
if heros, err = this.module.modelArena.queryUserHeros(session.GetUserId(), req.Battle.Format); err != nil {
code = pb.ErrorCode_DBError
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 {
cd = pb.ErrorCode_DBError
return
}
} else {
if bule, err = this.module.modelArena.getAI(req.MformatId); err != nil {
cd = pb.ErrorCode_DBError
return
}
}
redplay := &pb.ArenaPlayer{
Uid: red.Uid,
Integral: red.Integral,
Dan: red.Dan,
}
this.module.modelArena.integralCompute(redplay, &pb.ArenaPlayer{
Uid: bule.Uid,
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 {
code = pb.ErrorCode_DBError
return
}
cd, record = this.module.battle.CreatePvpBattle(session, &pb.BattlePVPReq{
Ptype: pb.PlayType_arena,
Redformat: &pb.PVPFormation{Uid: red.Uid, Leadpos: red.Attack.Leadpos, Format: red.Attack.Formt},
Buleformat: &pb.PVPFormation{Uid: bule.Uid, Leadpos: bule.Defend.Leadpos, Format: bule.Defend.Formt},
})
return
}