package logic import ( "ecs/proto/pb" ) type PlayerHeroBook struct { HeroId uint32 `json:"hero_id" key:"1"` Items [][2]uint32 `bson:"items"` } 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])}) } 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 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)}) } var heroBook = &PlayerHeroBook{ HeroId: heroId, Items: items, } this.HeroBook[heroBook.HeroId] = heroBook this.SaveModel(heroBook) _ = this.Send(pb.ModId_ModuleHero, pb.MsgId_ModHeroBookChange, &pb.HeroBookListAck{ BookList: append(this.refreshHeroBook(heroBook.HeroId), heroBook.BuildMsgHeroBook()), }) } 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 } 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} }