-
Notifications
You must be signed in to change notification settings - Fork 4
/
cache_data.go
220 lines (208 loc) · 5 KB
/
cache_data.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"strconv"
"github.com/fatih/color"
"zxq.co/ripple/ocl"
)
type s struct {
rankedScore int64
totalHits int64
level int
playTime int64
}
type mostPlayedK struct {
userID int
playMode int
beatmapID int
}
func opCacheData() {
defer wg.Done()
// get data
const fetchQuery = `
SELECT
scores.userid, scores.play_mode,
scores.score, scores.completed, scores.300_count,
scores.100_count, scores.50_count, scores.playtime, beatmaps.beatmap_id
FROM scores JOIN beatmaps USING(beatmap_md5)`
rows, err := db.Query(fetchQuery)
if err != nil {
queryError(err, fetchQuery)
return
}
// set up end map where all the data is
data := make(map[int]*[4]*s)
var mostPlayedData map[mostPlayedK]int
if c.CacheMostPlayedBeatmaps {
mostPlayedData = make(map[mostPlayedK]int)
}
count := 0
// analyse every result row of fetchQuery
for rows.Next() {
if count%1000 == 0 {
verboseln("> CacheData:", count)
}
var (
uid int
playMode int
score int64
completed int
count300 int
count100 int
count50 int
playTime int
beatmapID int
)
err := rows.Scan(
&uid, &playMode, &score, &completed, &count300, &count100, &count50, &playTime, &beatmapID,
)
if err != nil {
queryError(err, fetchQuery)
continue
}
// silently ignore invalid modes
if playMode > 3 || playMode < 0 {
continue
}
// create key in map if not already existing
if _, ex := data[uid]; !ex {
data[uid] = &[4]*s{}
for i := 0; i < 4; i++ {
data[uid][i] = &s{}
}
}
// if the score counts as completed and top score, add it to the ranked score sum
if c.CacheRankedScore && completed == 3 {
data[uid][playMode].rankedScore += score
}
// add to the number of totalhits count of {300,100,50} hits
if c.CacheTotalHits {
data[uid][playMode].totalHits += int64(count300) + int64(count100) + int64(count50)
}
// play time
if c.CachePlayTime {
data[uid][playMode].playTime += int64(playTime)
}
// most played beatmaps
if c.CacheMostPlayedBeatmaps {
mostPlayedData[mostPlayedK{uid, playMode, beatmapID}]++
}
count++
}
rows.Close()
if c.CacheLevel {
const totalScoreQuery = "SELECT id, total_score_std, total_score_taiko, total_score_ctb, total_score_mania FROM users_stats"
rows, err := db.Query(totalScoreQuery)
if err != nil {
queryError(err, totalScoreQuery)
return
}
count = 0
for rows.Next() {
if count%100 == 0 {
verboseln("> CacheLevel:", count)
}
var (
id int
std int64
taiko int64
ctb int64
mania int64
)
err := rows.Scan(&id, &std, &taiko, &ctb, &mania)
if err != nil {
queryError(err, totalScoreQuery)
continue
}
if _, ex := data[id]; !ex {
data[id] = &[4]*s{}
for i := 0; i < 4; i++ {
data[id][i] = &s{}
}
}
data[id][0].level = ocl.GetLevel(std)
data[id][1].level = ocl.GetLevel(taiko)
data[id][2].level = ocl.GetLevel(ctb)
data[id][3].level = ocl.GetLevel(mania)
count++
}
rows.Close()
}
if c.CacheMostPlayedBeatmaps {
// Blocks until the table has been truncated
// verboseln("> MostPlayedBeatmaps: Truncating table")
// runOperation(operation{"TRUNCATE TABLE users_beatmap_playcount", nil})
// verboseln("> MostPlayedBeatmaps: Table truncated")
// Start populating the table once it's been truncated
done, ignored := 0, 0
for k, v := range mostPlayedData {
if v < 3 {
ignored++
if ignored%1000 == 0 {
verboseln("> MostPlayedBeatmaps: Ignored", ignored)
}
} else {
op("INSERT INTO users_beatmap_playcount (user_id, beatmap_id, game_mode, playcount)"+
"VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE playcount = ?", k.userID, k.beatmapID, k.playMode, v, v)
done++
if done%1000 == 0 {
verboseln("> MostPlayedBeatmaps: Done", done)
}
}
delete(mostPlayedData, k)
}
}
for k, v := range data {
if v == nil {
continue
}
for modeInt, modeData := range v {
if modeData == nil {
continue
}
var setQ string
var params []interface{}
if c.CacheRankedScore {
setQ += "ranked_score_" + modeToString(modeInt) + " = ?"
params = append(params, (*modeData).rankedScore)
}
if c.CacheTotalHits {
if setQ != "" {
setQ += ", "
}
setQ += "total_hits_" + modeToString(modeInt) + " = ?"
params = append(params, (*modeData).totalHits)
}
if c.CacheLevel {
if setQ != "" {
setQ += ", "
}
setQ += "level_" + modeToString(modeInt) + " = ?"
params = append(params, (*modeData).level)
}
if c.CachePlayTime {
if setQ != "" {
setQ += ", "
}
setQ += "playtime_" + modeToString(modeInt) + " = ?"
params = append(params, (*modeData).playTime)
}
if setQ != "" {
params = append(params, k)
op("UPDATE users_stats SET "+setQ+" WHERE id = ?", params...)
}
}
}
color.Green("> CacheData: done!")
}
var modes = [...]string{
"std",
"taiko",
"ctb",
"mania",
}
func modeToString(modeID int) string {
if modeID < len(modes) {
return modes[modeID]
}
return strconv.Itoa(modeID)
}