-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_upload.go
351 lines (321 loc) · 9.77 KB
/
score_upload.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/albrow/forms"
)
// ScoreKeys are keys that must appear in a ScoreRecord
var ScoreKeys = []string{
"song_id", "difficulty", "score",
"shiny_perfect_count", "perfect_count", "near_count", "miss_count",
"health", "modifier", "beyond_gauge", "clear_type",
}
var errorZeroRating = errors.New("Rating for this chart is 0")
func init() {
}
func scoreTokenHandler(w http.ResponseWriter, r *http.Request) {
if NeedAuth {
_, err := verifyBearerAuth(r.Header.Get("Authorization"))
if err != nil {
c := Container{false, nil, 203}
http.Error(w, c.toJSON(), http.StatusUnauthorized)
return
}
}
token := new(ScoreToken)
container := Container{true, token, 0}
fmt.Fprint(w, container.toJSON())
}
func scoreUploadHandler(w http.ResponseWriter, r *http.Request) {
result := ScoreUploadResult{true, map[string]int{"user_rating": 0}}
var (
userID int
err error
)
if NeedAuth {
userID, err = verifyBearerAuth(r.Header.Get("Authorization"))
if err != nil {
c := Container{false, nil, 203}
http.Error(w, c.toJSON(), http.StatusUnauthorized)
return
}
} else {
userID = staticUserID
}
record, err := makeRecord(r)
tx, err := db.Begin()
if err != nil {
log.Printf("%s: Can't make transacation object: %s", r.URL.Path, err)
return
}
inserter, err := newInserter(tx, userID)
if err != nil {
tx.Rollback()
log.Println(err)
return
}
targets := []func(*sql.Tx, int, *ScoreRecord) (int, error){
insertScoreRecord,
updateBestScore,
inserter.insert,
updatePlayerRating,
}
var rating int
for _, target := range targets {
if rating, err = target(tx, userID, record); err != nil {
tx.Rollback()
log.Println(err)
http.Error(w, "Server side error", http.StatusInternalServerError)
return
}
}
result.Value["user_rating"] = rating
tx.Commit()
res, err := json.Marshal(result)
if err != nil {
log.Printf("%s: Error occured while generating output content: %s\n", r.URL.Path, err)
return
}
fmt.Fprint(w, string(res))
}
func makeRecord(r *http.Request) (*ScoreRecord, error) {
data, err := forms.Parse(r)
if err != nil {
return nil, fmt.Errorf("error occured while parsing forms: %s", err)
}
val := data.Validator()
for _, key := range ScoreKeys {
val.Require(key)
}
if val.HasErrors() {
for k, v := range val.ErrorMap() {
log.Printf(r.URL.Path, ": %s - %s\n", k, v)
}
return nil, fmt.Errorf("score record received lacks of necessary filed(s) in form")
}
record := scoreRecordFromForm(data)
record.TimePlayed = time.Now().Unix()
err = record.scoreToRating()
if err != nil {
return nil, err
}
return record, nil
}
func insertScoreRecord(tx *sql.Tx, userID int, record *ScoreRecord) (int, error) {
_, err := tx.Exec(sqlStmtInsertScore,
userID,
record.TimePlayed,
record.SongID,
record.Difficulty,
record.Score,
record.Shiny,
record.Pure,
record.Far,
record.Lost,
record.Rating,
record.Health,
record.ClearType,
)
if err != nil {
return 0, fmt.Errorf("error occured while inserting new score record: %w", err)
}
return 0, nil
}
func updateBestScore(tx *sql.Tx, userID int, record *ScoreRecord) (int, error) {
var (
score int
playedDate int64
)
err := tx.QueryRow(
sqlStmtLookupBestScore, userID, record.SongID, record.Difficulty,
).Scan(&score, &playedDate)
if err == sql.ErrNoRows {
_, err = tx.Exec(sqlStmtInsertBestScore, userID, record.TimePlayed)
if err != nil {
return 0, fmt.Errorf("error occured while insert new best score: %w", err)
}
} else if err != nil {
return 0, fmt.Errorf("error occured while looking up best score: %w", err)
} else if record.Score > score {
_, err = tx.Exec(sqlStmtReplaceBestScore, record.TimePlayed, playedDate)
if err != nil {
return 0, fmt.Errorf("error occured while replacing best score: %w", err)
}
}
return 0, nil
}
type recentScoreItem struct {
playedDate int64
rating float64
}
type recentScoreInserter struct {
r10 map[string]*recentScoreItem
normalItems []*recentScoreItem
}
func newInserter(tx *sql.Tx, userID int) (*recentScoreInserter, error) {
rows, err := tx.Query(sqlStmtLookupRecentScore, userID)
if err != nil {
return nil, err
}
defer rows.Close()
inserter := &recentScoreInserter{
r10: map[string]*recentScoreItem{},
normalItems: []*recentScoreItem{},
}
iden := ""
isR10 := ""
for rows.Next() {
item := new(recentScoreItem)
rows.Scan(&item.rating, &item.playedDate, &iden, &isR10)
if isR10 == "t" {
inserter.r10[iden] = item
} else {
inserter.normalItems = append(inserter.normalItems, item)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return inserter, nil
}
func (inserter *recentScoreInserter) insert(tx *sql.Tx, userID int, record *ScoreRecord) (int, error) {
// (newItem recentScoreItem, newIdentifier string, score int, clearType int8)
target := &recentScoreItem{
rating: record.Rating,
playedDate: record.TimePlayed,
}
newIdentifier := fmt.Sprintf("%s%d", record.SongID, record.Difficulty)
if target, replacement, isR10, needNewR10, err := inserter.insertIntoR10(tx, userID, newIdentifier, target, record.Score, record.ClearType); err != nil {
return 0, err
} else if err = inserter.insertIntoNormalItem(tx, userID, target, replacement, isR10, needNewR10); err != nil {
return 0, err
}
return 0, nil
}
func (inserter *recentScoreInserter) insertIntoR10(tx *sql.Tx, userID int, identifier string, target *recentScoreItem, score int, clearType int8) (*recentScoreItem, *recentScoreItem, bool, bool, error) {
// target may change during trying to insert it into r10, ret_target is
// the final target in this process, and the starting target for next
// process (insert into normat item).
var retTarget *recentScoreItem = nil
// candidate record that current target will possiblely replace.
var replacement *recentScoreItem = nil
// wheather current target record should be marked as an r10.
isR10 := false
// need_new_r10, if true, record with highest rating among normal item
// will become a new r10 item.
needNewR10 := false
if record, ok := inserter.r10[identifier]; ok {
if record.rating <= target.rating {
if _, err := tx.Exec(sqlStmtReplaceRecnetScore, target.playedDate, "t", userID, record.playedDate); err != nil {
return retTarget, replacement, isR10, needNewR10, err
}
retTarget = record
} else {
retTarget = target
}
} else {
r30NotFull := len(inserter.r10)+len(inserter.normalItems) < 30
if len(inserter.r10) < 10 {
if r30NotFull {
if _, err := tx.Exec(sqlStmtInsertRecentScore, userID, target.playedDate, "t"); err != nil {
return retTarget, replacement, isR10, needNewR10, err
}
// no need for further process, no target any more
retTarget = nil
} else {
needNewR10 = true
}
} else {
isEx := score >= 9_800_000
isHardClear := clearType == 5
for _, item := range inserter.r10 {
if (isEx || isHardClear || r30NotFull) && target.rating < item.rating {
continue
}
if item.rating <= target.rating {
isR10 = true
}
if replacement == nil {
replacement = item
} else if item.playedDate < replacement.playedDate {
replacement = item
}
}
if isR10 {
if _, err := tx.Exec(sqlStmtReplaceRecnetScore, target.playedDate, "t", userID, replacement.playedDate); err != nil {
return retTarget, replacement, isR10, needNewR10, err
}
retTarget = replacement
isR10 = false
replacement = nil
} else {
needNewR10 = true
}
}
}
return retTarget, replacement, isR10, needNewR10, nil
// Possible return values:
// None, None, false, false. When both r10 and r30 is not full.
// Some, None, true, false. When r10 is not full but r30 is full.
// Some, Some, false, true. When new record can't be insert into r10.
// Some, None, false, false. When new record's identifier collides, or
// new record insert into r10 and take a old
// record out of r10 group
}
func (inserter *recentScoreInserter) insertIntoNormalItem(tx *sql.Tx, userID int, target *recentScoreItem, replacement *recentScoreItem, isR10 bool, needNewR10 bool) error {
if target == nil {
return nil
}
if isR10 {
// is_r10 will be true only when r10 is not full but r30 is.
if _, err := tx.Exec(sqlStmtReplaceRecnetScore, target.playedDate, "t", userID, inserter.normalItems[0].playedDate); err != nil {
return err
}
target = inserter.normalItems[0]
}
if len(inserter.r10)+len(inserter.normalItems) < 30 {
if _, err := tx.Exec(sqlStmtInsertRecentScore, userID, target.playedDate, ""); err != nil {
return err
}
return nil
}
for _, item := range inserter.normalItems {
if replacement == nil {
replacement = item
} else if item.playedDate < replacement.playedDate {
replacement = item
needNewR10 = false
}
}
if replacement.playedDate != target.playedDate {
if _, err := tx.Exec(sqlStmtReplaceRecnetScore, target.playedDate, "", userID, replacement.playedDate); err != nil {
return err
}
}
if needNewR10 {
// if need_new_r10 is true, record being replaced is in r10, so it's
// safe to directly take highest rating record from normal item group
// as new a r10 record.
newR10 := inserter.normalItems[0].playedDate
if _, err := tx.Exec(sqlStmtInsertRecentScore, newR10, "t", userID, newR10); err != nil {
return err
}
}
return nil
}
func updatePlayerRating(tx *sql.Tx, userID int, _ *ScoreRecord) (int, error) {
var rating int
err := tx.QueryRow(sqlStmtComputeRating, userID).Scan(&rating)
if err != nil {
return rating, fmt.Errorf("error occured while compute user rating: %w", err)
}
if _, err := tx.Exec(sqlStmtUpdateRating, rating, userID); err != nil {
return rating, fmt.Errorf("error occured while modifying rating of user: %d: %w", userID, err)
}
return rating, nil
}