1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
}
|