class Elo4m::Game

Attributes

Public Class Methods

new(players) click to toggle source
# File lib/elo4m/game.rb, line 6
def initialize(players)
  self.player_cnt = players.length
  define_attr_player
  players.each.with_index(1) do |player, i|
    send("player#{i}=", player)
  end
end

Public Instance Methods

run() click to toggle source
# File lib/elo4m/game.rb, line 14
def run
  self.player_cnt.times.map.with_index(1) do |_, i|
    Rating.new(
      result: score_sum(i),
      old_rating: instance_variable_get("@player#{i}").rating,
      expected: expected_sum(i)
    ).new_rating
  end
end

Private Instance Methods

define_attr_player() click to toggle source
# File lib/elo4m/game.rb, line 26
def define_attr_player
  self.player_cnt.times.with_index(1) do |_, i|
    self.class.send :define_method, "player#{i}=" do |value|
      instance_variable_set("@player#{i}", value)
    end
  end
end
expected_sum(player_number) click to toggle source
# File lib/elo4m/game.rb, line 54
def expected_sum(player_number)
  my_rating = instance_variable_get("@player#{player_number}").rating
  es = 0
  self.player_cnt.times.with_index(1) do |_, i|
    next if i == player_number
    es += Rating.new(
      old_rating: my_rating,
      other_rating: instance_variable_get("@player#{i}").rating
    ).expected_al.round(3)
  end
  es
end
ranking2score(my_ranking, other_ranking) click to toggle source
# File lib/elo4m/game.rb, line 44
def ranking2score(my_ranking, other_ranking)
  if my_ranking < other_ranking
    1
  elsif my_ranking > other_ranking
    0
  else
    0.5
  end
end
score_sum(player_number) click to toggle source
# File lib/elo4m/game.rb, line 34
def score_sum(player_number)
  my_ranking = instance_variable_get("@player#{player_number}").ranking
  score = 0
  self.player_cnt.times.with_index(1) do |_, i|
    next if i == player_number
    score += ranking2score(my_ranking, instance_variable_get("@player#{i}").ranking)
  end
  score
end