From a23e060f625cd74d25a63a172b4df82f78023daf Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 19 Nov 2024 10:58:05 +0100 Subject: [PATCH] glicko calculations can fail somehow it happens --- core/src/main/scala/glicko/glicko.scala | 15 ++++++++++----- .../src/test/scala/glicko/GlickoCalculator.scala | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/glicko/glicko.scala b/core/src/main/scala/glicko/glicko.scala index 43fd550aa..beb494bd1 100644 --- a/core/src/main/scala/glicko/glicko.scala +++ b/core/src/main/scala/glicko/glicko.scala @@ -2,6 +2,7 @@ package chess package glicko import java.time.Instant +import scala.util.Try case class Player( rating: Double, @@ -31,23 +32,27 @@ trait GlickoCalculatorApi: // ): List[Player] // Simpler use case: a single game - def computeGame(game: Game, skipDeviationIncrease: Boolean = false): ByColor[Player] + def computeGame(game: Game, skipDeviationIncrease: Boolean = false): Try[ByColor[Player]] /** This is the formula defined in step 6. It is also used for players who have not competed during the rating period. */ - // def previewDeviation(player: Player, ratingPeriodEndDate: Instant, reverse: Boolean): Double + def previewDeviation(player: Player, ratingPeriodEndDate: Instant, reverse: Boolean): Double final class GlickoCalculator(config: Config) extends GlickoCalculatorApi: private val calculator = chess.glicko.impl.RatingCalculator(config.tau, config.ratingPeriodsPerDay) // Simpler use case: a single game - def computeGame(game: Game, skipDeviationIncrease: Boolean = false): ByColor[Player] = + def computeGame(game: Game, skipDeviationIncrease: Boolean = false): Try[ByColor[Player]] = val ratings = game.players.map(conversions.toRating) val gameResult = conversions.toGameResult(ratings, game.outcome) val periodResults = impl.GameRatingPeriodResults(List(gameResult)) - calculator.updateRatings(periodResults, skipDeviationIncrease) - ratings.map(conversions.toPlayer) + Try: + calculator.updateRatings(periodResults, skipDeviationIncrease) + ratings.map(conversions.toPlayer) + + def previewDeviation(player: Player, ratingPeriodEndDate: Instant, reverse: Boolean): Double = + calculator.previewDeviation(conversions.toRating(player), ratingPeriodEndDate, reverse) private object conversions: diff --git a/test-kit/src/test/scala/glicko/GlickoCalculator.scala b/test-kit/src/test/scala/glicko/GlickoCalculator.scala index 91899c4cb..870b29b91 100644 --- a/test-kit/src/test/scala/glicko/GlickoCalculator.scala +++ b/test-kit/src/test/scala/glicko/GlickoCalculator.scala @@ -11,7 +11,7 @@ class GlickoCalculatorTest extends ScalaCheckSuite with MunitExtensions: ) def computeGame(players: ByColor[Player], outcome: Outcome) = - calc.computeGame(Game(players, outcome), skipDeviationIncrease = true).toPair + calc.computeGame(Game(players, outcome), skipDeviationIncrease = true).get.toPair { val players = ByColor.fill: