diff options
| author | historia <[not public]> | 2026-06-19 18:20:26 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-19 18:20:26 -0400 |
| commit | 0ea4eec5dd2122c6704216971eb1249276297867 (patch) | |
| tree | 430fbbef04e837820f1d031c190f52ecd6112e25 /internal/action/store.go | |
| parent | 3a58125d14bb307f861b38c6c5b0a63babde7e08 (diff) | |
| download | thehouseoficarus-0ea4eec5dd2122c6704216971eb1249276297867.tar.gz | |
shop fixes, 'toggle' completely removed, movement speed increased
Diffstat (limited to 'internal/action/store.go')
| -rw-r--r-- | internal/action/store.go | 107 |
1 files changed, 5 insertions, 102 deletions
diff --git a/internal/action/store.go b/internal/action/store.go index fd58281..a8db9df 100644 --- a/internal/action/store.go +++ b/internal/action/store.go @@ -5,107 +5,10 @@ import ( "math/rand" "os" "path/filepath" - "sync" "gopkg.in/yaml.v3" ) -type Store struct { - dataDir string - mu sync.Mutex - cache map[string]*RawBehavior -} - -type RawBehavior struct { - ID string - Type string - Raw map[string]any -} - -func NewStore(dataDir string) *Store { - return &Store{ - dataDir: dataDir, - cache: make(map[string]*RawBehavior), - } -} - -func (s *Store) Load(id string) (*RawBehavior, error) { - s.mu.Lock() - if b, ok := s.cache[id]; ok { - s.mu.Unlock() - return b, nil - } - s.mu.Unlock() - - path := filepath.Join(s.dataDir, "behaviors", id+".yaml") - data, err := os.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("read behavior %s: %w", id, err) - } - - var raw map[string]any - if err := yaml.Unmarshal(data, &raw); err != nil { - return nil, fmt.Errorf("parse behavior %s: %w", id, err) - } - - rb := &RawBehavior{ID: id, Raw: raw} - if t, ok := raw["type"].(string); ok { - rb.Type = t - } - if rid, ok := raw["id"].(string); ok { - rb.ID = rid - } - - s.mu.Lock() - s.cache[id] = rb - s.mu.Unlock() - return rb, nil -} - -func unmarshalRaw[T any](rb *RawBehavior, expectedType string) (*T, error) { - if rb.Type != expectedType { - return nil, fmt.Errorf("behavior %s is type %s, expected %s", rb.ID, rb.Type, expectedType) - } - var cfg T - raw, _ := yaml.Marshal(rb.Raw) - if err := yaml.Unmarshal(raw, &cfg); err != nil { - return nil, fmt.Errorf("parse %s config %s: %w", expectedType, rb.ID, err) - } - return &cfg, nil -} - -func (s *Store) LoadGather(id string) (*GatherConfig, error) { - rb, err := s.Load(id) - if err != nil { - return nil, err - } - return unmarshalRaw[GatherConfig](rb, "gather") -} - -func (s *Store) LoadTalk(id string) (*TalkConfig, error) { - rb, err := s.Load(id) - if err != nil { - return nil, err - } - return unmarshalRaw[TalkConfig](rb, "talk") -} - -func (s *Store) LoadUse(id string) (*UseConfig, error) { - rb, err := s.Load(id) - if err != nil { - return nil, err - } - return unmarshalRaw[UseConfig](rb, "use") -} - -func (s *Store) LoadToggle(id string) (*ToggleConfig, error) { - rb, err := s.Load(id) - if err != nil { - return nil, err - } - return unmarshalRaw[ToggleConfig](rb, "toggle") -} - func SuccessChance(cfg SuccessFormula, level int, requiredLevel int) float64 { chance := cfg.Base + float64(level-requiredLevel)*cfg.PerLevel if chance > cfg.Cap { @@ -117,8 +20,8 @@ func SuccessChance(cfg SuccessFormula, level int, requiredLevel int) float64 { return chance } -func (s *Store) LoadDropTable(id string) (*DropTableDef, error) { - path := filepath.Join(s.dataDir, "drops", id+".yaml") +func LoadDropTable(dataDir, id string) (*DropTableDef, error) { + 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) @@ -130,7 +33,7 @@ func (s *Store) LoadDropTable(id string) (*DropTableDef, error) { return &dt, nil } -func (s *Store) ResolveDrop(drops []DropEntry) *DropEntry { +func ResolveDrop(dataDir string, drops []DropEntry) *DropEntry { if len(drops) == 0 { return nil } @@ -147,9 +50,9 @@ func (s *Store) ResolveDrop(drops []DropEntry) *DropEntry { cumulative += drops[i].Weight if roll < cumulative { if drops[i].Table != "" { - sub, err := s.LoadDropTable(drops[i].Table) + sub, err := LoadDropTable(dataDir, drops[i].Table) if err == nil { - if resolved := s.ResolveDrop(sub.Drops); resolved != nil { + if resolved := ResolveDrop(dataDir, sub.Drops); resolved != nil { qty := drops[i].Quantity if qty <= 0 { qty = resolved.Quantity |
