/
results-io.go
120 lines (97 loc) · 2.86 KB
/
results-io.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
package cmd
import (
"encoding/json"
"os"
"path/filepath"
"github.com/bloznelis/typioca/cmd/words"
)
type TestType = string
type NumericSetting = int
type WordListName = string
type AllPersistedResults = map[TestType]map[NumericSetting]map[WordListName][]PersistentResultsNode
func PersistResults(results Results) PersistentResults {
var resultsFile = getResultsPath()
var persistentResults PersistentResults
//File does not exist?
if _, err := os.Stat(resultsFile); os.IsNotExist(err) {
persistentResults = defaultPersistentResults()
} else {
readResults(&persistentResults)
// XXX: Once needed, version check should happen here
}
persistentResults.addResults(results)
writeResults(persistentResults)
return persistentResults
}
func defaultPersistentResults() PersistentResults {
return PersistentResults{
Results: AllPersistedResults{},
Version: 1,
}
}
func ReadResults(i ResultsIdentifier) []PersistentResultsNode {
var resultsFile = getResultsPath()
var persistentResults PersistentResults
//File does not exist?
if _, err := os.Stat(resultsFile); os.IsNotExist(err) {
persistentResults = defaultPersistentResults()
} else {
readResults(&persistentResults)
// XXX: Once needed, version check should happen here
}
var res = persistentResults.Results[i.testType][i.numeric][i.words]
if res == nil {
return make([]PersistentResultsNode, 0)
}
return res
}
func readResults(results *PersistentResults) {
var resultsFilePath = getResultsPath()
fh, err := os.Open(resultsFilePath)
if err != nil {
panic(err)
}
defer fh.Close()
decoder := json.NewDecoder(fh)
decoder.Decode(results)
}
func (p *PersistentResults) addResults(results Results) {
var limit = 25 // XXX: Should be configurable eventually
var node = PersistentResultsNode{
Wpm: results.wpm,
Accuracy: results.accuracy,
DeltaWpm: results.deltaWpm,
RawWpm: results.rawWpm,
Cpm: results.cpm,
WpmEachSecond: results.wpmEachSecond,
}
var i = results.identifier
if p.Results[i.testType] == nil {
p.Results[i.testType] = map[NumericSetting]map[WordListName][]PersistentResultsNode{}
}
if p.Results[i.testType][i.numeric] == nil {
p.Results[i.testType][i.numeric] = map[WordListName][]PersistentResultsNode{}
}
var nodes = append(p.Results[i.testType][i.numeric][i.words], node)
if len(nodes) > limit {
nodes = nodes[len(nodes)-limit:]
}
p.Results[i.testType][i.numeric][i.words] = nodes
}
func writeResults(results PersistentResults) {
var resultsFilePath = getResultsPath()
words.EnsureDir(resultsFilePath)
fh, err := os.Create(resultsFilePath)
if err != nil {
panic(err)
}
defer fh.Close()
encoder := json.NewEncoder(fh)
encoder.SetIndent("", "\t")
encoder.Encode(results)
}
func getResultsPath() string {
var cachePath = getCachePath()
var resultsFilePath = filepath.Join(cachePath, "results.json")
return resultsFilePath
}