go_dreamfactory/modules/hero/model_hero.go
2022-06-27 19:48:39 +08:00

180 lines
4.6 KiB
Go

package hero
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"math"
mengine "github.com/dengsgo/math-engine/engine"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ModelHero struct {
modules.Model_Comp
moduleHero *Hero
}
func (this *ModelHero) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.Model_Comp.Init(service, module, comp, options)
this.moduleHero = module.(*Hero)
this.TableName = "hero"
return
}
//初始化英雄
func (this *ModelHero) initHero(uid string, heroCfgId int32) *pb.DB_HeroData {
heroCfg := this.moduleHero.configure_comp.GetHero(heroCfgId)
if heroCfg == nil {
log.Errorf("%v hero not found from config %v", heroCfgId)
return nil
}
objId := primitive.NewObjectID().Hex()
newHero := &pb.DB_HeroData{
Id: objId,
Uid: uid,
HeroID: heroCfg.Hid,
Star: heroCfg.Star,
Lv: 1, //初始等级
NormalSkill: []*pb.SkillData{},
Skins: []int32{},
EquipID: make([]string, 6),
AddProperty: make(map[int32]int32),
Energy: make(map[int32]int32),
Property: make(map[int32]int32),
}
return newHero
}
//创建一个指定的英雄
func (this *ModelHero) createOneHero(uid string, heroCfgId int32) error {
hero := this.initHero(uid, heroCfgId)
if hero != nil {
return this.moduleHero.model_hero.AddList(uid, hero.Id, hero)
}
return nil
}
//创建多个指定的英雄
func (this *ModelHero) createMultiHero(uid string, heroCfgId ...int32) error {
data := make(map[string]interface{})
for _, v := range heroCfgId {
hero := this.initHero(uid, v)
if hero != nil {
data[hero.Id] = hero
}
}
return this.moduleHero.model_hero.AddLists(uid, data)
}
//获取一个英雄
func (this *ModelHero) getOneHero(uid, heroId string) *pb.DB_HeroData {
hero := &pb.DB_HeroData{}
err := this.moduleHero.model_hero.GetListObj(uid, heroId, hero)
if err != nil {
return nil
}
return hero
}
//获取玩家的英雄列表
func (this *ModelHero) getHeroList(uid string) ([]*pb.DB_HeroData, error) {
herokeys := make(map[string]string)
err := this.Get(uid, herokeys)
if err != nil {
return nil, err
}
heroes := make([]*pb.DB_HeroData, 0)
err = this.GetList(uid, &heroes)
if err != nil {
return nil, err
}
return heroes, nil
}
//更新装备
func (this *ModelHero) setEquipment(uid, heroId string, equipIds []string) pb.ErrorCode {
hero := this.getOneHero(uid, heroId)
if hero == nil {
return pb.ErrorCode_HeroNoExist
}
hero.EquipID = equipIds
return pb.ErrorCode_Success
}
//指定英雄升级
func (this *ModelHero) levelUp(uid string, heroId int32) error {
var heroes []*pb.DB_HeroData
err := this.moduleHero.model_hero.GetList(uid, heroes)
if err != nil {
log.Errorf("levelUp err:%v", err)
return err
}
return nil
}
//升星
func (this *ModelHero) starUp() {
}
//属性计算 - 暂时放在modelHero里实现
//英雄基础属性 + 英雄等级基础属性 * 英雄成长系数 + 英雄星级对应等级属性 * 英雄品质系数
func (this *ModelHero) PropertyCompute(uid, heroId string) map[int32]int32 {
hero := this.getOneHero(uid, heroId)
if hero == nil {
return nil
}
//英雄等级基础属性levelup
heroLvCfg := this.moduleHero.configure_comp.GetHeroLv(hero.Lv)
if heroLvCfg == nil {
return nil
}
//英雄基础配置 newhero
heroCfg := this.moduleHero.configure_comp.GetHero(hero.HeroID)
if heroCfg == nil {
return nil
}
//品质系数
stargrowCfg := this.moduleHero.configure_comp.GetHeroStar(heroCfg.Star)
if stargrowCfg == nil {
return nil
}
//英雄星级对应等级属性
heroStarCfg := this.moduleHero.configure_comp.GetHeroLv(stargrowCfg.Level)
if heroStarCfg == nil {
return nil
}
//成长系数
lvGrow := this.moduleHero.configure_comp.GetHeroLvgrow(hero.HeroID)
if lvGrow == nil {
return nil
}
exprHp := fmt.Sprintf("%v + %v * %v/1000 + %v * %v", lvGrow.Hp, heroLvCfg.Hp, lvGrow.Hpgrow, heroStarCfg.Hp, stargrowCfg.StarupHp)
hp, _ := mengine.ParseAndExec(exprHp)
exprAtk := fmt.Sprintf("%v +%v * %v/1000 + %v * %v", lvGrow.Atk, heroLvCfg.Atk, lvGrow.Atkgrow, heroStarCfg.Atk, stargrowCfg.StarupAtk)
atk, _ := mengine.ParseAndExec(exprAtk)
exprDef := fmt.Sprintf("%v +%v * %v/1000 + %v * %v", lvGrow.Def, heroLvCfg.Def, lvGrow.Defgrow, heroStarCfg.Def, stargrowCfg.StarupDef)
def, _ := mengine.ParseAndExec(exprDef)
return map[int32]int32{
comm.PropertyHp: int32(math.Floor(hp)),
comm.PropertyAtk: int32(math.Floor(atk)),
comm.PropertyDef: int32(math.Floor(def)),
}
}