package drops import ( "dk/internal/store" "fmt" ) // Drop represents a drop item in the game type Drop struct { ID int `json:"id"` Name string `json:"name"` Level int `json:"level"` Type int `json:"type"` Att string `json:"att"` } func (d *Drop) Save() error { return GetStore().UpdateWithRebuild(d.ID, d) } func (d *Drop) Delete() error { GetStore().RemoveWithRebuild(d.ID) return nil } // Creates a new Drop with sensible defaults func New() *Drop { return &Drop{ Name: "", Level: 1, // Default minimum level Type: TypeConsumable, // Default to consumable Att: "", } } // Validate checks if drop has valid values func (d *Drop) Validate() error { if d.Name == "" { return fmt.Errorf("drop name cannot be empty") } if d.Level < 1 { return fmt.Errorf("drop Level must be at least 1") } if d.Type < TypeConsumable { return fmt.Errorf("invalid drop type: %d", d.Type) } return nil } // DropType constants for drop types const ( TypeConsumable = 1 ) // DropStore with enhanced BaseStore type DropStore struct { *store.BaseStore[Drop] } // Global store with singleton pattern var GetStore = store.NewSingleton(func() *DropStore { ds := &DropStore{BaseStore: store.NewBaseStore[Drop]()} // Register indices ds.RegisterIndex("byLevel", store.BuildIntGroupIndex(func(d *Drop) int { return d.Level })) ds.RegisterIndex("byType", store.BuildIntGroupIndex(func(d *Drop) int { return d.Type })) ds.RegisterIndex("allByID", store.BuildSortedListIndex(func(a, b *Drop) bool { return a.ID < b.ID })) return ds }) // Enhanced CRUD operations func (ds *DropStore) AddDrop(drop *Drop) error { return ds.AddWithRebuild(drop.ID, drop) } func (ds *DropStore) RemoveDrop(id int) { ds.RemoveWithRebuild(id) } func (ds *DropStore) UpdateDrop(drop *Drop) error { return ds.UpdateWithRebuild(drop.ID, drop) } // Data persistence func LoadData(dataPath string) error { ds := GetStore() return ds.BaseStore.LoadData(dataPath) } func SaveData(dataPath string) error { ds := GetStore() return ds.BaseStore.SaveData(dataPath) } // Query functions using enhanced store func Find(id int) (*Drop, error) { ds := GetStore() drop, exists := ds.Find(id) if !exists { return nil, fmt.Errorf("drop with ID %d not found", id) } return drop, nil } func All() ([]*Drop, error) { ds := GetStore() return ds.AllSorted("allByID"), nil } func ByLevel(minLevel int) ([]*Drop, error) { ds := GetStore() return ds.FilterByIndex("allByID", func(d *Drop) bool { return d.Level <= minLevel }), nil } func ByType(dropType int) ([]*Drop, error) { ds := GetStore() return ds.GroupByIndex("byType", dropType), nil } // Insert with ID assignment func (d *Drop) Insert() error { ds := GetStore() if d.ID == 0 { d.ID = ds.GetNextID() } return ds.AddDrop(d) } // Helper methods func (d *Drop) IsConsumable() bool { return d.Type == TypeConsumable } func (d *Drop) TypeName() string { switch d.Type { case TypeConsumable: return "Consumable" default: return "Unknown" } }