Class: Glicko2::RatingPeriod
- Inherits:
-
Object
- Object
- Glicko2::RatingPeriod
- 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
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Class Method Summary collapse
-
.from_objs(objs, config = DEFAULT_CONFIG) ⇒ RatingPeriod
Create rating period from list of seed objects.
Instance Method Summary collapse
-
#game(game_seeds, ranks) ⇒ Object
Register a game with this rating period.
-
#generate_next ⇒ RatingPeriod
Generate a new RatingPeriod with a new list of updated Player.
-
#initialize(players) ⇒ RatingPeriod
constructor
A new instance of RatingPeriod.
-
#player(obj) ⇒ Player
Fetch the player associated with a seed object.
- #to_s ⇒ Object
Constructor Details
#initialize(players) ⇒ RatingPeriod
Returns a new instance of RatingPeriod.
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
#players ⇒ Object (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
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
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_next ⇒ RatingPeriod
Generate a new Glicko2::RatingPeriod with a new list of updated Player
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
62 63 64 |
# File 'lib/glicko2/rating_period.rb', line 62 def player(obj) @cache[obj] end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/glicko2/rating_period.rb', line 66 def to_s "#<RatingPeriod players=#{@players}" end |