package data import ( json "github.com/json-iterator/go" "github.com/oylshe1314/framework/util" ) type LotteryConfig struct { Id int `json:"id"` PoolId int `json:"pool_id"` BoxId int `json:"box_id"` ItemId int `json:"item_id"` ItemNum int `json:"item_num"` ItemWeight int `json:"item_weight"` } type LotteryBox struct { PoolId int BoxId int Weights int Tables []*LotteryConfig } type LotteryConfigTable struct { l []*LotteryConfig m map[uint64]*LotteryBox } func (this *LotteryConfigTable) load(buf []byte) error { var err = json.Unmarshal(buf, &this.l) if err != nil { return err } this.m = make(map[uint64]*LotteryBox) for i := range this.l { var key = util.Compose2uint32(uint32(this.l[i].PoolId), uint32(this.l[i].BoxId)) var box = this.m[key] if box == nil { box = &LotteryBox{ PoolId: this.l[i].PoolId, BoxId: this.l[i].BoxId, Weights: this.l[i].ItemWeight, Tables: []*LotteryConfig{this.l[i]}, } this.m[key] = box } else { box.Weights += this.l[i].ItemWeight box.Tables = append(box.Tables, this.l[i]) } } return nil } func (this *LotteryConfigTable) List() []*LotteryConfig { return this.l } //func (this *LotteryConfigTable) Get(i int) *LotteryConfig { // return this.l[i] //} func (this *LotteryConfigTable) Find(poolId, boxId int) *LotteryBox { return this.m[util.Compose2uint32(uint32(poolId), uint32(boxId))] }