-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscore.go
54 lines (43 loc) · 1.31 KB
/
score.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
package main
import (
"math"
"time"
"unicode/utf8"
)
const (
speedErrorRatio = 0.2
scoreScalar = 100.
scoreExponent = 2.3
scoreCharFactor = 10.
)
// Score between 0 and 1, 0.5 at 2 CPS
func speedScore(text string, time time.Duration) float64 {
spc := time.Seconds() / float64(utf8.RuneCountInString(text))
return 1. / (1. + 2*spc)
}
// Score between 0 and 1, 0.5 at 1 error
func errorScore(text string, errors int) float64 {
return 1. / (1. + float64(errors))
}
func score(text string, time time.Duration, errors int) float64 {
return speedErrorRatio*speedScore(text, time) +
(1-speedErrorRatio)*errorScore(text, errors)
}
func finalScore(text string, fast, slow, normal float64) float64 {
return maxScore(text) * math.Pow(0.15*fast+0.35*slow+0.5*normal, 2)
}
func maxScore(text string) float64 {
return scoreCharFactor * float64(utf8.RuneCountInString(text))
}
func requiredScore(level int) float64 {
return scoreScalar * math.Pow(float64(level), scoreExponent)
}
func level(score float64) int {
return int(math.Pow(score/scoreScalar, 1./scoreExponent))
}
func progress(score float64) float64 {
currentLevel := level(score)
currentLevelScore := requiredScore(currentLevel)
nextLevelScore := requiredScore(currentLevel + 1)
return (score - currentLevelScore) / (nextLevelScore - currentLevelScore)
}