class ICU::Tournament::SwissPerfect
This is the format produced by the Windows program, SwissPerfect. It consists of three files with the same name but different endings: .ini for meta data such as tournament name and tie-break rules, .trn for the player details such as name and rating, and .sco for the results. The first file is text and the other two are in an old binary format known as DBase 3.
To parse such a set of files, use either the parse_file! or parse_file method supplying the name of any one of the three files or just the stem name without any ending. In case of error, such as any of the files not being found, parse_file! will throw an exception while parse_file will return nil and record an error message. As well as a file name or stem name, you should also supply a start date in the options as SwissPerfect
does not record this information.
parser = ICU::Tournament::SwissPerfect.new tournament = parser.parse_file('champs', :start => '2010-07-03') # looks for "champs.ini", "champs.trn" and "champs.sco" puts tournament ? 'ok' : "problem: #{parser.error}"
Alternatively, if all three files are in a ZIP archive, the parser will extract them if the name of the archive file is supplied to the parse_file method and it ends in “.zip” (case insensitive):
tournament = parser.parse_file('champs.zip', :start => '2010-07-03')
Or, if the file is a ZIP archive but it’s name doesn’t end in “.zip”, that can be signalled with an option:
tournament = parser.parse_file('/tmp/a84f21ge', :zip => true, :start => '2010-07-03')
Note there must be only three files in the archive, they must all have the same stem name and their endings should be “.ini”, “.trn” and “.sco” (case insensitive).
If no start date is supplied it will default to 2000-01-01, and can be reset later.
tournament = parser.parse_file('champs.zip') tournament.start # => '2000-01-01' tournament.start = '2010-07-03'
SwissPerfect
files have slots for both local and international IDs and ratings and these, if present (and if integers) are copied to the id, fide, rating and fide_rating attributes.
tournament = parser.parse_file('ncc', :start => '2010-05-08') tournament.player(2).id # => 12379 (ICU ID) tournament.player(2).fide_id # => 1205064 (FIDE ID) tournament.player(2).rating # => 2556 (ICU rating) tournament.player(2).fide_rating # => 2530 (FIDE rating)
By default, the parse will fail completely if the “.trn” file contains any invalid federations (see ICU::Federation). There are two alternative behaviours controlled by setting the fed option:
tournament = parser.parse_file('ncc', :start => '2010-05-08', :fed == 'skip') # => silently skips invalid federations tournament = parser.parse_file('ncc', :start => '2010-05-08', :fed == 'ignore') # => ignores all federations
Note that federations that don’t match 3 letters are always silently skipped.
Because the data is in three parts, some of which are in a legacy binary format, serialization to this format is not supported. Instead, a method is provided to serialize any tournament type into the text export format of SwissPerfect
(see ICU::Tournament::SPExport
).
Constants
- SCO
- TRN
Attributes
Public Instance Methods
Parse SP data returning an ICU::Tournament
or a nil on failure. In the latter case, an error message will be available via the error method.
# File lib/icu_tournament/tournament_sp.rb, line 98 def parse_file(file, arg={}) begin parse_file!(file, arg) rescue => ex @error = ex.message nil end end
Parse SP data returning a Tournament
or raising an exception on error.
# File lib/icu_tournament/tournament_sp.rb, line 82 def parse_file!(file, arg={}) @t = Tournament.new('Unspecified', '2000-01-01') @t.start = arg[:start] if arg[:start] @bonus = {} @start_no = {} ini, trn, sco = get_files(file, arg) parse_ini(ini) parse_trn(trn, arg) parse_sco(sco) fixup @t.validate!(:rerank => true) @t end
Serialise a tournament to SwissPerfect
text export format.
# File lib/icu_tournament/tournament_sp.rb, line 108 def serialize(t, arg={}) t.validate!(:type => self) # Ensure a nice set of numbers. t.renumber(:order) # Widths for the rank, name and ID and the number of rounds. m1 = t.players.inject(2) { |l, p| p.num.to_s.length > l ? p.num.to_s.length : l } m2 = t.players.inject(4) { |l, p| p.name.length > l ? p.name.length : l } m3 = t.players.inject(6) { |l, p| p.id.to_s.length > l ? p.id.to_s.length : l } rounds = t.last_round # The header, followed by a blank line. formats = ["%-#{m1}s", "%-#{m2}s", "%-#{m3}s", "%-5s"] (1..rounds).each { |r| formats << "%#{m1}d " % r } sp = formats.join("\t") % ['No', 'Name', 'Loc Id', 'Total'] sp << "\r\n\r\n" # Adjust the round parts of the formats for players results. (1..t.last_round).each { |r| formats[r+3] = "%#{m1+2}s" } # Now add a line for each player. t.players.each { |p| sp << p.to_sp_text(rounds, "#{formats.join(%Q{\t})}\r\n") } # And return the whole lot. sp end
Additional tournament validation rules for this specific type.
# File lib/icu_tournament/tournament_sp.rb, line 137 def validate!(t) # None. end