Skip to content

Commit

Permalink
replace BigDecimal with Int in FIDE perf rating calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 11, 2024
1 parent 957507d commit 7ed4bd8
Showing 1 changed file with 103 additions and 104 deletions.
207 changes: 103 additions & 104 deletions core/src/main/scala/Elo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ object Elo extends RelaxedOpaqueInt[Elo]:
def computePerformanceRating(games: Seq[Game]): Option[Elo] =
games.nonEmpty.option:
val averageOpponentRating = games.map(_.opponentRating).sum / games.size
val percentageScore =
BigDecimal(games.map(_.points.value).sum / games.size).setScale(2, BigDecimal.RoundingMode.HALF_UP)
val percentageScore = Math.round(games.map(_.points.value).sum * 100 / games.size)
averageOpponentRating + performanceRatingTableFIDE.getOrElse(percentageScore, 0)

final class Player(val rating: Elo, val kFactor: KFactor)
Expand Down Expand Up @@ -103,106 +102,106 @@ object Elo extends RelaxedOpaqueInt[Elo]:
// https://handbook.fide.com/chapter/B022022 8.1.2

// 1.4.9 FIDE table
val performanceRatingTableFIDE: Map[BigDecimal, Int] = Map(
BigDecimal("0.00") -> -800,
BigDecimal("0.01") -> -677,
BigDecimal("0.02") -> -589,
BigDecimal("0.03") -> -538,
BigDecimal("0.04") -> -501,
BigDecimal("0.05") -> -470,
BigDecimal("0.06") -> -444,
BigDecimal("0.07") -> -422,
BigDecimal("0.08") -> -401,
BigDecimal("0.09") -> -383,
BigDecimal("0.10") -> -366,
BigDecimal("0.11") -> -351,
BigDecimal("0.12") -> -336,
BigDecimal("0.13") -> -322,
BigDecimal("0.14") -> -309,
BigDecimal("0.15") -> -296,
BigDecimal("0.16") -> -284,
BigDecimal("0.17") -> -273,
BigDecimal("0.18") -> -262,
BigDecimal("0.19") -> -251,
BigDecimal("0.20") -> -240,
BigDecimal("0.21") -> -230,
BigDecimal("0.22") -> -220,
BigDecimal("0.23") -> -211,
BigDecimal("0.24") -> -202,
BigDecimal("0.25") -> -193,
BigDecimal("0.26") -> -184,
BigDecimal("0.27") -> -175,
BigDecimal("0.28") -> -166,
BigDecimal("0.29") -> -158,
BigDecimal("0.30") -> -149,
BigDecimal("0.31") -> -141,
BigDecimal("0.32") -> -133,
BigDecimal("0.33") -> -125,
BigDecimal("0.34") -> -117,
BigDecimal("0.35") -> -110,
BigDecimal("0.36") -> -102,
BigDecimal("0.37") -> -95,
BigDecimal("0.38") -> -87,
BigDecimal("0.39") -> -80,
BigDecimal("0.40") -> -72,
BigDecimal("0.41") -> -65,
BigDecimal("0.42") -> -57,
BigDecimal("0.43") -> -50,
BigDecimal("0.44") -> -43,
BigDecimal("0.45") -> -36,
BigDecimal("0.46") -> -29,
BigDecimal("0.47") -> -21,
BigDecimal("0.48") -> -14,
BigDecimal("0.49") -> -7,
BigDecimal("0.50") -> 0,
BigDecimal("0.51") -> 7,
BigDecimal("0.52") -> 14,
BigDecimal("0.53") -> 21,
BigDecimal("0.54") -> 29,
BigDecimal("0.55") -> 36,
BigDecimal("0.56") -> 43,
BigDecimal("0.57") -> 50,
BigDecimal("0.58") -> 57,
BigDecimal("0.59") -> 65,
BigDecimal("0.60") -> 72,
BigDecimal("0.61") -> 80,
BigDecimal("0.62") -> 87,
BigDecimal("0.63") -> 95,
BigDecimal("0.64") -> 102,
BigDecimal("0.65") -> 110,
BigDecimal("0.66") -> 117,
BigDecimal("0.67") -> 125,
BigDecimal("0.68") -> 133,
BigDecimal("0.69") -> 141,
BigDecimal("0.70") -> 149,
BigDecimal("0.71") -> 158,
BigDecimal("0.72") -> 166,
BigDecimal("0.73") -> 175,
BigDecimal("0.74") -> 184,
BigDecimal("0.75") -> 193,
BigDecimal("0.76") -> 202,
BigDecimal("0.77") -> 211,
BigDecimal("0.78") -> 220,
BigDecimal("0.79") -> 230,
BigDecimal("0.80") -> 240,
BigDecimal("0.81") -> 251,
BigDecimal("0.82") -> 262,
BigDecimal("0.83") -> 273,
BigDecimal("0.84") -> 284,
BigDecimal("0.85") -> 296,
BigDecimal("0.86") -> 309,
BigDecimal("0.87") -> 322,
BigDecimal("0.88") -> 336,
BigDecimal("0.89") -> 351,
BigDecimal("0.90") -> 366,
BigDecimal("0.91") -> 383,
BigDecimal("0.92") -> 401,
BigDecimal("0.93") -> 422,
BigDecimal("0.94") -> 444,
BigDecimal("0.95") -> 470,
BigDecimal("0.96") -> 501,
BigDecimal("0.97") -> 538,
BigDecimal("0.98") -> 589,
BigDecimal("0.99") -> 677,
BigDecimal("1.00") -> 800
val performanceRatingTableFIDE: Map[Int, Int] = Map(
0 -> -800,
1 -> -677,
2 -> -589,
3 -> -538,
4 -> -501,
5 -> -470,
6 -> -444,
7 -> -422,
8 -> -401,
9 -> -383,
10 -> -366,
11 -> -351,
12 -> -336,
13 -> -322,
14 -> -309,
15 -> -296,
16 -> -284,
17 -> -273,
18 -> -262,
19 -> -251,
20 -> -240,
21 -> -230,
22 -> -220,
23 -> -211,
24 -> -202,
25 -> -193,
26 -> -184,
27 -> -175,
28 -> -166,
29 -> -158,
30 -> -149,
31 -> -141,
32 -> -133,
33 -> -125,
34 -> -117,
35 -> -110,
36 -> -102,
37 -> -95,
38 -> -87,
39 -> -80,
40 -> -72,
41 -> -65,
42 -> -57,
43 -> -50,
44 -> -43,
45 -> -36,
46 -> -29,
47 -> -21,
48 -> -14,
49 -> -7,
50 -> 0,
51 -> 7,
52 -> 14,
53 -> 21,
54 -> 29,
55 -> 36,
56 -> 43,
57 -> 50,
58 -> 57,
59 -> 65,
60 -> 72,
61 -> 80,
62 -> 87,
63 -> 95,
64 -> 102,
65 -> 110,
66 -> 117,
67 -> 125,
68 -> 133,
69 -> 141,
70 -> 149,
71 -> 158,
72 -> 166,
73 -> 175,
74 -> 184,
75 -> 193,
76 -> 202,
77 -> 211,
78 -> 220,
79 -> 230,
80 -> 240,
81 -> 251,
82 -> 262,
83 -> 273,
84 -> 284,
85 -> 296,
86 -> 309,
87 -> 322,
88 -> 336,
89 -> 351,
90 -> 366,
91 -> 383,
92 -> 401,
93 -> 422,
94 -> 444,
95 -> 470,
96 -> 501,
97 -> 538,
98 -> 589,
99 -> 677,
100 -> 800
)

0 comments on commit 7ed4bd8

Please sign in to comment.