go_dreamfactory/modules/hero/module.go
2022-06-28 12:03:14 +08:00

112 lines
2.6 KiB
Go

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
}
//模块名
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) GetHeroInfoByObjID(id string) (*pb.DB_HeroData, pb.ErrorCode) {
return nil, pb.ErrorCode_HeroNoExist
}
//创建新英雄
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
}