Skip to content

Commit

Permalink
up auto conf
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Nov 26, 2019
1 parent fb8f361 commit 3591b54
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 11 deletions.
12 changes: 11 additions & 1 deletion sportdb-clubs/NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Notes

## Fix

How to check if country is Import::Country or Model::Country?
Model::Country now gets NOT checked with is_a?
and lib should NOT depend on db models - what to do?
check for respond_to? key or check if is a string or symbol?
otherwise assume country record!!!!



## Todos

Add more short cut options to the format:
Expand Down Expand Up @@ -42,7 +52,7 @@ b) FC Barcelona B
FK Austria Wien, Wien (Favoriten)
| Austria Wien
| Austria Vienna [en]
(2) Young Violets Austria Wien
| Young Violets A.W.
-or-
Expand Down
8 changes: 8 additions & 0 deletions sportdb-readers/lib/sportdb/readers/conf_parser_auto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ module SportDb

class AutoConfParser

def self.parse( lines, start: )
## todo/fix: add support for txt and lines
## check if lines_or_txt is an array or just a string
parser = new( lines, start )
parser.parse
end


include LogUtils::Logging

def initialize( lines, start )
Expand Down
10 changes: 10 additions & 0 deletions sportdb-readers/lib/sportdb/readers/match_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
module SportDb

class MatchParserSimpleV2 ## simple match parser for club match schedules

def self.parse( lines, teams, start: )
## todo/fix: add support for txt and lines
## check if lines_or_txt is an array or just a string
## use teams: like start: why? why not?
parser = new( lines, teams, start )
parser.parse
end


include LogUtils::Logging

def initialize( lines, teams, start )
Expand Down
61 changes: 57 additions & 4 deletions sportdb-readers/lib/sportdb/readers/match_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module SportDb

class MatchReaderV2 ## todo/check: rename to MatchReaderV2 (use plural?) why? why not?

def self.config() Import.config; end



def self.read( path, season: nil ) ## use - rename to read_file or from_file etc. - why? why not?
txt = File.open( path, 'r:utf-8' ).read
parse( txt, season: season )
Expand All @@ -14,12 +18,61 @@ def self.parse( txt, season: nil )
pp recs

recs.each do |rec|
league = Sync::League.find!( rec[:league] )
season = Sync::Season.find!( rec[:season] )
league = Sync::League.find_or_create( rec[:league] )
season = Sync::Season.find_or_create( rec[:season] )

stage = nil
auto_conf_clubs = nil
if rec[:stage]
event = Sync::Event.find_or_create( league: league, season: season )
stage = Sync::Stage.find( rec[:stage], event: event )
if stage.nil?
## fix: just use Stage.create
stage = Sync::Stage.find_or_create( rec[:stage], event: event )

auto_conf_clubs = AutoConfParser.parse( rec[:lines],
start: event.start_at )
end
else
event = Sync::Event.find( league: league, season: season )
if event.nil?
## fix: just use Event.create
event = Sync::Event.find_or_create( league: league, season: season )

auto_conf_clubs = AutoConfParser.parse( rec[:lines],
start: event.start_at )
end
end


if auto_conf_clubs
## step 1: map/find clubs
club_recs = []
## note: loop over keys (holding the names); values hold the usage counter!! e.g. 'Arsenal' => 2, etc.
country = config.countries[ league.country.key ] ## hack/fix: convert to Import::Country from Model::Country
## todo: check is_a? Country check to respond_to? :key in match in sportdb-clubs !!!!!
## todo/fix: just pass in league.country (and NOT key)
auto_conf_clubs.keys.each do |name|
club_rec = config.clubs.find_by!( name: name, country: country )
club_recs << club_rec
end

## step 2: add to database
club_recs.each do |club_rec|
club = Sync::Club.find_or_create( club_rec )
## add teams to event
## todo/fix: check if team is alreay included?
## or clear/destroy_all first!!!
if stage
stage.teams << club
else
event.teams << club
end
end
end


event = Sync::Event.find!( league: league, season: season )
if rec[:stage]
stage = Sync::Stage.find!( rec[:stage], event: event )
teams = stage.teams
else
teams = event.teams
Expand Down
4 changes: 2 additions & 2 deletions sportdb-readers/lib/sportdb/readers/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module SportDb
module Readers

MAJOR = 0 ## todo: namespace inside version or something - why? why not??
MINOR = 3
PATCH = 7
MINOR = 4
PATCH = 0
VERSION = [MAJOR,MINOR,PATCH].join('.')

def self.version
Expand Down
51 changes: 49 additions & 2 deletions sportdb-readers/test/test_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,55 @@

class TestReader < MiniTest::Test

def test_read
def test_read_i
SportDb.connect( adapter: 'sqlite3', database: ':memory:' )
SportDb.create_all ## build schema

## turn on logging to console
ActiveRecord::Base.logger = Logger.new(STDOUT)

txt =<<TXT
= English Premier League 2017/18
Matchday 1
[Fri Aug/11]
Arsenal FC 4-3 Leicester City
[Sat Aug/12]
Watford FC 3-3 Liverpool FC
Chelsea FC 2-3 Burnley FC
Crystal Palace 0-3 Huddersfield Town
Everton FC 1-0 Stoke City
Southampton FC 0-0 Swansea City
West Bromwich Albion 1-0 AFC Bournemouth
Brighton & Hove Albion 0-2 Manchester City
[Sun Aug/13]
Newcastle United 0-2 Tottenham Hotspur
Manchester United 4-0 West Ham United
Matchday 2
[Sat Aug/19]
Swansea City 0-4 Manchester United
AFC Bournemouth 0-2 Watford FC
Burnley FC 0-1 West Bromwich Albion
Leicester City 2-0 Brighton & Hove Albion
Liverpool FC 1-0 Crystal Palace
Southampton FC 3-2 West Ham United
Stoke City 1-0 Arsenal FC
[Sun Aug/20]
Huddersfield Town 1-0 Newcastle United
Tottenham Hotspur 1-2 Chelsea FC
[Mon Aug/21]
Manchester City 1-1 Everton FC
TXT

SportDb::MatchReaderV2.parse( txt )
end # method test_read_i


def xxx_test_read_ii
SportDb.connect( adapter: 'sqlite3', database: ':memory:' )
SportDb.create_all ## build schema

Expand All @@ -33,5 +80,5 @@ def test_read
recs = SportDb::MatchReaderV2.read( path )
# path = "../../../openfootball/england/2017-18/1-premierleague-ii.txt"
#recs = SportDb::MatchReaderV2.read( path )
end # method test_read
end # method test_read_ii
end # class TestReader
4 changes: 2 additions & 2 deletions sportdb/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Hoe.spec 'sportdb' do

self.extra_deps = [
['sportdb-models', '>= 1.18.2'],
['sportdb-readers', '>= 0.3.6'],
['sportdb-readers', '>= 0.3.7'],
['footballdb-leagues', '>= 2019.11.22' ], ## add minimum for datasets
['footballdb-clubs', '>= 2019.11.22' ],

Expand All @@ -34,7 +34,7 @@ Hoe.spec 'sportdb' do
['sportdb-service'],

## 3rd party
['gli', '>= 2.12.2'],
['gli', '>= 2.19.0'],

## ['activerecord'], # Note: will include activesupport,etc.
['sqlite3']
Expand Down

0 comments on commit 3591b54

Please sign in to comment.