-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNAMParser_Individual.rb
More file actions
executable file
·229 lines (195 loc) · 5.6 KB
/
NAMParser_Individual.rb
File metadata and controls
executable file
·229 lines (195 loc) · 5.6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/ruby
require 'csv'
require 'time'
POOL_LANES = 8
class Swimmer
attr_accessor :first_name, :last_name, :club, :category
attr_reader :birth_year
def initialize(first_name = '', last_name = '', club = '', category = '')
@first_name = first_name
@last_name = last_name
@club = club
@category = category
@birth_year = '1990'
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, :start_time, :events
def initialize(name, date, events = [])
@name = name
@date = Date.parse(date)
@start_time = Time.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
if @filename == 'STEUER.TXT' || @filename.match('NAM')
@filename = @scoreboard_dir << '/' << @filename
elsif @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
class Storage
def initialize
@storage = Hash.new
@key = 0
end
def add (candidate)
if self.stored? candidate
candidate
else
@storage[@key] = candidate
@key += 1
end
end
def list
@storage.each do |value|
value
end
end
def stored? (candidate)
@storage.value? candidate
end
def storage_id (candidate)
@storage.each do |key, value|
if value == candidate
return key
end
end
end
end
##################
#
# Data processing
#
##################
line = 1
setting = Hash.new
event = Event.new
tournament = Tournament.new 'Salzpokal DLRG', '2013-09-14 09:00'
styles = Storage.new
distances = Storage.new
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
first_name = row[2+i].split(',')[1].strip
last_name = row[2+i].split(',')[0]
setting[i] = Swimmer.new first_name, last_name, nil, nil
end
end
elsif line == 2
1.upto POOL_LANES do
number = row[0]
category = row[1].split(' ')[2]
name = row[2]
distance = row[2].split(' ')[0].strip
event = Event.new number, name, distance, category
styles.add event.name.match('\D+')[0][2, event.name.length]
distances.add event.name.match('\d{1,3}')[0]
setting.each do |key, value|
value.club = row[2+key]unless value == nil
event.heat[key] = value
end
end
elsif line == 3
1.upto POOL_LANES do |i|
event.heat[i].category = row[2+i].split(' ').last unless row[2+i] == nil || row[2+i].length == 0
end
else
raise 'We are on a line count > 3. This is wrong.'
end
if line < 3
line += 1
elsif line == 3
tournament.events.push event
line = 1
end
end
#tournament.events.each do |event|
# puts "###"
# puts event.number
# event.heat.each do |key, value|
# puts "#{key}: #{value.inspect}"
# end
#end
#############
#
# File processing
#
#############
# ARES files
# LSTCAT.TXT
#header = "\"Kategorie\";\"AbrCat\"\r\n"
#body = "\"weiblich\";\"W\"\r\n\"männlich\";\"M\"\r\n\"mixed\";\"X\"\r\n"
#FileWriter.new "LSTCAT.TXT", header << body
# LSTLONG.TXT
#header = "\"idLength\";\"Longueur\";\"MLongueur\";\"Relais\"\r\n"
#body = "0;\"50 m\";50;0\r\n1;\"100 m\";100;0\r\n2;\"200 m\";200;0\r\n3;\"400 m\";400;0\r\n4;\"800 m\";800;0\r\n5;\"1500 m\";1500;0\r\n6;\"4x100m\";400;4\r\n7;\"4x200m\";800;4\r\n8;\"4x50 m\";200 ;4\r\n9;\"25 m\";25;1"
#FileWriter.new "LSTLONG.TXT", header << body
# LSTRACE.TXT
#header = "event;round;nbHeat;idLen;idStyle;abCat;date;time\r\n"
#FileWriter.new "LSTRACE.TXT", header
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("%-6s1 x%6s %-21s%s\r\n", event.number, event.name.match('\d*m')[0], event.name.match('\D+')[0][2,10], event_category))
# FileWriter.new "LSTRACE.TXT", sprintf("%s;%s;%s;%s;%s;%s;%s;\r\n", event.number, 0,0,1,styles.style_id(event.name.match('\D+')[0][2,event.name.length]),tournament.date,tournament.start_time+1)
# NAM File
filename = sprintf('%05d', event.number) << '001.NAM'
event.heat.each do |key, value|
#puts "#{key.class}: #{value}"
if value == nil
FileWriter.new filename, sprintf("%d\r\n", key)
else
#Format string #Whitespace #LaneNumber #LastName #FirstName, #Club
FileWriter.new filename, sprintf("%d%-30s%-30s%-20s\r\n", key, value.last_name, value.first_name, value.club[0..19])
end
end
end