344 lines
11 KiB
Go
344 lines
11 KiB
Go
package battle
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
///数据组件
|
|
type modelBattleComp struct {
|
|
modules.MCompModel
|
|
module *Battle
|
|
}
|
|
|
|
//组件初始化接口
|
|
func (this *modelBattleComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableForum
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*Battle)
|
|
this.TableName = "battle"
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "heroid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
//查询战斗记录
|
|
func (this *modelBattleComp) queryrecord(oid string) (record *pb.DBBattleRecord, err error) {
|
|
record = &pb.DBBattleRecord{}
|
|
if err = this.Get(oid, record); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//创建pve 战斗记录
|
|
func (this *modelBattleComp) createeve(session comm.IUserSession, conn *db.DBConn, btype pb.BattleType, req *pb.BattleEVEReq) (record *pb.DBBattleRecord, code pb.ErrorCode) {
|
|
record = &pb.DBBattleRecord{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Title: req.Title,
|
|
Btype: btype,
|
|
Ptype: req.Ptype,
|
|
State: pb.BBattleState_in,
|
|
RedCompId: session.GetUserId(),
|
|
Redflist: make([]*pb.DBBattleFormt, len(req.Redformat)),
|
|
BlueCompId: "",
|
|
Buleflist: make([]*pb.DBBattleFormt, len(req.Buleformat)),
|
|
}
|
|
|
|
for i, v := range req.Redformat {
|
|
if mf, err := this.module.configure.GetMonsterFormat(v); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
record.Buleflist[i] = &pb.DBBattleFormt{
|
|
Leadpos: mf.CaptainId,
|
|
Team: make([]*pb.BattleRole, len(mf.MonsterList)),
|
|
}
|
|
for i1, v1 := range mf.MonsterList {
|
|
if v1 == -1 {
|
|
record.Buleflist[i].Team[i1] = nil
|
|
} else {
|
|
if monst, err := this.module.configure.GetMonster(v1); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
} else {
|
|
hero := &pb.DBHero{}
|
|
if hero = this.module.ModuleHero.CreateMonster(monst.HeroId, monst.Star, mf.Lv); hero == nil {
|
|
log.Error("on found battle req data", log.Field{Key: "HeroId", Value: monst.HeroId})
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
} else {
|
|
record.Buleflist[i].Team[i1] = &pb.BattleRole{
|
|
Tid: int32(200 + i*10 + i1),
|
|
Oid: hero.Id,
|
|
HeroID: hero.HeroID,
|
|
Pos: int32(i1),
|
|
Star: hero.Star,
|
|
Lv: hero.Lv,
|
|
CaptainSkill: hero.CaptainSkill,
|
|
NormalSkill: hero.NormalSkill,
|
|
Property: hero.Property,
|
|
}
|
|
if monst.Equip4 != 0 {
|
|
if suit, err := this.module.configure.Getequipsuit(monst.Equip4); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
record.Buleflist[i].Team[i1].MainSuitSkill = suit.Skill
|
|
}
|
|
}
|
|
if monst.Equip2 != 0 {
|
|
if suit, err := this.module.configure.Getequipsuit(monst.Equip2); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
record.Buleflist[i].Team[i1].SubSuitSkill = suit.Skill
|
|
}
|
|
}
|
|
record.Buleflist[i].Team[i1].Property[comm.Hp] = int32(float32(record.Buleflist[i].Team[i1].Property[comm.Hp]) * mf.Hppro)
|
|
record.Buleflist[i].Team[i1].Property[comm.Atk] = int32(float32(record.Buleflist[i].Team[i1].Property[comm.Atk]) * mf.Atkpro)
|
|
record.Buleflist[i].Team[i1].Property[comm.Def] = int32(float32(record.Buleflist[i].Team[i1].Property[comm.Def]) * mf.Defpro)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//创建pve 战斗记录
|
|
func (this *modelBattleComp) createpve(session comm.IUserSession, conn *db.DBConn, btype pb.BattleType, req *pb.BattlePVEReq) (record *pb.DBBattleRecord, code pb.ErrorCode) {
|
|
record = &pb.DBBattleRecord{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Title: req.Title,
|
|
Btype: btype,
|
|
Ptype: req.Ptype,
|
|
State: pb.BBattleState_in,
|
|
RedCompId: session.GetUserId(),
|
|
Redflist: make([]*pb.DBBattleFormt, 1),
|
|
BlueCompId: "",
|
|
Buleflist: make([]*pb.DBBattleFormt, len(req.Mformat)),
|
|
}
|
|
record.Redflist[0] = &pb.DBBattleFormt{
|
|
Leadpos: req.Format.Leadpos,
|
|
Team: make([]*pb.BattleRole, len(req.Format.Format)),
|
|
}
|
|
model := db.NewDBModel(comm.TableHero, time.Hour, conn)
|
|
//自己的英雄阵营
|
|
for i, v := range req.Format.Format {
|
|
if v != "" {
|
|
hero := &pb.DBHero{}
|
|
if err := model.GetListObj(session.GetUserId(), v, hero); err != nil {
|
|
code = pb.ErrorCode_HeroNoExist
|
|
return
|
|
}
|
|
tid := 100 + i
|
|
if record.Redflist[0].Team[i], code = this.createBattleRole(hero, tid, i); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
} else {
|
|
record.Redflist[0].Team[i] = nil
|
|
}
|
|
}
|
|
//好友的英雄阵营
|
|
for i, v := range req.Format.Friendformat {
|
|
if v != "" {
|
|
var (
|
|
hero *pb.DBHero
|
|
err error
|
|
)
|
|
// 获取好友英雄信息
|
|
if this.module.IsCross() {
|
|
if hero, err = this.module.friend.UseAssistHero(session.GetUserId(), v); err != nil {
|
|
this.module.Errorln(err)
|
|
code = pb.ErrorCode_HeroNoExist
|
|
return
|
|
}
|
|
} else { //获取跨服数据
|
|
hero = &pb.DBHero{}
|
|
if err = this.module.service.AcrossClusterRpcCall(
|
|
context.Background(),
|
|
this.module.GetCrossTag(),
|
|
comm.Service_Worker,
|
|
string(comm.Rpc_ModuleFriendUseAssitHero),
|
|
pb.RPCGeneralReqA2{Param1: session.GetUserId(), Param2: v},
|
|
hero); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
}
|
|
tid := 100 + i
|
|
if record.Redflist[0].Team[i], code = this.createBattleRole(hero, tid, i); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
record.Redflist[0].Team[i].Ishelp = true
|
|
}
|
|
}
|
|
|
|
for i, v := range req.Mformat {
|
|
if mf, err := this.module.configure.GetMonsterFormat(v); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
record.Buleflist[i] = &pb.DBBattleFormt{
|
|
Leadpos: mf.CaptainId,
|
|
Team: make([]*pb.BattleRole, len(mf.MonsterList)),
|
|
}
|
|
for i1, v1 := range mf.MonsterList {
|
|
if v1 == -1 {
|
|
record.Buleflist[i].Team[i1] = nil
|
|
} else {
|
|
if monst, err := this.module.configure.GetMonster(v1); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
} else {
|
|
hero := &pb.DBHero{}
|
|
if hero = this.module.ModuleHero.CreateMonster(monst.HeroId, monst.Star, mf.Lv); hero == nil {
|
|
log.Error("on found battle req data", log.Field{Key: "HeroId", Value: monst.HeroId})
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
} else {
|
|
record.Buleflist[i].Team[i1] = &pb.BattleRole{
|
|
Tid: int32(200 + i*10 + i1),
|
|
Oid: hero.Id,
|
|
HeroID: hero.HeroID,
|
|
Pos: int32(i1),
|
|
Star: hero.Star,
|
|
Lv: hero.Lv,
|
|
CaptainSkill: hero.CaptainSkill,
|
|
NormalSkill: hero.NormalSkill,
|
|
Property: hero.Property,
|
|
}
|
|
if monst.Equip4 != 0 {
|
|
if suit, err := this.module.configure.Getequipsuit(monst.Equip4); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
record.Buleflist[i].Team[i1].MainSuitSkill = suit.Skill
|
|
}
|
|
}
|
|
if monst.Equip2 != 0 {
|
|
if suit, err := this.module.configure.Getequipsuit(monst.Equip2); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
record.Buleflist[i].Team[i1].SubSuitSkill = suit.Skill
|
|
}
|
|
}
|
|
record.Buleflist[i].Team[i1].Property[comm.Hp] = int32(float32(record.Buleflist[i].Team[i1].Property[comm.Hp]) * mf.Hppro)
|
|
record.Buleflist[i].Team[i1].Property[comm.Atk] = int32(float32(record.Buleflist[i].Team[i1].Property[comm.Atk]) * mf.Atkpro)
|
|
record.Buleflist[i].Team[i1].Property[comm.Def] = int32(float32(record.Buleflist[i].Team[i1].Property[comm.Def]) * mf.Defpro)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//创建pvp 战斗请求
|
|
func (this *modelBattleComp) createpvp(session comm.IUserSession, conn *db.DBConn, btype pb.BattleType, req *pb.BattlePVPReq) (record *pb.DBBattleRecord, code pb.ErrorCode) {
|
|
record = &pb.DBBattleRecord{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Title: req.Title,
|
|
Btype: btype,
|
|
Ptype: req.Ptype,
|
|
State: pb.BBattleState_in,
|
|
RedCompId: req.Redformat.Uid,
|
|
Redflist: make([]*pb.DBBattleFormt, 1),
|
|
BlueCompId: req.Buleformat.Uid,
|
|
Buleflist: make([]*pb.DBBattleFormt, 1),
|
|
}
|
|
record.Redflist[0] = &pb.DBBattleFormt{
|
|
Leadpos: req.Redformat.Leadpos,
|
|
Team: make([]*pb.BattleRole, len(req.Redformat.Format)),
|
|
}
|
|
//自己的英雄阵营
|
|
for i, v := range req.Redformat.Format {
|
|
if v != nil {
|
|
tid := 100 + i
|
|
if record.Redflist[0].Team[i], code = this.createBattleRole(v, tid, i); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
} else {
|
|
record.Redflist[0].Team[i] = nil
|
|
}
|
|
}
|
|
record.Buleflist[0] = &pb.DBBattleFormt{
|
|
Leadpos: req.Buleformat.Leadpos,
|
|
Team: make([]*pb.BattleRole, len(req.Buleformat.Format)),
|
|
}
|
|
//自己的英雄阵营
|
|
for i, v := range req.Buleformat.Format {
|
|
if v != nil {
|
|
tid := 200 + i
|
|
if record.Buleflist[0].Team[i], code = this.createBattleRole(v, tid, i); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
} else {
|
|
record.Buleflist[0].Team[i] = nil
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *modelBattleComp) createBattleRole(hero *pb.DBHero, tid, pos int) (role *pb.BattleRole, code pb.ErrorCode) {
|
|
role = &pb.BattleRole{
|
|
Tid: int32(tid),
|
|
Oid: hero.Id,
|
|
HeroID: hero.HeroID,
|
|
Pos: int32(pos),
|
|
Star: hero.Star,
|
|
Lv: hero.Lv,
|
|
CaptainSkill: hero.CaptainSkill,
|
|
NormalSkill: hero.NormalSkill,
|
|
EquipSkill: hero.EquipSkill,
|
|
Property: make(map[string]int32),
|
|
}
|
|
for k, v := range hero.Property {
|
|
role.Property[k] += v
|
|
}
|
|
for k, v := range hero.AddProperty {
|
|
role.Property[k] += v
|
|
}
|
|
for k, v := range hero.Energy {
|
|
role.Property[k] += v
|
|
}
|
|
for k, v := range hero.EnergyProperty {
|
|
role.Property[k] += v
|
|
}
|
|
for k, v := range hero.JuexProperty {
|
|
role.Property[k] += v
|
|
}
|
|
for k, v := range hero.HoroscopeProperty {
|
|
role.Property[k] += v
|
|
}
|
|
if hero.SuiteId != 0 { //主套装
|
|
if suit, err := this.module.configure.Getequipsuit(hero.SuiteId); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
role.MainSuitSkill = suit.Skill
|
|
}
|
|
}
|
|
if hero.SuiteExtId != 0 { //副套装
|
|
if suit, err := this.module.configure.Getequipsuit(hero.SuiteExtId); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
} else {
|
|
role.SubSuitSkill = suit.Skill
|
|
}
|
|
}
|
|
return
|
|
}
|