-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
144 lines (110 loc) · 3.23 KB
/
collection.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package collection
import (
"fmt"
"github.com/vikpe/qw-demobot/internal/pkg/demo/export"
"github.com/vikpe/qw-demobot/internal/pkg/demo/info"
"github.com/vikpe/qw-demobot/internal/pkg/futil"
"sort"
"strings"
)
type Collection struct {
Path string
}
func New(path string) *Collection {
return &Collection{
path,
}
}
func (d *Collection) GetStage(demoName string) string {
filePath, err := futil.FindFilepath(d.Path, demoName)
if err != nil || strings.Contains(filePath, "unsorted") {
return ""
}
parts := strings.SplitN(demoName, "_", 3)
return parts[1]
}
func (d *Collection) GetMvdParserFilename(demoName string) string {
return demoName + ".mvd.json"
}
func (d *Collection) GetInfo(demoName string) (info.Info, error) {
infoFilename := d.GetMvdParserFilename(demoName)
infoFilePath, err := futil.FindFilepath(d.Path, infoFilename)
if err != nil {
return info.Info{}, err
}
return info.NewFromMvdparserDataFile(infoFilePath)
}
func (d *Collection) GetFilename(demoName string) (string, error) {
mvdFilename := demoName + ".mvd"
if futil.DirHasFile(d.Path, mvdFilename) {
return mvdFilename, nil
}
demFilename := demoName + ".dem"
if futil.DirHasFile(d.Path, demFilename) {
return demFilename, nil
}
return "", fmt.Errorf("Data not found: %s", demoName)
}
func (d *Collection) GetAbsPath(demoName string) (string, error) {
mvdFilename := demoName + ".mvd"
path, err := futil.FindFilepath(d.Path, mvdFilename)
if err == nil {
return path, nil
}
demFilename := demoName + ".dem"
path, err = futil.FindFilepath(d.Path, demFilename)
if err == nil {
return path, nil
}
return "", fmt.Errorf("Data not found: %s", demoName)
}
func (d *Collection) GetEventInfo(demoName string) string {
filename, err := d.GetFilename(demoName)
if err != nil || strings.HasSuffix(filename, ".dem") {
return ""
}
infoFilename := d.GetMvdParserFilename(demoName)
infoFilePath, err := futil.FindFilepath(d.Path, infoFilename)
if err != nil || !strings.Contains(infoFilePath, "/tournaments") {
return ""
}
relPath := strings.TrimPrefix(infoFilePath, d.Path+"/")
dirs := strings.SplitN(relPath, "/", 3)
eventName := strings.ReplaceAll(dirs[1], "_", " ")
if strings.Count(demoName, "_") < 3 {
return eventName
}
parts := strings.SplitN(demoName, "_", 3)
stage := parts[1]
return fmt.Sprintf("%s %s", eventName, stage)
}
func (d *Collection) GetTitle(demoName string) string {
info, err := d.GetInfo(demoName)
if err != nil {
return demoName
}
return info.GetTitle()
}
func (d *Collection) GetSha256(demoName string) (string, error) {
fileName, err := d.GetFilename(demoName)
if err != nil {
return "", err
}
return futil.FindFileSha256(d.Path, fileName)
}
func (d *Collection) Export(itemCallback func(string, export.Export) export.Export) []export.Export {
infoPaths := futil.FindFilepathsByExtension(d.Path, ".mvd.json")
sort.Strings(infoPaths)
result := make([]export.Export, 0)
// collect demos
for _, infoPath := range infoPaths {
demoExport, err := export.NewFromInfoPath(infoPath)
if err != nil {
fmt.Println("unable to export", infoPath)
continue
}
demoPath := strings.ReplaceAll(infoPath, ".mvd.json", ".json")
result = append(result, itemCallback(demoPath, demoExport))
}
return result
}