go_dreamfactory/modules/hero/module.go
2022-07-01 13:53:17 +08:00

183 lines
4.4 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 *apiComp
configure *configureComp
hero *ModelHero
items comm.IItems
}
//模块名
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.hero = this.RegisterComp(new(ModelHero)).(*ModelHero)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
//创建新英雄
func (this *Hero) CreateHero(uid string, heroCfgId ...int32) error {
return this.hero.createMultiHero(uid, heroCfgId...)
}
//消耗英雄卡
func (this *Hero) ChangeCard(uId string, heroCfgId int32, count int32) (code pb.ErrorCode) {
heroes := this.GetHeroList(uId)
var curList []*pb.DBHero
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.hero.consumeOneHeroCard(v.Uid, v.Id, count)
if err != nil {
return pb.ErrorCode_DBError
}
}
return pb.ErrorCode_Success
}
//获取英雄
func (this *Hero) GetHero(uid, heroId string) (*pb.DBHero, pb.ErrorCode) {
hero := this.hero.getOneHero(uid, heroId)
if hero == nil {
return nil, pb.ErrorCode_HeroNoExist
}
return hero, pb.ErrorCode_Success
}
//佩戴装备
func (this *Hero) UpdateEquipment(hero *pb.DBHero, equip []*pb.DB_Equipment) (code pb.ErrorCode) {
equipIds := make([]string, 4)
property := make(map[string]int32) //主属性
addProperty := make(map[string]int32) //副属性
for _, v := range equip {
equipIds = append(equipIds, v.Id)
//主属性
property[v.MainEntry.AttrName] = v.MainEntry.Value
//附加属性
for _, v := range v.AdverbEntry {
addProperty[v.AttrName] = v.Value
}
}
this.hero.mergeMainProperty(hero.Uid, hero.Id, property)
this.hero.mergeAddProperty(hero.Uid, hero.Id, addProperty)
return this.hero.setEquipment(hero.Uid, hero.Id, equipIds)
}
//英雄列表
func (this *Hero) GetHeroList(uid string) []*pb.DBHero {
list, err := this.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) AddCardExp(uid string, heroId string, exp int32) (code pb.ErrorCode) {
var (
curExp int32
curLv int32
)
_hero, err := this.GetHero(uid, heroId) // 获取英雄信息
if err != 0 {
code = pb.ErrorCode_HeroNoExist
return
}
curExp = _hero.Exp
curLv = _hero.Lv
var maxLv int32 // 校验等级达到上限
maxLv = _hero.Star * comm.HeroStarLvRatio
_data := this.configure.GetHeroLv(curLv)
if _data != nil {
if maxLv <= _hero.Lv && curExp >= _data.Heroexp[0].N { // 加经验之前校验是否达到最大等级
code = pb.ErrorCode_HeroMaxLv
return
}
curExp += exp // 先把经验加上
for { // 死循环判断一键升级
if maxLv <= _hero.Lv && curExp >= _data.Heroexp[0].N { // 设置最大经验和等级
curLv = maxLv
curExp = _data.Heroexp[0].N
break
}
if _data.Heroexp[0].N > curExp { // 经验不够升级则不能执行升级操作
break
} else { // 升级操作
curExp -= _data.Heroexp[0].N
curLv += 1 // 经验够了 那么等级+1
_data = this.configure.GetHeroLv(curLv)
if _data == nil { // 等级加失败了 回到原来的等级
curLv -= 1
break
}
}
}
// _hero.Lv = curLv
// _hero.Exp = curExp
update := map[string]interface{}{
"lv": curLv,
"exp": curExp,
}
if err := this.hero.modifyHeroData(uid, heroId, update); err != nil {
code = pb.ErrorCode_DBError
} // 修改英雄数据
} else {
code = pb.ErrorCode_HeroNoExist
return
}
return
}
// 删除指定卡牌
func (this *Hero) DelCard(cardid string, amount int32) (code pb.ErrorCode) {
return
}