Class: SportDb::JsonExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/exporters/json_exporter.rb,
lib/sportdb/exporters/json_exporter_copa.rb,
lib/sportdb/exporters/json_exporter_euro.rb,
lib/sportdb/exporters/json_exporter_worldcup.rb

Constant Summary collapse

LEAGUE_TO_BASENAME =
{
  'eng.1' => 'en.1',
  'eng.2' => 'en.2',
  'eng.3' => 'en.3',
  'eng.4' => 'en.4',

  'world' => 'worldcup',
  ## 'cl'    => 'cl',    ## keep cl as is :-)
  'uefa.cl'       => 'cl',
  'uefa.cl.quali' => 'cl.quali'
}
CITY_TO_TIMEZONE =

hack: for timezone (add timezone to city - fix/fix/fix)

{
  ## brazil 2014
  'Rio de Janeiro'   => 'UTC-3',
  'Brasília'         => 'UTC-3',
  'São Paulo'        => 'UTC-3',
  'Fortaleza'        => 'UTC-3',
  'Belo Horizonte'   => 'UTC-3',
  'Salvador'         => 'UTC-3',
  'Natal'            => 'UTC-3',
  'Porto Alegre'     => 'UTC-3',
  'Recife'           => 'UTC-3',
  'Curitiba'         => 'UTC-3',
  'Cuiabá'           => 'UTC-4',
  'Manaus'           => 'UTC-4',
  ## russia 2018
  'Kaliningrad'      => 'UTC+2',
  'Nizhny Novgorod'  => 'UTC+3',
  'Volgograd'        => 'UTC+3',
  'Saransk'          => 'UTC+3',
  'Rostov-on-Don'    => 'UTC+3',
  'Kazan'            => 'UTC+3',
  'Sochi'            => 'UTC+3',
  'Saint Petersburg' => 'UTC+3',
  'Moscow'           => 'UTC+3',
  'Samara'           => 'UTC+4',
  'Ekaterinburg'     => 'UTC+5',
}

Class Method Summary collapse

Class Method Details

.build_clubs(event) ⇒ Object

helpers



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sportdb/exporters/json_exporter.rb', line 92

def self.build_clubs( event )
  clubs = []
  event.teams.each do |team|
    clubs << { name:    team.name,
               code:    team.code,
               country: team.country.name, }
  end

  data = {
   name:  event.name,
   clubs: clubs
  }

  data
end

.build_goals(match) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/sportdb/exporters/json_exporter.rb', line 208

def self.build_goals( match )
  goals1 = []
  goals2 = []

  match.goals.each do |goal|
    if goal.team_id == match.team1_id
      goals = goals1
    elsif goal.team_id == match.team2_id
      goals = goals2
    else
      puts "*** team id NOT matching for goal; must be team1 or team2 id"
      exit 1   ## exit - why? why not?
    end

    g = { name:   goal.person.name,
          minute: goal.minute,
        }

    g[:offset]  = goal.offset     if goal.offset != 0
    g[:penalty] = goal.penalty    if goal.penalty
    g[:owngoal] = goal.owngoal    if goal.owngoal

=begin        
    g[:score1]  = goal.score1
    g[:score2]  = goal.score2
=end

    goals << g
  end
  [goals1, goals2]
end

.build_groups(event) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sportdb/exporters/json_exporter.rb', line 109

def self.build_groups( event )
  groups = []
  event.groups.each do |group|
    teams  = []

    if group.teams.empty?
      puts "!! WARN - group #{group.name} has no teams/is empty"
    end

    group.teams.each do |team|
      teams << team.name
    end
    groups << { name: group.name, teams: teams }
  end

  data = {
   name:   event.name,
   groups: groups
  }

  data
end

.build_matches(event) ⇒ Object



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
# File 'lib/sportdb/exporters/json_exporter.rb', line 133

def self.build_matches( event )
  ## note: no longer list by rounds
  ##  now list by dates and add round as a "regular" field
  ##    note: make round optional too!!!

  matches = []
  event.matches.order( 'date ASC' ).each do |match|
      h = {}

      ## let stage and/or group go first if present/available
      h[ :stage ] = match.stage.name    if match.stage
      h[ :round ] = match.round.name    if match.round
      h[ :group ] = match.group.name    if match.group


      h[ :date  ] = match.date.strftime( '%Y-%m-%d')
      h[ :team1 ] = match.team1.name
      h[ :team2 ] = match.team2.name


      score = {}
      if match.score1 && match.score2
        score[:ft] = [match.score1, match.score2]
      end

      if match.score1et && match.score2et
        score[:et] = [match.score1et, match.score2et]
      end

      if match.score1p && match.score2p
        score[:p]  = [match.score1p, match.score2p]
      end

      h[ :score ] = score   unless score.empty?  ## note: only add if has some data


      unless match.goals.empty?
        goals1, goals2 = build_goals( match )
        h[ :goals1 ] = goals1
        h[ :goals2 ] = goals2
      end


      if match.status
        case match.status
        when Status::CANCELLED
          h[ :status ] = 'CANCELLED'
        when Status::AWARDED
          h[ :status ] = 'AWARDED'
        when Status::ABANDONED
          h[ :status ] = 'ABANDONED'
        when Status::REPLAY
          h[ :status ] = 'REPLAY'
        when Status::POSTPONED
          ## note: add NOTHING for postponed for now
        else
          puts "!! WARN - unknown match status >#{match.status}<:"
          pp match
          h[ :status ] = match.status.downcase  ## print "literal" downcased for now
        end
      end

      matches << h
  end

  data = {
    name:    event.name,
    matches: matches
  }

  data
end

.city_to_timezone(city) ⇒ Object



34
35
36
# File 'lib/sportdb/exporters/json_exporter_worldcup.rb', line 34

def self.city_to_timezone( city )
  CITY_TO_TIMEZONE[ city ] || '?'
end

.export(league_key, out_root:) ⇒ Object



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
# File 'lib/sportdb/exporters/json_exporter.rb', line 18

def self.export( league_key, out_root: )

  puts "find league >#{league_key}<"
  league = Model::League.find_by( key: league_key )

  if league.nil?
    puts "!! WARN: no league found for >#{league_key}<; skipping export json"
    return
  end


  league.events.each do |event|
     puts "** event:"
     pp event.name
     pp event.season
     pp event.league
     puts "teams.count: #{event.teams.count}"
     puts "rounds.count: #{event.rounds.count}"
     puts "groups.count: #{event.groups.count}"
     puts "matches.count: #{event.matches.count}"


     ## build path e.g.
     ##  2014-15/at.1.clubs.json

     ##  -- check for remapping (e.g. add .1); if not found use league key as is
     league_basename = LEAGUE_TO_BASENAME[ event.league.key ] || event.league.key

     season_basename = event.season.name.sub('/', '-')  ## e.g. change 2014/15 to 2014-15


     out_dir   = "#{out_root}/#{season_basename}"
     ## make sure folders exist
     FileUtils.mkdir_p( out_dir ) unless Dir.exist?( out_dir )

     ### note:
     ##  skip teams for now if no teams on "top-level"
     ##  - what to do? use all unique teams from matches? yes, yes!!
     ##  - maybe add stages array to team - why? why not?
     ##  -  or use teams by stage?

     ## if empty - try regular season stage
     ##                or apertura stage?


     unless event.teams.empty?
       data_clubs = build_clubs( event )
       ## pp data_clubs
       File.open( "#{out_dir}/#{league_basename}.clubs.json", 'w:utf-8' ) do |f|
         f.write JSON.pretty_generate( data_clubs )
       end
     end

     # note: make groups export optional for now - why? why not?
     unless event.groups.empty?
       data_groups = build_groups( event )
       ## pp data_groups
       File.open( "#{out_dir}/#{league_basename}.groups.json", 'w' ) do |f|
         f.write JSON.pretty_generate( data_groups )
       end
     end

     data_matches = build_matches( event )
     ## pp data_matches
     File.open( "#{out_dir}/#{league_basename}.json", 'w:utf-8' ) do |f|
       f.write JSON.pretty_generate( data_matches )
     end
  end
end

.export_copa(league_key, out_root:) ⇒ Object



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
# File 'lib/sportdb/exporters/json_exporter_copa.rb', line 6

def self.export_copa( league_key, out_root: )

  league = Model::League.find_by_key!( league_key )

  league.events.each do |event|
     puts "** event:"
     pp event.name
     pp event.season
     pp event.league
     puts "teams.count: #{event.teams.count}"
     puts "rounds.count: #{event.rounds.count}"
     puts "groups.count: #{event.groups.count}"
     puts "grounds.count: #{event.grounds.count}"


     grounds = []
     event.grounds.each do |ground|
        grounds << { key:      ground.key,
                     name:     ground.name,
                     capacity: ground.capacity,
                     city:     ground.city.name,
                  #   timezone: 'UTC+1'   ## use summertime (CEST+2) why? why not? 
                     }
     end

     hash_grounds = {
      name:     event.name,
      stadiums: grounds
     }

     pp hash_grounds


     teams = []
     event.teams.each do |team|
       t = { name:      team.name,
             code:      team.code }
       teams << t
     end

     hash_teams = {
      name: event.name,
      teams: teams
     }

     pp hash_teams


     standings = []
     event.groups.each do |group|
       entries = []
       group_standing = Model::GroupStanding.find_by( group_id: group.id )
       if group_standing
         group_standing.entries.each do |entry|
            entries << { team: { name: entry.team.name, code: entry.team.code },
                         pos:           entry.pos,
                         played:        entry.played,
                         won:           entry.won,
                         drawn:         entry.drawn,
                         lost:          entry.lost,
                         goals_for:     entry.goals_for,
                         goals_against: entry.goals_against,
                         pts:           entry.pts
                       }
          end
        end
        standings << { name: group.name, standings: entries }
     end

     hash_standings = {
      name:   event.name,
      groups: standings
     }

     pp hash_standings


     groups = []
     event.groups.each do |group|
       teams  = []
       group.teams.each do |team|
         teams << { name: team.name,
                    code: team.code
                  }
       end
       groups << { name: group.name, teams: teams }
     end

     hash_groups = {
      name: event.name,
      groups: groups
     }

     pp hash_groups


     rounds = []
     event.rounds.each do |round|
       matches = []
       round.matches.each do |game|
         m = {        num:  game.num || game.pos,    ## use id - why? why not?
                      date: game.date.strftime( '%Y-%m-%d'),
                      time: game.time,  ## note: time is stored as string!!!
                      team1: {
                        name: game.team1.name,
                        code: game.team1.code
                      },
                      team2: {
                        name: game.team2.name,
                        code: game.team2.code
                      },
                }

               score = {}

                if game.score1
                  score[:ft] = [game.score1,
                                game.score2]
                   if game.score1i
                      # half time / first third (opt)
                      # half time - team 2
                      score[:ht] = [game.score1i,
                                    game.score2i]
                  end
                end

                ## note: change do NOT check for knockout flag
                ##         check for scores for now 
                if game.score1et
                    # extratime - team 1 (opt)
                    # extratime - team 2 (opt)
                    score[:et] = [game.score1et,
                                  game.score2et] 
                  if game.score1p
                     # penalty  - team 1 (opt)
                     # penalty  - team 2 (opt) elfmeter (opt)
                     score[:p] = [game.score1p,
                                 game.score2p  
                                ]
                  end
                end  

                m[:score] = score

                #  m[ :knockout ] = game.knockout
                

                unless game.goals.empty?
                  goals1, goals2 = build_goals( game )
                  m[ :goals1 ] = goals1
                  m[ :goals2 ] = goals2
                end

                if game.group
                  m[ :group ]    =  game.group.name
                end

                if game.ground
                  m[ :stadium  ] = { key: game.ground.key, name: game.ground.name }
                  m[ :city     ] = game.ground.city.name
                 # m[ :timezone ] = 'UTC+1'   ## use summertime (CEST+2) why? why not?
                end

          matches << m
       end

       rounds << { name: round.name, matches: matches }
     end

     hash_matches =  {
       name: event.name,
       rounds: rounds
     }

     pp hash_matches


     ## build path e.g.
     ##  2014-15/at.1.clubs.json
     ##  2018/worldcup.teams.json

     ##  -- check for remapping (e.g. add .1); if not found use league key as is
     ## league_basename =  event.league.key
     league_basename = 'copa'    ## use copa (not southamerica) for now - why? why not?

     season_basename = event.season.name.sub('/', '-')  ## e.g. change 2014/15 to 2014-15


     out_dir   = "#{out_root}/#{season_basename}"
     ## make sure folders exist
     FileUtils.mkdir_p( out_dir ) unless Dir.exist?( out_dir )

     File.open( "#{out_dir}/#{league_basename}.stadiums.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_grounds )
     end

     File.open( "#{out_dir}/#{league_basename}.teams.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_teams )
     end

     File.open( "#{out_dir}/#{league_basename}.groups.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_groups )
     end

     File.open( "#{out_dir}/#{league_basename}.standings.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_standings )
     end

     File.open( "#{out_dir}/#{league_basename}.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_matches )
     end
  end
end

.export_euro(league_key, out_root:) ⇒ Object



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
# File 'lib/sportdb/exporters/json_exporter_euro.rb', line 6

def self.export_euro( league_key, out_root: )

  league = Model::League.find_by_key!( league_key )

  league.events.each do |event|
     puts "** event:"
     pp event.name
     pp event.season
     pp event.league
     puts "teams.count: #{event.teams.count}"
     puts "rounds.count: #{event.rounds.count}"
     puts "groups.count: #{event.groups.count}"
     puts "grounds.count: #{event.grounds.count}"


     grounds = []
     event.grounds.each do |ground|
        grounds << { key:      ground.key,
                     name:     ground.name,
                     capacity: ground.capacity,
                     city:     ground.city.name,
                     timezone: 'UTC+1'   ## use summertime (CEST+2) why? why not? 
                     }
     end

     hash_grounds = {
      name:     event.name,
      stadiums: grounds
     }

     pp hash_grounds


     teams = []
     event.teams.each do |team|
       t = { name:      team.name,
             code:      team.code }
       teams << t
     end

     hash_teams = {
      name: event.name,
      teams: teams
     }

     pp hash_teams


     standings = []
     event.groups.each do |group|
       entries = []
       group_standing = Model::GroupStanding.find_by( group_id: group.id )
       if group_standing
         group_standing.entries.each do |entry|
            entries << { team: { name: entry.team.name, code: entry.team.code },
                         pos:           entry.pos,
                         played:        entry.played,
                         won:           entry.won,
                         drawn:         entry.drawn,
                         lost:          entry.lost,
                         goals_for:     entry.goals_for,
                         goals_against: entry.goals_against,
                         pts:           entry.pts
                       }
          end
        end
        standings << { name: group.name, standings: entries }
     end

     hash_standings = {
      name:   event.name,
      groups: standings
     }

     pp hash_standings


     groups = []
     event.groups.each do |group|
       teams  = []
       group.teams.each do |team|
         teams << { name: team.name,
                    code: team.code
                  }
       end
       groups << { name: group.name, teams: teams }
     end

     hash_groups = {
      name: event.name,
      groups: groups
     }

     pp hash_groups


     rounds = []
     event.rounds.each do |round|
       matches = []
       round.matches.each do |game|
         m = {        num:  game.num || game.pos,    ## use id - why? why not?
                      date: game.date.strftime( '%Y-%m-%d'),
                      time: game.time,  ## note: time is stored as string!!!
                      team1: {
                        name: game.team1.name,
                        code: game.team1.code
                      },
                      team2: {
                        name: game.team2.name,
                        code: game.team2.code
                      },
                }

               score = {}

                if game.score1
                  score[:ft] = [game.score1,
                                game.score2]
                   if game.score1i
                      # half time / first third (opt)
                      # half time - team 2
                      score[:ht] = [game.score1i,
                                    game.score2i]
                  end
                end

                ## note: change do NOT check for knockout flag
                ##         check for scores for now 
                if game.score1et
                    # extratime - team 1 (opt)
                    # extratime - team 2 (opt)
                    score[:et] = [game.score1et,
                                  game.score2et] 
                  if game.score1p
                     # penalty  - team 1 (opt)
                     # penalty  - team 2 (opt) elfmeter (opt)
                     score[:p] = [game.score1p,
                                 game.score2p  
                                ]
                  end
                end  

                m[:score] = score

                #  m[ :knockout ] = game.knockout
                

                unless game.goals.empty?
                  goals1, goals2 = build_goals( game )
                  m[ :goals1 ] = goals1
                  m[ :goals2 ] = goals2
                end

                if game.group
                  m[ :group ]    =  game.group.name
                end

                if game.ground
                  m[ :stadium  ] = { key: game.ground.key, name: game.ground.name }
                  m[ :city     ] = game.ground.city.name
                  m[ :timezone ] = 'UTC+1'   ## use summertime (CEST+2) why? why not?
                end

          matches << m
       end

       rounds << { name: round.name, matches: matches }
     end

     hash_matches =  {
       name: event.name,
       rounds: rounds
     }

     pp hash_matches


     ## build path e.g.
     ##  2014-15/at.1.clubs.json
     ##  2018/worldcup.teams.json

     ##  -- check for remapping (e.g. add .1); if not found use league key as is
     league_basename =  event.league.key

     season_basename = event.season.name.sub('/', '-')  ## e.g. change 2014/15 to 2014-15


     out_dir   = "#{out_root}/#{season_basename}"
     ## make sure folders exist
     FileUtils.mkdir_p( out_dir ) unless Dir.exist?( out_dir )

     File.open( "#{out_dir}/#{league_basename}.stadiums.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_grounds )
     end

     File.open( "#{out_dir}/#{league_basename}.teams.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_teams )
     end

     File.open( "#{out_dir}/#{league_basename}.groups.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_groups )
     end

     File.open( "#{out_dir}/#{league_basename}.standings.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_standings )
     end

     File.open( "#{out_dir}/#{league_basename}.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_matches )
     end
  end
end

.export_worldcup(league_key, out_root:) ⇒ Object



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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/sportdb/exporters/json_exporter_worldcup.rb', line 39

def self.export_worldcup( league_key, out_root: )

  league = Model::League.find_by_key!( league_key )

  league.events.each do |event|
     puts "** event:"
     pp event.title
     pp event.season
     pp event.league
     puts "teams.count: #{event.teams.count}"
     puts "rounds.count: #{event.rounds.count}"
     puts "groups.count: #{event.groups.count}"
     puts "grounds.count: #{event.grounds.count}"


     grounds = []
     event.grounds.each do |ground|
        grounds << { key:      ground.key,
                     name:     ground.title,
                     capacity: ground.capacity,
                     city:     ground.city.name,
                     timezone: city_to_timezone( ground.city.name ) }
     end

     hash_grounds = {
      name:     event.title,
      stadiums: grounds
     }

     pp hash_grounds


     teams = []
     event.teams.each do |team|
       if team.country.assoc
         continental = {}
         team.country.assoc.parent_assocs.each do |parent|
           ## next if parent.key == 'fifa'  ## skip fifa
           ##  todo/fix: only include / use (single) continental (parent) org/assoc
           ##  find/use continental parent only for now
           if ['caf', 'afc', 'concacaf', 'uefa', 'conmebol', 'ofc'].include? parent.key
             continental = { name: parent.title,
                             code: parent.key.upcase }
           end
         end
         assoc = { key:          team.country.assoc.key,
                   name:         team.country.assoc.title,
                 }
         assoc[ :continental ] = continental   unless continental.empty?
       else
         assoc = {}
       end
       t = { name:      team.title,
             code:      team.code,
             continent: team.country.continent.name }
       t[ :assoc ] = assoc   unless assoc.empty?
       teams << t
     end

     hash_teams = {
      name: event.title,
      teams: teams
     }

     pp hash_teams


     standings = []
     event.groups.each do |group|
       entries = []
       group_standing = Model::GroupStanding.find_by( group_id: group.id )
       if group_standing
         group_standing.entries.each do |entry|
            entries << { team: { name: entry.team.name, code: entry.team.code },
                         pos:           entry.pos,
                         played:        entry.played,
                         won:           entry.won,
                         drawn:         entry.drawn,
                         lost:          entry.lost,
                         goals_for:     entry.goals_for,
                         goals_against: entry.goals_against,
                         pts:           entry.pts
                       }
          end
        end
        standings << { name: group.title, standings: entries }
     end

     hash_standings = {
      name:   event.title,
      groups: standings
     }

     pp hash_standings


     groups = []
     event.groups.each do |group|
       teams  = []
       group.teams.each do |team|
         teams << { name: team.title,
                    code: team.code
                  }
       end
       groups << { name: group.title, teams: teams }
     end

     hash_groups = {
      name: event.title,
      groups: groups
     }

     pp hash_groups


     rounds = []
     event.rounds.each do |round|
       matches = []
       round.games.each do |game|
         m = {        num:  game.pos,    ## use id - why? why not?
                      date: game.play_at.strftime( '%Y-%m-%d'),
                      time: game.play_at.strftime( '%H:%M'),
                      team1: {
                        name: game.team1.title,
                        code: game.team1.code
                      },
                      team2: {
                        name: game.team2.title,
                        code: game.team2.code
                      },
                      score1:    game.score1,
                      score2:    game.score2,
                      score1i:   game.score1i,   # half time / first third (opt)
                      score2i:   game.score2i,   # half time - team 2
                }

                if game.knockout
                  m[ :score1et ] = game.score1et  # extratime - team 1 (opt)
                  m[ :score2et ] = game.score2et  # extratime - team 2 (opt)
                  m[ :score1p  ] = game.score1p   # penalty  - team 1 (opt)
                  m[ :score2p  ] = game.score2p   # penalty  - team 2 (opt) elfmeter (opt)
                  m[ :knockout ] = game.knockout
                end

                unless game.goals.empty?
                  goals1, goals2 = build_goals( game )
                  m[ :goals1 ] = goals1
                  m[ :goals2 ] = goals2
                end

                if game.group
                  m[ :group ]    =  game.group.title
                end

                if game.ground
                  m[ :stadium  ] = { key: game.ground.key, name: game.ground.title }
                  m[ :city     ] = game.ground.city.name
                  m[ :timezone ] = city_to_timezone( game.ground.city.name )
                end

          matches << m
       end

       rounds << { name: round.title, matches: matches }
     end

     hash_matches =  {
       name: event.title,
       rounds: rounds
     }

     pp hash_matches


     ## build path e.g.
     ##  2014-15/at.1.clubs.json
     ##  2018/worldcup.teams.json

     ##  -- check for remapping (e.g. add .1); if not found use league key as is
     league_basename = LEAGUE_TO_BASENAME[ event.league.key ] || event.league.key

     season_basename = event.season.title.sub('/', '-')  ## e.g. change 2014/15 to 2014-15


     out_dir   = "#{out_root}/#{season_basename}"
     ## make sure folders exist
     FileUtils.mkdir_p( out_dir ) unless Dir.exist?( out_dir )

     File.open( "#{out_dir}/#{league_basename}.stadiums.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_grounds )
     end

     File.open( "#{out_dir}/#{league_basename}.teams.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_teams )
     end

     File.open( "#{out_dir}/#{league_basename}.groups.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_groups )
     end

     File.open( "#{out_dir}/#{league_basename}.standings.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_standings )
     end

     File.open( "#{out_dir}/#{league_basename}.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_matches )
     end
  end
end