Module: Acts::Elo::InstanceMethods

Defined in:
lib/acts_as_elo/acts/elo.rb

Instance Method Summary collapse

Instance Method Details

#elo_draw!(opponent, opts = {}) ⇒ Object



64
65
66
# File 'lib/acts_as_elo/acts/elo.rb', line 64

def elo_draw!(opponent, opts={})
  elo_update(opponent, opts.merge!(:result => :draw))
end

#elo_lose!(opponent, opts = {}) ⇒ Object



60
61
62
# File 'lib/acts_as_elo/acts/elo.rb', line 60

def elo_lose!(opponent, opts={})
  elo_update(opponent, opts.merge!(:result => :lose))
end

#elo_update(opponent, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/acts_as_elo/acts/elo.rb', line 68

def elo_update(opponent, opts={})
  begin
    if self.class.respond_to?(:acts_as_elo_options) && self.class.acts_as_elo_options[:one_way]
      one_way = true
    end
    one_way   ||= opts[:one_way]
    send_opts = {one_way: true}

    # Formula from: http://en.wikipedia.org/wiki/Elo_rating_system
    diff        = opponent.elo_rank.to_f - elo_rank.to_f
    expected    = 1 / (1 + 10 ** (diff / 400))
    coefficient = elo_proficiency_map[:coefficients][1]

    if elo_rank < elo_proficiency_map[:rank_limits].first
      coefficient = elo_proficiency_map[:coefficients].first
    end

    if elo_rank > elo_proficiency_map[:rank_limits].last
      coefficient = elo_proficiency_map[:coefficients].last
    end

    if opts[:result] == :win
      points = 1
      send_opts.merge!(result: :lose)
    elsif opts[:result] == :lose
      points = 0
      send_opts.merge!(result: :win)
    elsif opts[:result] == :draw
      points = 0.5
      send_opts.merge!(result: :draw)
    end

    opponent.elo_update(self, send_opts) unless one_way

    self.elo_rank = (elo_rank + coefficient*(points-expected)).round
  rescue Exception => e
    puts "Exception: #{e.message}"
  end
end

#elo_win!(opponent, opts = {}) ⇒ Object



56
57
58
# File 'lib/acts_as_elo/acts/elo.rb', line 56

def elo_win!(opponent, opts={})
  elo_update(opponent, opts.merge!(:result => :win))
end