sportdb 1.9.16 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.md +2 -19
  3. data/Manifest.txt +1 -137
  4. data/README.md +2 -21
  5. data/Rakefile +14 -25
  6. data/lib/sportdb.rb +9 -260
  7. data/lib/sportdb/cli/main.rb +5 -14
  8. data/lib/sportdb/cli/opts.rb +2 -0
  9. data/lib/sportdb/cli/version.rb +26 -0
  10. metadata +10 -278
  11. data/.gemtest +0 -0
  12. data/config/fixtures/de.yml +0 -46
  13. data/config/fixtures/en.yml +0 -54
  14. data/config/fixtures/es.yml +0 -48
  15. data/config/fixtures/fr.yml +0 -53
  16. data/config/fixtures/it.yml +0 -55
  17. data/config/fixtures/pt.yml +0 -46
  18. data/config/fixtures/ro.yml +0 -55
  19. data/data/seasons.txt +0 -74
  20. data/data/setups/all.txt +0 -5
  21. data/lib/sportdb/calc.rb +0 -279
  22. data/lib/sportdb/deleter.rb +0 -52
  23. data/lib/sportdb/finders/date.rb +0 -374
  24. data/lib/sportdb/finders/goals.rb +0 -260
  25. data/lib/sportdb/finders/scores.rb +0 -122
  26. data/lib/sportdb/lang.rb +0 -216
  27. data/lib/sportdb/matcher.rb +0 -31
  28. data/lib/sportdb/models/assoc.rb +0 -106
  29. data/lib/sportdb/models/assoc_assoc.rb +0 -15
  30. data/lib/sportdb/models/badge.rb +0 -14
  31. data/lib/sportdb/models/event.rb +0 -65
  32. data/lib/sportdb/models/event_ground.rb +0 -15
  33. data/lib/sportdb/models/event_team.rb +0 -16
  34. data/lib/sportdb/models/forward.rb +0 -55
  35. data/lib/sportdb/models/game.rb +0 -244
  36. data/lib/sportdb/models/goal.rb +0 -15
  37. data/lib/sportdb/models/ground.rb +0 -100
  38. data/lib/sportdb/models/group.rb +0 -23
  39. data/lib/sportdb/models/group_team.rb +0 -14
  40. data/lib/sportdb/models/league.rb +0 -83
  41. data/lib/sportdb/models/person.rb +0 -21
  42. data/lib/sportdb/models/roster.rb +0 -18
  43. data/lib/sportdb/models/round.rb +0 -22
  44. data/lib/sportdb/models/season.rb +0 -14
  45. data/lib/sportdb/models/stats/alltime_standing.rb +0 -44
  46. data/lib/sportdb/models/stats/alltime_standing_entry.rb +0 -23
  47. data/lib/sportdb/models/stats/event_standing.rb +0 -55
  48. data/lib/sportdb/models/stats/event_standing_entry.rb +0 -21
  49. data/lib/sportdb/models/stats/group_standing.rb +0 -50
  50. data/lib/sportdb/models/stats/group_standing_entry.rb +0 -22
  51. data/lib/sportdb/models/team.rb +0 -119
  52. data/lib/sportdb/models/team_comp.rb +0 -64
  53. data/lib/sportdb/models/utils.rb +0 -78
  54. data/lib/sportdb/models/world/city.rb +0 -21
  55. data/lib/sportdb/models/world/continent.rb +0 -20
  56. data/lib/sportdb/models/world/country.rb +0 -19
  57. data/lib/sportdb/models/world/region.rb +0 -19
  58. data/lib/sportdb/patterns.rb +0 -38
  59. data/lib/sportdb/reader.rb +0 -130
  60. data/lib/sportdb/reader_file.rb +0 -123
  61. data/lib/sportdb/reader_zip.rb +0 -165
  62. data/lib/sportdb/readers/assoc.rb +0 -54
  63. data/lib/sportdb/readers/event.rb +0 -200
  64. data/lib/sportdb/readers/game.rb +0 -877
  65. data/lib/sportdb/readers/ground.rb +0 -53
  66. data/lib/sportdb/readers/league.rb +0 -54
  67. data/lib/sportdb/readers/season.rb +0 -83
  68. data/lib/sportdb/readers/squad_club.rb +0 -201
  69. data/lib/sportdb/readers/squad_national_team.rb +0 -173
  70. data/lib/sportdb/readers/team.rb +0 -53
  71. data/lib/sportdb/schema.rb +0 -373
  72. data/lib/sportdb/standings.rb +0 -178
  73. data/lib/sportdb/stats.rb +0 -27
  74. data/lib/sportdb/utils.rb +0 -89
  75. data/lib/sportdb/utils_date.rb +0 -26
  76. data/lib/sportdb/utils_goals.rb +0 -20
  77. data/lib/sportdb/utils_group.rb +0 -63
  78. data/lib/sportdb/utils_map.rb +0 -44
  79. data/lib/sportdb/utils_round.rb +0 -165
  80. data/lib/sportdb/utils_scores.rb +0 -17
  81. data/lib/sportdb/utils_teams.rb +0 -43
  82. data/lib/sportdb/version.rb +0 -22
  83. data/test/data/at-austria/2013_14/bl.txt +0 -227
  84. data/test/data/at-austria/2013_14/bl.yml +0 -30
  85. data/test/data/at-austria/2013_14/bl_ii.txt +0 -154
  86. data/test/data/at-austria/2013_14/el.txt +0 -4
  87. data/test/data/at-austria/2013_14/el.yml +0 -25
  88. data/test/data/at-austria/2013_14/squads/austria.txt +0 -40
  89. data/test/data/at-austria/2013_14/squads/salzburg.txt +0 -35
  90. data/test/data/at-austria/leagues.txt +0 -11
  91. data/test/data/at-austria/teams.txt +0 -75
  92. data/test/data/at-austria/teams_2.txt +0 -34
  93. data/test/data/national-teams/assocs.txt +0 -231
  94. data/test/data/national-teams/europe/assocs.txt +0 -13
  95. data/test/data/national-teams/europe/teams.txt +0 -13
  96. data/test/data/national-teams/north-america/assocs.txt +0 -10
  97. data/test/data/national-teams/north-america/teams.txt +0 -7
  98. data/test/data/national-teams/teams.txt +0 -19
  99. data/test/data/players/europe/at-austria/players.txt +0 -45
  100. data/test/data/players/europe/de-deutschland/players.txt +0 -41
  101. data/test/data/players/south-america/br-brazil/players.txt +0 -51
  102. data/test/data/world-cup/1930/cup.txt +0 -71
  103. data/test/data/world-cup/1930/cup.yml +0 -23
  104. data/test/data/world-cup/1930/cup_goals.txt +0 -47
  105. data/test/data/world-cup/1930/cup_goals.yml +0 -23
  106. data/test/data/world-cup/1954/cup.txt +0 -90
  107. data/test/data/world-cup/1954/cup.yml +0 -30
  108. data/test/data/world-cup/1962/cup.txt +0 -86
  109. data/test/data/world-cup/1962/cup.yml +0 -32
  110. data/test/data/world-cup/1974/cup.yml +0 -35
  111. data/test/data/world-cup/1974/cup_finals.txt +0 -14
  112. data/test/data/world-cup/1974/cup_i.txt +0 -55
  113. data/test/data/world-cup/1974/cup_ii.txt +0 -34
  114. data/test/data/world-cup/2014/cup.txt +0 -5
  115. data/test/data/world-cup/2014/cup.yml +0 -54
  116. data/test/data/world-cup/2014/squads/br-brazil.txt +0 -46
  117. data/test/data/world-cup/2014/squads/de-deutschland.txt +0 -8
  118. data/test/data/world-cup/2014/squads/jp-japan.txt +0 -30
  119. data/test/data/world-cup/2014/squads/uy-uruguay.txt +0 -32
  120. data/test/data/world-cup/leagues.txt +0 -5
  121. data/test/data/world-cup/seasons_1930.txt +0 -4
  122. data/test/data/world-cup/seasons_1954.txt +0 -4
  123. data/test/data/world-cup/seasons_1962.txt +0 -4
  124. data/test/data/world-cup/seasons_1974.txt +0 -5
  125. data/test/data/world-cup/teams_1930.txt +0 -26
  126. data/test/data/world-cup/teams_1954.txt +0 -30
  127. data/test/data/world-cup/teams_1962.txt +0 -29
  128. data/test/data/world-cup/teams_1974.txt +0 -29
  129. data/test/helper.rb +0 -120
  130. data/test/test_assoc_reader.rb +0 -201
  131. data/test/test_changes.rb +0 -74
  132. data/test/test_cursor.rb +0 -50
  133. data/test/test_date.rb +0 -100
  134. data/test/test_goals.rb +0 -109
  135. data/test/test_lang.rb +0 -130
  136. data/test/test_load.rb +0 -61
  137. data/test/test_reader.rb +0 -88
  138. data/test/test_reader_from_string.rb +0 -65
  139. data/test/test_round_auto.rb +0 -370
  140. data/test/test_round_def.rb +0 -109
  141. data/test/test_round_header.rb +0 -183
  142. data/test/test_scores.rb +0 -70
  143. data/test/test_squad_club_reader.rb +0 -76
  144. data/test/test_squad_national_team_reader.rb +0 -116
  145. data/test/test_standings.rb +0 -279
  146. data/test/test_standings_ii.rb +0 -46
  147. data/test/test_utils.rb +0 -124
  148. data/test/test_winner.rb +0 -95
@@ -1,53 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module SportDb
4
-
5
-
6
- class TeamReader
7
-
8
- include LogUtils::Logging
9
-
10
- ## make models available by default with namespace
11
- # e.g. lets you use Usage instead of Model::Usage
12
- include Models
13
-
14
- def self.from_zip( zip_file, entry_path, more_attribs={} )
15
- ## get text content from zip
16
- entry = zip_file.find_entry( entry_path )
17
-
18
- text = entry.get_input_stream().read()
19
- text = text.force_encoding( Encoding::UTF_8 )
20
-
21
- self.from_string( text, more_attribs )
22
- end
23
-
24
- def self.from_file( path, more_attribs={} )
25
- ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
26
- ## - see textutils/utils.rb
27
- text = File.read_utf8( path )
28
- self.from_string( text, more_attribs )
29
- end
30
-
31
- def self.from_string( text, more_attribs={} )
32
- TeamReader.new( text, more_attribs )
33
- end
34
-
35
- def initialize( text, more_attribs={} )
36
- ## todo/fix: how to add opts={} ???
37
- @text = text
38
- @more_attribs = more_attribs
39
- end
40
-
41
-
42
- def read()
43
- reader = ValuesReader.from_string( @text, @more_attribs )
44
-
45
- reader.each_line do |new_attributes, values|
46
- Team.create_or_update_from_values( new_attributes, values )
47
- end # each lines
48
- end
49
-
50
-
51
-
52
- end # class TeamReader
53
- end # module SportDb
@@ -1,373 +0,0 @@
1
-
2
- module SportDb
3
-
4
- class CreateDb
5
-
6
- def up
7
- ActiveRecord::Schema.define do
8
-
9
- create_table :teams do |t|
10
- t.string :key, null: false # import/export key
11
- t.string :title, null: false
12
- t.string :title2
13
- t.string :code # make it not null? - three letter code (short title)
14
- t.string :synonyms # comma separated list of synonyms
15
- t.references :country, null: false
16
- t.references :city # NB: city is optional (should be required for clubs e.g. non-national teams)
17
- t.boolean :club, null: false, default: false # is it a club (not a national team)?
18
-
19
- t.integer :since # founding year
20
- t.string :address
21
- t.string :web
22
-
23
- t.references :assoc # optional: national football assoc(iation), for example - used for national teams
24
-
25
- ### fix: remove and add virtual attribute in model instead
26
- t.boolean :national, null: false, default: false # is it a national selection team (not a club)?
27
- t.timestamps
28
- end
29
-
30
- add_index :teams, :key, unique: true
31
-
32
- ###########
33
- # check: use table (rename to) venues / stadiums - why? why not?
34
- create_table :grounds do |t|
35
- t.string :key, null: false # import/export key
36
- t.string :title, null: false
37
- t.string :synonyms # comma separated list of synonyms
38
-
39
- t.references :country, null: false
40
- t.references :city # todo: make city required ???
41
-
42
- t.integer :since # founding year
43
- t.integer :capacity # attentence capacity e.g. 10_000 or 50_000 etc.
44
- t.string :address
45
-
46
-
47
- ### fix/todo: add since/founded/opened/build attrib eg. 2011 or 1987
48
- ## - add capacity e.g. 40_000
49
- ## fix: add address !!!! etc
50
-
51
- ## add region ??? or just use region from city ??
52
-
53
- t.timestamps
54
- end
55
-
56
- add_index :grounds, :key, unique: true
57
-
58
-
59
- # join table: person+game(team1+team2+event(season+league))
60
- create_table :goals do |t|
61
- t.references :person, null: false
62
- t.references :game, null: false
63
- t.references :team, null: false ## use integer instead w/ values 1 or 2 for team1 or team2 ?? why? why not?
64
-
65
- t.integer :minute
66
- t.integer :offset, null: false, default: 0 # e.g. 45' +3 or 90' +2
67
-
68
- t.integer :score1
69
- t.integer :score2
70
-
71
- ## type of goal (penalty, owngoal)
72
- t.boolean :penalty, null: false, default: false
73
- t.boolean :owngoal, null: false, default: false # de: Eigentor -> # todo: find better name?
74
-
75
- t.timestamps
76
- end
77
-
78
-
79
- ################
80
- # fix/todo: rename to squads / lineups
81
-
82
- # join table -> person+team+event(season+league)
83
- create_table :rosters do |t| # use squads as an alternative name? why? why not??
84
- t.references :person, null: false
85
- t.references :team, null: false
86
- t.references :event # make required?
87
- t.integer :pos, null: false
88
-
89
- t.timestamps
90
- end
91
-
92
-
93
- create_table :events do |t|
94
- t.string :key, null: false # import/export key
95
- t.references :league, null: false
96
- t.references :season, null: false
97
- t.date :start_at, null: false # NB: only use date (w/o time)
98
- t.date :end_at # make it required??? # NB: only use date (w/o time)
99
- t.boolean :team3, null: false, default: true ## e.g. Champions League has no 3rd place (only 1st and 2nd/final)
100
-
101
- #### track 1-n sources (from repos) - # todo move to its own table later
102
- ## NB: relative to event.yml - use mapper to "resolve" to full path w/ repo; use league+season keys
103
- t.string :sources # e.g. cup or bl,bl_ii # NB: for now store all in on string separated by comma
104
- t.string :config # e.g. cup or bl # e.g assumes cup.yml, bl.yml etc. for now
105
-
106
-
107
- t.timestamps
108
- end
109
-
110
- add_index :events, :key, unique: true
111
-
112
-
113
- create_table :rounds do |t|
114
- t.references :event, null: false
115
- t.string :title, null: false
116
- t.string :title2
117
- t.integer :pos, null: false
118
- ## add new table stage/stages for grouping rounds in group rounds and playoff rounds, for example???
119
- ## # "regular" season (group) games or post-season (playoff) knockouts (k.o's)
120
- t.boolean :knockout, null: false, default: false
121
- t.date :start_at, null: false # NB: only use date (w/o time)
122
- t.date :end_at # todo: make it required e.g. :null => false # NB: only use date (w/o time)
123
-
124
- ## auto-added flag (e.g. start_at n end_at dates got calculated)
125
- ## if auto-added flag is false - do NOT auto-update start_at, end_at etc.
126
- t.boolean :auto, null: false, default: true
127
-
128
- t.timestamps
129
- end
130
-
131
- add_index :rounds, :event_id # fk event_id index
132
-
133
-
134
- create_table :groups do |t| # Teamgruppe (zB Gruppe A, Gruppe B, etc.)
135
- t.references :event, null: false
136
- t.string :title, null: false
137
- t.integer :pos, null: false
138
- t.timestamps
139
- end
140
-
141
- add_index :groups, :event_id # fk event_id index
142
-
143
- ###########################
144
- # fix: rename table to matches
145
- create_table :games do |t|
146
- t.string :key # import/export key
147
- t.references :round, null: false
148
- t.integer :pos, null: false
149
- t.references :group # note: group is optional
150
- t.references :team1, null: false
151
- t.references :team2, null: false
152
-
153
- t.datetime :play_at, null: false
154
- t.boolean :postponed, null: false, default: false
155
- t.datetime :play_at_v2 # optional old date (when postponed)
156
- t.datetime :play_at_v3 # optional odl date (when postponed twice)
157
-
158
- t.references :ground # optional - stadium (lets you get city,region,country,etc)
159
- t.references :city # optional - convenience for ground.city_id ???
160
-
161
-
162
- t.boolean :knockout, null: false, default: false
163
- t.boolean :home, null: false, default: true # is team1 play at home (that is, at its home stadium)
164
- t.integer :score1
165
- t.integer :score2
166
- t.integer :score1et # extratime - team 1 (opt)
167
- t.integer :score2et # extratime - team 2 (opt)
168
- t.integer :score1p # penalty - team 1 (opt)
169
- t.integer :score2p # penalty - team 2 (opt) elfmeter (opt)
170
- t.integer :score1i # half time / first third (opt)
171
- t.integer :score2i # half time - team 2
172
- t.integer :score1ii # second third (opt)
173
- t.integer :score2ii # second third - team2 (opt)
174
- t.references :next_game # for hinspiel bei rueckspiel in knockout game
175
- t.references :prev_game
176
-
177
- t.integer :winner # 1,2,0,nil calculate on save - "real" winner (after 90 or extra time or penalty, aggregated first+second leg?)
178
- t.integer :winner90 # 1,2,0,nil calculate on save - winner after 90 mins (or regugular play time depending on sport - add alias or find a better name!)
179
-
180
- t.timestamps
181
- end
182
-
183
- add_index :games, :key, unique: true
184
- add_index :games, :round_id # fk round_id index
185
- add_index :games, :group_id # fk group_id index
186
- add_index :games, :next_game_id # fk next_game_id index
187
- add_index :games, :prev_game_id # fk next_game_id index
188
- add_index :games, :team1_id
189
- add_index :games, :team2_id
190
-
191
-
192
- # todo: remove id from join table (without extra fields)? why?? why not??
193
- create_table :events_teams do |t|
194
- t.references :event, null: false
195
- t.references :team, null: false
196
- t.timestamps
197
- end
198
-
199
- add_index :events_teams, [:event_id,:team_id], unique: true
200
- add_index :events_teams, :event_id
201
-
202
-
203
- # todo: remove id from join table (without extra fields)? why?? why not??
204
- create_table :events_grounds do |t|
205
- t.references :event, null: false
206
- t.references :ground, null: false
207
- t.timestamps
208
- end
209
-
210
- add_index :events_grounds, [:event_id,:ground_id], unique: true
211
- add_index :events_grounds, :event_id
212
-
213
-
214
-
215
- create_table :groups_teams do |t|
216
- t.references :group, null: false
217
- t.references :team, null: false
218
- t.timestamps
219
- end
220
-
221
- add_index :groups_teams, [:group_id,:team_id], unique: true
222
- add_index :groups_teams, :group_id
223
-
224
-
225
- ### todo: add models and some seed data
226
-
227
- create_table :seasons do |t| ## also used for years
228
- t.string :key, null: false
229
- t.string :title, null: false # e.g. 2011/12, 2012/13 ### what to do w/ 2012? for world cup etc?
230
- t.timestamps
231
- end
232
-
233
- create_table :leagues do |t| ## also for cups/conferences/tournaments/world series/etc.
234
- t.string :key, null: false
235
- t.string :title, null: false # e.g. Premier League, Deutsche Bundesliga, World Cup, Champions League, etc.
236
- t.references :country ## optional for now , :null => false ### todo: create "virtual" country for international leagues e.g. use int? or world (ww?)/europe (eu)/etc. similar? already taken??
237
-
238
- ## fix: rename to :clubs from :club
239
- t.boolean :club, null: false, default: false # club teams or national teams?
240
- ## todo: add t.boolean :national flag? for national teams?
241
- ## t.boolean :international, :null => false, :default => false # national league or international?
242
- ## t.boolean :cup ## or regular season league??
243
- t.timestamps
244
- end
245
-
246
- create_table :badges do |t|
247
- t.references :team, null: false
248
- ## todo/fix: use event insead of league+season ??
249
- ## t.references :event, :null => false # event => league+season
250
- t.references :league, null: false
251
- t.references :season, null: false
252
- t.string :title, null: false # Meister, Weltmeister, Europameister, Cupsieger, Vize-Meister, Aufsteiger, Absteiger, etc.
253
- t.timestamps
254
- end
255
-
256
-
257
- create_table :assocs do |t|
258
- t.string :key, null: false
259
- t.string :title, null: false # e.g. Premier League, Deutsche Bundesliga, World Cup, Champions League, etc.
260
-
261
- t.integer :since # founding year
262
- t.string :web
263
-
264
- ### if national assoc - has (optional) country ref
265
- t.references :country # note: optional - only used/set (required) for national assocs (or subnational too?)
266
- t.boolean :national, null: false, default: false
267
-
268
- ## add :world flag for FIFA? - just check if parent is null? for root assoc(s)? why? why not?
269
- ## add :regional flag for continental subdivision?
270
- t.boolean :continental, null: false, default: false
271
- t.boolean :intercontinental, null: false, default: false # e.g. arab football league (africa+western asia/middle east)
272
- t.timestamps
273
- end
274
-
275
- add_index :assocs, :key, unique: true
276
-
277
-
278
-
279
- create_table :assocs_assocs do |t|
280
- t.references :assoc1, null: false # parent assoc
281
- t.references :assoc2, null: false # child assoc is_member_of parent assoc
282
- t.timestamps
283
- end
284
-
285
- add_index :assocs_assocs, [:assoc1_id,:assoc2_id], unique: true
286
- add_index :assocs_assocs, :assoc1_id
287
- add_index :assocs_assocs, :assoc2_id
288
-
289
-
290
-
291
- ############################################
292
- # stats tables
293
-
294
- # use tables for standings e.g group_tables? - why? why not?
295
- #
296
- # todo: add group_standings per round with pos diffs e.g +1,+2, -3 etc.
297
-
298
- create_table :group_standings do |t|
299
- t.references :group, null: false
300
- t.timestamps
301
- end
302
-
303
- ### use items instead of entries - why (shorter! simple plural e.g. just add s)
304
-
305
- create_table :group_standing_entries do |t|
306
- t.references :group_standing, null: false
307
- t.references :team, null: false
308
- t.integer :pos
309
- t.integer :played
310
- t.integer :won
311
- t.integer :lost
312
- t.integer :drawn
313
- t.integer :goals_for # todo: find a short name - gf? why? why not?
314
- t.integer :goals_against # todo: find a shorter name - ga? why? why not?
315
- t.integer :pts
316
- t.string :comments
317
- t.timestamps
318
- end
319
-
320
-
321
- create_table :event_standings do |t|
322
- t.references :event, null: false
323
- t.timestamps
324
- end
325
-
326
- create_table :event_standing_entries do |t|
327
- t.references :event_standing, null: false
328
- t.references :team, null: false
329
- t.integer :pos
330
- t.integer :played
331
- t.integer :won
332
- t.integer :lost
333
- t.integer :drawn
334
- t.integer :goals_for # todo: find a short name - gf? or for? why? why not?
335
- t.integer :goals_against # todo: find a shorter name - ga? or against? why? why not?
336
- t.integer :pts
337
- t.string :comments
338
- t.timestamps
339
- end
340
-
341
-
342
- ## flex (free-style/form) standings table - lets you add as many events as you like (not bound to single event/season/etc.)
343
- ## -use (find a better) a different name? why? why not?
344
- create_table :alltime_standings do |t|
345
- t.string :key, null: false
346
- t.string :title, null: false
347
- t.timestamps
348
- end
349
-
350
- create_table :alltime_standing_entries do |t|
351
- t.references :alltime_standing, null: false
352
- t.references :team, null: false
353
- t.integer :pos
354
- t.integer :played # todo: use a different name - why? why not?
355
- t.integer :won
356
- t.integer :lost
357
- t.integer :drawn
358
- t.integer :goals_for # todo: find a short name - gf? why? why not?
359
- t.integer :goals_against # todo: find a shorter name - ga? why? why not?
360
- t.integer :pts
361
- t.integer :recs # note: specific to alltime - stats records counter (e.g. appearance counter)
362
- t.string :comments
363
- t.timestamps
364
- end
365
-
366
-
367
- end # Schema.define
368
- end # method up
369
-
370
-
371
- end # class CreateDb
372
-
373
- end # module SportDb
@@ -1,178 +0,0 @@
1
- # encoding: utf-8
2
-
3
-
4
- ##############
5
- ##
6
- ## fix/todo:
7
- ## - reuse in footballcsv/build e.g. require sportdb and remove "old" code!!!!
8
-
9
- module SportDb
10
-
11
-
12
- class StandingsLine
13
- attr_accessor :rank, :team_name,
14
- :played, :won, :lost, :drawn, ## -- total
15
- :goals_for, :goals_against, :pts,
16
- :home_played, :home_won, :home_lost, :home_drawn, ## -- home
17
- :home_goals_for, :home_goals_against, :home_pts,
18
- :away_played, :away_won, :away_lost, :away_drawn, ## -- away
19
- :away_goals_for, :away_goals_against, :away_pts
20
-
21
- def initialize( team_name )
22
- @rank = nil # use 0? why? why not?
23
- @team_name = team_name
24
- @played = @home_played = @away_played = 0
25
- @won = @home_won = @away_won = 0
26
- @lost = @home_lost = @away_lost = 0
27
- @drawn = @home_drawn = @away_drawn = 0
28
- @goals_for = @home_goals_for = @away_goals_for = 0
29
- @goals_against = @home_goals_against = @away_goals_against = 0
30
- @pts = @home_pts = @away_pts = 0
31
-
32
- ### fix: add @recs too - to count number of records (e.g. appearances/seasons etc.)
33
- end
34
- end # class StandingsLine
35
-
36
-
37
- class Standings
38
-
39
- def initialize( opts={} )
40
- ## fix:
41
- # passing in e.g. pts for win (3? 2? etc.)
42
- # default to 3 for now
43
-
44
- ## lets you pass in 2 as an alterantive, for example
45
- @pts_won = opts[:pts_won] || 3
46
-
47
- @lines = {} # StandingsLines cached by team name/key
48
- end
49
-
50
-
51
- def update( match_or_matches )
52
- puts " [debug] update match_or_matches.class.name: #{match_or_matches.class.name}"
53
-
54
- ## convenience - update all matches at once
55
- if match_or_matches.is_a?( Array ) ||
56
- match_or_matches.is_a?( ActiveRecord::Associations::CollectionProxy )
57
- matches = match_or_matches
58
- matches.each_with_index do |match,i| # note: index(i) starts w/ zero (0)
59
- update_match( match )
60
- end
61
- else
62
- match = match_or_matches
63
- update_match( match )
64
- end
65
- self # note: return self to allow chaining
66
- end
67
-
68
- def to_a
69
- ## return lines; sort and add rank
70
- ## note: will update rank!!!! (side effect)
71
-
72
- #############################
73
- ### calc ranking position (rank)
74
- ## fix/allow same rank e.g. all 1 or more than one team 3rd etc.
75
-
76
- # build array from hash
77
- ary = []
78
- @lines.each do |k,v|
79
- ary << v
80
- end
81
-
82
- ary.sort! do |l,r|
83
- ## note: reverse order (thus, change l,r to r,l)
84
- value = r.pts <=> l.pts
85
- if value == 0 # same pts try goal diff
86
- value = (r.goals_for-r.goals_against) <=> (l.goals_for-l.goals_against)
87
- if value == 0 # same goal diff too; try assume more goals better for now
88
- value = r.goals_for <=> l.goals_for
89
- end
90
- end
91
- value
92
- end
93
-
94
- ## update rank using ordered array
95
- ary.each_with_index do |line,i|
96
- line.rank = i+1 ## add ranking (e.g. 1,2,3 etc.) - note: i starts w/ zero (0)
97
- end
98
-
99
- ary
100
- end # to_a
101
-
102
-
103
- private
104
- def update_match( m ) ## add a match
105
-
106
- ## puts " #{m.team1} - #{m.team2} #{m.score_str}"
107
- unless m.over?
108
- puts " !!!! skipping match - not yet over (play_at date in the future)"
109
- return
110
- end
111
-
112
- unless m.complete?
113
- puts "!!! [calc_standings] skipping match #{m.team1_name} - #{m.team2_name} - scores incomplete #{m.score_str}"
114
- return
115
- end
116
-
117
- ### fix/todo: use team1_name n team2_name ???
118
- ### fix/todo: - add extra time and penalty shootout !!!!
119
-
120
- line1 = @lines[ m.team1_name ] || StandingsLine.new( m.team1_name )
121
- line2 = @lines[ m.team2_name ] || StandingsLine.new( m.team2_name )
122
-
123
- line1.played += 1
124
- line1.home_played += 1
125
-
126
- line2.played += 1
127
- line2.away_played += 1
128
-
129
- if m.winner == 1
130
- line1.won += 1
131
- line1.home_won += 1
132
-
133
- line2.lost += 1
134
- line2.away_lost += 1
135
-
136
- line1.pts += @pts_won
137
- line1.home_pts += @pts_won
138
- elsif m.winner == 2
139
- line1.lost += 1
140
- line1.home_lost += 1
141
-
142
- line2.won += 1
143
- line2.away_won += 1
144
-
145
- line2.pts += @pts_won
146
- line2.away_pts += @pts_won
147
- else ## assume drawn/tie (that is, 0)
148
- line1.drawn += 1
149
- line1.home_drawn += 1
150
-
151
- line2.drawn += 1
152
- line2.away_drawn += 1
153
-
154
- line1.pts += 1
155
- line1.home_pts += 1
156
- line2.pts += 1
157
- line2.away_pts += 1
158
- end
159
-
160
- line1.goals_for += m.score1
161
- line1.home_goals_for += m.score1
162
- line1.goals_against += m.score2
163
- line1.home_goals_against += m.score2
164
-
165
- line2.goals_for += m.score2
166
- line2.away_goals_for += m.score2
167
- line2.goals_against += m.score1
168
- line2.away_goals_against += m.score1
169
-
170
- @lines[ m.team1_name ] = line1
171
- @lines[ m.team2_name ] = line2
172
- end # method update_match
173
-
174
- end # class Standings
175
-
176
-
177
-
178
- end # module SportDb