diff options
Diffstat (limited to 'internal/action/recipe.go')
| -rw-r--r-- | internal/action/recipe.go | 110 |
1 files changed, 43 insertions, 67 deletions
diff --git a/internal/action/recipe.go b/internal/action/recipe.go index d1acaa5..c1f30fb 100644 --- a/internal/action/recipe.go +++ b/internal/action/recipe.go @@ -1,7 +1,6 @@ package action import ( - "fmt" "os" "path/filepath" @@ -34,13 +33,52 @@ type RecipeDef struct { type RecipeStore struct { dataDir string + cache map[string][]RecipeDef } func NewRecipeStore(dataDir string) *RecipeStore { - return &RecipeStore{dataDir: dataDir} + return &RecipeStore{dataDir: dataDir, cache: make(map[string][]RecipeDef)} } func (s *RecipeStore) LoadAll() ([]RecipeDef, error) { + return s.LoadByType("") +} + +func (s *RecipeStore) LoadByType(recipeType string) ([]RecipeDef, error) { + if recipeType == "" { + recipeType = "_all" + } + if cached, ok := s.cache[recipeType]; ok { + result := make([]RecipeDef, len(cached)) + copy(result, cached) + return result, nil + } + + all, err := s.loadAllRaw() + if err != nil { + return nil, err + } + + if recipeType == "_all" { + s.cache[recipeType] = all + result := make([]RecipeDef, len(all)) + copy(result, all) + return result, nil + } + + var filtered []RecipeDef + for _, r := range all { + if r.Type == recipeType { + filtered = append(filtered, r) + } + } + s.cache[recipeType] = filtered + result := make([]RecipeDef, len(filtered)) + copy(result, filtered) + return result, nil +} + +func (s *RecipeStore) loadAllRaw() ([]RecipeDef, error) { dir := filepath.Join(s.dataDir, "recipes") entries, err := os.ReadDir(dir) if err != nil { @@ -76,14 +114,11 @@ func (r *RecipeDef) MatchesEntry(itemID string) bool { } func (s *RecipeStore) FindByInput(recipeType string, itemID string) *RecipeDef { - all, err := s.LoadAll() + all, err := s.LoadByType(recipeType) if err != nil { return nil } for i := range all { - if recipeType != "" && all[i].Type != recipeType { - continue - } for _, e := range all[i].Consume { for _, id := range e.Items { if id == itemID { @@ -95,54 +130,9 @@ func (s *RecipeStore) FindByInput(recipeType string, itemID string) *RecipeDef { return nil } -func (s *RecipeStore) FindByItem(itemID, recipeType string) ([]RecipeDef, error) { - all, err := s.LoadAll() - if err != nil { - return nil, err - } - var matches []RecipeDef - for _, r := range all { - if recipeType != "" && r.Type != recipeType { - continue - } - if r.MatchesEntry(itemID) { - matches = append(matches, r) - } - } - return matches, nil -} - -func (s *RecipeStore) FindByItems(a, b string) ([]RecipeDef, error) { - all, err := s.LoadAll() - if err != nil { - return nil, err - } - var matches []RecipeDef - for _, r := range all { - if len(r.Consume) < 2 { - continue - } - aEntry := -1 - bEntry := -1 - for ei, e := range r.Consume { - for _, id := range e.Items { - if id == a && aEntry < 0 { - aEntry = ei - } - if id == b && bEntry < 0 { - bEntry = ei - } - } - } - if aEntry >= 0 && bEntry >= 0 && aEntry != bEntry { - matches = append(matches, r) - } - } - return matches, nil -} func (s *RecipeStore) FindByStation(itemID string, stationIDs []string, recipeType string) ([]RecipeDef, error) { - all, err := s.LoadAll() + all, err := s.LoadByType(recipeType) if err != nil { return nil, err } @@ -181,21 +171,7 @@ func (r *RecipeDef) EffectiveSkill() string { return r.Type } -func (s *RecipeStore) FindByNameOrID(query string, recipeType string) (*RecipeDef, error) { - all, err := s.LoadAll() - if err != nil { - return nil, err - } - for _, r := range all { - if recipeType != "" && r.Type != recipeType { - continue - } - if r.ID == query || r.Output == query { - return &r, nil - } - } - return nil, fmt.Errorf("recipe not found: %s", query) -} + func (r *RecipeDef) FirstItemName(load func(string) (string, bool)) string { for _, e := range r.Consume { |
