go_dreamfactory/modules/moonfantasy/api_battle.go
2022-10-19 18:29:08 +08:00

120 lines
3.0 KiB
Go

package moonfantasy
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) BattleCheck(session comm.IUserSession, req *pb.MoonfantasyBattleReq) (code pb.ErrorCode) {
return
}
///挑战怪物请求
func (this *apiComp) Battle(session comm.IUserSession, req *pb.MoonfantasyBattleReq) (code pb.ErrorCode, data proto.Message) {
var (
boss *cfg.GameDreamlandBoosData
umfantasy *pb.DBUserMFantasy
mdata *pb.DBMoonFantasy
record *pb.DBBattleRecord
cd pb.ErrorCode
isjoin bool
err error
)
defer func() {
if cd == pb.ErrorCode_Success {
this.module.modelUserMF.Change(session.GetUserId(), map[string]interface{}{
"battleNum": umfantasy.BattleNum,
})
this.module.modelDream.Change(mdata.Id, map[string]interface{}{
"record": mdata.Record,
})
session.SendMsg(string(this.module.GetType()), "battle", &pb.MoonfantasyBattleResp{
Code: cd,
Mid: mdata.Monster,
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,
},
})
} else {
session.SendMsg(string(this.module.GetType()), "battle", &pb.MoonfantasyBattleResp{Code: cd})
}
}()
if cd = this.BattleCheck(session, req); cd != pb.ErrorCode_Success {
return
}
if mdata, err = this.module.modelDream.querymfantasy(req.Mid); err != nil {
cd = pb.ErrorCode_DBError
return
}
if mdata == nil {
cd = pb.ErrorCode_MoonfantasyHasExpired
return
}
if this.module.modelDream.checkMFantasyExpiration(mdata) { //已过期
cd = pb.ErrorCode_MoonfantasyHasExpired
return
}
for _, v := range mdata.Join {
if v.Uid == session.GetUserId() {
isjoin = true
}
}
if !isjoin {
cd = pb.ErrorCode_MoonfantasyNoJoin
return
}
if umfantasy, err = this.module.modelUserMF.queryUsermfantasy(session.GetUserId()); err != nil {
cd = pb.ErrorCode_CacheReadError
}
if umfantasy.BattleNum <= 0 {
cd = pb.ErrorCode_MoonfantasyNotEnoughbattles
}
umfantasy.BattleNum--
if boss, err = this.module.configure.GetMonsterById(mdata.Monster); err != nil {
cd = pb.ErrorCode_ConfigNoFound
return
}
if len(mdata.Join) >= int(mdata.Numup) {
cd = pb.ErrorCode_MoonfantasyJoinUp
return
}
if this.module.modelDream.checkMFantasyExpiration(mdata) { //已过期
cd = pb.ErrorCode_MoonfantasyHasExpired
return
}
if v, ok := mdata.Record[session.GetUserId()]; ok {
if v >= mdata.Unitmup {
cd = pb.ErrorCode_MoonfantasyDareUp
return
} else {
mdata.Record[session.GetUserId()]++
}
} else {
mdata.Record[session.GetUserId()] = 1
}
cd, record = this.module.battle.CreatePvbBattle(session, &pb.BattlePVEReq{
Ptype: pb.PlayType_moonfantasy,
Leadpos: req.Leadpos,
Teamids: req.Teamids,
Mformat: boss.Monsterformatid,
})
return
}