ecs/servers/game/logic/player_hero_book.go

95 lines
2.3 KiB
Go
Raw Normal View History

2025-06-05 17:48:23 +08:00
package logic
import (
"ecs/proto/pb"
)
type PlayerHeroBook struct {
HeroId uint32 `json:"hero_id" key:"1"`
Items [][2]uint32 `bson:"items"`
2025-06-05 17:48:23 +08:00
}
func (this *PlayerHeroBook) BuildMsgHeroBook() *pb.HeroBook {
var list []*pb.HeroBookItem
for _, item := range this.Items {
list = append(list, &pb.HeroBookItem{BookId: item[0], Status: pb.AbleStatus(item[1])})
2025-06-05 17:48:23 +08:00
}
return &pb.HeroBook{HeroId: this.HeroId, ItemList: list}
}
func (this *Player) addHeroBook(heroId uint32) {
if _, ok := this.HeroBook[heroId]; ok {
return
}
var items [][2]uint32
2025-06-05 17:48:23 +08:00
var heroBookTables = this.manager.tables.HeroBook.Find2(int(heroId))
for _, heroBookTable := range heroBookTables {
var status = pb.AbleStatus_Able
for _, needHeroId := range heroBookTable.NeedHeroes {
if _, ok := this.HeroBook[uint32(needHeroId)]; !ok {
status = pb.AbleStatus_Unable
break
}
}
items = append(items, [2]uint32{uint32(heroBookTable.Id), uint32(status)})
2025-06-05 17:48:23 +08:00
}
var heroBook = &PlayerHeroBook{
HeroId: heroId,
Items: items,
}
this.HeroBook[heroBook.HeroId] = heroBook
this.SaveModel(heroBook)
2025-06-20 15:34:46 +08:00
_ = this.Send(pb.ModId_ModuleHero, pb.MsgId_ModHeroBookChange, &pb.HeroBookListAck{
BookList: append(this.refreshHeroBook(heroBook.HeroId), heroBook.BuildMsgHeroBook()),
2025-06-05 17:48:23 +08:00
})
}
func (this *Player) refreshHeroBook(heroId uint32) (changeList []*pb.HeroBook) {
for _, heroBook := range this.HeroBook {
if heroBook.HeroId == heroId {
continue
}
var dirty = false
itemsLoop:
for _, item := range heroBook.Items {
if pb.AbleStatus(item[1]) >= pb.AbleStatus_Able {
continue itemsLoop
}
var heroBookTable = this.manager.tables.HeroBook.Find1(int(item[0]))
if heroBookTable == nil {
this.manager.logger.Error("Find hero book table failed, bookId: ", item[0])
continue itemsLoop
}
for _, needHeroId := range heroBookTable.NeedHeroes {
if _, ok := this.HeroBook[uint32(needHeroId)]; !ok {
continue itemsLoop
}
}
item[1] = uint32(pb.AbleStatus_Able)
dirty = true
}
if dirty {
this.SaveModel(heroBook)
changeList = append(changeList, heroBook.BuildMsgHeroBook())
}
}
return
}
2025-06-05 17:48:23 +08:00
func (this *Player) BuildMsgHeroBookListAck() *pb.HeroBookListAck {
var list []*pb.HeroBook
for _, heroBook := range this.HeroBook {
list = append(list, heroBook.BuildMsgHeroBook())
}
return &pb.HeroBookListAck{BookList: list}
}