arpad 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85bfb380bf16095e4eb27c6ca732ed9449aefb50
4
+ data.tar.gz: 8b6a4c264b3c84c4195a7b70f5fdb3e1d0fe2db8
5
+ SHA512:
6
+ metadata.gz: 62afe56fcd8173bde808c19c0f2cda09a950e2d8a53e5c6df36804d2297d88be29e7d888918e957d60fe1de57b937ce579f6525dd0eead6a076dbcdf231e27bc
7
+ data.tar.gz: 193aa8d738ce6bbeed47f5b1b8066513e92a4792244e471316ad2ee4d636cede9b10fc07a1829bddcc46ec4bffa477aefca4fdf7ced05ee3a9deae35b1b72809
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in arpad.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Elijah Kim
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Arpad
2
+
3
+ A lightweight gem that calculates
4
+ [Elo](http://en.wikipedia.org/wiki/Elo_rating_system)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```
11
+ gem 'arpad'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install arpad
21
+
22
+ ## Usage
23
+ `require "arpad"` on the top of your ruby file
24
+
25
+ To calculate elo: `Arpad::Calculator.calculate_elo(elo_player, elo_opponent,
26
+ outcome)`
27
+
28
+ i.e `Arpad::Calculator.calculate_elo(1400, 1500, "win")` returns 1410
29
+
30
+ outcome inputs include `"win"`, `"lose"`, or `"tie"`
31
+
32
+ `calculate_elo` can also take an optional hash `k_factor`. `k_factor` is defaulted
33
+ to 16 unless otherwise stated.
34
+
35
+ i.e `Arpad::Calculator.calculate_elo(1400, 1500, "win", k_factor: 32)` returns
36
+ 1420
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/elijahkim/arpad/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arpad/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "arpad"
8
+ spec.version = Arpad::VERSION
9
+ spec.authors = ["Elijah Kim"]
10
+ spec.email = ["kimelijah90@gmail.com"]
11
+ spec.summary = %q{A lightweight gem that calculates elo}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,40 @@
1
+ require "arpad/version"
2
+
3
+ module Arpad
4
+ class Calculator
5
+ def self.calculate_elo(elo_one, elo_two, outcome, config = {})
6
+ k_factor = config[:k_factor] || 16
7
+ transformed_rating_one = calculate_transformed_rating(elo_one)
8
+ transformed_rating_two = calculate_transformed_rating(elo_two)
9
+ expected_score = calculate_expected_score(
10
+ transformed_rating_one,
11
+ transformed_rating_two
12
+ )
13
+ score_value = calculate_score_value(outcome)
14
+
15
+ (elo_one.to_i + (k_factor * (score_value - expected_score))).to_i
16
+ end
17
+
18
+ private
19
+
20
+ def self.calculate_transformed_rating(elo)
21
+ (10**(elo.to_f / 400))
22
+ end
23
+
24
+ def self.calculate_expected_score(t_rating_one, t_rating_two)
25
+ t_rating_one / (t_rating_one + t_rating_two)
26
+ end
27
+
28
+ def self.calculate_score_value(outcome)
29
+ if outcome == "win"
30
+ 1
31
+ elsif outcome == "lose"
32
+ 0
33
+ elsif outcome == "tie"
34
+ 0.5
35
+ else
36
+ raise "user inputted #{outcome} which is not a valid outcome"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Arpad
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ describe Arpad::Calculator do
4
+ describe "#calcualte_elo" do
5
+ context "with a default k factor" do
6
+ context "if the player wins" do
7
+ it { expect(calculate_elo(1400, 1400, "win")).to be 1408 }
8
+ it { expect(calculate_elo(1400, 1500, "win")).to be 1410 }
9
+ it { expect(calculate_elo(1800, 1300, "win")).to be 1800 }
10
+ it { expect(calculate_elo(1300, 1800, "win")).to be 1315 }
11
+ end
12
+ context "if the player loses" do
13
+ it { expect(calculate_elo(1400, 1400, "lose")).to be 1392 }
14
+ it { expect(calculate_elo(1400, 1500, "lose")).to be 1394 }
15
+ it { expect(calculate_elo(1800, 1300, "lose")).to be 1784 }
16
+ it { expect(calculate_elo(1300, 1800, "lose")).to be 1299 }
17
+ end
18
+ context "if the player ties" do
19
+ it { expect(calculate_elo(1400, 1400, "tie")).to be 1400 }
20
+ it { expect(calculate_elo(1400, 1500, "tie")).to be 1402 }
21
+ it { expect(calculate_elo(1800, 1300, "tie")).to be 1792 }
22
+ it { expect(calculate_elo(1300, 1800, "tie")).to be 1307 }
23
+ end
24
+ end
25
+
26
+ context "win a k factor of 32" do
27
+ context "if the player wins" do
28
+ it { expect(calculate_elo(1400, 1400, "win", k_factor: 32)).to be 1416 }
29
+ it { expect(calculate_elo(1800, 1300, "win", k_factor: 32)).to be 1801 }
30
+ it { expect(calculate_elo(1400, 1500, "win", k_factor: 32)).to be 1420 }
31
+ it { expect(calculate_elo(1300, 1800, "win", k_factor: 32)).to be 1330 }
32
+ end
33
+ context "if the player loses" do
34
+ it { expect(calculate_elo(1400, 1400, "lose", k_factor: 32)).to be 1384 }
35
+ it { expect(calculate_elo(1400, 1500, "lose", k_factor: 32)).to be 1388 }
36
+ it { expect(calculate_elo(1800, 1300, "lose", k_factor: 32)).to be 1769 }
37
+ it { expect(calculate_elo(1300, 1800, "lose", k_factor: 32)).to be 1298 }
38
+ end
39
+ context "if the player ties" do
40
+ it { expect(calculate_elo(1400, 1400, "tie", k_factor: 32)).to be 1400 }
41
+ it { expect(calculate_elo(1400, 1500, "tie", k_factor: 32)).to be 1404 }
42
+ it { expect(calculate_elo(1800, 1300, "tie", k_factor: 32)).to be 1785 }
43
+ it { expect(calculate_elo(1300, 1800, "tie", k_factor: 32)).to be 1314 }
44
+ end
45
+ end
46
+
47
+ context "with an invalid outcome value" do
48
+ it { expect{calculate_elo(1300, 1800, "foo")}.to raise_error "user inputted foo which is not a valid outcome"}
49
+ end
50
+
51
+ context "if the elo input is non-integer" do
52
+ it "should try to force integer" do
53
+ expect(calculate_elo("1300", "1800", "win")).to be 1315
54
+ end
55
+ end
56
+ end
57
+
58
+ def calculate_elo(elo_one, elo_two, outcome, config = {})
59
+ Arpad::Calculator.calculate_elo(elo_one, elo_two, outcome, config)
60
+ end
61
+ end
@@ -0,0 +1 @@
1
+ require "arpad"
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arpad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Elijah Kim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - kimelijah90@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - arpad.gemspec
68
+ - lib/arpad.rb
69
+ - lib/arpad/version.rb
70
+ - spec/arpad/calculator_spec.rb
71
+ - spec/spec_helper.rb
72
+ - tasks/rspec.rake
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A lightweight gem that calculates elo
97
+ test_files:
98
+ - spec/arpad/calculator_spec.rb
99
+ - spec/spec_helper.rb