ecs/servers/game/logic/player_hero.go
2025-06-05 17:48:23 +08:00

277 lines
7.7 KiB
Go

package logic
import (
"ecs/proto"
"ecs/proto/pb"
"ecs/servers/game/data"
"github.com/oylshe1314/framework/util"
"slices"
)
type Attrs [pb.AttrType_Nums]uint64
type PlayerHero struct {
Uid uint64 `json:"uid" key:"1"`
Id uint32 `bson:"id"`
Exp uint64 `bson:"exp"`
Level uint32 `bson:"level"`
BreakLevel uint32 `bson:"break_level"`
SoulLevel uint32 `bson:"soul_level"`
SoulList []uint32 `bson:"soul_list"`
Awaken uint32 `bson:"awaken"`
Equips [4]uint64 `bson:"equips"`
Treasures [2]uint64 `bson:"treasures"`
Mounts [1]uint64 `bson:"mounts"`
Artifact [1]uint64 `bson:"artifact"`
attrs Attrs
}
func (this *PlayerHero) BuildMsgHero() *pb.Hero {
return &pb.Hero{
Uid: this.Uid,
Id: this.Id,
Exp: this.Exp,
Level: this.Level,
BreakLevel: this.BreakLevel,
SoulLevel: this.SoulLevel,
SoulList: this.SoulList,
Awaken: this.Awaken,
Equips: this.Equips[:],
Treasures: this.Treasures[:],
Mounts: this.Mounts[:],
Artifact: this.Artifact[:],
Attrs: this.attrs[:],
}
}
func (this *PlayerHero) Attrs() Attrs {
return this.attrs
}
func (this *Player) addHero(heroTable *data.Hero) error {
var hero = &PlayerHero{
Uid: util.RandomUid(),
Id: uint32(heroTable.Id),
}
var heroLevelTable *data.HeroLevel
if heroTable.Level > 0 {
heroLevelTable = this.manager.tables.HeroLevel.Find3(heroTable.Id, 1)
if heroLevelTable == nil {
this.manager.logger.Errorf("find hero level table failed, heroId %d, level: %d", heroTable.Id, 1)
return proto.ErrDataTablesError
}
hero.Level = uint32(heroLevelTable.Level)
}
var heroBreakTable *data.HeroBreak
if heroTable.BreaksLevels > 0 {
heroBreakTable = this.manager.tables.HeroBreak.Find3(heroTable.Id, 0)
if heroBreakTable == nil {
this.manager.logger.Errorf("find hero break table failed, heroId %d, level: %d", heroTable.Id, 1)
return proto.ErrDataTablesError
}
hero.BreakLevel = uint32(heroBreakTable.BreakLevel)
}
//SoulLevel
//Awaken
this.Hero[hero.Uid] = hero
this.SaveModel(hero)
this.addHeroBook(hero.Id)
return nil
}
func (this *Player) AddHero(heroId uint32) error {
var heroTable = this.manager.tables.Hero.Find(int(heroId))
if heroTable == nil {
this.manager.logger.Error("find hero table failed, heroId: ", heroId)
return proto.ErrDataTablesError
}
return this.addHero(heroTable)
}
func (this *Player) calcHeroLevelAttrs(hero *PlayerHero) (attrs Attrs) {
var heroLevelTable = this.manager.tables.HeroLevel.Find3(int(hero.Id), int(hero.Level))
if heroLevelTable != nil {
attrs[heroLevelTable.AttrType1] = uint64(heroLevelTable.AttrValue1)
attrs[heroLevelTable.AttrType2] = uint64(heroLevelTable.AttrValue2)
attrs[heroLevelTable.AttrType3] = uint64(heroLevelTable.AttrValue3)
attrs[heroLevelTable.AttrType4] = uint64(heroLevelTable.AttrValue4)
attrs[heroLevelTable.AttrType5] = uint64(heroLevelTable.AttrValue5)
attrs[heroLevelTable.AttrType6] = uint64(heroLevelTable.AttrValue6)
attrs[heroLevelTable.AttrType7] = uint64(heroLevelTable.AttrValue7)
attrs[heroLevelTable.AttrType8] = uint64(heroLevelTable.AttrValue8)
attrs[heroLevelTable.AttrType9] = uint64(heroLevelTable.AttrValue9)
attrs[heroLevelTable.AttrType10] = uint64(heroLevelTable.AttrValue10)
}
return
}
func (this *Player) calcHeroBreakAttrs(hero *PlayerHero) (attrs Attrs) {
var heroBreakTable = this.manager.tables.HeroBreak.Find3(int(hero.Id), int(hero.BreakLevel))
if heroBreakTable != nil {
attrs[heroBreakTable.AttrType1] = uint64(heroBreakTable.AttrValue1)
attrs[heroBreakTable.AttrType2] = uint64(heroBreakTable.AttrValue2)
attrs[heroBreakTable.AttrType3] = uint64(heroBreakTable.AttrValue3)
attrs[heroBreakTable.AttrType4] = uint64(heroBreakTable.AttrValue4)
attrs[heroBreakTable.AttrType5] = uint64(heroBreakTable.AttrValue5)
attrs[heroBreakTable.AttrType6] = uint64(heroBreakTable.AttrValue6)
attrs[heroBreakTable.AttrType7] = uint64(heroBreakTable.AttrValue7)
attrs[heroBreakTable.AttrType8] = uint64(heroBreakTable.AttrValue8)
attrs[heroBreakTable.AttrType9] = uint64(heroBreakTable.AttrValue9)
attrs[heroBreakTable.AttrType10] = uint64(heroBreakTable.AttrValue10)
}
return
}
func (this *Player) calcHeroBondAttrs(hero *PlayerHero) (attrs Attrs) {
var heroBondTables = this.manager.tables.HeroBond.Find(int(hero.Id))
for i := range heroBondTables {
var yes = false
switch heroBondTables[i].BondType {
case 1: //装备
for _, equipUid := range hero.Equips {
if equipUid == 0 {
continue
}
if equip, ok := this.Equip[equipUid]; ok && slices.Contains(heroBondTables[i].BondId, int(equip.Id)) {
yes = true
break
}
}
case 2: //宝物
for _, treasureUid := range hero.Treasures {
if treasureUid == 0 {
continue
}
if treasure, ok := this.Treasure[treasureUid]; ok && slices.Contains(heroBondTables[i].BondId, int(treasure.Id)) {
yes = true
break
}
}
case 3: //神兵
for _, artifactUid := range hero.Artifact {
if artifactUid == 0 {
continue
}
if artifact, ok := this.Artifact[artifactUid]; ok && slices.Contains(heroBondTables[i].BondId, int(artifact.Id)) {
yes = true
break
}
}
case 4: //英雄
}
if yes {
attrs[heroBondTables[i].AttrType1] += uint64(heroBondTables[i].AttrValue1)
attrs[heroBondTables[i].AttrType2] += uint64(heroBondTables[i].AttrValue2)
attrs[heroBondTables[i].AttrType3] += uint64(heroBondTables[i].AttrValue3)
}
}
return
}
func (this *Player) calcHeroAwakenAttrs(hero *PlayerHero) (attrs Attrs) {
return
}
func (this *Player) calcHeroEquipsAttrs(hero *PlayerHero) (attrs Attrs) {
for _, equipUid := range hero.Equips {
if equipUid == 0 {
continue
}
var equip = this.Equip[equipUid]
var equipLevelTable = this.manager.tables.EquipLevel.Find3(int(equip.Id), int(equip.Level))
if equipLevelTable != nil {
attrs[equipLevelTable.AttrType] += uint64(equipLevelTable.AttrValue)
}
}
return
}
func (this *Player) calcHeroTreasuresAttrs(hero *PlayerHero) (attrs Attrs) {
return
}
func (this *Player) calcHeroMountsAttrs(hero *PlayerHero) (attrs Attrs) {
return
}
func (this *Player) calcHeroArtifactAttrs(hero *PlayerHero) (attrs Attrs) {
return
}
func (this *Player) calcHeroBookAttrs(hero *PlayerHero) (attrs Attrs) {
var heroBook = this.HeroBook[hero.Id]
if heroBook == nil {
return
}
for _, item := range heroBook.Items {
if !item.Value {
continue
}
var heroGeneralTable = this.manager.tables.HeroBook.Find1(int(item.Key))
if heroGeneralTable == nil {
continue
}
attrs[heroGeneralTable.AttrType1] += uint64(heroGeneralTable.AttrValue1)
attrs[heroGeneralTable.AttrType2] += uint64(heroGeneralTable.AttrValue2)
attrs[heroGeneralTable.AttrType3] += uint64(heroGeneralTable.AttrValue3)
}
return
}
func (this *Player) calcHeroAttrs(hero *PlayerHero, attrsList ...Attrs) bool {
var newAttrs Attrs
for _, attrs := range attrsList {
for i, attr := range attrs {
newAttrs[i] += attr
}
}
for i := range newAttrs {
if hero.attrs[i] != newAttrs[i] {
hero.attrs = newAttrs
return true
}
}
return false
}
func (this *Player) updateHeroAttrs(hero *PlayerHero) bool {
return this.calcHeroAttrs(hero,
this.calcHeroLevelAttrs(hero),
this.calcHeroBreakAttrs(hero),
this.calcHeroBondAttrs(hero),
this.calcHeroAwakenAttrs(hero),
this.calcHeroEquipsAttrs(hero),
this.calcHeroTreasuresAttrs(hero),
this.calcHeroMountsAttrs(hero),
this.calcHeroArtifactAttrs(hero),
this.calcHeroBookAttrs(hero),
)
}
func (this *Player) UpdateHeroAttrs(hero *PlayerHero) bool {
return this.updateHeroAttrs(hero)
}
func (this *Player) BuildMsgHeroListAck() *pb.HeroListAck {
var list []*pb.Hero
for _, hero := range this.Hero {
list = append(list, hero.BuildMsgHero())
}
return &pb.HeroListAck{HeroList: list}
}