-
Notifications
You must be signed in to change notification settings - Fork 0
/
elo.go
51 lines (41 loc) · 902 Bytes
/
elo.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
package main
import "math"
type Elo struct {
k float64
}
type EloDict struct {
Player Player
Rank int
}
type ByRank []*EloDict
func (a ByRank) Len() int { return len(a) }
func (a ByRank) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRank) Less(i, j int) bool {
if a[i].Rank != a[j].Rank {
return a[i].Rank > a[j].Rank
}
return a[i].Player.Nickname < a[j].Player.Nickname
}
func NewEloDict(p Player) *EloDict {
return &EloDict{Rank: 1000, Player: p}
}
func (e *Elo) getExpected(a, b int) float64 {
return float64(1) / (1 + math.Pow(10, float64((b-a))/400))
}
func pow(a, b int) int {
p := 1
for b > 0 {
if b&1 != 0 {
p *= a
}
b >>= 1
a *= a
}
return p
}
func round(f float64) int {
return int(math.Floor(f + .5))
}
func (e *Elo) updateRating(expected float64, actual float64, current int) int {
return round(float64(current) + e.k*(actual-expected))
}