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" "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(), BlueCompId: "", Buleflist: make([]*pb.DBBattleFormt, len(req.Buleformat)), } var ( captain int32 masters []*pb.BattleRole ) 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) { 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 } } 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 } } 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 } //创建怪物阵营 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.Fields{"HeroId": 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, } 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) } } } } return }