ittf_points 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/exe/ittf_points +3 -0
- data/ittf_points.gemspec +24 -0
- data/lib/ittf_points/player.rb +111 -0
- data/lib/ittf_points/r.rb +48 -0
- data/lib/ittf_points/version.rb +3 -0
- data/lib/ittf_points.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f4851982f9af2f265f5d49c1a108a624d8b70ef
|
4
|
+
data.tar.gz: 42f5ac52479781bad7c463c8a40e75421dd01b9a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0a4f0a4345bd2b212aff69d8d95e0de6c729aeaf63db624bf4cbd96880229e992b51d073435784ab69095227d3ba4d7fc57cef351a274706df96cfe65f60024
|
7
|
+
data.tar.gz: e6087bffa8ab46060e27a5030843c3bebe99e5efa5eac1f9767003db84f2de1f3d039886936c06233a68506863094edcaa3df18724473e662045018b9b1a40d5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 hachy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# IttfPoints
|
2
|
+
|
3
|
+
Calculate ITTF rating points.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ittf_points'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ittf_points
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'ittf_points'
|
25
|
+
include IttfPoints
|
26
|
+
|
27
|
+
p1 = Player.new(rating_points: 2731, weight: :r2)
|
28
|
+
p1.win([2122, 1471, 2621]).lose(2788)
|
29
|
+
# or
|
30
|
+
# p1.win(2122).win(1471).win(2621).lose(2788)
|
31
|
+
|
32
|
+
puts p1.new_rating_points
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hachy/ittf_points.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ittf_points"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/ittf_points
ADDED
data/ittf_points.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ittf_points/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ittf_points'
|
8
|
+
spec.version = IttfPoints::VERSION
|
9
|
+
spec.authors = ['hachy']
|
10
|
+
spec.email = ['pekoett@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %(Calculate rating points based on ITTF ranking system.)
|
13
|
+
spec.description = %(Calculate rating points based on ITTF ranking system.)
|
14
|
+
spec.homepage = 'https://github.com/hachy/ittf_points'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module IttfPoints
|
2
|
+
class Player
|
3
|
+
attr_reader :gained_rating_points, :lost_rating_points, :points_difference
|
4
|
+
attr_accessor :weight
|
5
|
+
|
6
|
+
def initialize(rating_points: nil, weight: :r2)
|
7
|
+
@rating_points = rating_points.to_i
|
8
|
+
@weight = weight.downcase.to_sym
|
9
|
+
end
|
10
|
+
|
11
|
+
def win(*rating_points)
|
12
|
+
@winner_flag = true
|
13
|
+
rating_points.flatten.each do |points|
|
14
|
+
@opponent_rating_pts = points.to_i
|
15
|
+
expected_or_unexpected(winner: @rating_points, loser: @opponent_rating_pts)
|
16
|
+
end
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def lose(*rating_points)
|
21
|
+
@winner_flag = false
|
22
|
+
rating_points.flatten.each do |points|
|
23
|
+
@opponent_rating_pts = points.to_i
|
24
|
+
expected_or_unexpected(winner: @opponent_rating_pts, loser: @rating_points)
|
25
|
+
end
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def new_added_rating_points
|
30
|
+
if defined?(@gained_rating_points)
|
31
|
+
gained = @gained_rating_points.inject(:+)
|
32
|
+
else
|
33
|
+
gained = 0
|
34
|
+
end
|
35
|
+
|
36
|
+
if defined?(@lost_rating_points)
|
37
|
+
lost = @lost_rating_points.inject(:+)
|
38
|
+
else
|
39
|
+
lost = 0
|
40
|
+
end
|
41
|
+
gained - lost
|
42
|
+
end
|
43
|
+
|
44
|
+
def new_rating_points
|
45
|
+
new_added_rating_points + @rating_points
|
46
|
+
end
|
47
|
+
|
48
|
+
def weighting(weight)
|
49
|
+
@weight = weight.downcase.to_sym
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def expected_or_unexpected(winner: nil, loser: nil)
|
56
|
+
if winner > loser
|
57
|
+
weight_ary = R.__send__("#{@weight}_expected")
|
58
|
+
@winners_pts_ary, @losers_pts_ary = weight_ary
|
59
|
+
expected
|
60
|
+
else
|
61
|
+
weight_ary = R.__send__("#{@weight}_unexpected")
|
62
|
+
@winners_pts_ary, @losers_pts_ary = weight_ary
|
63
|
+
unexpected
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def expected
|
68
|
+
case pts_diff
|
69
|
+
when 0..25 then results(9)
|
70
|
+
when 26..50 then results(8)
|
71
|
+
when 51..100 then results(7)
|
72
|
+
when 101..150 then results(6)
|
73
|
+
when 151..200 then results(5)
|
74
|
+
when 201..300 then results(4)
|
75
|
+
when 301..400 then results(3)
|
76
|
+
when 401..500 then results(2)
|
77
|
+
when 501..750 then results(1)
|
78
|
+
when 751..Float::INFINITY then results(0)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def unexpected
|
83
|
+
case pts_diff
|
84
|
+
when 0..24 then results(0)
|
85
|
+
when 25..49 then results(1)
|
86
|
+
when 50..99 then results(2)
|
87
|
+
when 101..150 then results(3)
|
88
|
+
when 150..199 then results(4)
|
89
|
+
when 200..299 then results(5)
|
90
|
+
when 300..399 then results(6)
|
91
|
+
when 400..499 then results(7)
|
92
|
+
when 500..749 then results(8)
|
93
|
+
when 750..Float::INFINITY then results(9)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def pts_diff
|
98
|
+
diff = @rating_points - @opponent_rating_pts
|
99
|
+
(@points_difference ||= []) << diff.abs
|
100
|
+
diff.abs
|
101
|
+
end
|
102
|
+
|
103
|
+
def results(idx)
|
104
|
+
if @winner_flag
|
105
|
+
(@gained_rating_points ||= []) << @winners_pts_ary[idx]
|
106
|
+
else
|
107
|
+
(@lost_rating_points ||= []) << @losers_pts_ary[idx]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module IttfPoints
|
2
|
+
module R
|
3
|
+
class << self
|
4
|
+
def r1_expected
|
5
|
+
win = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
|
6
|
+
lose = [*0..9]
|
7
|
+
[win, lose]
|
8
|
+
end
|
9
|
+
|
10
|
+
def r1_unexpected
|
11
|
+
win = [20, 24, 28, 32, 40, 48, 56, 64, 72, 80]
|
12
|
+
lose = [10, 12, 14, 16, 20, 24, 28, 32, 36, 40]
|
13
|
+
[win, lose]
|
14
|
+
end
|
15
|
+
|
16
|
+
def r2_expected
|
17
|
+
win = [0, 2, 3, 5, 6, 8, 9, 11, 12, 14]
|
18
|
+
lose = [0, 1, 2, 3, 3, 4, 5, 6, 6, 7]
|
19
|
+
[win, lose]
|
20
|
+
end
|
21
|
+
|
22
|
+
def r2_unexpected
|
23
|
+
win = [15, 18, 21, 24, 30, 36, 42, 48, 54, 60]
|
24
|
+
lose = [8, 9, 11, 12, 15, 18, 21, 24, 27, 30]
|
25
|
+
[win, lose]
|
26
|
+
end
|
27
|
+
|
28
|
+
def r3_expected
|
29
|
+
win = [*0..9]
|
30
|
+
lose = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
|
31
|
+
[win, lose]
|
32
|
+
end
|
33
|
+
|
34
|
+
def r3_unexpected
|
35
|
+
win = [10, 12, 14, 16, 20, 24, 28, 32, 36, 40]
|
36
|
+
lose = [5, 6, 7, 8, 10, 12, 14, 16, 18, 20]
|
37
|
+
[win, lose]
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_method :w1_expected, :r1_expected
|
41
|
+
alias_method :w1_unexpected, :r1_unexpected
|
42
|
+
alias_method :w2_expected, :r2_expected
|
43
|
+
alias_method :w2_unexpected, :r2_unexpected
|
44
|
+
alias_method :w3_expected, :r3_expected
|
45
|
+
alias_method :w3_unexpected, :r3_unexpected
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/ittf_points.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ittf_points
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hachy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-09 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: Calculate rating points based on ITTF ranking system.
|
56
|
+
email:
|
57
|
+
- pekoett@gmail.com
|
58
|
+
executables:
|
59
|
+
- ittf_points
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- exe/ittf_points
|
73
|
+
- ittf_points.gemspec
|
74
|
+
- lib/ittf_points.rb
|
75
|
+
- lib/ittf_points/player.rb
|
76
|
+
- lib/ittf_points/r.rb
|
77
|
+
- lib/ittf_points/version.rb
|
78
|
+
homepage: https://github.com/hachy/ittf_points
|
79
|
+
licenses: []
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Calculate rating points based on ITTF ranking system.
|
101
|
+
test_files: []
|