package battle import ( "context" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/sys/db" "time" "go.mongodb.org/mongo-driver/bson/primitive" ) ///数据组件 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" // _, err = 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(), BlueCompId: "", Buleflist: make([]*pb.DBBattleFormt, len(req.Buleformat)), Tasks: make([]int32, 0), } var ( battletas *cfg.GameBattletasktestingData captain int32 masters []*pb.BattleRole ) if battletas, _ = this.module.configure.GetBattleTask(int32(req.Ptype)); battletas != nil { record.Tasks = battletas.BattletaskTestingId } if req.Format != nil { model := db.NewDBModel(comm.TableHero, time.Hour, conn) record.Redflist = make([]*pb.DBBattleFormt, 1) record.Redflist[0] = &pb.DBBattleFormt{ Leadpos: req.Format.Leadpos, Team: make([]*pb.BattleRole, len(req.Format.Format)), } //自己的英雄阵营 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 } } if req.Sysformat != nil && len(req.Sysformat) > 0 { for i, v := range req.Sysformat { if v == 0 { continue } if captain, masters, code = this.createMasterRoles(100, i, v); code != pb.ErrorCode_Success { return } record.Redflist[i].Systeam = masters if record.Redflist[i].Leadpos == -1 && captain != -1 { record.Redflist[i].Leadpos = captain } } } if req.Backupformat != nil && len(req.Backupformat) > 0 { for i, v := range req.Backupformat { if v == 0 { continue } if captain, masters, code = this.createMasterRoles(100, i, v); code != pb.ErrorCode_Success { return } record.Redflist[i].Backupteam = masters } } } else { record.Redflist = make([]*pb.DBBattleFormt, len(req.Sysformat)) for i, v := range req.Sysformat { if captain, masters, code = this.createMasterRoles(100, i, v); code != pb.ErrorCode_Success { return } record.Redflist[i] = &pb.DBBattleFormt{ Leadpos: captain, Systeam: masters, } } if req.Backupformat != nil && len(req.Backupformat) > 0 { for i, v := range req.Backupformat { if captain, masters, code = this.createMasterRoles(100, i, v); code != pb.ErrorCode_Success { return } record.Redflist[i].Backupteam = masters } } } for i, v := range req.Buleformat { if captain, masters, code = this.createMasterRoles(200, i, v); code != pb.ErrorCode_Success { return } record.Buleflist[i] = &pb.DBBattleFormt{ Leadpos: captain, Team: masters, } } return } //创建pve 战斗记录 func (this *modelBattleComp) createpve(session comm.IUserSession, conn *db.DBConn, btype pb.BattleType, req *pb.BattlePVEReq) (record *pb.DBBattleRecord, code pb.ErrorCode) { var ( heros []*pb.DBHero = make([]*pb.DBHero, 5) ) 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 != "" { heros[i] = &pb.DBHero{} if err := model.GetListObj(session.GetUserId(), v, heros[i]); err != nil { code = pb.ErrorCode_HeroNoExist return } tid := 100 + i if record.Redflist[0].Team[i], code = this.createBattleRole(heros[i], tid, i); code != pb.ErrorCode_Success { return } } else { record.Redflist[0].Team[i] = nil } } //好友的英雄阵营 for i, v := range req.Format.Friendformat { if v != "" { var ( err error ) // 获取好友英雄信息 if this.module.IsCross() { if heros[i], err = this.module.friend.UseAssistHero(session.GetUserId(), v); err != nil { this.module.Errorln(err) code = pb.ErrorCode_HeroNoExist return } } else { //获取跨服数据 heros[i] = &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}, heros[i]); err != nil { this.module.Errorln(err) } } tid := 100 + i if record.Redflist[0].Team[i], code = this.createBattleRole(heros[i], tid, i); code != pb.ErrorCode_Success { return } record.Redflist[0].Team[i].Ishelp = true } } if ok := this.checkBattlereadyCapskill(req.Format.Leadpos, heros); ok { code = pb.ErrorCode_BattleCapskillCheckFailed return } var ( captain int32 masters []*pb.BattleRole ) for i, v := range req.Mformat { if captain, masters, code = this.createMasterRoles(200, i, v); code != pb.ErrorCode_Success { return } record.Buleflist[i] = &pb.DBBattleFormt{ Leadpos: captain, Team: masters, } } return } //创建pve 战斗记录 func (this *modelBattleComp) createpvb(session comm.IUserSession, conn *db.DBConn, btype pb.BattleType, req *pb.BattlePVBReq) (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.Format)), BlueCompId: "", Buleflist: make([]*pb.DBBattleFormt, len(req.Mformat)), } for ii, v := range req.Format { heros := make([]*pb.DBHero, 5) record.Redflist[ii] = &pb.DBBattleFormt{ Leadpos: v.Leadpos, Team: make([]*pb.BattleRole, len(v.Format)), } model := db.NewDBModel(comm.TableHero, time.Hour, conn) //自己的英雄阵营 for i, v := range v.Format { if v != "" { heros[i] = &pb.DBHero{} if err := model.GetListObj(session.GetUserId(), v, heros[i]); err != nil { code = pb.ErrorCode_HeroNoExist return } tid := 100 + i if record.Redflist[ii].Team[i], code = this.createBattleRole(heros[i], tid, i); code != pb.ErrorCode_Success { return } } else { record.Redflist[ii].Team[i] = nil } } //好友的英雄阵营 for i, v := range v.Friendformat { if v != "" { var ( err error ) // 获取好友英雄信息 if this.module.IsCross() { if heros[i], err = this.module.friend.UseAssistHero(session.GetUserId(), v); err != nil { this.module.Errorln(err) code = pb.ErrorCode_HeroNoExist return } } else { //获取跨服数据 heros[i] = &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}, heros[i]); err != nil { this.module.Errorln(err) } } tid := 100 + i if record.Redflist[ii].Team[i], code = this.createBattleRole(heros[i], tid, i); code != pb.ErrorCode_Success { return } record.Redflist[ii].Team[i].Ishelp = true } } if ok := this.checkBattlereadyCapskill(v.Leadpos, heros); ok { code = pb.ErrorCode_BattleCapskillCheckFailed return } } var ( captain int32 masters []*pb.BattleRole ) for i, v := range req.Mformat { if captain, masters, code = this.createMasterRoles(200, i, v); code != pb.ErrorCode_Success { return } record.Buleflist[i] = &pb.DBBattleFormt{ Leadpos: captain, Team: masters, } } 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 } } if ok := this.checkBattlereadyCapskill(req.Redformat.Leadpos, req.Redformat.Format); ok { code = pb.ErrorCode_BattleCapskillCheckFailed return } 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 } } if ok := this.checkBattlereadyCapskill(req.Buleformat.Leadpos, req.Buleformat.Format); ok { code = pb.ErrorCode_BattleCapskillCheckFailed return } return } //创建pvp 战斗请求 func (this *modelBattleComp) creatertpvp(redmodel, bluemodel *db.DBModel, btype pb.BattleType, req *pb.BattleRTPVPReq) (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.RedCompId, Redflist: make([]*pb.DBBattleFormt, len(req.Redformat)), BlueCompId: req.BlueCompId, Buleflist: make([]*pb.DBBattleFormt, len(req.Bulefformat)), } for ii, v := range req.Redformat { heros := make([]*pb.DBHero, 5) record.Redflist[ii] = &pb.DBBattleFormt{ Leadpos: v.Leadpos, Team: make([]*pb.BattleRole, len(v.Format)), } for i, v := range v.Format { if v != "" { heros[i] = &pb.DBHero{} if err := redmodel.GetListObj(req.RedCompId, v, heros[i]); err != nil { code = pb.ErrorCode_HeroNoExist return } tid := 100 + i if record.Redflist[ii].Team[i], code = this.createBattleRole(heros[i], tid, i); code != pb.ErrorCode_Success { return } } else { record.Redflist[ii].Team[i] = nil } } if ok := this.checkBattlereadyCapskill(v.Leadpos, heros); ok { code = pb.ErrorCode_BattleCapskillCheckFailed return } } for ii, v := range req.Bulefformat { heros := make([]*pb.DBHero, 5) record.Buleflist[ii] = &pb.DBBattleFormt{ Leadpos: v.Leadpos, Team: make([]*pb.BattleRole, len(v.Format)), } for i, v := range v.Format { if v != "" { heros[i] = &pb.DBHero{} if err := bluemodel.GetListObj(req.BlueCompId, v, heros[i]); err != nil { code = pb.ErrorCode_HeroNoExist return } tid := 200 + i if record.Buleflist[ii].Team[i], code = this.createBattleRole(heros[i], tid, i); code != pb.ErrorCode_Success { return } } else { record.Buleflist[ii].Team[i] = nil } } if ok := this.checkBattlereadyCapskill(v.Leadpos, heros); ok { code = pb.ErrorCode_BattleCapskillCheckFailed return } } 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.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 } //创建怪物阵营 func (this *modelBattleComp) createMasterRoles(comp, wheel int, fid int32) (captain int32, roles []*pb.BattleRole, code pb.ErrorCode) { var ( result []*cfg.GameMonsterFormatData err error ) if result, err = this.module.configure.GetMonsterFormat(fid); err != nil { code = pb.ErrorCode_ConfigNoFound return } captain = -1 roles = make([]*pb.BattleRole, len(result)) for i, v := range result { if v != nil { if v.CaptainId == 1 { captain = int32(i) } if monst, err := this.module.configure.GetMonster(v.Monster); err != nil { code = pb.ErrorCode_ConfigNoFound } else { hero := &pb.DBHero{} if hero = this.module.ModuleHero.CreateMonster(monst.HeroId, monst.Star, v.Lv); hero == nil { this.module.Error("on found battle req data", log.Field{Key: "HeroId", Value: monst.HeroId}, ) code = pb.ErrorCode_ReqParameterError return } else { roles[i] = &pb.BattleRole{ Tid: int32(comp + wheel*10 + i), Oid: hero.Id, HeroID: hero.HeroID, Pos: int32(i), Star: hero.Star, Lv: hero.Lv, CaptainSkill: hero.CaptainSkill, NormalSkill: hero.NormalSkill, Property: hero.Property, Isboos: v.IsBoss, Monsterid: v.Monster, } for i, v := range roles[i].NormalSkill { if i == 0 { v.SkillLv = monst.Skill1 } else if i == 1 { v.SkillLv = monst.Skill2 } else { v.SkillLv = monst.Skill3 } } if monst.Equip4 != 0 { if suit, err := this.module.configure.Getequipsuit(monst.Equip4); err != nil { code = pb.ErrorCode_ConfigNoFound return } else { roles[i].MainSuitSkill = suit.Skill } } if monst.Equip2 != 0 { if suit, err := this.module.configure.Getequipsuit(monst.Equip2); err != nil { code = pb.ErrorCode_ConfigNoFound return } else { roles[i].SubSuitSkill = suit.Skill } } roles[i].Property[comm.Hp] = int32(float32(roles[i].Property[comm.Hp]) * v.Hppro) roles[i].Property[comm.Atk] = int32(float32(roles[i].Property[comm.Atk]) * v.Atkpro) roles[i].Property[comm.Def] = int32(float32(roles[i].Property[comm.Def]) * v.Defpro) roles[i].Property[comm.Speed] = monst.Speed } } } } return } //校验队长技是否激活 暂时不启用 func (this *modelBattleComp) checkBattlereadyCapskill(leadpos int32, heros []*pb.DBHero) (ok bool) { // if leadpos < 0 || leadpos > 5 { ok = true return // } var ( conf []*cfg.GameHeroData caprule *cfg.GameBattleready_capskillData err error ) if heros[leadpos] == nil { ok = true return } conf = make([]*cfg.GameHeroData, len(heros)) for i, v := range heros { if v != nil { conf[i] = this.module.configure.GetHeroConfig(v.HeroID) } } if caprule, err = this.module.configure.GetBattlereadyCapskill(conf[leadpos].Capskillrule); err != nil { this.module.Errorln(err) ok = false return } if len(caprule.Lv) == 2 { //等级校验 n := 0 for _, v := range heros { if v != nil && v.Lv >= caprule.Lv[0] { n++ } } if n < int(caprule.Lv[1]) { ok = false } } if len(caprule.Start) == 2 { //星级校验 n := 0 for _, v := range heros { if v != nil && v.Star >= caprule.Start[0] { n++ } } if n < int(caprule.Start[1]) { ok = false } } if len(caprule.Race) == 2 { //阵营校验 n := 0 for _, v := range conf { if v != nil && v.Race == caprule.Race[0] { n++ } } if n < int(caprule.Race[1]) { ok = false } } if len(caprule.Heroid) > 0 { //阵营校验 n := 0 for _, v := range conf { if v != nil && v.Race == caprule.Race[0] { n++ } } if n < int(caprule.Race[1]) { ok = false } } return }