forked from osuripple/ripple-cron-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_pp.go
81 lines (70 loc) · 1.67 KB
/
calculate_pp.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
package main
import (
"math"
"github.com/fatih/color"
)
type ppUserMode struct {
countScores int
ppTotal int
}
func opCalculatePP() {
defer wg.Done()
const ppQuery = "SELECT scores.userid, pp, scores.play_mode FROM scores INNER JOIN users ON users.id=scores.userid JOIN beatmaps USING(beatmap_md5) WHERE completed = 3 AND ranked >= 2 AND disable_pp = 0 AND pp IS NOT NULL ORDER BY pp DESC"
rows, err := db.Query(ppQuery)
if err != nil {
queryError(err, ppQuery)
return
}
users := make(map[int]*[4]*ppUserMode)
var count int
for rows.Next() {
if count%1000 == 0 {
verboseln("> CalculatePP:", count)
}
var (
userid int
ppAmt *float64
playMode int
)
err := rows.Scan(&userid, &ppAmt, &playMode)
if err != nil {
queryError(err, ppQuery)
continue
}
if ppAmt == nil {
continue
}
if users[userid] == nil {
users[userid] = &[4]*ppUserMode{
new(ppUserMode),
new(ppUserMode),
new(ppUserMode),
new(ppUserMode),
}
}
if users[userid][playMode].countScores > 500 {
continue
}
currentScorePP := round(round(*ppAmt) * math.Pow(0.95, float64(users[userid][playMode].countScores)))
users[userid][playMode].countScores++
users[userid][playMode].ppTotal += int(currentScorePP)
count++
}
rows.Close()
for userid, pps := range users {
for mode, ppUM := range *pps {
op("UPDATE users_stats SET pp_"+modeToString(mode)+" = ? WHERE id = ? LIMIT 1", ppUM.ppTotal, userid)
}
}
color.Green("> CalculatePP: done!")
if c.PopulateRedis {
verboseln("Starting to populate redis")
go opPopulateRedis()
}
}
func round(a float64) float64 {
if a < 0 {
return math.Ceil(a - 0.5)
}
return math.Floor(a + 0.5)
}