-
Notifications
You must be signed in to change notification settings - Fork 0
/
NAMParser_Team.rb
executable file
·165 lines (142 loc) · 3.77 KB
/
NAMParser_Team.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/ruby
require 'csv'
require 'time'
POOL_LANES = 8
class Team
attr_accessor :name, :club
def initialize(name = '', club = '', category = '')
@name = name
@club = club
@category = category
end
end
class Event
attr_accessor :number, :name, :distance, :category, :heat
def initialize(number=0, name='', distance='', category='', heat={})
@number = number
@name = name
@distance = distance
@category = category
@heat = heat
end
end
class Tournament
attr_accessor :name, :date, :events
def initialize(name, date, events = [])
@name = name
@date = Date.parse(date)
@events = events
end
end
class FileWriter
attr_accessor :filename, :payload
def initialize(filename, payload)
@filename = filename
@payload = payload
@scoreboard_dir = '_SCOREBOARD_'
@ares_dir = '_Ares_'
Dir.mkdir(@scoreboard_dir) unless File.exists? @scoreboard_dir
Dir.mkdir(@ares_dir) unless File.exists? @ares_dir
case
when 'STEUER.TXT' || @filename.match('NAM')
@filename = @scoreboard_dir << '/' << @filename
when @filename.match('LST')
@filename = @ares_dir << '/' << @filename
else
raise "We can't handle this filename."
end
self.write_file
end
def write_file
begin
file = File.open(@filename, 'a:iso-8859-1')
file.write(@payload)
rescue IOError => e
puts "Error while writing #{@filename}: #{e}."
ensure
file.close unless file == nil
end
end
end
##################
#
# Data processing
#
##################
line = 1
setting = Hash.new
event = Event.new
tournament = Tournament.new 'Salzpokal DLRG', '2013-09-14 09:00'
raise 'Please provide a filename as argument.' unless ARGV.length > 0
CSV.foreach(ARGV[0], :headers => true, :col_sep => ',', :encoding => 'iso-8859-1:UTF-8') do |row|
if line == 1
1.upto POOL_LANES do |i|
if row[2+i] == nil || row[2+i].length == 0
setting[i] = nil
else
name = row[2+i].split(',')[0]
category = row[1].split(' ').last
setting[i] = Team.new name, nil, category
end
end
number = row[0].to_i + 100
category = row[1].split(' ')[1]
name = row[2]
distance = row[2].split(' ')[0].strip
event = Event.new number, name, distance, category
elsif line == 2
1.upto POOL_LANES do
setting.each do |key, value|
value.club = row[2+key]unless value == nil
event.heat[key] = value
end
end
else
raise 'We are on a line count > 2. This is wrong.'
end
if line == 1
line += 1
elsif line == 2
tournament.events.push event
line = 1
end
end
#tournament.events.each do |event|
# puts "###"
# puts "#{event.number}: #{event.name}"
# event.heat.each do |key, value|
# puts "#{key}: #{value.inspect}"
# end
#end
#############
#
# File processing
#
#############
tournament.events.each do |event|
# STEUER.TXT
# We need a certain entry for the event category in this file
case event.category
when 'w'
event_category = 'weiblich'
when 'm'
event_category = 'männlich'
when 'gem.'
event_category = 'mixed'
else
raise 'Unknown category.'
end
#Format string: #EventNumber, #Count #Distance #Number #Category
FileWriter.new('STEUER.TXT', sprintf("%-6s4 x%6s %-21s%s\r\n", event.number, event.name.split(' ').first, event.name.split(' ').last[0..10], event_category))
# NAM Files
filename = sprintf('%05d', event.number) << '001.NAM'
event.heat.each do |key, value|
#puts "#{key.class}: #{value}"
if value == nil
FileWriter.new filename, sprintf("%02d\r\n", key)
else
#Format string #Whitespace #LaneNumber #LastName #FirstName, #Club
FileWriter.new filename, sprintf("%02d%-30s%-20s\r\n", key, value.name, value.club[0..19])
end
end
end