Class: Glicko2::RatingPeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/glicko2/rating_period.rb

Overview

Glicko ratings are calculated in bulk at the end of arbitrary, but fixed length, periods named rating periods. Where a period is fixed to be long enough that the average number of games that each player has played in is about 5 to 10 games. It could be weekly, monthly or more as required.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(players) ⇒ RatingPeriod

Returns a new instance of RatingPeriod.

Parameters:



10
11
12
13
14
15
16
17
18
# File 'lib/glicko2/rating_period.rb', line 10

def initialize(players)
  @players = players
  @games = Hash.new { |h, k| h[k] = [] }
  @cache = players.reduce({}) do |memo, player|
    raise DuplicatePlayerError if memo[player.obj] != nil
    memo[player.obj] = player
    memo
  end
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



7
8
9
# File 'lib/glicko2/rating_period.rb', line 7

def players
  @players
end

Class Method Details

.from_objs(objs, config = DEFAULT_CONFIG) ⇒ RatingPeriod

Create rating period from list of seed objects

Parameters:

  • objs (Array<#rating,#rating_deviation,#volatility>)

    seed value objects

Returns:



24
25
26
# File 'lib/glicko2/rating_period.rb', line 24

def self.from_objs(objs, config=DEFAULT_CONFIG)
  new(objs.map { |obj| Player.from_obj(obj, config) })
end

Instance Method Details

#game(game_seeds, ranks) ⇒ Object

Register a game with this rating period

Parameters:

  • game_seeds (Array<#rating,#rating_deviation,#volatility>)

    ratings participating in a game

  • ranks (Array<Integer>)

    corresponding ranks



32
33
34
35
36
37
38
39
40
# File 'lib/glicko2/rating_period.rb', line 32

def game(game_seeds, ranks)
  game_seeds.zip(ranks).each do |seed, rank|
    game_seeds.zip(ranks).each do |other, other_rank|
      next if seed == other
      @games[player(seed)] << [player(other),
                             Util.ranks_to_score(rank, other_rank)]
    end
  end
end

#generate_nextRatingPeriod

Generate a new Glicko2::RatingPeriod with a new list of updated Player

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/glicko2/rating_period.rb', line 45

def generate_next
  p = []
  @players.each do |player|
    games = @games[player]
    if games.compact.length > 0
      p << player.generate_next(*games.transpose)
    else
      p << player.generate_next([], [])
    end
  end
  self.class.new(p)
end

#player(obj) ⇒ Player

Fetch the player associated with a seed object

Parameters:

  • obj (#rating, #rating_deviation, #volatility)

    seed object

Returns:



62
63
64
# File 'lib/glicko2/rating_period.rb', line 62

def player(obj)
  @cache[obj]
end

#to_sObject



66
67
68
# File 'lib/glicko2/rating_period.rb', line 66

def to_s
  "#<RatingPeriod players=#{@players}"
end