package hero import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" "go_dreamfactory/pb" ) func NewModule() core.IModule { m := new(Hero) return m } type Hero struct { modules.ModuleBase api_comp *Api_Comp configure_comp *Configure_Comp model_hero *ModelHero items comm.IItems } //模块名 func (this *Hero) GetType() core.M_Modules { return comm.SM_HeroModule } //模块初始化接口 注册用户创建角色事件 func (this *Hero) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) return } //装备组件 func (this *Hero) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) this.model_hero = this.RegisterComp(new(ModelHero)).(*ModelHero) this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } //创建新英雄 func (this *Hero) CreatHero(uid string, heroCfgId ...int32) error { return this.model_hero.createMultiHero(uid, heroCfgId...) } //消耗英雄卡 func (this *Hero) ChangeCard(uId string, heroCfgId int32, count int32) (code pb.ErrorCode) { heroes := this.GetHeroList(uId) var curList []*pb.DB_HeroData for _, v := range heroes { if heroCfgId == v.HeroID { curList = append(curList, v) } } if int32(len(curList)) < count { return pb.ErrorCode_HeroNoEnough } for _, v := range curList { err := this.model_hero.consumeOneHeroCard(uId, v.Id) if err != nil { return pb.ErrorCode_DBError } } return pb.ErrorCode_Success } //获取英雄 func (this *Hero) GetHero(uid, heroId string) (*pb.DB_HeroData, pb.ErrorCode) { hero := this.model_hero.getOneHero(uid, heroId) if hero == nil { return nil, pb.ErrorCode_HeroNoExist } return hero, pb.ErrorCode_Success } //佩戴装备 func (this *Hero) UpdateEquipment(hero *pb.DB_HeroData, equip []*pb.DB_Equipment) (code pb.ErrorCode) { equipIds := make([]string, 4) for _, v := range equip { equipIds = append(equipIds, v.Id) } return this.model_hero.setEquipment(hero.Uid, hero.Id, equipIds) } //英雄列表 func (this *Hero) GetHeroList(uid string) []*pb.DB_HeroData { list, err := this.model_hero.getHeroList(uid) if err != nil { return nil } return list } //查询英雄数量 func (this *Hero) QueryHeroAmount(uId string, heroCfgId int32) (amount uint32) { heroes := this.GetHeroList(uId) for _, v := range heroes { if v.HeroID == heroCfgId { amount++ } } return amount } func (this *Hero) QueryCardAmount(uId string, cardId int32) (amount uint32) { return 0 } // 给指定英雄加经验 func (this *Hero) AddCardExp(uid string, cardid string, exp int32) (code pb.ErrorCode) { var ( curExp int32 curLv int32 ) _hero, err := this.GetHero(uid, cardid) // 获取英雄信息 if err != 0 { code = pb.ErrorCode_HeroNoExist return } curExp = _hero.Exp curLv = _hero.Lv curExp += exp // 先把经验加上 if _data := this.configure_comp.GetHeroLv(curLv); _data != nil { for { // 死循环判断一键升级 // 校验等级达到上限 var maxLv int32 maxLv = _hero.Star * comm.HeroStarLvRatio if maxLv >= _hero.Lv && _hero.Exp >= curExp { // 设置最大经验和等级 curLv = maxLv curExp = _data.Heroexp[0].N break } if _data.Heroexp[0].N <= curExp { // 升级操作 curExp = _data.Heroexp[0].N if curExp >= 0 { // 大于下一级经验 curLv += 1 // 经验够了 那么等级+1 if _data := this.configure_comp.GetHeroLv(curLv); _data != nil { if _data.Heroexp[0].N > curExp { // 经验不足则 直接返回 break } } else { break } } } else { break } } } _hero.Lv = curLv _hero.Exp = curExp // 校验是否达到最大等级 this.ModifyHero(_hero) // 修改英雄数据 return } // 删除指定卡牌 func (this *Hero) DelCard(cardid string, amount int32) (code pb.ErrorCode) { return }