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



39
40
41
# File 'lib/acts_as_elo/acts/elo.rb', line 39

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

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



35
36
37
# File 'lib/acts_as_elo/acts/elo.rb', line 35

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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/acts_as_elo/acts/elo.rb', line 43

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]
    diff      = (opponent.elo_rank.to_f - elo_rank.to_f).abs
    expected  = 1 / (1 + 10 ** (diff / 400))
    send_opts = {one_way: true}

    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

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

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



31
32
33
# File 'lib/acts_as_elo/acts/elo.rb', line 31

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