rating.gno
5.23 Kb · 182 lines
1package standings
2
3const (
4 startRating = 1000
5 maxRating = 0xFFFF // a ladder key holds the rating in 2 bytes
6 playerK = 32
7 clanK = 16
8 maxClans = 255 // a clan number fills one byte of a standing and a result entry
9 maxPlayerCount = 0xFFFF // a player's counts are stored in 2 bytes
10 maxClanCount = 0xFFFFFFFF
11)
12
13// clanRecord is a clan's rating and its record in rated matches against other clans.
14type clanRecord struct {
15 name string
16 rating, wins, losses, draws int
17}
18
19// expectedTable is the expected score, in thousandths, of a side whose opponent is rated
20// 0, 25, …, 400 points above it: 1 / (1 + 10^(d / 400)), rounded.
21var expectedTable = [17]int{500, 464, 429, 394, 360, 327, 297, 267, 240, 215, 192, 170, 151, 133, 118, 104, 91}
22
23// expected is the expected score, in thousandths, of a side rated a against one rated b.
24// The gap is capped at 400 points and rounded to the nearest 25.
25func expected(a, b int) int {
26 d := b - a
27 if d < 0 {
28 return 1000 - expected(b, a)
29 }
30 if d > 400 {
31 d = 400
32 }
33 return expectedTable[(d+12)/25]
34}
35
36// eloChange is the change, rounded to the nearest point, of a side rated a that scored
37// score thousandths (1000 a win, 500 a draw, 0 a loss) against one rated b.
38func eloChange(a, b, score, k int) int {
39 x := k * (score - expected(a, b))
40 if x < 0 {
41 return -((-x + 500) / 1000)
42 }
43 return (x + 500) / 1000
44}
45
46// applyChange gives a the change delta and b its negative, each kept within 0 to maxRating.
47func applyChange(a, b, delta int) (int, int) {
48 return clampRating(a + delta), clampRating(b - delta)
49}
50
51func clampRating(r int) int {
52 if r < 0 {
53 return 0
54 }
55 if r > maxRating {
56 return maxRating
57 }
58 return r
59}
60
61// rate counts a match between a, playing clan number ca, and b, playing clan number cb,
62// in which a scored score thousandths: in both standings, on the ladder, and in the clan
63// records when both clans are counted and differ.
64func rate(a, b address, ca, cb, score int) {
65 sa, sb := ratedStanding(a), ratedStanding(b)
66 sa.rating, sb.rating = applyChange(sa.rating, sb.rating, eloChange(sa.rating, sb.rating, score, playerK))
67 sa.count(score, ca)
68 sb.count(1000-score, cb)
69 ladder, _ = ladder.Set(ladderKey(sa.rating, a), nil)
70 ladder, _ = ladder.Set(ladderKey(sb.rating, b), nil)
71 saveStanding(a, sa)
72 saveStanding(b, sb)
73
74 if ca == cb || ca == 0 || cb == 0 {
75 return
76 }
77 x, y := &clans[ca-1], &clans[cb-1]
78 x.rating, y.rating = applyChange(x.rating, y.rating, eloChange(x.rating, y.rating, score, clanK))
79 tally(score, &x.wins, &x.losses, &x.draws, maxClanCount)
80 tally(1000-score, &y.wins, &y.losses, &y.draws, maxClanCount)
81}
82
83// ratedStanding returns a's standing, created at startRating for a first rated match, and
84// takes its key off the ladder until it is counted.
85func ratedStanding(a address) *standing {
86 st := loadStanding(a)
87 if st == nil {
88 st = &standing{rating: startRating}
89 } else {
90 ladder, _, _, _ = ladder.Remove(ladderKey(st.rating, a))
91 }
92 return st
93}
94
95// loadStanding returns a's standing, nil before their first rated match.
96func loadStanding(a address) *standing {
97 return decodeStanding(stringAt(&players, string(a)))
98}
99
100func saveStanding(a address, st *standing) {
101 players.Set(string(a), encodeStanding(st))
102}
103
104// clanNumber is the clan number of name, its first-seen position plus one. A name not
105// seen before gets the next number, or 0, no clan, once maxClans are numbered: a writer
106// that fills the table must not abort another writer's endings.
107func clanNumber(name string) int {
108 for i, c := range clans {
109 if c.name == name {
110 return i + 1
111 }
112 }
113 if len(clans) == maxClans {
114 return 0
115 }
116 clans = append(clans, clanRecord{name: name, rating: startRating})
117 return len(clans)
118}
119
120// scoreOf is the challenger's score, in thousandths, in a match that ended with result.
121func scoreOf(result string) int {
122 switch result {
123 case "a":
124 return 1000
125 case "b":
126 return 0
127 case "draw":
128 return 500
129 }
130 panic("unknown result")
131}
132
133// count adds a match scored score thousandths and played as clan number clan, 0 for none.
134// perClan grows only as far as the clan numbers s has played.
135func (s *standing) count(score, clan int) {
136 tally(score, &s.wins, &s.losses, &s.draws, maxPlayerCount)
137 s.lastClan = clan
138 if clan == 0 {
139 return
140 }
141 for len(s.perClan) < clan {
142 s.perClan = append(s.perClan, 0)
143 }
144 if s.perClan[clan-1] < maxPlayerCount {
145 s.perClan[clan-1]++
146 }
147}
148
149// tally adds a win, loss or draw for score, each count stopping at ceiling.
150func tally(score int, wins, losses, draws *int, ceiling int) {
151 n := draws
152 switch score {
153 case 1000:
154 n = wins
155 case 0:
156 n = losses
157 }
158 if *n < ceiling {
159 *n++
160 }
161}
162
163// mainClan is the clan number s has finished the most rated matches with, 0 for none. A
164// tie goes to the last match's clan when it is one of the tied, else to first-seen order.
165func (s standing) mainClan() int {
166 best := 0
167 for i, n := range s.perClan {
168 if n > 0 && (best == 0 || n > s.perClan[best-1]) {
169 best = i + 1
170 }
171 }
172 if best != 0 && s.lastClan >= 1 && s.lastClan <= len(s.perClan) && s.perClan[s.lastClan-1] == s.perClan[best-1] {
173 return s.lastClan
174 }
175 return best
176}
177
178// rankOf is the rank a rating holds: 1 plus the number of players rated above it.
179func rankOf(rating int) int {
180 i, _, _ := ladder.Get(ladderKey(rating, ""))
181 return i + 1
182}