169 lines
3.2 KiB
Go

package items
import (
"dk/internal/store"
"fmt"
)
// Item represents an item in the game
type Item struct {
ID int `json:"id"`
Type int `json:"type"`
Name string `json:"name"`
Value int `json:"value"`
Att int `json:"att"`
Special string `json:"special"`
}
func (i *Item) Save() error {
return GetStore().UpdateWithRebuild(i.ID, i)
}
func (i *Item) Delete() error {
GetStore().RemoveWithRebuild(i.ID)
return nil
}
// Creates a new Item with sensible defaults
func New() *Item {
return &Item{
Type: TypeWeapon, // Default to weapon
Name: "",
Value: 0,
Att: 0,
Special: "",
}
}
// Validate checks if item has valid values
func (i *Item) Validate() error {
if i.Name == "" {
return fmt.Errorf("item name cannot be empty")
}
if i.Type < TypeWeapon || i.Type > TypeShield {
return fmt.Errorf("invalid item type: %d", i.Type)
}
if i.Value < 0 {
return fmt.Errorf("item Value cannot be negative")
}
if i.Att < 0 {
return fmt.Errorf("item Att cannot be negative")
}
return nil
}
// ItemType constants for item types
const (
TypeWeapon = 1
TypeArmor = 2
TypeShield = 3
)
// ItemStore with enhanced BaseStore
type ItemStore struct {
*store.BaseStore[Item]
}
// Global store with singleton pattern
var GetStore = store.NewSingleton(func() *ItemStore {
is := &ItemStore{BaseStore: store.NewBaseStore[Item]()}
// Register indices
is.RegisterIndex("byType", store.BuildIntGroupIndex(func(i *Item) int {
return i.Type
}))
is.RegisterIndex("allByID", store.BuildSortedListIndex(func(a, b *Item) bool {
return a.ID < b.ID
}))
return is
})
// Enhanced CRUD operations
func (is *ItemStore) AddItem(item *Item) error {
return is.AddWithRebuild(item.ID, item)
}
func (is *ItemStore) RemoveItem(id int) {
is.RemoveWithRebuild(id)
}
func (is *ItemStore) UpdateItem(item *Item) error {
return is.UpdateWithRebuild(item.ID, item)
}
// Data persistence
func LoadData(dataPath string) error {
is := GetStore()
return is.BaseStore.LoadData(dataPath)
}
func SaveData(dataPath string) error {
is := GetStore()
return is.BaseStore.SaveData(dataPath)
}
// Query functions using enhanced store
func Find(id int) (*Item, error) {
is := GetStore()
item, exists := is.Find(id)
if !exists {
return nil, fmt.Errorf("item with ID %d not found", id)
}
return item, nil
}
func All() ([]*Item, error) {
is := GetStore()
return is.AllSorted("allByID"), nil
}
func ByType(itemType int) ([]*Item, error) {
is := GetStore()
return is.GroupByIndex("byType", itemType), nil
}
// Insert with ID assignment
func (i *Item) Insert() error {
is := GetStore()
if i.ID == 0 {
i.ID = is.GetNextID()
}
return is.AddItem(i)
}
// Helper methods
func (i *Item) IsWeapon() bool {
return i.Type == TypeWeapon
}
func (i *Item) IsArmor() bool {
return i.Type == TypeArmor
}
func (i *Item) IsShield() bool {
return i.Type == TypeShield
}
func (i *Item) TypeName() string {
switch i.Type {
case TypeWeapon:
return "Weapon"
case TypeArmor:
return "Armor"
case TypeShield:
return "Shield"
default:
return "Unknown"
}
}
func (i *Item) HasSpecial() bool {
return i.Special != ""
}
func (i *Item) IsEquippable() bool {
return i.Type == TypeWeapon || i.Type == TypeArmor || i.Type == TypeShield
}