package arena import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/modules" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" ) var _ comm.IArena = (*Arena)(nil) const moduleName = "竞技场" /* 模块名:竞技场 描述:pvp相关业务 开发:李伟 */ func NewModule() core.IModule { m := new(Arena) return m } type Arena struct { modules.ModuleBase service base.IRPCXService mail comm.Imail battle comm.IBattle privilege comm.IPrivilege api *apiComp configure *configureComp modelArena *modelArena modelRank *modelRank } //模块名 func (this *Arena) GetType() core.M_Modules { return comm.ModuleArena } //模块初始化接口 注册用户创建角色事件 func (this *Arena) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(base.IRPCXService) return } func (this *Arena) Start() (err error) { err = this.ModuleBase.Start() var module core.IModule if module, err = this.service.GetModule(comm.ModuleBattle); err != nil { return } this.battle = module.(comm.IBattle) if module, err = this.service.GetModule(comm.ModuleMail); err != nil { return } this.mail = module.(comm.Imail) if module, err = this.service.GetModule(comm.ModulePrivilege); err != nil { return } this.privilege = module.(comm.IPrivilege) this.service.RegisterFunctionName(string(comm.Rpc_ModuleArenaRaceSettlement), this.Rpc_ModuleArenaRaceSettlement) this.service.RegisterFunctionName(string(comm.Rpc_ModuleArenaModifyIntegral), this.Rpc_ModuleArenaModifyIntegral) return } //装备组件 func (this *Arena) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) this.modelArena = this.RegisterComp(new(modelArena)).(*modelArena) this.modelRank = this.RegisterComp(new(modelRank)).(*modelRank) } //比赛结算 func (this *Arena) Rpc_ModuleArenaRaceSettlement(ctx context.Context, args *pb.EmptyReq, reply *pb.EmptyResp) (err error) { this.Debug("Rpc_ModuleArenaRaceSettlement", log.Field{Key: "args", Value: args.String()}, ) this.modelRank.raceSettlement() return } //修改用户积分 func (this *Arena) Rpc_ModuleArenaModifyIntegral(ctx context.Context, args *pb.RPCModifyIntegralReq, reply *pb.EmptyResp) (err error) { this.Debug("Rpc_ModuleArenaModifyIntegral", log.Field{Key: "args", Value: args.String()}) err = this.modelArena.modifyIntegral(args.Uid, args.Integral) return } //红点需求 func (this *Arena) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (result map[comm.ReddotType]*pb.ReddotItem) { result = make(map[comm.ReddotType]*pb.ReddotItem) for _, v := range rid { switch v { case comm.Reddot22102: result[comm.Reddot22102] = &pb.ReddotItem{ Rid: int32(comm.Reddot22102), } result[comm.Reddot22102].Activated = this.modelArena.reddot(session) break } } return } func (this *Arena) SetUserIntegral(session comm.IUserSession, Integral int32) (err error) { if this.IsCross() { err = this.modelArena.modifyIntegral(session.GetUserId(), Integral) } else { if _, err = this.service.AcrossClusterRpcGo( context.Background(), this.GetCrossTag(), comm.Service_Worker, string(comm.Rpc_ModuleArenaModifyIntegral), &pb.RPCModifyIntegralReq{Uid: session.GetUserId(), Integral: Integral}, nil); err != nil { this.Errorln(err) } } return } ///获取竞技场匹配目标战斗阵型数据 func (this *Arena) GetMatcheBattleRoles(uid string) (captain int32, rules []*pb.BattleRole, err error) { var ( global *cfg.GameGlobalData info *pb.DBArenaUser players []*pb.ArenaPlayer ais []*pb.ArenaPlayer errdata *pb.ErrorData ) global = this.ModuleTools.GetGlobalConf() if info, err = this.modelArena.queryPlayerInfo(uid); err != nil && err != mgo.MongodbNil { return } if err == mgo.MongodbNil { info = &pb.DBArenaUser{ Uid: uid, Integral: global.ArenaInitiaIntegral, Isdef: false, } if info.Dan, err = this.modelArena.computedan(info.Integral); err != nil { return } } if players, err = this.modelArena.matchePlayer(info.Uid, info.Dan, 1); err != nil && err != mgo.MongodbNil { return } if len(players) > 0 { captain = players[0].Defend.Leadpos if rules, errdata = this.battle.CreateRolesByHeros(players[0].Defend.Formt); errdata != nil { err = fmt.Errorf("Player CreateRolesByHeros fail:%v", errdata) return } } else { if ais, err = this.modelArena.matcheAI(info.Dan, 1); err != nil && err != mgo.MongodbNil { return } if len(ais) > 0 { captain = ais[0].Defend.Leadpos if rules, errdata = this.battle.CreateRolesByHeros(ais[0].Defend.Formt); errdata != nil { err = fmt.Errorf("AI CreateRolesByHeros fail:%v", errdata) return } } else { err = fmt.Errorf("matche fail!") } } return }