recordResult method

void recordResult(
  1. T player1,
  2. T player2,
  3. double score
)

Records the result of a game.

The score should be a value satisfying 0 <= score <= 1. Generally score is:

  • 1.0, if player1 won the game.
  • 0.5, if there was a draw.
  • 0.0, if player2 won the game.

Implementation

void recordResult(T player1, T player2, double score) {
  var oldRating1 = _ratings[player1];
  var oldRating2 = _ratings[player2];
  if (oldRating1 == null && _defaultInitialRating == null) {
    throw ArgumentError('Player $player1 not registered.');
  }
  if (oldRating2 == null && _defaultInitialRating == null) {
    throw ArgumentError('Player $player2 not registered.');
  }
  oldRating1 ??= _defaultInitialRating!;
  oldRating2 ??= _defaultInitialRating!;

  var diff = oldRating1 - oldRating2;
  var exponent = -(diff / _n);

  var expected1 = 1.0 / (1.0 + math.pow(10.0, exponent));
  _ratings[player1] = oldRating1 + _kFactor * (score - expected1);

  var expected2 = 1.0 / (1.0 + math.pow(10.0, -exponent));
  _ratings[player2] = oldRating2 + _kFactor * ((1.0 - score) - expected2);
}