aboutsummaryrefslogtreecommitdiff
path: root/internal/action
diff options
context:
space:
mode:
authorworkhorse <workhorse@localhost.localdomain>2026-06-19 20:24:52 -0400
committerworkhorse <workhorse@localhost.localdomain>2026-06-19 20:24:52 -0400
commit8ee8982b8759bcae116647cc993867f4c8b0a6ce (patch)
treeee01f7b1d745cbc94d9981108a1209527487b3e5 /internal/action
parent2bec24859af8f03c80a0a58e3240519942450167 (diff)
downloadthehouseoficarus-8ee8982b8759bcae116647cc993867f4c8b0a6ce.tar.gz
reorganized items, objects, and rooms in directories. oranized module names. added startup checks.
Diffstat (limited to 'internal/action')
-rw-r--r--internal/action/recipe.go39
-rw-r--r--internal/action/store.go89
2 files changed, 102 insertions, 26 deletions
diff --git a/internal/action/recipe.go b/internal/action/recipe.go
index c1f30fb..5a69195 100644
--- a/internal/action/recipe.go
+++ b/internal/action/recipe.go
@@ -1,7 +1,6 @@
package action
import (
- "os"
"path/filepath"
"gopkg.in/yaml.v3"
@@ -9,7 +8,7 @@ import (
type ConsumeEntry struct {
Items []string `yaml:"items"`
- Qty int `yaml:"qty"`
+ Quantity int `yaml:"quantity"`
Byproducts []string `yaml:"byproducts"`
}
@@ -80,25 +79,15 @@ func (s *RecipeStore) LoadByType(recipeType string) ([]RecipeDef, error) {
func (s *RecipeStore) loadAllRaw() ([]RecipeDef, error) {
dir := filepath.Join(s.dataDir, "recipes")
- entries, err := os.ReadDir(dir)
- if err != nil {
- return nil, err
- }
var recipes []RecipeDef
- for _, e := range entries {
- if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" {
- continue
- }
- data, err := os.ReadFile(filepath.Join(dir, e.Name()))
- if err != nil {
- continue
- }
+ WalkYAMLDir(dir, func(path, id string, data []byte) error {
var r RecipeDef
if err := yaml.Unmarshal(data, &r); err != nil {
- continue
+ return nil
}
recipes = append(recipes, r)
- }
+ return nil
+ })
return recipes, nil
}
@@ -190,23 +179,23 @@ func (r *RecipeDef) ConsumeAll(hasItem func(string) bool, removeItem func(string
found := false
for _, id := range e.Items {
if hasItem(id) {
- removeItem(id, e.Qty)
- found = true
- break
- }
- }
- if !found {
- return false
+ removeItem(id, e.Quantity)
+ found = true
+ break
}
}
- return true
+ if !found {
+ return false
+ }
+}
+return true
}
func (r *RecipeDef) HasAllItemsQty(countItem func(string) int) bool {
for _, e := range r.Consume {
found := false
for _, id := range e.Items {
- qty := e.Qty
+ qty := e.Quantity
if qty <= 0 {
qty = 1
}
diff --git a/internal/action/store.go b/internal/action/store.go
index a8db9df..6cb2e39 100644
--- a/internal/action/store.go
+++ b/internal/action/store.go
@@ -5,10 +5,79 @@ import (
"math/rand"
"os"
"path/filepath"
+ "strings"
+ "sync"
"gopkg.in/yaml.v3"
)
+func BuildPathIndex(dir string) map[string]string {
+ index := make(map[string]string)
+ filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
+ return nil
+ }
+ id := strings.TrimSuffix(d.Name(), ".yaml")
+ index[id] = path
+ return nil
+ })
+ return index
+}
+
+func BuildRoomIndex(dir string) map[int]string {
+ index := make(map[int]string)
+ filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
+ return nil
+ }
+ id := strings.TrimSuffix(d.Name(), ".yaml")
+ var n int
+ if _, scanErr := fmt.Sscanf(id, "%d", &n); scanErr == nil {
+ index[n] = path
+ }
+ return nil
+ })
+ return index
+}
+
+func WalkYAMLDir(dir string, fn func(path, id string, data []byte) error) error {
+ return filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
+ return nil
+ }
+ data, readErr := os.ReadFile(path)
+ if readErr != nil {
+ return nil
+ }
+ id := strings.TrimSuffix(d.Name(), ".yaml")
+ return fn(path, id, data)
+ })
+}
+
+type Duplicate struct {
+ ID string
+ Paths []string
+}
+
+func CheckDuplicateIDs(dir string) []Duplicate {
+ seen := make(map[string][]string)
+ filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
+ return nil
+ }
+ id := strings.TrimSuffix(d.Name(), ".yaml")
+ seen[id] = append(seen[id], path)
+ return nil
+ })
+ var dups []Duplicate
+ for id, paths := range seen {
+ if len(paths) > 1 {
+ dups = append(dups, Duplicate{ID: id, Paths: paths})
+ }
+ }
+ return dups
+}
+
func SuccessChance(cfg SuccessFormula, level int, requiredLevel int) float64 {
chance := cfg.Base + float64(level-requiredLevel)*cfg.PerLevel
if chance > cfg.Cap {
@@ -20,8 +89,26 @@ func SuccessChance(cfg SuccessFormula, level int, requiredLevel int) float64 {
return chance
}
+var (
+ dropIndex map[string]string
+ dropIndexMu sync.Mutex
+)
+
+func loadDropIndex(dataDir string) map[string]string {
+ dropIndexMu.Lock()
+ defer dropIndexMu.Unlock()
+ if dropIndex == nil {
+ dropIndex = BuildPathIndex(filepath.Join(dataDir, "drops"))
+ }
+ return dropIndex
+}
+
func LoadDropTable(dataDir, id string) (*DropTableDef, error) {
- path := filepath.Join(dataDir, "drops", id+".yaml")
+ index := loadDropIndex(dataDir)
+ path, ok := index[id]
+ if !ok {
+ path = filepath.Join(dataDir, "drops", id+".yaml")
+ }
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read drop table %s: %w", id, err)