elo4m 0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/elo4m.rb +2 -3
- data/lib/elo4m/configuration.rb +3 -44
- data/lib/elo4m/game.rb +1 -5
- data/lib/elo4m/player.rb +0 -1
- data/lib/elo4m/rating.rb +8 -5
- data/lib/elo4m/version.rb +1 -1
- data/spec/elo_spec.rb +21 -20
- metadata +2 -3
- data/lib/elo4m/helper.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf144583804ad9b8e92f3aa5c69bb9a0662ca2d7
|
4
|
+
data.tar.gz: d8e5ce8612eee672f95f13057cfb1a483e1ed204
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a4a94d10ca69e2eed3f6f7841a9a4e860d30b0f6e2da6c1951b9c3d2dd95002bcb452fe566583a1819179a1487777e614bb577a16ded50b2b1c2c730632bef4
|
7
|
+
data.tar.gz: 7fdf4732b4fc622c90d1349d70f3f0ccbb10183e05a53b3943669c15c04dc93f25eb5d4c67740de9f4d40afc0e071dcf12934e35e8bbf8baf46972ce126e04f8
|
data/README.md
CHANGED
@@ -59,6 +59,18 @@ game.run
|
|
59
59
|
#=> [1601, 1646, 1499, 1350, 1533, 1762]
|
60
60
|
```
|
61
61
|
|
62
|
+
## K-factor
|
63
|
+
|
64
|
+
Set your config if you want.
|
65
|
+
|
66
|
+
`config/initializers/elo4m.rb.`
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
Elo4m.configure do |c|
|
70
|
+
c.k_factor = <K-FACTOR>
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
62
74
|
## Contributing
|
63
75
|
|
64
76
|
1. Fork it ( https://github.com/kidach1/elo4m/fork )
|
data/lib/elo4m.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require "elo4m/version"
|
2
|
-
require "elo4m/helper"
|
3
2
|
require "elo4m/configuration"
|
4
3
|
require "elo4m/game"
|
5
4
|
require "elo4m/player"
|
@@ -10,7 +9,7 @@ module Elo4m
|
|
10
9
|
@config ||= Configuration.new
|
11
10
|
end
|
12
11
|
|
13
|
-
def self.configure
|
14
|
-
yield
|
12
|
+
def self.configure
|
13
|
+
yield config
|
15
14
|
end
|
16
15
|
end
|
data/lib/elo4m/configuration.rb
CHANGED
@@ -1,50 +1,9 @@
|
|
1
1
|
module Elo4m
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :
|
4
|
-
attr_accessor :default_rating
|
3
|
+
attr_accessor :k_factor
|
5
4
|
|
6
|
-
def initialize
|
7
|
-
@
|
8
|
-
@default_k_factor = 15
|
9
|
-
end
|
10
|
-
|
11
|
-
# Add a K-factor rule. The first argument is the k-factor value.
|
12
|
-
# The block should return a boolean that determines if this K-factor rule applies.
|
13
|
-
# The first rule that applies is the one determining the K-factor.
|
14
|
-
#
|
15
|
-
# The block is instance_eval'ed into the player, so you can access all it's
|
16
|
-
# properties directly. The K-factor is recalculated every time a match is played.
|
17
|
-
#
|
18
|
-
# By default, the FIDE settings are used (see: +use_FIDE_settings+). To implement
|
19
|
-
# that yourself, you could write:
|
20
|
-
#
|
21
|
-
# Elo.configure do |config|
|
22
|
-
# config.k_factor(10) { pro? or pro_rating? }
|
23
|
-
# config.k_factor(25) { starter? }
|
24
|
-
# config.default_k_factor = 15
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
def k_factor(factor, &rule)
|
28
|
-
k_factors << { :factor => factor, :rule => rule }
|
29
|
-
end
|
30
|
-
|
31
|
-
def applied_k_factors #:nodoc:
|
32
|
-
apply_fide_k_factors if use_FIDE_settings
|
33
|
-
k_factors
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def k_factors
|
39
|
-
@k_factors ||= []
|
40
|
-
end
|
41
|
-
|
42
|
-
def apply_fide_k_factors
|
43
|
-
unless @applied_fide_k_factors
|
44
|
-
k_factor(10) { pro? or pro_rating? }
|
45
|
-
k_factor(25) { starter? }
|
46
|
-
@applied_fide_k_factors = true
|
47
|
-
end
|
5
|
+
def initialize
|
6
|
+
@k_factor = 32
|
48
7
|
end
|
49
8
|
end
|
50
9
|
end
|
data/lib/elo4m/game.rb
CHANGED
@@ -1,10 +1,7 @@
|
|
1
1
|
module Elo4m
|
2
2
|
class Game
|
3
3
|
# http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
|
4
|
-
|
5
|
-
include Helper
|
6
4
|
attr_accessor :player_cnt
|
7
|
-
K_FACTOR = 32
|
8
5
|
|
9
6
|
def initialize(players)
|
10
7
|
self.player_cnt = players.length
|
@@ -19,8 +16,7 @@ module Elo4m
|
|
19
16
|
Rating.new(
|
20
17
|
result: score_sum(i),
|
21
18
|
old_rating: instance_variable_get("@player#{i}").rating,
|
22
|
-
expected: expected_sum(i)
|
23
|
-
k_factor: K_FACTOR
|
19
|
+
expected: expected_sum(i)
|
24
20
|
).new_rating
|
25
21
|
end
|
26
22
|
end
|
data/lib/elo4m/player.rb
CHANGED
data/lib/elo4m/rating.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
module Elo4m
|
2
2
|
class Rating
|
3
|
-
include Helper
|
4
|
-
|
5
3
|
attr_reader :other_rating
|
6
4
|
attr_reader :old_rating
|
7
|
-
attr_reader :
|
8
|
-
|
5
|
+
attr_reader :expected
|
6
|
+
|
7
|
+
def initialize(args = {})
|
8
|
+
args.each do |key, value|
|
9
|
+
instance_variable_set("@#{key}", value)
|
10
|
+
end
|
11
|
+
end
|
9
12
|
|
10
13
|
def new_rating
|
11
14
|
(old_rating.to_f + change).to_i
|
@@ -29,7 +32,7 @@ module Elo4m
|
|
29
32
|
|
30
33
|
def change
|
31
34
|
expected = self.expected.nil? ? expected_al : self.expected
|
32
|
-
k_factor.to_f * ( result.to_f - expected )
|
35
|
+
Elo4m.config.k_factor.to_f * ( result.to_f - expected )
|
33
36
|
end
|
34
37
|
end
|
35
38
|
end
|
data/lib/elo4m/version.rb
CHANGED
data/spec/elo_spec.rb
CHANGED
@@ -2,24 +2,17 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
require "elo4m"
|
3
3
|
|
4
4
|
describe "Elo" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
This update can be performed after each game or each tournament, or after any suitable rating period.
|
16
|
-
An example may help clarify. Suppose Player A has a rating of 1613, and plays in a five-round tournament.
|
17
|
-
He or she loses to a player rated 1609, draws with a player rated 1477, defeats a player rated 1388,
|
18
|
-
defeats a player rated 1586, and loses to a player rated 1720. The player's actual score is (0 + 0.5 + 1 + 1 + 0) = 2.5.
|
19
|
-
The expected score, calculated according to the formula above, was (0.506 + 0.686 + 0.785 + 0.539 + 0.351) = 2.867.
|
20
|
-
Therefore the player's new rating is (1613 + 32×(2.5 − 2.867)) = 1601, assuming that a K-factor of 32 is used.
|
5
|
+
# Elo rating system - Wikipedia, the free encyclopedia
|
6
|
+
# http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
|
7
|
+
#
|
8
|
+
# R_A^' = R_A + K(S_A - E_A).
|
9
|
+
# This update can be performed after each game or each tournament, or after any suitable rating period.
|
10
|
+
# An example may help clarify. Suppose Player A has a rating of 1613, and plays in a five-round tournament.
|
11
|
+
# He or she loses to a player rated 1609, draws with a player rated 1477, defeats a player rated 1388,
|
12
|
+
# defeats a player rated 1586, and loses to a player rated 1720. The player's actual score is (0 + 0.5 + 1 + 1 + 0) = 2.5.
|
13
|
+
# The expected score, calculated according to the formula above, was (0.506 + 0.686 + 0.785 + 0.539 + 0.351) = 2.867.
|
14
|
+
# Therefore the player's new rating is (1613 + 32×(2.5 − 2.867)) = 1601, assuming that a K-factor of 32 is used.
|
21
15
|
|
22
|
-
=end
|
23
16
|
describe 'wikipedia-way' do
|
24
17
|
let(:players) do [
|
25
18
|
Elo4m::Player.new(1613, 4),
|
@@ -29,10 +22,18 @@ http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
|
|
29
22
|
Elo4m::Player.new(1586, 6),
|
30
23
|
Elo4m::Player.new(1720, 2) ]
|
31
24
|
end
|
25
|
+
subject { Elo4m::Game.new(players).run.first }
|
32
26
|
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
context 'K-factor is 32'do
|
28
|
+
it { expect(subject).to eq(1601) }
|
29
|
+
end
|
30
|
+
context 'K-factor is 16'do
|
31
|
+
before do
|
32
|
+
Elo4m.configure do |cfg|
|
33
|
+
cfg.k_factor = 16
|
34
|
+
end
|
35
|
+
end
|
36
|
+
it { expect(subject).to eq(1607) }
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elo4m
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kidach1
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,7 +83,6 @@ files:
|
|
83
83
|
- lib/elo4m.rb
|
84
84
|
- lib/elo4m/configuration.rb
|
85
85
|
- lib/elo4m/game.rb
|
86
|
-
- lib/elo4m/helper.rb
|
87
86
|
- lib/elo4m/player.rb
|
88
87
|
- lib/elo4m/rating.rb
|
89
88
|
- lib/elo4m/version.rb
|
data/lib/elo4m/helper.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Elo4m
|
2
|
-
module Helper
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
# Every object can be initialized with a hash,
|
8
|
-
# almost, but not quite, entirely unlike ActiveRecord.
|
9
|
-
def initialize(attributes = {})
|
10
|
-
attributes.each do |key, value|
|
11
|
-
instance_variable_set("@#{key}", value)
|
12
|
-
end
|
13
|
-
self.class.all << self
|
14
|
-
end
|
15
|
-
|
16
|
-
module ClassMethods
|
17
|
-
# Provides a list of all instantiated objects of the class.
|
18
|
-
def all
|
19
|
-
@all ||= []
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|