Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

rating.gno

0.77 Kb · 33 lines
 1package arena
 2
 3const kFactor = 32
 4
 5// expected is a's chance of beating b, in thousandths, from an Elo-style
 6// rating gap. It uses a straight-line approximation of the logistic curve:
 7// even ratings give 500, and a gap of 400 or more is capped at 50 / 950.
 8func expected(ra, rb int) int {
 9	e := 500 + (ra-rb)*450/400
10	if e < 50 {
11		return 50
12	}
13	if e > 950 {
14		return 950
15	}
16	return e
17}
18
19// rate updates both ratings for a result (0, 1, or -1 for a draw) and
20// returns the change for a (b's is the opposite).
21func rate(a, b *Bot, winner int) int {
22	score := 500 // thousandths: 1000 win, 500 draw, 0 loss for side a
23	switch winner {
24	case 0:
25		score = 1000
26	case 1:
27		score = 0
28	}
29	d := kFactor * (score - expected(a.Rating, b.Rating)) / 1000
30	a.Rating += d
31	b.Rating -= d
32	return d
33}