sportdb-models 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/HISTORY.md +21 -0
- data/Manifest.txt +142 -0
- data/README.md +28 -0
- data/Rakefile +59 -0
- data/config/fixtures/de.yml +46 -0
- data/config/fixtures/en.yml +54 -0
- data/config/fixtures/es.yml +48 -0
- data/config/fixtures/fr.yml +53 -0
- data/config/fixtures/it.yml +55 -0
- data/config/fixtures/pt.yml +46 -0
- data/config/fixtures/ro.yml +55 -0
- data/data/seasons.txt +74 -0
- data/data/setups/all.txt +5 -0
- data/lib/sportdb/calc.rb +279 -0
- data/lib/sportdb/deleter.rb +52 -0
- data/lib/sportdb/finders/date.rb +374 -0
- data/lib/sportdb/finders/goals.rb +260 -0
- data/lib/sportdb/finders/scores.rb +122 -0
- data/lib/sportdb/lang.rb +216 -0
- data/lib/sportdb/matcher.rb +31 -0
- data/lib/sportdb/models.rb +259 -0
- data/lib/sportdb/models/assoc.rb +106 -0
- data/lib/sportdb/models/assoc_assoc.rb +15 -0
- data/lib/sportdb/models/badge.rb +14 -0
- data/lib/sportdb/models/event.rb +65 -0
- data/lib/sportdb/models/event_ground.rb +15 -0
- data/lib/sportdb/models/event_team.rb +16 -0
- data/lib/sportdb/models/forward.rb +55 -0
- data/lib/sportdb/models/game.rb +244 -0
- data/lib/sportdb/models/goal.rb +15 -0
- data/lib/sportdb/models/ground.rb +100 -0
- data/lib/sportdb/models/group.rb +23 -0
- data/lib/sportdb/models/group_team.rb +14 -0
- data/lib/sportdb/models/league.rb +83 -0
- data/lib/sportdb/models/person.rb +21 -0
- data/lib/sportdb/models/roster.rb +18 -0
- data/lib/sportdb/models/round.rb +22 -0
- data/lib/sportdb/models/season.rb +14 -0
- data/lib/sportdb/models/stats/alltime_standing.rb +44 -0
- data/lib/sportdb/models/stats/alltime_standing_entry.rb +23 -0
- data/lib/sportdb/models/stats/event_standing.rb +55 -0
- data/lib/sportdb/models/stats/event_standing_entry.rb +21 -0
- data/lib/sportdb/models/stats/group_standing.rb +50 -0
- data/lib/sportdb/models/stats/group_standing_entry.rb +22 -0
- data/lib/sportdb/models/team.rb +119 -0
- data/lib/sportdb/models/team_comp.rb +64 -0
- data/lib/sportdb/models/utils.rb +78 -0
- data/lib/sportdb/models/world/city.rb +21 -0
- data/lib/sportdb/models/world/continent.rb +20 -0
- data/lib/sportdb/models/world/country.rb +19 -0
- data/lib/sportdb/models/world/region.rb +19 -0
- data/lib/sportdb/patterns.rb +38 -0
- data/lib/sportdb/reader.rb +130 -0
- data/lib/sportdb/reader_file.rb +123 -0
- data/lib/sportdb/reader_zip.rb +165 -0
- data/lib/sportdb/readers/assoc.rb +54 -0
- data/lib/sportdb/readers/event.rb +200 -0
- data/lib/sportdb/readers/game.rb +877 -0
- data/lib/sportdb/readers/ground.rb +53 -0
- data/lib/sportdb/readers/league.rb +54 -0
- data/lib/sportdb/readers/season.rb +83 -0
- data/lib/sportdb/readers/squad_club.rb +201 -0
- data/lib/sportdb/readers/squad_national_team.rb +173 -0
- data/lib/sportdb/readers/team.rb +53 -0
- data/lib/sportdb/schema.rb +373 -0
- data/lib/sportdb/standings.rb +178 -0
- data/lib/sportdb/stats.rb +27 -0
- data/lib/sportdb/utils.rb +89 -0
- data/lib/sportdb/utils_date.rb +26 -0
- data/lib/sportdb/utils_goals.rb +20 -0
- data/lib/sportdb/utils_group.rb +63 -0
- data/lib/sportdb/utils_map.rb +44 -0
- data/lib/sportdb/utils_round.rb +165 -0
- data/lib/sportdb/utils_scores.rb +17 -0
- data/lib/sportdb/utils_teams.rb +43 -0
- data/lib/sportdb/version.rb +23 -0
- data/test/data/at-austria/2013_14/bl.txt +227 -0
- data/test/data/at-austria/2013_14/bl.yml +30 -0
- data/test/data/at-austria/2013_14/bl_ii.txt +154 -0
- data/test/data/at-austria/2013_14/el.txt +4 -0
- data/test/data/at-austria/2013_14/el.yml +25 -0
- data/test/data/at-austria/2013_14/squads/austria.txt +40 -0
- data/test/data/at-austria/2013_14/squads/salzburg.txt +35 -0
- data/test/data/at-austria/leagues.txt +11 -0
- data/test/data/at-austria/teams.txt +75 -0
- data/test/data/at-austria/teams_2.txt +34 -0
- data/test/data/national-teams/assocs.txt +231 -0
- data/test/data/national-teams/europe/assocs.txt +13 -0
- data/test/data/national-teams/europe/teams.txt +13 -0
- data/test/data/national-teams/north-america/assocs.txt +10 -0
- data/test/data/national-teams/north-america/teams.txt +7 -0
- data/test/data/national-teams/teams.txt +19 -0
- data/test/data/players/europe/at-austria/players.txt +45 -0
- data/test/data/players/europe/de-deutschland/players.txt +41 -0
- data/test/data/players/south-america/br-brazil/players.txt +51 -0
- data/test/data/world-cup/1930/cup.txt +71 -0
- data/test/data/world-cup/1930/cup.yml +23 -0
- data/test/data/world-cup/1930/cup_goals.txt +47 -0
- data/test/data/world-cup/1930/cup_goals.yml +23 -0
- data/test/data/world-cup/1954/cup.txt +90 -0
- data/test/data/world-cup/1954/cup.yml +30 -0
- data/test/data/world-cup/1962/cup.txt +86 -0
- data/test/data/world-cup/1962/cup.yml +32 -0
- data/test/data/world-cup/1974/cup.yml +35 -0
- data/test/data/world-cup/1974/cup_finals.txt +14 -0
- data/test/data/world-cup/1974/cup_i.txt +55 -0
- data/test/data/world-cup/1974/cup_ii.txt +34 -0
- data/test/data/world-cup/2014/cup.txt +5 -0
- data/test/data/world-cup/2014/cup.yml +54 -0
- data/test/data/world-cup/2014/squads/br-brazil.txt +46 -0
- data/test/data/world-cup/2014/squads/de-deutschland.txt +8 -0
- data/test/data/world-cup/2014/squads/jp-japan.txt +30 -0
- data/test/data/world-cup/2014/squads/uy-uruguay.txt +32 -0
- data/test/data/world-cup/leagues.txt +5 -0
- data/test/data/world-cup/seasons_1930.txt +4 -0
- data/test/data/world-cup/seasons_1954.txt +4 -0
- data/test/data/world-cup/seasons_1962.txt +4 -0
- data/test/data/world-cup/seasons_1974.txt +5 -0
- data/test/data/world-cup/teams_1930.txt +26 -0
- data/test/data/world-cup/teams_1954.txt +30 -0
- data/test/data/world-cup/teams_1962.txt +29 -0
- data/test/data/world-cup/teams_1974.txt +29 -0
- data/test/helper.rb +120 -0
- data/test/test_assoc_reader.rb +201 -0
- data/test/test_changes.rb +74 -0
- data/test/test_cursor.rb +50 -0
- data/test/test_date.rb +100 -0
- data/test/test_goals.rb +109 -0
- data/test/test_lang.rb +130 -0
- data/test/test_load.rb +61 -0
- data/test/test_reader.rb +88 -0
- data/test/test_reader_from_string.rb +65 -0
- data/test/test_round_auto.rb +370 -0
- data/test/test_round_def.rb +109 -0
- data/test/test_round_header.rb +183 -0
- data/test/test_scores.rb +70 -0
- data/test/test_squad_club_reader.rb +76 -0
- data/test/test_squad_national_team_reader.rb +116 -0
- data/test/test_standings.rb +279 -0
- data/test/test_standings_ii.rb +46 -0
- data/test/test_utils.rb +124 -0
- data/test/test_winner.rb +95 -0
- metadata +378 -0
@@ -0,0 +1,122 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SportDb
|
4
|
+
|
5
|
+
class ScoresFinder
|
6
|
+
|
7
|
+
include LogUtils::Logging
|
8
|
+
|
9
|
+
# e.g. 1:2 or 0:2 or 3:3 or
|
10
|
+
# 1-1 or 0-2 or 3-3
|
11
|
+
FT_REGEX = /\b
|
12
|
+
(?<score1>\d{1,2})
|
13
|
+
[:\-]
|
14
|
+
(?<score2>\d{1,2})
|
15
|
+
\b/x
|
16
|
+
|
17
|
+
|
18
|
+
# e.g. 1:2nV => after extra time a.e.t
|
19
|
+
|
20
|
+
# note: possible ending w/ . -> thus cannot use /b will not work w/ .; use zero look-ahead
|
21
|
+
ET_REGEX = /\b
|
22
|
+
(?<score1>\d{1,2})
|
23
|
+
[:\-]
|
24
|
+
(?<score2>\d{1,2})
|
25
|
+
\s? # allow optional space
|
26
|
+
(?:nV|n\.V\.|aet|a\.e\.t\.) # allow optional . e.g. nV or n.V.
|
27
|
+
(?=[\s\)\]]|$)/xi
|
28
|
+
|
29
|
+
## todo: add/allow english markers e.g. pen or p ??
|
30
|
+
|
31
|
+
# e.g. 5:4iE => penalty / after penalty a.p
|
32
|
+
|
33
|
+
|
34
|
+
# note: possible ending w/ . -> thus cannot use /b will not work w/ .; use zero look-ahead
|
35
|
+
P_REGEX = /\b
|
36
|
+
(?<score1>\d{1,2})
|
37
|
+
[:\-]
|
38
|
+
(?<score2>\d{1,2})
|
39
|
+
\s? # allow optional space
|
40
|
+
(?:iE|i\.E\.|p|pen|PSO) # allow optional . e.g. iE or i.E.
|
41
|
+
(?=[\s\)\]]|$)/xi
|
42
|
+
|
43
|
+
|
44
|
+
## todo: allow all-in-one "literal form a la kicker" e.g.
|
45
|
+
# 2:2 (1:1, 1:0) n.V. 5:1 i.E.
|
46
|
+
|
47
|
+
def initialize
|
48
|
+
# nothing here for now
|
49
|
+
end
|
50
|
+
|
51
|
+
def find!( line, opts={} )
|
52
|
+
|
53
|
+
### fix: add and match all-in-one literal first, followed by
|
54
|
+
|
55
|
+
# note: always call after find_dates !!!
|
56
|
+
# scores match date-like patterns!! e.g. 10-11 or 10:00 etc.
|
57
|
+
# -- note: score might have two digits too
|
58
|
+
|
59
|
+
### fix: depending on language allow 1:1 or 1-1
|
60
|
+
## do NOT allow mix and match
|
61
|
+
## e.g. default to en is 1-1
|
62
|
+
## de is 1:1 etc.
|
63
|
+
|
64
|
+
|
65
|
+
# extract score from line
|
66
|
+
# and return it
|
67
|
+
# NB: side effect - removes date from line string
|
68
|
+
|
69
|
+
score1 = nil
|
70
|
+
score2 = nil
|
71
|
+
|
72
|
+
score1et = nil
|
73
|
+
score2et = nil
|
74
|
+
|
75
|
+
score1p = nil
|
76
|
+
score2p = nil
|
77
|
+
|
78
|
+
if (md = ET_REGEX.match( line ))
|
79
|
+
score1et = md[:score1].to_i
|
80
|
+
score2et = md[:score2].to_i
|
81
|
+
|
82
|
+
logger.debug " score.et: >#{score1et}-#{score2et}<"
|
83
|
+
|
84
|
+
line.sub!( md[0], '[SCORE.ET]' )
|
85
|
+
end
|
86
|
+
|
87
|
+
if (md = P_REGEX.match( line ))
|
88
|
+
score1p = md[:score1].to_i
|
89
|
+
score2p = md[:score2].to_i
|
90
|
+
|
91
|
+
logger.debug " score.p: >#{score1p}-#{score2p}<"
|
92
|
+
|
93
|
+
line.sub!( md[0], '[SCORE.P]' )
|
94
|
+
end
|
95
|
+
|
96
|
+
## let full time (ft) standard regex go last - has no marker
|
97
|
+
|
98
|
+
if (md = FT_REGEX.match( line ))
|
99
|
+
score1 = md[:score1].to_i
|
100
|
+
score2 = md[:score2].to_i
|
101
|
+
|
102
|
+
logger.debug " score: >#{score1}-#{score2}<"
|
103
|
+
|
104
|
+
line.sub!( md[0], '[SCORE]' )
|
105
|
+
end
|
106
|
+
|
107
|
+
## todo: how to handle game w/o extra time
|
108
|
+
# but w/ optional penalty ??? e.g. used in copa liberatores, for example
|
109
|
+
# retrun 0,0 or nil,nil for extra time score ?? or -1, -1 ??
|
110
|
+
# for now use nil,nil
|
111
|
+
|
112
|
+
scores = []
|
113
|
+
scores += [score1, score2] if score1p || score2p || score1et || score2et || score1 || score2
|
114
|
+
scores += [score1et, score2et] if score1p || score2p || score1et || score2et
|
115
|
+
scores += [score1p, score2p] if score1p || score2p
|
116
|
+
|
117
|
+
scores
|
118
|
+
end
|
119
|
+
|
120
|
+
end # class ScoresFinder
|
121
|
+
|
122
|
+
end # module SportDb
|
data/lib/sportdb/lang.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SportDb
|
4
|
+
|
5
|
+
class Lang
|
6
|
+
|
7
|
+
include LogUtils::Logging
|
8
|
+
|
9
|
+
attr_reader :lang
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
# fix/todo: load on demand; only if no fixtures loaded/configured use builtin
|
13
|
+
load_builtin_words
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def load_builtin_words
|
18
|
+
builtin_words = {
|
19
|
+
'en' => 'fixtures/en',
|
20
|
+
'de' => 'fixtures/de',
|
21
|
+
'es' => 'fixtures/es',
|
22
|
+
'fr' => 'fixtures/fr',
|
23
|
+
'it' => 'fixtures/it',
|
24
|
+
'pt' => 'fixtures/pt',
|
25
|
+
'ro' => 'fixtures/ro'
|
26
|
+
}
|
27
|
+
|
28
|
+
load_words( builtin_words, SportDb.config_path )
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def load_words( h, include_path )
|
33
|
+
@lang = 'en' # make default lang english/en
|
34
|
+
@words = {} # resets fixtures
|
35
|
+
@cache = {} # reset cached values
|
36
|
+
|
37
|
+
h.each_with_index do |(key,value),i|
|
38
|
+
name = value
|
39
|
+
path = "#{include_path}/#{name}.yml"
|
40
|
+
logger.debug( "loading words #{key} (#{i+1}/#{h.size}) in '#{name}' (#{path})..." )
|
41
|
+
@words[ key ] = YAML.load( File.read_utf8( path ))
|
42
|
+
end
|
43
|
+
|
44
|
+
@classifier = TextUtils::Classifier.new
|
45
|
+
@words.each_with_index do |(key,value),i|
|
46
|
+
logger.debug "train classifier for #{key} (#{i+1}/#{@words.size})"
|
47
|
+
@classifier.train( key, value )
|
48
|
+
end
|
49
|
+
|
50
|
+
@classifier.dump # for debugging dump all words
|
51
|
+
end
|
52
|
+
|
53
|
+
def classify( text )
|
54
|
+
@classifier.classify( text )
|
55
|
+
end
|
56
|
+
|
57
|
+
def classify_file( path )
|
58
|
+
@classifier.classify_file( path )
|
59
|
+
end
|
60
|
+
|
61
|
+
def lang=(value)
|
62
|
+
logger.debug "setting lang to #{value}"
|
63
|
+
|
64
|
+
if @lang != value
|
65
|
+
|
66
|
+
### todo: make reset cached values into method/function for reuse (see load_words)
|
67
|
+
# reset cached values on language change
|
68
|
+
logger.debug "reseting cached lang values (lang changed from #{@lang} to #{value})"
|
69
|
+
|
70
|
+
@cache = {}
|
71
|
+
end
|
72
|
+
|
73
|
+
@lang = value
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def group
|
79
|
+
@cache[ :group ] ||= group_getter
|
80
|
+
end
|
81
|
+
|
82
|
+
def round
|
83
|
+
@cache[ :round ] ||= round_getter
|
84
|
+
end
|
85
|
+
|
86
|
+
def knockout_round
|
87
|
+
@cache[ :knockout_round ] ||= knockout_round_getter
|
88
|
+
end
|
89
|
+
|
90
|
+
def leg1
|
91
|
+
@cache[ :leg1 ] ||= leg1_getter
|
92
|
+
end
|
93
|
+
|
94
|
+
def leg2
|
95
|
+
@cache[ :leg2 ] ||= leg2_getter
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
def regex_group
|
101
|
+
@cache [ :regex_group ] ||= regex_group_getter
|
102
|
+
end
|
103
|
+
|
104
|
+
def regex_round
|
105
|
+
@cache[ :regex_round ] ||= regex_round_getter
|
106
|
+
end
|
107
|
+
|
108
|
+
def regex_knockout_round
|
109
|
+
@cache[ :regex_knockout_round ] ||= regex_knockout_round_getter
|
110
|
+
end
|
111
|
+
|
112
|
+
def regex_leg1
|
113
|
+
@cache[ :regex_leg1 ] ||= regex_leg1_getter
|
114
|
+
end
|
115
|
+
|
116
|
+
def regex_leg2
|
117
|
+
@cache[ :regex_leg2 ] ||= regex_leg2_getter
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
def group_getter
|
122
|
+
h = @words[ lang ]
|
123
|
+
values = "" # NB: always construct a new string (do NOT use a reference to hash value)
|
124
|
+
values << h['group']
|
125
|
+
values
|
126
|
+
end
|
127
|
+
|
128
|
+
def round_getter
|
129
|
+
# e.g. Spieltag|Runde|Achtelfinale|Viertelfinale|Halbfinale|Finale
|
130
|
+
|
131
|
+
## fix/todo:
|
132
|
+
## sort by length first - to allow best match e.g.
|
133
|
+
## 3rd place play-off instead of Play-off ?? etc. - why? why not?
|
134
|
+
|
135
|
+
h = @words[ lang ]
|
136
|
+
values = "" # NB: always construct a new string (do NOT use a reference to hash value)
|
137
|
+
values << h['round']
|
138
|
+
|
139
|
+
### add knockout rounds values too
|
140
|
+
values << "|" << h['round32']
|
141
|
+
values << "|" << h['round16']
|
142
|
+
values << "|" << h['quarterfinals']
|
143
|
+
values << "|" << h['semifinals']
|
144
|
+
values << "|" << h['fifthplace'] if h['fifthplace'] # nb: allow empty/is optional!!
|
145
|
+
values << "|" << h['thirdplace']
|
146
|
+
values << "|" << h['final']
|
147
|
+
values << "|" << h['playoffs'] if h['playoffs'] # nb: allow empty/is optional!!
|
148
|
+
values
|
149
|
+
end
|
150
|
+
|
151
|
+
def leg1_getter
|
152
|
+
h = @words[ lang ]
|
153
|
+
values = "" # NB: always construct a new string (do NOT use a reference to hash value)
|
154
|
+
values << h['leg1']
|
155
|
+
values
|
156
|
+
end
|
157
|
+
|
158
|
+
def leg2_getter
|
159
|
+
h = @words[ lang ]
|
160
|
+
values = "" # NB: always construct a new string (do NOT use a reference to hash value)
|
161
|
+
values << h['leg2']
|
162
|
+
values
|
163
|
+
end
|
164
|
+
|
165
|
+
def knockout_round_getter
|
166
|
+
h = @words[ lang ]
|
167
|
+
values = "" # NB: always construct a new string (do NOT use a reference to hash value)
|
168
|
+
values << h['round32']
|
169
|
+
values << "|" << h['round16']
|
170
|
+
values << "|" << h['quarterfinals']
|
171
|
+
values << "|" << h['semifinals']
|
172
|
+
values << "|" << h['fifthplace'] if h['fifthplace'] # nb: allow empty/is optional!!
|
173
|
+
values << "|" << h['thirdplace']
|
174
|
+
values << "|" << h['final']
|
175
|
+
values << "|" << h['playoffs'] if h['playoffs'] # nb: allow empty/is optional!!
|
176
|
+
values
|
177
|
+
end
|
178
|
+
|
179
|
+
def regex_group_getter
|
180
|
+
## todo: escape for regex?
|
181
|
+
## NB: let's ignore case (that is, UPCASE,downcase); always use /i flag
|
182
|
+
/#{group}/i
|
183
|
+
end
|
184
|
+
|
185
|
+
def regex_round_getter
|
186
|
+
## todo: escape for regex?
|
187
|
+
## todo: sort by length - biggest words go first? does regex match biggest word automatically?? - check
|
188
|
+
## todo/fix: make - optional e.g. convert to ( |-) or better [ \-] ??
|
189
|
+
## NB: let's ignore case (that is, UPCASE,downcase); always use /i flag
|
190
|
+
/#{round}/i
|
191
|
+
end
|
192
|
+
|
193
|
+
def regex_knockout_round_getter
|
194
|
+
## todo: escape for regex?
|
195
|
+
## todo: sort by length - biggest words go first? does regex match biggest word automatically?? - check
|
196
|
+
## todo/fix: make - optional e.g. convert to ( |-) or better [ \-] ??
|
197
|
+
## NB: let's ignore case (that is, UPCASE,downcase); always use /i flag
|
198
|
+
/#{knockout_round}/i
|
199
|
+
end
|
200
|
+
|
201
|
+
def regex_leg1_getter
|
202
|
+
## todo: escape for regex?
|
203
|
+
## NB: let's ignore case (that is, UPCASE,downcase); always use /i flag
|
204
|
+
/#{leg1}/i
|
205
|
+
end
|
206
|
+
|
207
|
+
def regex_leg2_getter
|
208
|
+
## todo: escape for regex?
|
209
|
+
## NB: let's ignore case (that is, UPCASE,downcase); always use /i flag
|
210
|
+
/#{leg2}/i
|
211
|
+
end
|
212
|
+
|
213
|
+
end # class Lang
|
214
|
+
|
215
|
+
|
216
|
+
end # module SportDb
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SportDb
|
4
|
+
|
5
|
+
module Matcher
|
6
|
+
|
7
|
+
include WorldDb::Matcher
|
8
|
+
|
9
|
+
def match_leagues_for_country( name, &blk )
|
10
|
+
match_xxx_for_country( name, 'leagues', &blk )
|
11
|
+
end
|
12
|
+
|
13
|
+
def match_teams_for_country( name, &blk )
|
14
|
+
match_xxx_for_country( name, 'teams', &blk )
|
15
|
+
end
|
16
|
+
|
17
|
+
def match_clubs_for_country( name, &blk )
|
18
|
+
match_xxx_for_country( name, 'clubs', &blk )
|
19
|
+
end
|
20
|
+
|
21
|
+
def match_players_for_country( name, &blk )
|
22
|
+
match_xxx_for_country( name, 'players', &blk )
|
23
|
+
end
|
24
|
+
|
25
|
+
def match_stadiums_for_country( name, &blk )
|
26
|
+
match_xxx_for_country( name, 'stadiums', &blk )
|
27
|
+
end
|
28
|
+
|
29
|
+
end # module Matcher
|
30
|
+
|
31
|
+
end # module SportDb
|
@@ -0,0 +1,259 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
|
4
|
+
# core and stlibs (note: get included via worlddb-models gem; see worlddb-models gem/lib)
|
5
|
+
|
6
|
+
|
7
|
+
require 'worlddb/models' # NOTE: include worlddb-models gem (not cli tools gem, that is, worlddb)
|
8
|
+
require 'persondb'
|
9
|
+
|
10
|
+
|
11
|
+
## todo: check if already included in worlddb ???
|
12
|
+
## require 'fetcher' # for fetching/downloading fixtures via HTTP/HTTPS etc.
|
13
|
+
|
14
|
+
|
15
|
+
# our own code
|
16
|
+
|
17
|
+
require 'sportdb/version' # let version always go first
|
18
|
+
|
19
|
+
require 'sportdb/patterns'
|
20
|
+
require 'sportdb/models/forward'
|
21
|
+
require 'sportdb/models/world/city'
|
22
|
+
require 'sportdb/models/world/country'
|
23
|
+
require 'sportdb/models/world/continent'
|
24
|
+
require 'sportdb/models/world/region'
|
25
|
+
require 'sportdb/models/assoc'
|
26
|
+
require 'sportdb/models/assoc_assoc'
|
27
|
+
require 'sportdb/models/badge'
|
28
|
+
require 'sportdb/models/event'
|
29
|
+
require 'sportdb/models/event_ground'
|
30
|
+
require 'sportdb/models/event_team'
|
31
|
+
require 'sportdb/models/game'
|
32
|
+
require 'sportdb/models/goal'
|
33
|
+
require 'sportdb/models/ground'
|
34
|
+
require 'sportdb/models/group'
|
35
|
+
require 'sportdb/models/group_team'
|
36
|
+
require 'sportdb/models/league'
|
37
|
+
require 'sportdb/models/person'
|
38
|
+
require 'sportdb/models/roster'
|
39
|
+
require 'sportdb/models/round'
|
40
|
+
require 'sportdb/models/season'
|
41
|
+
require 'sportdb/models/team'
|
42
|
+
require 'sportdb/models/team_comp'
|
43
|
+
|
44
|
+
|
45
|
+
require 'sportdb/models/stats/alltime_standing'
|
46
|
+
require 'sportdb/models/stats/alltime_standing_entry'
|
47
|
+
require 'sportdb/models/stats/event_standing'
|
48
|
+
require 'sportdb/models/stats/event_standing_entry'
|
49
|
+
require 'sportdb/models/stats/group_standing'
|
50
|
+
require 'sportdb/models/stats/group_standing_entry'
|
51
|
+
|
52
|
+
require 'sportdb/models/utils' # e.g. GameCursor
|
53
|
+
|
54
|
+
|
55
|
+
require 'sportdb/schema' # NB: requires sportdb/models (include SportDB::Models)
|
56
|
+
|
57
|
+
require 'sportdb/finders/date'
|
58
|
+
require 'sportdb/finders/scores'
|
59
|
+
|
60
|
+
require 'sportdb/utils'
|
61
|
+
require 'sportdb/utils_date'
|
62
|
+
require 'sportdb/utils_group'
|
63
|
+
require 'sportdb/utils_map'
|
64
|
+
require 'sportdb/utils_round'
|
65
|
+
require 'sportdb/utils_scores'
|
66
|
+
require 'sportdb/utils_teams'
|
67
|
+
require 'sportdb/utils_goals'
|
68
|
+
require 'sportdb/matcher'
|
69
|
+
require 'sportdb/calc' # fix/todo: obsolete - replace w/ standings
|
70
|
+
require 'sportdb/standings'
|
71
|
+
|
72
|
+
|
73
|
+
require 'sportdb/finders/goals' # no: requires FixturesHelpers
|
74
|
+
|
75
|
+
require 'sportdb/readers/assoc'
|
76
|
+
require 'sportdb/readers/event'
|
77
|
+
require 'sportdb/readers/game'
|
78
|
+
require 'sportdb/readers/ground'
|
79
|
+
require 'sportdb/readers/league'
|
80
|
+
require 'sportdb/readers/season'
|
81
|
+
require 'sportdb/readers/squad_club' # roster
|
82
|
+
require 'sportdb/readers/squad_national_team'
|
83
|
+
require 'sportdb/readers/team'
|
84
|
+
require 'sportdb/reader'
|
85
|
+
require 'sportdb/reader_file'
|
86
|
+
require 'sportdb/reader_zip'
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
require 'sportdb/lang'
|
91
|
+
|
92
|
+
require 'sportdb/deleter'
|
93
|
+
require 'sportdb/stats'
|
94
|
+
|
95
|
+
|
96
|
+
module SportDb
|
97
|
+
|
98
|
+
def self.config_path
|
99
|
+
"#{root}/config"
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.data_path
|
103
|
+
"#{root}/data"
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.test_data_path
|
107
|
+
"#{root}/test/data"
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
def self.lang
|
112
|
+
# todo/fix: find a better way for single instance ??
|
113
|
+
# will get us ruby warning: instance variable @lang not initialized => find a better way!!!
|
114
|
+
# just use @lang w/o .nil? e.g.
|
115
|
+
# @lang =|| Lang.new why? why not?? or better use @@lang =|| Lang.new for class variable!!!
|
116
|
+
if @lang.nil?
|
117
|
+
@lang = Lang.new
|
118
|
+
end
|
119
|
+
@lang
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
def self.create
|
124
|
+
CreateDb.new.up
|
125
|
+
ConfDb::Model::Prop.create!( key: 'db.schema.sport.version', value: VERSION )
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.create_all
|
129
|
+
## build schema - convenience helper
|
130
|
+
LogDb.create
|
131
|
+
ConfDb.create
|
132
|
+
TagDb.create
|
133
|
+
WorldDb.create
|
134
|
+
PersonDb.create
|
135
|
+
SportDb.create
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
def self.read_setup( setup, include_path )
|
140
|
+
reader = Reader.new( include_path )
|
141
|
+
reader.load_setup( setup )
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.read_setup_from_zip( zip_name, setup, include_path, opts={} ) ## todo/check - use a better (shorter) name ??
|
145
|
+
reader = ZipReader.new( zip_name, include_path, opts )
|
146
|
+
reader.load_setup( setup )
|
147
|
+
reader.close
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.read_all( include_path ) # convenience helper
|
151
|
+
read_setup( 'setups/all', include_path )
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.read_builtin
|
155
|
+
read_setup( 'setups/all', data_path )
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
# load built-in (that is, bundled within the gem) named plain text seeds
|
160
|
+
# - pass in an array of pairs of event/seed names e.g.
|
161
|
+
# [['at.2012/13', 'at/2012_13/bl'],
|
162
|
+
# ['cl.2012/13', 'cl/2012_13/cl']] etc.
|
163
|
+
|
164
|
+
def self.read( ary, include_path )
|
165
|
+
reader = Reader.new( include_path )
|
166
|
+
## todo: check kind_of?( Array ) or kind_of?(String) to support array or string
|
167
|
+
ary.each do |name|
|
168
|
+
reader.load( name )
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
# delete ALL records (use with care!)
|
174
|
+
def self.delete!
|
175
|
+
puts '*** deleting sport table records/data...'
|
176
|
+
Deleter.new.run
|
177
|
+
end # method delete!
|
178
|
+
|
179
|
+
def self.update!
|
180
|
+
puts '*** update event fixtures...'
|
181
|
+
Updater.new.run
|
182
|
+
end
|
183
|
+
|
184
|
+
|
185
|
+
def self.stats
|
186
|
+
Stats.new.tables # NOTE: same as tables (remove??? why? why not??)
|
187
|
+
end
|
188
|
+
|
189
|
+
def self.tables
|
190
|
+
Stats.new.tables
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
def self.connect( db_config={} )
|
195
|
+
|
196
|
+
if db_config.empty?
|
197
|
+
puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"
|
198
|
+
|
199
|
+
### change default to ./sport.db ?? why? why not?
|
200
|
+
db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///sport.db' )
|
201
|
+
|
202
|
+
if db.scheme == 'postgres'
|
203
|
+
config = {
|
204
|
+
adapter: 'postgresql',
|
205
|
+
host: db.host,
|
206
|
+
port: db.port,
|
207
|
+
username: db.user,
|
208
|
+
password: db.password,
|
209
|
+
database: db.path[1..-1],
|
210
|
+
encoding: 'utf8'
|
211
|
+
}
|
212
|
+
else # assume sqlite3
|
213
|
+
config = {
|
214
|
+
adapter: db.scheme, # sqlite3
|
215
|
+
database: db.path[1..-1] # sport.db (NB: cut off leading /, thus 1..-1)
|
216
|
+
}
|
217
|
+
end
|
218
|
+
else
|
219
|
+
config = db_config # use passed in config hash
|
220
|
+
end
|
221
|
+
|
222
|
+
## todo/check: use if defined?( JRUBY_VERSION ) instead ??
|
223
|
+
if RUBY_PLATFORM =~ /java/ && config[:adapter] == 'sqlite3'
|
224
|
+
# quick hack for JRuby sqlite3 support via jdbc
|
225
|
+
puts "jruby quick hack - adding jdbc libs for jruby sqlite3 database support"
|
226
|
+
require 'jdbc/sqlite3'
|
227
|
+
require 'active_record/connection_adapters/jdbc_adapter'
|
228
|
+
require 'active_record/connection_adapters/jdbcsqlite3_adapter'
|
229
|
+
end
|
230
|
+
|
231
|
+
puts "Connecting to db using settings: "
|
232
|
+
pp config
|
233
|
+
ActiveRecord::Base.establish_connection( config )
|
234
|
+
# ActiveRecord::Base.logger = Logger.new( STDOUT )
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
def self.setup_in_memory_db
|
239
|
+
|
240
|
+
# Database Setup & Config
|
241
|
+
ActiveRecord::Base.logger = Logger.new( STDOUT )
|
242
|
+
## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
|
243
|
+
|
244
|
+
self.connect( adapter: 'sqlite3',
|
245
|
+
database: ':memory:' )
|
246
|
+
|
247
|
+
## build schema
|
248
|
+
SportDb.create_all
|
249
|
+
|
250
|
+
## read builtins - why? why not?
|
251
|
+
SportDb.read_builtin
|
252
|
+
end # setup_in_memory_db (using SQLite :memory:)
|
253
|
+
|
254
|
+
|
255
|
+
end # module SportDb
|
256
|
+
|
257
|
+
|
258
|
+
## say hello
|
259
|
+
puts SportDb.banner if $DEBUG || (defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG)
|