Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foundations for rating color advantage #598

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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