155 lines
3.8 KiB
Go
155 lines
3.8 KiB
Go
package hero
|
|
|
|
import (
|
|
"errors"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
)
|
|
|
|
func NewModule() core.IModule {
|
|
m := new(Hero)
|
|
return m
|
|
}
|
|
|
|
type Hero struct {
|
|
modules.ModuleBase
|
|
api *apiComp
|
|
configure *configureComp
|
|
modelHero *ModelHero
|
|
}
|
|
|
|
//模块名
|
|
func (this *Hero) GetType() core.M_Modules {
|
|
return comm.ModuleHero
|
|
}
|
|
|
|
//模块初始化接口 注册用户创建角色事件
|
|
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 = this.RegisterComp(new(apiComp)).(*apiComp)
|
|
this.modelHero = this.RegisterComp(new(ModelHero)).(*ModelHero)
|
|
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
|
|
|
|
}
|
|
func (this *Hero) Start() (err error) {
|
|
err = this.ModuleBase.Start()
|
|
event.RegisterGO(comm.EventUserOffline, this.EventUserOffline)
|
|
return
|
|
}
|
|
|
|
//创建新英雄
|
|
func (this *Hero) CreateHeroes(uid string, heroCfgId ...string) error {
|
|
return this.modelHero.createMultiHero(uid, heroCfgId...)
|
|
}
|
|
|
|
//创建叠加英雄
|
|
func (this *Hero) CreateRepeatHero(uid string, heroCfgId string, num int32) (*pb.DBHero, error) {
|
|
return this.modelHero.createHeroOverlying(uid, heroCfgId, num)
|
|
}
|
|
|
|
//获取英雄
|
|
func (this *Hero) GetHeroByObjID(uid, heroId string) (*pb.DBHero, pb.ErrorCode) {
|
|
hero := this.modelHero.getOneHero(uid, heroId)
|
|
if hero == nil {
|
|
return nil, pb.ErrorCode_HeroNoExist
|
|
}
|
|
return hero, pb.ErrorCode_Success
|
|
}
|
|
|
|
//佩戴装备
|
|
func (this *Hero) UpdateEquipment(session comm.IUserSession, hero *pb.DBHero, equip []*pb.DB_Equipment) (code pb.ErrorCode) {
|
|
|
|
if hero == nil {
|
|
code = pb.ErrorCode_HeroNoExist
|
|
return
|
|
}
|
|
|
|
list := make([]*pb.DBHero, 0)
|
|
if newHero, err := this.modelHero.setEquipment(hero); err != nil {
|
|
code = pb.ErrorCode_HeroEquipUpdate
|
|
return
|
|
} else {
|
|
if newHero != nil {
|
|
list = append(list, newHero)
|
|
}
|
|
}
|
|
list = append(list, hero)
|
|
|
|
this.modelHero.setEquipProperty(hero, equip)
|
|
session.SendMsg("hero", "change", &pb.HeroChangePush{List: list})
|
|
|
|
return
|
|
}
|
|
|
|
//英雄列表
|
|
func (this *Hero) GetHeroList(uid string) []*pb.DBHero {
|
|
return this.modelHero.getHeroList(uid)
|
|
}
|
|
|
|
//查询英雄数量
|
|
func (this *Hero) QueryHeroAmount(uId string, heroCfgId string) (amount uint32) {
|
|
heroes := this.GetHeroList(uId)
|
|
for _, v := range heroes {
|
|
if v.HeroID == heroCfgId {
|
|
amount++
|
|
}
|
|
}
|
|
return amount
|
|
}
|
|
|
|
// 删除指定卡牌
|
|
func (this *Hero) DelCard(udi string, hero *pb.DBHero, amount int32) (code pb.ErrorCode) {
|
|
err := this.modelHero.consumeHeroCard(udi, hero, amount)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 清空数据
|
|
func (this *Hero) CleanData(uid string) {
|
|
this.modelHero.cleanData(uid)
|
|
}
|
|
|
|
// 创建一些特殊的英雄
|
|
func (this *Hero) GetSpecifiedHero(uid, heroConfId string, star, lv int32) (hero *pb.DBHero, err error) {
|
|
if uid == "" || heroConfId == "" || star == 0 || lv == 0 {
|
|
return nil, errors.New("parameter err")
|
|
}
|
|
hero, err = this.modelHero.createOneHero(uid, heroConfId)
|
|
if err == nil {
|
|
return
|
|
}
|
|
hero.Lv = lv
|
|
hero.Star = star
|
|
_heroMap := map[string]interface{}{
|
|
"lv": hero.Lv,
|
|
"star": hero.Star,
|
|
"isOverlying": false,
|
|
}
|
|
// 保存数据
|
|
err = this.modelHero.ChangeList(uid, hero.Id, _heroMap)
|
|
if err != nil {
|
|
log.Errorf("GetSpecified failed:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//Event-------------------------------------------------------------------------------------------------玩家离线
|
|
func (this *Hero) EventUserOffline(session comm.IUserSession) {
|
|
err := this.modelHero.RemoveUserHeroInfo(session)
|
|
this.Debugf("EventUserOffline:%s err:%v", session, err)
|
|
}
|