Skip to content

Commit

Permalink
glicko calculations can fail
Browse files Browse the repository at this point in the history
somehow it happens
  • Loading branch information
ornicar committed Nov 19, 2024
1 parent bef2941 commit a23e060
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions core/src/main/scala/glicko/glicko.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chess
package glicko

import java.time.Instant
import scala.util.Try

case class Player(
rating: Double,
Expand Down Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion test-kit/src/test/scala/glicko/GlickoCalculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a23e060

Please sign in to comment.