diff options
| author | workhorse <workhorse@localhost.localdomain> | 2026-06-19 20:24:52 -0400 |
|---|---|---|
| committer | workhorse <workhorse@localhost.localdomain> | 2026-06-19 20:24:52 -0400 |
| commit | 8ee8982b8759bcae116647cc993867f4c8b0a6ce (patch) | |
| tree | ee01f7b1d745cbc94d9981108a1209527487b3e5 /internal/object | |
| parent | 2bec24859af8f03c80a0a58e3240519942450167 (diff) | |
| download | thehouseoficarus-8ee8982b8759bcae116647cc993867f4c8b0a6ce.tar.gz | |
reorganized items, objects, and rooms in directories. oranized module names. added startup checks.
Diffstat (limited to 'internal/object')
| -rw-r--r-- | internal/object/item.go | 2 | ||||
| -rw-r--r-- | internal/object/item_store.go | 56 | ||||
| -rw-r--r-- | internal/object/object.go | 2 | ||||
| -rw-r--r-- | internal/object/store.go | 28 |
4 files changed, 61 insertions, 27 deletions
diff --git a/internal/object/item.go b/internal/object/item.go index 7c682a4..8ec1565 100644 --- a/internal/object/item.go +++ b/internal/object/item.go @@ -74,7 +74,7 @@ type ItemDef struct { type MadeFromEntry struct { Items []string `yaml:"items"` - Qty int `yaml:"qty"` + Quantity int `yaml:"quantity"` Byproducts []string `yaml:"byproducts"` } diff --git a/internal/object/item_store.go b/internal/object/item_store.go index 9e932ba..d75179f 100644 --- a/internal/object/item_store.go +++ b/internal/object/item_store.go @@ -4,28 +4,34 @@ import ( "fmt" "os" "path/filepath" - "strings" + "thehouseoficarus/internal/action" "gopkg.in/yaml.v3" ) type ItemStore struct { - dataDir string - cache map[string]*ItemDef + dataDir string + pathIndex map[string]string + cache map[string]*ItemDef } func NewItemStore(dataDir string) *ItemStore { - return &ItemStore{ - dataDir: dataDir, - cache: make(map[string]*ItemDef), + s := &ItemStore{ + dataDir: dataDir, + pathIndex: action.BuildPathIndex(filepath.Join(dataDir, "items")), + cache: make(map[string]*ItemDef), } + return s } func (s *ItemStore) Load(id string) (*ItemDef, error) { if def, ok := s.cache[id]; ok { return def, nil } - path := filepath.Join(s.dataDir, "items", id+".yaml") + path, ok := s.pathIndex[id] + if !ok { + return nil, fmt.Errorf("read item %s: no such item", id) + } data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read item %s: %w", id, err) @@ -40,21 +46,31 @@ func (s *ItemStore) Load(id string) (*ItemDef, error) { func (s *ItemStore) LoadAll() ([]*ItemDef, error) { dir := filepath.Join(s.dataDir, "items") - entries, err := os.ReadDir(dir) - if err != nil { - return nil, err - } var defs []*ItemDef - for _, e := range entries { - if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" { - continue + err := action.WalkYAMLDir(dir, func(path, id string, data []byte) error { + if def, ok := s.cache[id]; ok { + defs = append(defs, def) + return nil } - id := strings.TrimSuffix(e.Name(), ".yaml") - def, err := s.Load(id) - if err != nil { - continue + var def ItemDef + if err := yaml.Unmarshal(data, &def); err != nil { + return nil } - defs = append(defs, def) + s.cache[id] = &def + defs = append(defs, &def) + return nil + }) + return defs, err +} + +func (s *ItemStore) PathIndex() map[string]string { + return s.pathIndex +} + +func (s *ItemStore) IDSet() map[string]bool { + ids := make(map[string]bool) + for id := range s.pathIndex { + ids[id] = true } - return defs, nil + return ids } diff --git a/internal/object/object.go b/internal/object/object.go index 4d8db69..826a315 100644 --- a/internal/object/object.go +++ b/internal/object/object.go @@ -3,7 +3,7 @@ package object import "thehouseoficarus/internal/action" type UseInteraction struct { - Item string `yaml:"item"` + Item string `yaml:"item_id"` Condition *action.Condition `yaml:"condition"` Message string `yaml:"message"` Action *action.NodeAction `yaml:"action"` diff --git a/internal/object/store.go b/internal/object/store.go index a884601..a11ff7a 100644 --- a/internal/object/store.go +++ b/internal/object/store.go @@ -5,18 +5,21 @@ import ( "os" "path/filepath" + "thehouseoficarus/internal/action" "gopkg.in/yaml.v3" ) type ObjectStore struct { - dataDir string - cache map[string]*ObjectDef + dataDir string + pathIndex map[string]string + cache map[string]*ObjectDef } func NewObjectStore(dataDir string) *ObjectStore { return &ObjectStore{ - dataDir: dataDir, - cache: make(map[string]*ObjectDef), + dataDir: dataDir, + pathIndex: action.BuildPathIndex(filepath.Join(dataDir, "objects")), + cache: make(map[string]*ObjectDef), } } @@ -24,7 +27,10 @@ func (s *ObjectStore) Load(id string) (*ObjectDef, error) { if def, ok := s.cache[id]; ok { return def, nil } - path := filepath.Join(s.dataDir, "objects", fmt.Sprintf("%s.yaml", id)) + path, ok := s.pathIndex[id] + if !ok { + return nil, fmt.Errorf("read object %s: no such object", id) + } data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read object %s: %w", id, err) @@ -36,3 +42,15 @@ func (s *ObjectStore) Load(id string) (*ObjectDef, error) { s.cache[id] = &def return &def, nil } + +func (s *ObjectStore) PathIndex() map[string]string { + return s.pathIndex +} + +func (s *ObjectStore) IDSet() map[string]bool { + ids := make(map[string]bool) + for id := range s.pathIndex { + ids[id] = true + } + return ids +} |
