icu_tournament 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -10
- data/lib/icu_tournament/tournament_sp.rb +29 -3
- data/lib/icu_tournament/version.rb +1 -1
- data/spec/tournament_sp_spec.rb +90 -10
- metadata +12 -2
data/README.rdoc
CHANGED
@@ -9,11 +9,10 @@ The gem is hosted on gemcutter only:
|
|
9
9
|
|
10
10
|
sudo gem install icu_tournament
|
11
11
|
|
12
|
-
For
|
12
|
+
For handling SwissPerfect files the _dbf_, _inifile_ and _rubyzip_ gems are required.
|
13
|
+
For Ruby prior to version 1.9 the _fastercsv_ gem is needed to handle CSV files.
|
14
|
+
|
13
15
|
|
14
|
-
sudo gem install fastercsv
|
15
|
-
|
16
|
-
|
17
16
|
== Usage
|
18
17
|
|
19
18
|
There are two main uses for this gem:
|
@@ -24,11 +23,11 @@ There are two main uses for this gem:
|
|
24
23
|
* You have a file in a supported format and you need to extract the information it contains.
|
25
24
|
For example, you have a Krause formatted file and want to extract the data and insert it into a database.
|
26
25
|
|
27
|
-
The currently supported formats
|
26
|
+
The currently supported formats are:
|
28
27
|
|
29
28
|
* ICU::Tournament::Krause - the format used by FIDE.
|
30
29
|
* ICU::Tournament::ForeignCSV - used by Irish players to report their individual results in foreign tournaments.
|
31
|
-
* ICU::Tournament::SwissPerfect -
|
30
|
+
* ICU::Tournament::SwissPerfect - often used by Irish tournament controllers to report results.
|
32
31
|
|
33
32
|
|
34
33
|
== Writing Files
|
@@ -41,7 +40,7 @@ the information that a Krause file might contain is included here (see ICU::Tour
|
|
41
40
|
t.add_player(ICU::Player.new('Robert J.', 'Fischer', 1))
|
42
41
|
t.add_player(ICU::Player.new('Boris V.', 'Spassky', 2))
|
43
42
|
|
44
|
-
Then the results for each round are added using the ID numbers to refer to the players.
|
43
|
+
Then the results for each round are added using the unique ID numbers to refer to the players.
|
45
44
|
|
46
45
|
t.add_result(ICU::Result.new(1, 1, 'L', :opponent => 2, :colour => 'B'))
|
47
46
|
|
@@ -67,11 +66,13 @@ Suppose you have a tournament file in Krause format. Parse it into a tournament
|
|
67
66
|
data = open('tournament.txt') { |f| f.read }
|
68
67
|
parser = ICU::Tournament::Krause.new
|
69
68
|
tournament = parser.parse(data)
|
70
|
-
|
69
|
+
|
71
70
|
On success, the _parse_ method returns an object of type ICU::Tournament. A tournament (ICU::Tournament)
|
72
71
|
has two or more players (ICU::Player), and each player has one or more results (ICU::Result). See the
|
73
|
-
rdoc for more details.
|
74
|
-
|
72
|
+
rdoc for more details. You can traverse these objects and do what you want with them, such as storing the
|
73
|
+
information in a database.
|
74
|
+
|
75
|
+
On error, the _parse_ method returns _nil_ and an error message can be retrieved from the parser (parser.error).
|
75
76
|
|
76
77
|
== Author
|
77
78
|
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'inifile'
|
2
2
|
require 'dbf'
|
3
|
+
require 'zip/zipfilesystem'
|
4
|
+
require 'tempfile'
|
3
5
|
|
4
6
|
module ICU
|
5
7
|
class Tournament
|
@@ -149,7 +151,7 @@ Should you wish to rank the tournament using a different set of tie-break rules,
|
|
149
151
|
private
|
150
152
|
|
151
153
|
def get_files(file)
|
152
|
-
file.match(/\.zip$/i) ?
|
154
|
+
file.match(/\.zip$/i) ? get_zip_files(file) : get_bare_files(file)
|
153
155
|
end
|
154
156
|
|
155
157
|
def get_bare_files(file)
|
@@ -161,8 +163,32 @@ Should you wish to rank the tournament using a different set of tie-break rules,
|
|
161
163
|
end
|
162
164
|
end
|
163
165
|
|
164
|
-
def
|
165
|
-
|
166
|
+
def get_zip_files(file)
|
167
|
+
temp = Hash.new
|
168
|
+
begin
|
169
|
+
Zip::ZipFile.open(file) do |zf|
|
170
|
+
raise "ZIP file should contain exactly 3 files (.ini, .trn and .sco)" unless zf.size == 3
|
171
|
+
stem = Hash.new
|
172
|
+
zf.entries.each do |e|
|
173
|
+
if e.file? && e.name.match(/^(.+)\.(ini|trn|sco)$/i)
|
174
|
+
stm = $1
|
175
|
+
ext = $2.downcase
|
176
|
+
stem[ext] = stm
|
177
|
+
tmp = Tempfile.new(e.name)
|
178
|
+
tmp.close
|
179
|
+
e.extract(tmp.path) { true }
|
180
|
+
temp[ext] = tmp.path
|
181
|
+
end
|
182
|
+
end
|
183
|
+
%w(ini trn sco).each { |ext| raise "no #{ext.upcase} file found" unless stem[ext] }
|
184
|
+
raise "different stem names found" unless stem['ini'] == stem['trn'] && stem['trn'] == stem['sco']
|
185
|
+
end
|
186
|
+
rescue Zip::ZipError
|
187
|
+
raise "invalid ZIP file"
|
188
|
+
rescue => ex
|
189
|
+
raise ex
|
190
|
+
end
|
191
|
+
%w(ini trn sco).map { |ext| temp[ext] }
|
166
192
|
end
|
167
193
|
|
168
194
|
def parse_ini(file)
|
data/spec/tournament_sp_spec.rb
CHANGED
@@ -43,11 +43,11 @@ module ICU
|
|
43
43
|
@t.player(45).signature.should == "Catre, Loredan||507|3.5|18|123456|WDLWLW|BWBWBW|FTTTFT" # had two byes
|
44
44
|
@t.player(56).signature.should == "McDonnell, Cathal||498|0.0|54|1|L|-|F" # last
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
it "should have consistent ranks" do
|
48
48
|
@t.players.map{ |p| p.rank }.sort.join('').should == (1..@t.players.size).to_a.join('')
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
it "should have the correct tie breaks" do
|
52
52
|
@t.tie_breaks.join('|').should == "buchholz|harkness"
|
53
53
|
end
|
@@ -76,11 +76,11 @@ module ICU
|
|
76
76
|
@t.player(3).signature.should == "Hulleman, Leon|6409|1466|1.0|3|123|LWL|BBW|TTT"
|
77
77
|
@t.player(4).signature.should == "Dunne, Thomas|10914||0.0|4|123|LLL|BWB|TTT"
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
it "should have consistent ranks" do
|
81
81
|
@t.players.map{ |p| p.rank }.sort.join('').should == (1..@t.players.size).to_a.join('')
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
it "should have the no tie breaks" do
|
85
85
|
@t.tie_breaks.join('|').should == ""
|
86
86
|
end
|
@@ -110,11 +110,11 @@ module ICU
|
|
110
110
|
@t.player(18).signature.should == "Freeman, Ruiri|||2.0|17|1234567|DDLLLLW|--WBBWB|FFTTTTF" # had byes and bonus (in BONUS)
|
111
111
|
@t.player(16).signature.should == "O'Connor, David|||1.0|19|123|WLL|WBW|FTF" # last
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
it "should have consistent ranks" do
|
115
115
|
@t.players.map{ |p| p.rank }.sort.join('').should == (1..@t.players.size).to_a.join('')
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
it "should have the correct tie breaks" do
|
119
119
|
@t.tie_breaks.join('|').should == "harkness|buchholz"
|
120
120
|
end
|
@@ -138,11 +138,11 @@ module ICU
|
|
138
138
|
@t.player(8).signature.should == "Berney, Mark|10328|1948|2.0|3|23|WW|BW|TT" # didn't play in round 1
|
139
139
|
@t.player(10).signature.should == "O'Donnell, Conor E.|10792|1073|2.0|10|123|LWW|WBW|TFT" # got just 1 point for a bye
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
it "should have consistent ranks" do
|
143
143
|
@t.players.map{ |p| p.rank }.sort.join('').should == (1..@t.players.size).to_a.join('')
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
it "should have the correct tie breaks" do
|
147
147
|
@t.tie_breaks.join('|').should == "neustadtl"
|
148
148
|
end
|
@@ -167,7 +167,7 @@ module ICU
|
|
167
167
|
@t.player(46).signature.should == "O'Riordan, Pat|10696|900|2.0|42|123456|LDDLDD|BWBWWB|TTTTFT"
|
168
168
|
@t.player(38).signature.should == "Gill, Craig I.|10637|1081|2.0|43|123456|LLWDDL|BWBWWB|TTTTFT"
|
169
169
|
end
|
170
|
-
|
170
|
+
|
171
171
|
it "should have consistent ranks" do
|
172
172
|
@t.players.map{ |p| p.rank }.sort.join('').should == (1..@t.players.size).to_a.join('')
|
173
173
|
end
|
@@ -191,7 +191,87 @@ module ICU
|
|
191
191
|
@t.player(8).signature.should == "Vega, Marcos Llaneza||2475|3.0|16|1234|DDWW|BWBW|TTTT"
|
192
192
|
@t.player(67).signature.should == "Lee, Shane|780|1633|1.0|52|134|DLD|WWW|TTT"
|
193
193
|
end
|
194
|
-
|
194
|
+
|
195
|
+
it "should have correct details for selection of players, including international IDs and ratings when so configured" do
|
196
|
+
@t = @p.parse_file(SAMPLES + 'ncc', "2010-05-08", :id => :intl, :rating => :intl)
|
197
|
+
@t.player(2).signature.should == "Szabo, Gergely|1205064||4.0|4|1234|WWWW|WBWB|TTTT"
|
198
|
+
@t.player(5).signature.should == "Daly, Colm|2500434||3.5|7|1234|WWWD|WBWB|TTTT"
|
199
|
+
@t.player(8).signature.should == "Vega, Marcos Llaneza|2253585||3.0|16|1234|DDWW|BWBW|TTTT"
|
200
|
+
@t.player(67).signature.should == "Lee, Shane|||1.0|52|134|DLD|WWW|TTT"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context "Non-existant ZIP file" do
|
205
|
+
|
206
|
+
before(:all) do
|
207
|
+
@p = ICU::Tournament::SwissPerfect.new
|
208
|
+
@t = @p.parse_file(SAMPLES + 'nosuchzipfile.zip', "2010-05-08")
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should not parse and should have a relevant error" do
|
212
|
+
@t.should be_nil
|
213
|
+
@p.error.should match(/invalid ZIP file/i)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
context "Invalid ZIP file" do
|
218
|
+
|
219
|
+
before(:all) do
|
220
|
+
@p = ICU::Tournament::SwissPerfect.new
|
221
|
+
@t = @p.parse_file(SAMPLES + 'notazipfile.zip', "2010-05-08")
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should not parse and have a should have relevant error" do
|
225
|
+
@t.should be_nil
|
226
|
+
@p.error.should match(/invalid ZIP file/i)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context "ZIP file containing the wrong number of files" do
|
231
|
+
|
232
|
+
before(:all) do
|
233
|
+
@p = ICU::Tournament::SwissPerfect.new
|
234
|
+
@t = @p.parse_file(SAMPLES + 'notenoughfiles.zip', "2010-05-08")
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should not parse and should have a relevant error" do
|
238
|
+
@t.should be_nil
|
239
|
+
@p.error.should match(/3 files/i)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "ZIP file containing the files with mixed stems" do
|
244
|
+
|
245
|
+
before(:all) do
|
246
|
+
@p = ICU::Tournament::SwissPerfect.new
|
247
|
+
@t = @p.parse_file(SAMPLES + 'mixedstems.zip', "2010-05-08")
|
248
|
+
end
|
249
|
+
|
250
|
+
it "should not parse and should have a relevant error" do
|
251
|
+
@t.should be_nil
|
252
|
+
@p.error.should match(/different stem/i)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
context "National Club Champiomships 2010 ZIP file" do
|
257
|
+
|
258
|
+
before(:all) do
|
259
|
+
@p = ICU::Tournament::SwissPerfect.new
|
260
|
+
@t = @p.parse_file(SAMPLES + 'nccz.zip', "2010-05-08")
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should parse and have the right basic details" do
|
264
|
+
@p.error.should be_nil
|
265
|
+
@t.signature.should == "National Club Championship 2010|Gerry Graham|4|2010-05-08|77"
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should have correct details for selection of players, including ICU IDs" do
|
269
|
+
@t.player(2).signature.should == "Szabo, Gergely|12379|2530|4.0|4|1234|WWWW|WBWB|TTTT"
|
270
|
+
@t.player(5).signature.should == "Daly, Colm|295|2314|3.5|7|1234|WWWD|WBWB|TTTT"
|
271
|
+
@t.player(8).signature.should == "Vega, Marcos Llaneza||2475|3.0|16|1234|DDWW|BWBW|TTTT"
|
272
|
+
@t.player(67).signature.should == "Lee, Shane|780|1633|1.0|52|134|DLD|WWW|TTT"
|
273
|
+
end
|
274
|
+
|
195
275
|
it "should have correct details for selection of players, including international IDs and ratings when so configured" do
|
196
276
|
@t = @p.parse_file(SAMPLES + 'ncc', "2010-05-08", :id => :intl, :rating => :intl)
|
197
277
|
@t.player(2).signature.should == "Szabo, Gergely|1205064||4.0|4|1234|WWWW|WBWB|TTTT"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: icu_tournament
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Orr
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-07-17 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -42,6 +42,16 @@ dependencies:
|
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: 1.2.5
|
44
44
|
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: rubyzip
|
47
|
+
type: :runtime
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.4
|
54
|
+
version:
|
45
55
|
- !ruby/object:Gem::Dependency
|
46
56
|
name: rspec
|
47
57
|
type: :development
|