tennis 0.3.0 → 0.4.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 +4 -4
- data/Makefile +6 -5
- data/lib/tennis.rb +43 -4
- data/lib/tennis/version.rb +2 -1
- data/spec/tennis_spec.rb +37 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a4965dfe7cf8388b7e4fd98dcca47d60f970fe61
|
|
4
|
+
data.tar.gz: 0cbe367e8353bfd785c806c699dea34afd7952ce
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e7df702d79a93dc6cef85d1a016d9c4049e4eb6d0d9e94ee168e75ec3e47feeb0c37066ef04c9ba15388d9b65347633aa32b282b3c4ec4c2ea38c1519ae44bea
|
|
7
|
+
data.tar.gz: 3468b6ba2e86653b1414ba007a6a2b163d336f2c8231a57fc9f4ba24a355d6cac85ec910cb08025950ea3ecde14555351b35351fb40adb8013e325637bb3844e
|
data/Makefile
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
# NOTE: this needs to be changed in lib/tennis/version.rb at the same time!
|
|
3
|
+
VERSION=0.4.0
|
|
3
4
|
|
|
4
5
|
all:
|
|
5
|
-
rake spec
|
|
6
|
+
bundle exec rake spec
|
|
6
7
|
|
|
7
8
|
build:
|
|
8
|
-
gem build tennis.gemspec
|
|
9
|
+
bundle exec gem build tennis.gemspec
|
|
9
10
|
|
|
10
11
|
irb:
|
|
11
|
-
irb -Ilib -rtennis
|
|
12
|
+
bundle exec irb -Ilib -rtennis
|
|
12
13
|
|
|
13
14
|
# to push to rubygems
|
|
14
15
|
push:
|
|
15
|
-
gem push tennis-$(VERSION).gem
|
|
16
|
+
bundle exec gem push tennis-$(VERSION).gem
|
data/lib/tennis.rb
CHANGED
|
@@ -7,28 +7,67 @@ class Tennis
|
|
|
7
7
|
# sets_won/lost is an array: [sets won/lost by 0, sets won/lost by 1]
|
|
8
8
|
# games_won/lost is an array: [games won/lost by 0, games won/lost by 1]
|
|
9
9
|
|
|
10
|
+
# representation of defaults:
|
|
11
|
+
#
|
|
12
|
+
# p0-<reason> means player 0 won, e.g. p0-win-by-forfeit
|
|
13
|
+
# p1-<reason> means player 1 won
|
|
14
|
+
#
|
|
15
|
+
# the reason is ignored for the purposes of this gem.
|
|
16
|
+
# possible reason fields are: win-by-forfeit, win-by-retired, win-by-no-show
|
|
17
|
+
#
|
|
18
|
+
|
|
10
19
|
attr_reader :winner, :sets_won, :sets_lost, :games_won, :games_lost
|
|
11
20
|
|
|
12
21
|
def initialize(score)
|
|
13
|
-
#
|
|
14
|
-
|
|
22
|
+
# check if it's a default first:
|
|
23
|
+
if score =~ /^p/
|
|
24
|
+
process_default(score)
|
|
25
|
+
@default = true
|
|
26
|
+
return
|
|
27
|
+
end
|
|
28
|
+
@default = false
|
|
15
29
|
process_score(score)
|
|
16
30
|
end
|
|
17
31
|
|
|
18
32
|
# to_s
|
|
19
33
|
# return the score in string format
|
|
20
34
|
def to_s
|
|
21
|
-
@score.map{|set| set.join('-') }.join(', ')
|
|
35
|
+
@default ? "Default" : @score.map{|set| set.join('-') }.join(', ')
|
|
22
36
|
end
|
|
23
37
|
|
|
24
38
|
# flip score ( P1-P2 to P2-P1)
|
|
25
39
|
# returns the flipped score as a string
|
|
26
40
|
def flipped
|
|
27
|
-
@score.map{|set| set.reverse.join('-') }.join(', ')
|
|
41
|
+
@default ? "Default" : @score.map{|set| set.reverse.join('-') }.join(', ')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def score
|
|
45
|
+
@default ? "Default" : @score
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def default?
|
|
49
|
+
@default
|
|
28
50
|
end
|
|
29
51
|
|
|
30
52
|
private
|
|
31
53
|
|
|
54
|
+
def process_default(default)
|
|
55
|
+
@score = default
|
|
56
|
+
if default =~ /p0-win/
|
|
57
|
+
# process as 6-0, 6-0
|
|
58
|
+
@winner = 0
|
|
59
|
+
@sets_won, @sets_lost = [[2, 0], [0, 2]]
|
|
60
|
+
@games_won, @games_lost = [[12, 0], [0, 12]]
|
|
61
|
+
elsif default =~ /p1-win/
|
|
62
|
+
# process as 0-6, 0-6
|
|
63
|
+
@winner = 1
|
|
64
|
+
@sets_won, @sets_lost = [[0, 2], [2, 0]]
|
|
65
|
+
@games_won, @games_lost = [[0, 12], [12, 0]]
|
|
66
|
+
else
|
|
67
|
+
raise "Invalid default score report: '#{default}'"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
32
71
|
def process_score(score, best_of=3)
|
|
33
72
|
begin
|
|
34
73
|
sets = score.split(/,/)
|
data/lib/tennis/version.rb
CHANGED
data/spec/tennis_spec.rb
CHANGED
|
@@ -32,10 +32,24 @@ describe Tennis, "#scores" do
|
|
|
32
32
|
expect(score.winner).to eq 0
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
it "finds the winner properly in three sets with last set to 7-5" do
|
|
36
|
+
score = Tennis.new("6-4, 6-7, 7-5")
|
|
37
|
+
expect(score.winner).to eq 0
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "finds the winner properly in three sets with last set to 8-6" do
|
|
41
|
+
score = Tennis.new("6-4, 6-7, 8-6")
|
|
42
|
+
expect(score.winner).to eq 0
|
|
43
|
+
end
|
|
44
|
+
|
|
35
45
|
it "report error is the last set is off by more than 2" do
|
|
36
46
|
expect { Tennis.new("6-4, 6-7, 18-15") }.to raise_error
|
|
37
47
|
end
|
|
38
48
|
|
|
49
|
+
it "report error is the last set is off by more than 2" do
|
|
50
|
+
expect { Tennis.new("6-4, 6-7, 8-7") }.to raise_error
|
|
51
|
+
end
|
|
52
|
+
|
|
39
53
|
it "reports incomplete match score (set 1-1)" do
|
|
40
54
|
expect { Tennis.new("6-4,4-6") }.to raise_error
|
|
41
55
|
end
|
|
@@ -118,3 +132,26 @@ describe Tennis, "#games_lost" do
|
|
|
118
132
|
end
|
|
119
133
|
end
|
|
120
134
|
end
|
|
135
|
+
|
|
136
|
+
describe Tennis, "#defaults" do
|
|
137
|
+
it "finds the winner properly in a default" do
|
|
138
|
+
score = Tennis.new("p0-win-by-something")
|
|
139
|
+
expect(score.winner).to eq 0
|
|
140
|
+
expect(score.sets_won).to eq [2, 0]
|
|
141
|
+
expect(score.games_won).to eq [12, 0]
|
|
142
|
+
expect(score.to_s).to eq "Default"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
it "finds the winner properly in a default" do
|
|
146
|
+
score = Tennis.new("p1-win-by-something")
|
|
147
|
+
expect(score.winner).to eq 1
|
|
148
|
+
expect(score.sets_won).to eq [0, 2]
|
|
149
|
+
expect(score.games_won).to eq [0, 12]
|
|
150
|
+
expect(score.to_s).to eq "Default"
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
it "reports some error in a bad default" do
|
|
154
|
+
expect { Tennis.new("p2-blah") }.to raise_error
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tennis
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rohan Katyal
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2015-03-
|
|
12
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: bundler
|