Skip to content

Commit

Permalink
more rating types and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 19, 2024
1 parent 18adc97 commit 3f5d4fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
inThisBuild(
Seq(
scalaVersion := "3.5.2",
version := "16.4.0",
version := "16.4.0.1",
organization := "org.lichess",
licenses += ("MIT" -> url("https://opensource.org/licenses/MIT")),
publishTo := Option(Resolver.file("file", new File(sys.props.getOrElse("publishTo", "")))),
Expand Down
13 changes: 12 additions & 1 deletion core/src/main/scala/glicko/glicko.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ case class Glicko(
deviation: Double,
volatility: Double
):
def intRating: IntRating = IntRating(rating.toInt)
def intDeviation = deviation.toInt
def provisional = RatingProvisional(deviation >= Glicko.provisionalDeviation)
def established = provisional.no
def establishedIntRating = Option.when(established)(intRating)
def clueless = deviation >= Glicko.cluelessDeviation
def display = s"$intRating${if provisional.yes then "?" else ""}"
def average(other: Glicko, weight: Float = 0.5f): Glicko =
if weight >= 1 then other
else if weight <= 0 then this
Expand All @@ -18,7 +25,11 @@ case class Glicko(
deviation = deviation * (1 - weight) + other.deviation * weight,
volatility = volatility * (1 - weight) + other.volatility * weight
)
override def toString = f"${rating.toInt}/${deviation.toInt}/${volatility}%.3f"
override def toString = f"$intRating/$intDeviation/${volatility}%.3f"

object Glicko:
val provisionalDeviation = 110
val cluelessDeviation = 230

case class Player(
glicko: Glicko,
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/scala/glicko/rating.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package chess
package glicko // todo move to chess.rating?

import alleycats.Zero

opaque type IntRating = Int
object IntRating extends RichOpaqueInt[IntRating]:
extension (r: IntRating) def applyDiff(diff: IntRatingDiff): IntRating = r + diff.value

opaque type IntRatingDiff = Int
object IntRatingDiff extends RichOpaqueInt[IntRatingDiff]:
extension (diff: IntRatingDiff)
def positive: Boolean = diff > 0
def negative: Boolean = diff < 0
def zero: Boolean = diff == 0
given Zero[IntRatingDiff] = Zero(0)

opaque type Rating = Double
object Rating extends OpaqueDouble[Rating]

opaque type RatingProvisional = Boolean
object RatingProvisional extends YesNo[RatingProvisional]

0 comments on commit 3f5d4fa

Please sign in to comment.