Skip to content

Commit

Permalink
foundations for rating color advantage
Browse files Browse the repository at this point in the history
Before changing the implementation to use the color advantage,
the implementer should start with adding tests to
test-kit/src/test/scala/rating/glicko/impl/RatingCalculatorWithColorAdvantageTest.scala

```sh
sbt
project testKit
~testQuick chess.rating.glicko.*
```
  • Loading branch information
ornicar committed Nov 20, 2024
1 parent a68f539 commit 5746f15
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
5 changes: 3 additions & 2 deletions rating/src/main/scala/glicko/GlickoCalculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import scala.util.Try
/* Purely functional interface hiding the mutable implementation */
final class GlickoCalculator(
tau: Tau = Tau.default,
ratingPeriodsPerDay: RatingPeriodsPerDay = RatingPeriodsPerDay.default
ratingPeriodsPerDay: RatingPeriodsPerDay = RatingPeriodsPerDay.default,
colorAdvantage: ColorAdvantage = ColorAdvantage.zero
):

private val calculator = new impl.RatingCalculator(tau, ratingPeriodsPerDay)
private val calculator = new impl.RatingCalculator(tau, ratingPeriodsPerDay, colorAdvantage)

// Simpler use case: a single game
def computeGame(game: Game, skipDeviationIncrease: Boolean = false): Try[ByColor[Player]] =
Expand Down
9 changes: 6 additions & 3 deletions rating/src/main/scala/glicko/impl/Rating.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ final private[glicko] class Rating(
private[impl] var workingRatingDeviation: Double = scala.compiletime.uninitialized
private[impl] var workingVolatility: Double = scala.compiletime.uninitialized

/** Return the average skill value of the player scaled down to the scale used by the algorithm's internal
* workings.
*/
/** Return the average skill value of the player
* scaled down to the scale used by the algorithm's internal workings.
*/
private[impl] def getGlicko2Rating: Double = convertRatingToGlicko2Scale(this.rating)

private[impl] def getGlicko2RatingWithAdvantage(advantage: ColorAdvantage): Double =
convertRatingToGlicko2Scale(this.rating + advantage.value)

/** Set the average skill value, taking in a value in Glicko2 scale.
*/
private[impl] def setGlicko2Rating(r: Double) =
Expand Down
3 changes: 2 additions & 1 deletion rating/src/main/scala/glicko/impl/RatingCalculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ private object RatingCalculator:

final private[glicko] class RatingCalculator(
tau: Tau = Tau.default,
ratingPeriodsPerDay: RatingPeriodsPerDay = RatingPeriodsPerDay.default
ratingPeriodsPerDay: RatingPeriodsPerDay = RatingPeriodsPerDay.default,
colorAdvantage: ColorAdvantage = ColorAdvantage.zero
):

import RatingCalculator.*
Expand Down
7 changes: 7 additions & 0 deletions rating/src/main/scala/glicko/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ object Tau extends OpaqueDouble[Tau]:
opaque type RatingPeriodsPerDay = Double
object RatingPeriodsPerDay extends OpaqueDouble[RatingPeriodsPerDay]:
val default: RatingPeriodsPerDay = 0d

opaque type ColorAdvantage = Double
object ColorAdvantage extends OpaqueDouble[ColorAdvantage]:
val zero: ColorAdvantage = 0d
val standard: ColorAdvantage = 7.786d
val crazyhouse: ColorAdvantage = 15.171d
extension (c: ColorAdvantage) def negate: ColorAdvantage = -c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package chess.rating.glicko

import cats.syntax.all.*
import munit.ScalaCheckSuite

class RatingCalculatorWithColorAdvantageTest extends ScalaCheckSuite with chess.MunitExtensions:

val smallAdvantage = GlickoCalculator(
Tau.default,
RatingPeriodsPerDay.default,
ColorAdvantage(7d)
)

// test that the rating calculator correctly applies the color advantage

0 comments on commit 5746f15

Please sign in to comment.