summaryrefslogtreecommitdiff
path: root/ingest
diff options
context:
space:
mode:
authorewy <ewy0@protonmail.com>2026-05-22 16:54:49 +0200
committerewy <ewy0@protonmail.com>2026-05-22 16:54:49 +0200
commit007e2de369f9fc26da3237646de14f2af5052ee8 (patch)
treef81557385628fc93f1ef9616b8bc75a304f9d740 /ingest
initial commit
Diffstat (limited to 'ingest')
-rw-r--r--ingest/run.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/ingest/run.go b/ingest/run.go
new file mode 100644
index 0000000..87b86b1
--- /dev/null
+++ b/ingest/run.go
@@ -0,0 +1,74 @@
+package ingest
+
+import (
+ "io/fs"
+ "os"
+ "path/filepath"
+ "slices"
+ "sts2stats/model"
+ "sts2stats/spool"
+ "sts2stats/stats"
+ "sts2stats/storage"
+ "sync"
+)
+
+type Profile struct {
+ RunSaves []*model.Run
+}
+
+var SkippedExtensions = []string{".backup"}
+
+// AddProfile adds a profile by reading from a folder.
+// the path should be something like ~/.local/share/SlayTheSpire2/steam/<steamid>/profile1
+func AddProfile(f fs.FS, index int) error {
+ existingRows, err := storage.Query("SELECT RunId FROM RunStat WHERE InProgress = 0")
+ if err != nil {
+ return err
+ }
+ var existingRunIds []string
+ for existingRows.Next() {
+ var id string
+ err = existingRows.Scan(&id)
+ if err != nil {
+ return err
+ }
+ existingRunIds = append(existingRunIds, id)
+ }
+
+ var runs []*model.Run
+ wg := sync.WaitGroup{}
+ err = fs.WalkDir(f, "saves/history", func(path string, d fs.DirEntry, err error) error {
+ if d == nil || d.IsDir() {
+ return nil
+ }
+ ext := filepath.Ext(path)
+ if ext != ".run" {
+ if !slices.Contains(SkippedExtensions, ext) {
+ spool.Warn("skipping file with unexpected file extension: %s\n", path)
+ }
+ return nil
+ }
+ content, err := os.ReadFile(path)
+ save, err := model.NewRun(content)
+ if slices.Contains(existingRunIds, save.RunId) {
+ return nil
+ }
+ if err != nil {
+ return err
+ }
+ wg.Go(func() {
+ err := stats.Enrich(save)
+ if err != nil {
+ spool.Warn("%v\n", err)
+ }
+ })
+ runs = append(runs, &save)
+ return nil
+ })
+ if err != nil {
+ return err
+ }
+ wg.Wait()
+ spool.Warn("indexed all runs")
+ return nil
+}