269 lines
7.7 KiB
Go
269 lines
7.7 KiB
Go
![]() |
package logic
|
||
|
|
||
|
import (
|
||
|
"ecs/proto"
|
||
|
"ecs/proto/pb"
|
||
|
"ecs/servers/game/data"
|
||
|
"slices"
|
||
|
)
|
||
|
|
||
|
type Attrs [pb.AttrType_Nums]uint64
|
||
|
|
||
|
type PlayerHero struct {
|
||
|
Id uint32 `bson:"id" key:"1"`
|
||
|
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"`
|
||
|
General []uint32 `bson:"general"`
|
||
|
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{
|
||
|
HeroId: this.Id,
|
||
|
Exp: this.Exp,
|
||
|
Level: this.Level,
|
||
|
BreakLevel: this.BreakLevel,
|
||
|
SoulLevel: this.SoulLevel,
|
||
|
SoulList: this.SoulList,
|
||
|
Awaken: this.Awaken,
|
||
|
General: this.General,
|
||
|
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{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
|
||
|
|
||
|
var heroGeneralBookTables = this.manager.tables.HeroGeneral.Find2(heroTable.Id)
|
||
|
hero.General = make([]uint32, len(heroGeneralBookTables))
|
||
|
|
||
|
this.Hero[hero.Id] = hero
|
||
|
this.SaveModel(hero)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (this *Player) AddHero(heroId uint32) error {
|
||
|
var hero = this.Hero[heroId]
|
||
|
if hero != nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
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) calcHeroGeneralAttrs(hero *PlayerHero) (attrs Attrs) {
|
||
|
for i := range hero.General {
|
||
|
if hero.General[i] == 0 {
|
||
|
continue
|
||
|
}
|
||
|
var heroGeneralTable = this.manager.tables.HeroGeneral.Find1(int(hero.General[i]))
|
||
|
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) 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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
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) 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.calcHeroAwakenAttrs(hero),
|
||
|
this.calcHeroEquipsAttrs(hero),
|
||
|
this.calcHeroTreasuresAttrs(hero),
|
||
|
this.calcHeroMountsAttrs(hero),
|
||
|
this.calcHeroArtifactAttrs(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}
|
||
|
}
|