package battle import ( "context" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "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" ) /* 模块名:战斗模块 描述:处理游侠战斗相关业务 开发:李伟 */ func NewModule() core.IModule { m := new(Battle) return m } type Battle struct { modules.ModuleBase service base.IRPCXService options *Options friend comm.IFriend //好友系统 moonfantasy comm.IMoonFantasy //月之秘境模块 pvp comm.IPvp //实时pvp api_comp *apiComp configure *configureComp modelBattle *modelBattleComp clientmgr *battleClientMgrComp //c#战斗客户端端管理 } // 模块名 func (this *Battle) GetType() core.M_Modules { return comm.ModuleBattle } func (this *Battle) NewOptions() (options core.IModuleOptions) { return new(Options) } // 模块初始化接口 注册用户创建角色事件 func (this *Battle) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { if err = this.ModuleBase.Init(service, module, options); err != nil { return } this.service = service.(base.IRPCXService) this.options = options.(*Options) return } func (this *Battle) Start() (err error) { if err = this.ModuleBase.Start(); err != nil { return } var module core.IModule if module, err = this.service.GetModule(comm.ModuleFriend); err != nil { return } this.friend = module.(comm.IFriend) if module, err = this.service.GetModule(comm.ModuleMoonfantasy); err != nil { return } this.moonfantasy = module.(comm.IMoonFantasy) if module, err = this.service.GetModule(comm.ModulePvp); err != nil { return } this.pvp = module.(comm.IPvp) return } // 装备组件 func (this *Battle) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) this.modelBattle = this.RegisterComp(new(modelBattleComp)).(*modelBattleComp) this.clientmgr = this.RegisterComp(new(battleClientMgrComp)).(*battleClientMgrComp) } // 创建pve战斗 func (this *Battle) CreateEveBattle(session comm.IUserSession, req *pb.BattleEVEReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn stag string err error ) if !this.IsCross() { conn, err = db.Local() stag = this.service.GetTag() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) stag = session.GetServiecTag() } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.createeve(session, stag, conn, pb.BattleType_eve, req, conf); errdata != nil { return } return } // 创建pve战斗 func (this *Battle) CreatePveBattle(session comm.IUserSession, req *pb.BattlePVEReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn stag string err error tasks []*pb.BuriedParam ) if !this.IsCross() { conn, err = db.Local() stag = this.service.GetTag() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) stag = session.GetServiecTag() } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if req.Format == nil || req.Format.Format == nil || len(req.Format.Format) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), Message: "Format data err!", } return } if req.Format.Friendformat != nil && len(req.Format.Friendformat) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } } var flag bool for _, v := range req.Format.Friendformat { if v != "" { flag = true tasks = append(tasks, comm.GetBuriedParam(comm.Rtype108, 1)) break } } if record, errdata = this.modelBattle.createpve(session, stag, conn, pb.BattleType_pve, req, conf); errdata != nil { return } if flag { tasks = append(tasks, comm.GetBuriedParam(comm.Rtype12, 1)) } if len(tasks) > 0 { go this.AsynHandleSession(session.Clone(), func(session comm.IUserSession) { this.ModuleBuried.TriggerBuried(session, tasks...) }) } return } // 创建连续战斗 func (this *Battle) CreateHeroPveBattle(session comm.IUserSession, req *pb.BattleHeroPVEReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn err error ) if !this.IsCross() { conn, err = db.Local() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if req.Redformat == nil || req.Redformat.Format == nil || len(req.Redformat.Format) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.createheropve(session, conn, pb.BattleType_lpev, req, conf); errdata != nil { return } return } // 创建pve战斗 func (this *Battle) CreatePvbBattle(session comm.IUserSession, req *pb.BattlePVBReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn stag string err error ) if !this.IsCross() { conn, err = db.Local() stag = this.service.GetTag() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) stag = session.GetServiecTag() } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if req.Format == nil || len(req.Format) == 0 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.createpvb(session, stag, conn, pb.BattleType_pvb, req, conf); errdata != nil { return } return } // 创建pve战斗 func (this *Battle) CreatePvpBattle(session comm.IUserSession, req *pb.BattlePVPReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn err error ) if !this.IsCross() { conn, err = db.Local() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.createpvp(session, conn, pb.BattleType_pvp, req, conf); errdata != nil { return } return } // 只有跨服环境下才可使用 func (this *Battle) CreateRtPvpBattle(req *pb.BattleRTPVPReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData redmodel *db.DBModel bluemodel *db.DBModel err error ) if !this.IsCross() { errdata = &pb.ErrorData{ Code: pb.ErrorCode_Exception, Title: pb.ErrorCode_Exception.ToString(), } return } if redmodel, err = this.GetDBModelByUid(req.RedCompId, comm.TableHero); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } return } if bluemodel, err = this.GetDBModelByUid(req.BlueCompId, comm.TableHero); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.creatertpvp(redmodel, bluemodel, pb.BattleType_rtpvp, req, conf); errdata != nil { return } return } // 创建连续战斗 func (this *Battle) CreateLPVEBattle(session comm.IUserSession, req *pb.BattleLPVEReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn stag string err error tasks []*pb.BuriedParam ) if !this.IsCross() { conn, err = db.Local() stag = this.service.GetTag() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) stag = session.GetServiecTag() } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if req.Format == nil || req.Format.Format == nil || len(req.Format.Format) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } return } if req.Format.Friendformat != nil && len(req.Format.Friendformat) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } } var flag bool for _, v := range req.Format.Friendformat { if v != "" { flag = true // this.ModuleBuried.SendToRtask(session, comm.Rtype108, 1) tasks = append(tasks, comm.GetBuriedParam(comm.Rtype108, 1)) break } } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.createlpve(session, stag, conn, pb.BattleType_lpev, req, conf); errdata != nil { return } if flag { tasks = append(tasks, comm.GetBuriedParam(comm.Rtype12, 1)) } if len(tasks) > 0 { go this.AsynHandleSession(session.Clone(), func(session comm.IUserSession) { this.ModuleBuried.TriggerBuried(session, tasks...) }) } return } // /创建角色列表 更具Format表格 func (this *Battle) CreateRolesByFormat(fid int32) (captain int32, roles []*pb.BattleRole, errdata *pb.ErrorData) { captain, roles, errdata = this.modelBattle.createMasterRoles(2, 0, fid) return } // /创建角色列表 更具英雄列表 func (this *Battle) CreateRolesByHeros(heros []*pb.DBHero) (roles []*pb.BattleRole, errdata *pb.ErrorData) { roles = make([]*pb.BattleRole, len(heros)) for i, v := range heros { if v != nil { if roles[i], errdata = this.modelBattle.createBattleRole(v, 0, 200+i, i+1); errdata != nil { return } } } return } // /获取战斗详情 func (this *Battle) GetBattleInfo(req *pb.BattleGetInfoReq) (errdata *pb.ErrorData, resp *pb.BattleGetInfoResp) { this.Debug("GetBattleInfo", log.Field{Key: "req", Value: req}) var ( ctx context.Context err error ) ctx, _ = context.WithTimeout(context.Background(), time.Second*5) if resp, err = this.clientmgr.GetInfo(ctx, req); err != nil { this.Errorln(err) errdata = &pb.ErrorData{ Code: pb.ErrorCode_RpcFuncExecutionError, Title: pb.ErrorCode_RpcFuncExecutionError.ToString(), } } return } // 创建战斗服务 func (this *Battle) CreateBattleServer(req *pb.BattleInfo) (errdata *pb.ErrorData) { this.Debug("CreateBattleServer", log.Field{Key: "req", Value: req}) var ( resp *pb.BattleCreateServerResp ctx context.Context err error ) ctx, _ = context.WithTimeout(context.Background(), time.Second*5) if resp, err = this.clientmgr.CreateBattle(ctx, &pb.BattleCreateServerReq{Info: req}); err != nil { this.Errorln(err) errdata = &pb.ErrorData{ Code: pb.ErrorCode_RpcFuncExecutionError, Title: pb.ErrorCode_RpcFuncExecutionError.ToString(), } } if !resp.Issucc { errdata = &pb.ErrorData{ Code: pb.ErrorCode_BattleCreateFailed, Title: pb.ErrorCode_BattleCreateFailed.ToString(), } } return } func (this *Battle) InCmdBattle(req *pb.BattleInCmdReq) (errdata *pb.ErrorData) { var ( resp *pb.BattleInCmdResp ctx context.Context err error ) ctx, _ = context.WithTimeout(context.Background(), time.Second*5) if resp, err = this.clientmgr.InCmdBattle(ctx, req); err != nil { this.Errorln(err) errdata = &pb.ErrorData{ Code: pb.ErrorCode_RpcFuncExecutionError, Title: pb.ErrorCode_RpcFuncExecutionError.ToString(), } } if !resp.Issucc { errdata = &pb.ErrorData{ Code: pb.ErrorCode_BattleInCmdFailed, Title: pb.ErrorCode_BattleInCmdFailed.ToString(), } } return } // pvp认输 func (this *Battle) ConcedeBattle(req *pb.BattleConcedeReq) (errdata *pb.ErrorData) { var ( resp *pb.BattleConcedeResp err error ) if resp, err = this.clientmgr.ConcedeBattle(context.Background(), req); err != nil { this.Errorln(err) errdata = &pb.ErrorData{ Code: pb.ErrorCode_RpcFuncExecutionError, Title: pb.ErrorCode_RpcFuncExecutionError.ToString(), } } if !resp.Issucc { errdata = &pb.ErrorData{ Code: pb.ErrorCode_BattleInCmdFailed, Title: pb.ErrorCode_BattleInCmdFailed.ToString(), } } return } // 校验战报是否成功 func (this *Battle) CheckBattleReport(session comm.IUserSession, report *pb.BattleReport) (errdata *pb.ErrorData, iswin bool) { var ( reply *pb.BattleCheckResults ctx context.Context err error tasks []*pb.BuriedParam ) if report == nil { iswin = false return } if this.options.OpenCheck { stime := time.Now() ctx, _ = context.WithTimeout(context.Background(), time.Second*5) if reply, err = this.clientmgr.CheckBattle(ctx, report); err != nil || !reply.Ischeck { errdata = &pb.ErrorData{ Code: pb.ErrorCode_BattleValidationFailed, Title: pb.ErrorCode_BattleValidationFailed.ToString(), } this.Error("[Battle Check]", log.Field{Key: "t", Value: time.Since(stime).Milliseconds()}, log.Field{Key: "reply", Value: reply.String()}, log.Field{Key: "err", Value: err.Error()}, ) return } this.Debug("[Battle Check]", log.Field{Key: "t", Value: time.Since(stime).Milliseconds()}, log.Field{Key: "reply", Value: reply.String()}, ) } this.moonfantasy.Trigger(session, report) for _, v := range report.Completetask { tasks = append(tasks, comm.GetBuriedParam(comm.Rtype157, int32(report.Info.Ptype), v)) } if len(tasks) > 0 { go this.AsynHandleSession(session.Clone(), func(session comm.IUserSession) { this.ModuleBuried.TriggerBuried(session, tasks...) }) } if (report.WinSide == 1 && report.Info.RedCompId == session.GetUserId()) || (report.WinSide == 2 && report.Info.BlueCompId == session.GetUserId()) { return nil, true } else { return nil, false } } // 校验战报是否成功 func (this *Battle) CheckBattleReportByTest(report *pb.BattleReport) (err error) { var ( reply *pb.BattleCheckResults ctx context.Context ) if this.options.OpenCheck { stime := time.Now() ctx, _ = context.WithTimeout(context.Background(), time.Second*5) if reply, err = this.clientmgr.CheckBattle(ctx, report); err != nil || !reply.Ischeck { return } this.Debug("[Battle Check]", log.Field{Key: "t", Value: time.Since(stime).Milliseconds()}, log.Field{Key: "reply", Value: reply.String()}, ) } return } // 创建石阵秘境战斗 func (this *Battle) CreateStoneBattle(session comm.IUserSession, stoneBattle *pb.StroneBattleReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { if record, errdata = this.modelBattle.creatStoneBattle(session, stoneBattle); errdata != nil { return } return } // 创建pve战斗 func (this *Battle) CreateDebuffBattle(session comm.IUserSession, req *pb.BattlePVEReq, webuff []int32, dibuff []int32) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn stag string err error tasks []*pb.BuriedParam ) if !this.IsCross() { conn, err = db.Local() stag = this.service.GetTag() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) stag = session.GetServiecTag() } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if req.Format == nil || req.Format.Format == nil || len(req.Format.Format) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), Message: "Format data err!", } return } if req.Format.Friendformat != nil && len(req.Format.Friendformat) != 5 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } } var flag bool for _, v := range req.Format.Friendformat { if v != "" { flag = true tasks = append(tasks, comm.GetBuriedParam(comm.Rtype108, 1)) break } } if record, errdata = this.modelBattle.createAddDebuffPve(session, stag, conn, pb.BattleType_pve, req, conf, webuff, dibuff); errdata != nil { return } if flag { tasks = append(tasks, comm.GetBuriedParam(comm.Rtype12, 1)) } if len(tasks) > 0 { go this.AsynHandleSession(session.Clone(), func(session comm.IUserSession) { this.ModuleBuried.TriggerBuried(session, tasks...) }) } return } // 炮服务端战斗 func (this *Battle) RunServerBattle(session comm.IUserSession, req *pb.BattleRunReq) (errdata *pb.ErrorData, reply *pb.BattleRunResp) { var ( ctx context.Context err error ) if this.options.OpenCheck { stime := time.Now() ctx, _ = context.WithTimeout(context.Background(), time.Second*5) if reply, err = this.clientmgr.RunBattle(ctx, req); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_BattleValidationFailed, Title: pb.ErrorCode_BattleValidationFailed.ToString(), } this.Error("[Battle Run]", log.Field{Key: "t", Value: time.Since(stime).Milliseconds()}, log.Field{Key: "reply", Value: reply.String()}, log.Field{Key: "err", Value: err.Error()}, ) return } this.Debug("[Battle Check]", log.Field{Key: "t", Value: time.Since(stime).Milliseconds()}, log.Field{Key: "reply", Value: reply.String()}, ) } else { reply = &pb.BattleRunResp{ Reports: make([]*pb.BattleReport, 0), } for _, v := range req.Info { reply.Reports = append(reply.Reports, &pb.BattleReport{ Info: v, Costtime: 10, WinSide: 1, Round: 10, }) } } return } func (this *Battle) CreatePlunderPvpBattle(session comm.IUserSession, req *pb.BattlePVEPlunderReq) (errdata *pb.ErrorData, record *pb.DBBattleRecord) { var ( conf *cfg.GameBattleReadyData conn *db.DBConn err error ) if !this.IsCross() { conn, err = db.Local() } else { conn, err = db.ServerDBConn(session.GetServiecTag()) } if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } this.Errorf("session:%v err:", session, err) return } if conf, err = this.configure.GetBattleReady(req.Rulesid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if record, errdata = this.modelBattle.createPlunderpvp(session, conn, pb.BattleType_pvp, req, conf); errdata != nil { return } return }