elo4m 0.0.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b674afae937af9be30bd785e2599a8ae39cf5851
4
- data.tar.gz: 15de3862089257038b5373a6eeeecc4fecae9f3e
3
+ metadata.gz: cf144583804ad9b8e92f3aa5c69bb9a0662ca2d7
4
+ data.tar.gz: d8e5ce8612eee672f95f13057cfb1a483e1ed204
5
5
  SHA512:
6
- metadata.gz: 2b206cf3720564da2889244f32dc94ae6f51c8eb931c6f54ac629c267c4a831c338cf0fd1f67a55357f6190471d17876e8b24239e91606c6c53be6ab4a4ec19c
7
- data.tar.gz: 7a5b3c4b7fa98a3c0b71a50979d8f9153c72e0c153f4a5c951680dbeaa4259419a5b13c9f017fe17c475142603bafd5db5f42a13827feff1b79d998359d032ac
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 )
@@ -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(&block)
14
- yield(config)
12
+ def self.configure
13
+ yield config
15
14
  end
16
15
  end
@@ -1,50 +1,9 @@
1
1
  module Elo4m
2
2
  class Configuration
3
- attr_accessor :default_k_factor
4
- attr_accessor :default_rating
3
+ attr_accessor :k_factor
5
4
 
6
- def initialize #:nodoc:
7
- @default_rating = 1000
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
@@ -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
@@ -1,6 +1,5 @@
1
1
  module Elo4m
2
2
  class Player
3
- include Helper
4
3
  attr_accessor :rating
5
4
  attr_accessor :ranking
6
5
 
@@ -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 :k_factor
8
- attr_reader :expected
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
@@ -1,3 +1,3 @@
1
1
  module Elo4m
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -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
- after do
6
- Elo4m.instance_eval { @config = nil }
7
- end
8
-
9
- =begin
10
-
11
- Elo rating system - Wikipedia, the free encyclopedia
12
- http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
13
-
14
- R_A^' = R_A + K(S_A - E_A).
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
- it do
34
- game = Elo4m::Game.new(players)
35
- expect(game.run.first).to eq(1601)
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: 0.0.1
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-18 00:00:00.000000000 Z
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
@@ -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