sportdb 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ # encoding: utf-8
2
+
3
+ # note: timezone for games (play_at) is *always* CET (central european time)
4
+
5
+ ###########################################
6
+ ## WM 2014 - Qualification Europe Group C
7
+
8
+ world = Event.find_by_key!( 'wmq' )
9
+
10
+ world1 = Round.find_by_event_id_and_pos!( world.id, 1 ) # 7. Sep 2012
11
+ world2 = Round.find_by_event_id_and_pos!( world.id, 2 ) # 11. """"""
12
+ world3 = Round.find_by_event_id_and_pos!( world.id, 3 ) # 12. Okt 2012
13
+ world4 = Round.find_by_event_id_and_pos!( world.id, 4 ) # 16. """""""
14
+ world5 = Round.find_by_event_id_and_pos!( world.id, 5 ) # 22. Mär 2013
15
+ world6 = Round.find_by_event_id_and_pos!( world.id, 6 ) # 26. """"""
16
+ world7 = Round.find_by_event_id_and_pos!( world.id, 7 ) # 7. Jun 2013
17
+ world8 = Round.find_by_event_id_and_pos!( world.id, 8 ) # 11. """""""
18
+ world9 = Round.find_by_event_id_and_pos!( world.id, 9 ) # 6. Sep 2013
19
+ world10 = Round.find_by_event_id_and_pos!( world.id, 10 ) # 10. """""""
20
+ world11 = Round.find_by_event_id_and_pos!( world.id, 11 ) # 11. Okt 2013
21
+ world12 = Round.find_by_event_id_and_pos!( world.id, 12 ) # 15. """"""""
22
+
23
+
24
+
25
+
26
+ #################################3
27
+ # Gruppe C
28
+
29
+ # note: for teams see seeds/euro/teams.rb
30
+
31
+ ger = Team.find_by_key!( 'ger' )
32
+ fro = Team.find_by_key!( 'fro' )
33
+ irl = Team.find_by_key!( 'irl' )
34
+ kaz = Team.find_by_key!( 'kaz' )
35
+ aut = Team.find_by_key!( 'aut' )
36
+ swe = Team.find_by_key!( 'swe' )
37
+
38
+
39
+ worldc = Group.create!( event: world, pos: 3, title: 'Gruppe C' )
40
+
41
+ worldc.teams << ger
42
+ worldc.teams << fro
43
+ worldc.teams << irl
44
+ worldc.teams << kaz
45
+ worldc.teams << aut
46
+ worldc.teams << swe
47
+
48
+
49
+
50
+ world.teams << ger
51
+ world.teams << fro
52
+ world.teams << irl
53
+ world.teams << kaz
54
+ world.teams << aut
55
+ world.teams << swe
56
+
57
+
58
+ games_world1c = [
59
+ [ 1, ger, [3,0], fro, Time.cet( '2012-09-07 00:00' ) ],
60
+ [ 2, kaz, [1,2], irl, Time.cet( '2012-09-07 00:00' ) ]]
61
+
62
+ games_world2c = [
63
+ [ 3, aut, [1,2], ger, Time.cet( '2012-09-11 00:00' ) ],
64
+ [ 4, swe, [2,0], kaz, Time.cet( '2012-09-11 00:00' ) ]]
65
+
66
+ games_world3c = [
67
+ [ 5, irl, [1,6], ger, Time.cet( '2012-10-12 20:45' ) ],
68
+ [ 6, fro, [1,2], swe, Time.cet( '2012-10-12 18:00' ) ],
69
+ [ 7, kaz, [0,0], aut, Time.cet( '2012-10-12 18:00' ) ]]
70
+
71
+ games_world4c = [
72
+ [ 8, ger, [], swe, Time.cet( '2012-10-16 20:45' ) ],
73
+ [ 9, fro, [], irl, Time.cet( '2012-10-16 20:00' ) ],
74
+ [ 10, aut, [], kaz, Time.cet( '2012-10-16 20:30' ) ]]
75
+
76
+ games_world5c = [
77
+ [ 11, kaz, [], ger, Time.cet( '2013-03-22 00:00' ) ],
78
+ [ 12, aut, [], fro, Time.cet( '2013-03-22 00:00' ) ],
79
+ [ 13, swe, [], irl, Time.cet( '2013-03-22 00:00' ) ]]
80
+
81
+ games_world6c = [
82
+ [ 14, ger, [], kaz, Time.cet( '2013-03-26 00:00' ) ],
83
+ [ 15, irl, [], aut, Time.cet( '2013-03-26 00:00' ) ]]
84
+
85
+ games_world7c = [
86
+ [ 16, irl, [], fro, Time.cet( '2013-06-07 00:00' ) ],
87
+ [ 17, aut, [], swe, Time.cet( '2013-06-07 00:00' ) ]]
88
+
89
+ games_world8c = [
90
+ [ 18, swe, [], fro, Time.cet( '2013-06-11 00:00' ) ]]
91
+
92
+ games_world9c = [
93
+ [ 19, ger, [], aut, Time.cet( '2013-09-06 00:00' ) ],
94
+ [ 20, kaz, [], fro, Time.cet( '2013-09-06 00:00' ) ],
95
+ [ 21, irl, [], swe, Time.cet( '2013-09-06 00:00' ) ]]
96
+
97
+ games_world10c = [
98
+ [ 22, fro, [], ger, Time.cet( '2013-09-10 00:00' ) ],
99
+ [ 23, aut, [], irl, Time.cet( '2013-09-10 00:00' ) ],
100
+ [ 24, kaz, [], swe, Time.cet( '2013-09-10 00:00' ) ]]
101
+
102
+ games_world11c = [
103
+ [ 25, ger, [], irl, Time.cet( '2013-10-11 00:00' ) ],
104
+ [ 26, fro, [], kaz, Time.cet( '2013-10-11 00:00' ) ],
105
+ [ 27, swe, [], aut, Time.cet( '2013-10-11 00:00' ) ]]
106
+
107
+ games_world12c = [
108
+ [ 28, swe, [], ger, Time.cet( '2013-10-15 00:00' ) ],
109
+ [ 29, fro, [], aut, Time.cet( '2013-10-15 00:00' ) ],
110
+ [ 30, irl, [], kaz, Time.cet( '2013-10-15 00:00' ) ]]
111
+
112
+
113
+ Game.create_from_ary!( games_world1c, world1 )
114
+ Game.create_from_ary!( games_world2c, world2 )
115
+ Game.create_from_ary!( games_world3c, world3 )
116
+ Game.create_from_ary!( games_world4c, world4 )
117
+ Game.create_from_ary!( games_world5c, world5 )
118
+ Game.create_from_ary!( games_world6c, world6 )
119
+ Game.create_from_ary!( games_world7c, world7 )
120
+ Game.create_from_ary!( games_world8c, world8 )
121
+ Game.create_from_ary!( games_world9c, world9 )
122
+ Game.create_from_ary!( games_world10c, world10 )
123
+ Game.create_from_ary!( games_world11c, world11 )
124
+ Game.create_from_ary!( games_world12c, world12 )
125
+
126
+
127
+ Prop.create!( key: 'db.world.quali.2012/13.c.version', value: '1' )
@@ -0,0 +1,114 @@
1
+ # encoding: utf-8
2
+
3
+ # note: timezone for games (play_at) is *always* CET (central european time)
4
+
5
+ ###########################################
6
+ ## WM 2014 - Qualification Europe Group I
7
+
8
+
9
+ world = Event.find_by_key!( 'wmq' )
10
+
11
+ world1 = Round.find_by_event_id_and_pos!( world.id, 1 ) # 7. Sep 2012
12
+ world2 = Round.find_by_event_id_and_pos!( world.id, 2 ) # 11. """"""
13
+ world3 = Round.find_by_event_id_and_pos!( world.id, 3 ) # 12. Okt 2012
14
+ world4 = Round.find_by_event_id_and_pos!( world.id, 4 ) # 16. """""""
15
+ world5 = Round.find_by_event_id_and_pos!( world.id, 5 ) # 22. Mär 2013
16
+ world6 = Round.find_by_event_id_and_pos!( world.id, 6 ) # 26. """"""
17
+ world7 = Round.find_by_event_id_and_pos!( world.id, 7 ) # 7. Jun 2013
18
+ world8 = Round.find_by_event_id_and_pos!( world.id, 8 ) # 11. """""""
19
+ world9 = Round.find_by_event_id_and_pos!( world.id, 9 ) # 6. Sep 2013
20
+ world10 = Round.find_by_event_id_and_pos!( world.id, 10 ) # 10. """""""
21
+ world11 = Round.find_by_event_id_and_pos!( world.id, 11 ) # 11. Okt 2013
22
+ world12 = Round.find_by_event_id_and_pos!( world.id, 12 ) # 15. """"""""
23
+
24
+
25
+ #################################3
26
+ # Gruppe I
27
+
28
+ # note: for teams see seeds/euro/teams.rb
29
+
30
+ fra = Team.find_by_key!( 'fra' )
31
+ esp = Team.find_by_key!( 'esp' )
32
+ blr = Team.find_by_key!( 'blr' )
33
+ fin = Team.find_by_key!( 'fin' )
34
+ geo = Team.find_by_key!( 'geo' )
35
+
36
+
37
+ worldi = Group.create!( event: world, pos: 9, title: 'Gruppe I' )
38
+
39
+ worldi.teams << fra
40
+ worldi.teams << esp
41
+ worldi.teams << blr
42
+ worldi.teams << fin
43
+ worldi.teams << geo
44
+
45
+
46
+ world.teams << fra
47
+ world.teams << esp
48
+ world.teams << blr
49
+ world.teams << fin
50
+ world.teams << geo
51
+
52
+
53
+
54
+
55
+ games_world1i = [
56
+ [ 1, geo, [1,0], blr, Time.cet( '2012-09-07 00:00' ) ],
57
+ [ 2, fin, [0,1], fra, Time.cet( '2012-09-07 00:00' ) ]]
58
+
59
+ games_world2i = [
60
+ [ 3, geo, [0,1], esp, Time.cet( '2012-09-11 00:00' ) ],
61
+ [ 4, fra, [3,1], blr, Time.cet( '2012-09-11 00:00' ) ]]
62
+
63
+ games_world3i = [
64
+ [ 5, fin, [1,1], geo, Time.cet( '2012-10-12 17:30' ) ],
65
+ [ 6, blr, [0,4], esp, Time.cet( '2012-10-12 20:00' ) ]]
66
+
67
+ games_world4i = [
68
+ [ 7, esp, [], fra, Time.cet( '2012-10-16 21:00' ) ],
69
+ [ 8, blr, [], geo, Time.cet( '2012-10-16 18:00' ) ]]
70
+
71
+ games_world5i = [
72
+ [ 9, fra, [], geo, Time.cet( '2013-03-22 00:00' ) ],
73
+ [ 10, esp, [], fin, Time.cet( '2013-03-22 00:00' ) ]]
74
+
75
+ games_world6i = [
76
+ [ 11, fra, [], esp, Time.cet( '2013-03-26 00:00' ) ]]
77
+
78
+ games_world7i = [
79
+ [ 12, fin, [], blr, Time.cet( '2013-06-07 00:00' ) ]]
80
+
81
+ games_world8i = [
82
+ [ 13, blr, [], fin, Time.cet( '2013-06-11 00:00' ) ]]
83
+
84
+ games_world9i = [
85
+ [ 14, geo, [], fra, Time.cet( '2013-09-06 00:00' ) ],
86
+ [ 15, fin, [], esp, Time.cet( '2013-09-06 00:00' ) ]]
87
+
88
+ games_world10i = [
89
+ [ 16, blr, [], fra, Time.cet( '2013-09-10 00:00' ) ],
90
+ [ 17, geo, [], fin, Time.cet( '2013-09-10 00:00' ) ]]
91
+
92
+ games_world11i = [
93
+ [ 18, esp, [], blr, Time.cet( '2013-10-11 00:00' ) ]]
94
+
95
+ games_world12i = [
96
+ [ 19, fra, [], fin, Time.cet( '2013-10-15 00:00' ) ],
97
+ [ 20, esp, [], geo, Time.cet( '2013-10-15 00:00' ) ]]
98
+
99
+
100
+ Game.create_from_ary!( games_world1i, world1 )
101
+ Game.create_from_ary!( games_world2i, world2 )
102
+ Game.create_from_ary!( games_world3i, world3 )
103
+ Game.create_from_ary!( games_world4i, world4 )
104
+ Game.create_from_ary!( games_world5i, world5 )
105
+ Game.create_from_ary!( games_world6i, world6 )
106
+ Game.create_from_ary!( games_world7i, world7 )
107
+ Game.create_from_ary!( games_world8i, world8 )
108
+ Game.create_from_ary!( games_world9i, world9 )
109
+ Game.create_from_ary!( games_world10i, world10 )
110
+ Game.create_from_ary!( games_world11i, world11 )
111
+ Game.create_from_ary!( games_world12i, world12 )
112
+
113
+
114
+ Prop.create!( key: 'db.world.quali.2012/13.i.version', value: '1' )
data/lib/sportdb.rb CHANGED
@@ -19,8 +19,16 @@ require 'active_record' ## todo: add sqlite3? etc.
19
19
 
20
20
  # our own code
21
21
 
22
- require 'sportdb/schema'
23
- require 'sportdb/models'
22
+ require 'sportdb/models/country'
23
+ require 'sportdb/models/event'
24
+ require 'sportdb/models/event_team'
25
+ require 'sportdb/models/game'
26
+ require 'sportdb/models/group'
27
+ require 'sportdb/models/group_team'
28
+ require 'sportdb/models/prop'
29
+ require 'sportdb/models/round'
30
+ require 'sportdb/models/team'
31
+ require 'sportdb/schema' # NB: requires sportdb/models (include SportDB::Models)
24
32
  require 'sportdb/utils'
25
33
  require 'sportdb/loader'
26
34
  require 'sportdb/version'
@@ -50,16 +58,32 @@ module SportDB
50
58
  end
51
59
 
52
60
 
61
+ class Deleter
62
+ ## todo: move into its own file???
63
+
64
+ ## make models available in sportdb module by default with namespace
65
+ # e.g. lets you use Team instead of Models::Team
66
+ include SportDB::Models
67
+
68
+ def run( args=[] )
69
+ # for now delete all tables
70
+
71
+ Team.delete_all
72
+ Game.delete_all
73
+ Event.delete_all
74
+ EventTeam.delete_all
75
+ Group.delete_all
76
+ GroupTeam.delete_all
77
+ Round.delete_all
78
+ Prop.delete_all
79
+ Country.delete_all
80
+ end
81
+
82
+ end
83
+
53
84
  # delete ALL records (use with care!)
54
85
  def self.delete!
55
- Team.delete_all
56
- Game.delete_all
57
- Event.delete_all
58
- EventTeam.delete_all
59
- Group.delete_all
60
- GroupTeam.delete_all
61
- Round.delete_all
62
- Prop.delete_all
86
+ Deleter.new.run
63
87
  end # method delete!
64
88
 
65
89
  end # module SportDB
@@ -3,6 +3,11 @@ module SportDB
3
3
 
4
4
  class Runner
5
5
 
6
+
7
+ ## make models available in sportdb module by default with namespace
8
+ # e.g. lets you use Team instead of Models::Team
9
+ include SportDB::Models
10
+
6
11
  def initialize
7
12
  @logger = Logger.new(STDOUT)
8
13
  @logger.level = Logger::INFO
@@ -39,7 +44,7 @@ class Runner
39
44
  logger.datetime_format = "%H:%H:%S"
40
45
  logger.level = Logger::DEBUG
41
46
 
42
- ## todo: set ActiveRecord Logger to stdout??
47
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
43
48
  end
44
49
 
45
50
  cmd.on_tail( "-h", "--help", "Show this message" ) do
@@ -119,7 +124,11 @@ EOS
119
124
 
120
125
  text = File.read( path )
121
126
 
122
- SportDB.module_eval( text )
127
+ # SportDB.module_eval( text )
128
+
129
+ ## evaluate in class context of SportDB::Runner
130
+ ## change to loader class later
131
+ self.class_eval( text )
123
132
 
124
133
  # NB: same as
125
134
  #
@@ -141,6 +150,7 @@ EOS
141
150
  puts "Stats:"
142
151
  puts " #{Event.count} events"
143
152
  puts " #{Team.count} teams"
153
+ puts " #{Country.count} countries"
144
154
  puts " #{Game.count} games"
145
155
  end
146
156
 
@@ -3,6 +3,11 @@ module SportDB
3
3
 
4
4
  class Loader
5
5
 
6
+ ## make models available in sportdb module by default with namespace
7
+ # e.g. lets you use Team instead of Models::Team
8
+ include SportDB::Models
9
+
10
+
6
11
  def initialize
7
12
  @logger = Logger.new(STDOUT)
8
13
  @logger.level = Logger::INFO
@@ -43,7 +48,7 @@ class Loader
43
48
 
44
49
  text = File.read( path )
45
50
 
46
- SportDB.module_eval( text )
51
+ self.class_eval( text )
47
52
 
48
53
  # NB: same as
49
54
  #
@@ -0,0 +1,26 @@
1
+ module SportDB
2
+ module Models
3
+
4
+
5
+ class Country < ActiveRecord::Base
6
+ self.table_name = 'countries'
7
+
8
+ def self.create_from_ary!( countries )
9
+ countries.each do |values|
10
+
11
+ ## key & title required
12
+ attr = {
13
+ :key => values[0],
14
+ :title => values[1],
15
+ :tag => values[2]
16
+ }
17
+
18
+ Country.create!( attr )
19
+ end # each country
20
+ end
21
+
22
+ end # class Country
23
+
24
+
25
+ end # module Models
26
+ end # module SportDB
@@ -0,0 +1,19 @@
1
+ module SportDB
2
+ module Models
3
+
4
+
5
+ class Event < ActiveRecord::Base
6
+
7
+ has_many :rounds, :order => 'pos' # all (fix and flex) rounds
8
+
9
+ has_many :groups, :order => 'pos'
10
+
11
+ has_many :event_teams, :class_name => 'EventTeam'
12
+ has_many :teams, :through => :event_teams
13
+
14
+ end # class Event
15
+
16
+
17
+
18
+ end # module Models
19
+ end # module SportDB
@@ -0,0 +1,14 @@
1
+ module SportDB
2
+ module Models
3
+
4
+
5
+ class EventTeam < ActiveRecord::Base
6
+ self.table_name = 'events_teams'
7
+
8
+ belongs_to :event
9
+ belongs_to :team
10
+ end # class EventTeam
11
+
12
+
13
+ end # module Models
14
+ end # module SportDB
@@ -1,215 +1,141 @@
1
- module SportDB
2
-
3
- class Team < ActiveRecord::Base
4
-
5
- has_many :home_games, :class_name => 'Game', :foreign_key => 'team1_id'
6
- has_many :away_games, :class_name => 'Game', :foreign_key => 'team2_id'
7
-
8
-
9
- def self.create_from_ary!( teams )
10
- teams.each do |values|
11
-
12
- ## key & title required
13
- attr = {
14
- :key => values[0],
15
- :title => values[1]
16
- }
17
-
18
- ## check for optional values
19
- values[2..-1].each do |value|
20
- if value.length == 3 ## assume its a tag (three latters)
21
- attr[ :tag ] = value
22
- else
23
- attr[ :title2 ] = value
24
- end
25
- end
26
-
27
- Team.create!( attr )
28
- end # each team
29
- end
30
-
31
- def key
32
- #todo/fix: generate_key w/ on save callback
33
-
34
- # generate key if necessary (for debugging only - should be not null field!)
35
- value = read_attribute(:key)
36
- value = "?#{title.downcase.gsub( /\s/, '_').gsub( /[^a-z0-9_]/, '*' )}?" if value.blank?
37
- value
38
- end
39
-
40
- end # class Team
41
-
42
-
43
- class Game < ActiveRecord::Base
44
-
45
- belongs_to :team1, :class_name => 'Team', :foreign_key => 'team1_id'
46
- belongs_to :team2, :class_name => 'Team', :foreign_key => 'team2_id'
47
-
48
- belongs_to :round
49
- belongs_to :group # group is optional
50
-
51
- before_save :calc_toto12x
52
-
53
-
54
- def self.create_knockouts_from_ary!( games, round )
55
- Game.create_from_ary!( games, round, true )
56
- end
57
-
58
- def self.create_from_ary!( games, round, knockout=false )
59
- games.each do |values|
60
- Game.create!(
61
- :round =>round,
62
- :pos =>values[0],
63
- :team1 =>values[1],
64
- :score1 =>values[2][0],
65
- :score2 =>values[2][1],
66
- :score3 =>values[2][2],
67
- :score4 =>values[2][3],
68
- :score5 =>values[2][4],
69
- :score6 =>values[2][5],
70
- :team2 =>values[3],
71
- :play_at =>values[4],
72
- :group =>values[5], # Note: group is optional (may be null/nil)
73
- :knockout =>knockout )
74
- end # each games
75
- end
76
-
77
- def self.create_pairs_from_ary_for_group!( pairs, group )
78
-
79
- pairs.each do |pair|
80
- game1_attribs = {
81
- :round =>pair[0][5],
82
- :pos =>pair[0][0],
83
- :team1 =>pair[0][1],
84
- :score1 =>pair[0][2][0],
85
- :score2 =>pair[0][2][1],
86
- :team2 =>pair[0][3],
87
- :play_at =>pair[0][4],
88
- :group =>group }
89
-
90
- game2_attribs = {
91
- :round =>pair[1][5],
92
- :pos =>pair[1][0],
93
- :team1 =>pair[1][1],
94
- :score1 =>pair[1][2][0],
95
- :score2 =>pair[1][2][1],
96
- :team2 =>pair[1][3],
97
- :play_at =>pair[1][4],
98
- :group =>group }
99
-
100
- game1 = Game.create!( game1_attribs )
101
- game2 = Game.create!( game2_attribs )
102
-
103
- # linkup games
104
- game1.next_game_id = game2.id
105
- game1.save!
106
-
107
- game2.prev_game_id = game1.id
108
- game2.save!
109
- end # each pair
110
- end
111
-
112
- def self.create_knockout_pairs_from_ary!( pairs, round1, round2 )
113
-
114
- pairs.each do |pair|
115
- game1_attribs = {
116
- :round =>round1,
117
- :pos =>pair[0][0],
118
- :team1 =>pair[0][1],
119
- :score1 =>pair[0][2][0],
120
- :score2 =>pair[0][2][1],
121
- :team2 =>pair[0][3],
122
- :play_at =>pair[0][4] }
123
-
124
- game2_attribs = {
125
- :round =>round2,
126
- :pos =>pair[1][0],
127
- :team1 =>pair[1][1],
128
- :score1 =>pair[1][2][0],
129
- :score2 =>pair[1][2][1],
130
- :score3 =>pair[1][2][2],
131
- :score4 =>pair[1][2][3],
132
- :score5 =>pair[1][2][4],
133
- :score6 =>pair[1][2][5],
134
- :team2 =>pair[1][3],
135
- :play_at =>pair[1][4],
136
- :knockout =>true }
137
-
138
- game1 = Game.create!( game1_attribs )
139
- game2 = Game.create!( game2_attribs )
140
-
141
- # linkup games
142
- game1.next_game_id = game2.id
143
- game1.save!
144
-
145
- game2.prev_game_id = game1.id
146
- game2.save!
147
- end # each pair
148
- end
149
-
150
- def calc_toto12x
151
- if score1.nil? || score2.nil?
152
- self.toto12x = nil
153
- elsif score1 == score2
154
- self.toto12x = 'X'
155
- elsif score1 > score2
156
- self.toto12x = '1'
157
- elsif score1 < score2
158
- self.toto12x = '2'
159
- end
160
- end
161
-
162
- end # class Game
163
-
164
-
165
- class Event < ActiveRecord::Base
166
-
167
- has_many :rounds, :order => 'pos' # all (fix and flex) rounds
168
-
169
- has_many :groups, :order => 'pos'
170
-
171
- has_many :event_teams, :class_name => 'EventTeam'
172
- has_many :teams, :through => :event_teams
173
-
174
- end # class Event
175
-
176
-
177
- class EventTeam < ActiveRecord::Base
178
- self.table_name = 'events_teams'
179
-
180
- belongs_to :event
181
- belongs_to :team
182
- end
183
-
184
- class Group < ActiveRecord::Base
185
-
186
- has_many :games, :order => 'pos'
187
- belongs_to :event
188
-
189
- has_many :group_teams, :class_name => 'GroupTeam'
190
- has_many :teams, :through => :group_teams
191
-
192
- end # class Group
193
-
194
- class GroupTeam < ActiveRecord::Base
195
- self.table_name = 'groups_teams'
196
-
197
- belongs_to :group
198
- belongs_to :team
199
- end
200
-
201
-
202
- class Round < ActiveRecord::Base
203
-
204
- has_many :games, :order => 'pos'
205
- belongs_to :event
206
-
207
- end # class Round
208
-
209
-
210
- class Prop < ActiveRecord::Base
211
-
212
- end # class Prop
213
-
214
-
215
- end # module SportDB
1
+ module SportDB
2
+ module Models
3
+
4
+
5
+ class Game < ActiveRecord::Base
6
+
7
+ belongs_to :team1, :class_name => 'Team', :foreign_key => 'team1_id'
8
+ belongs_to :team2, :class_name => 'Team', :foreign_key => 'team2_id'
9
+
10
+ belongs_to :round
11
+ belongs_to :group # group is optional
12
+
13
+ before_save :calc_toto12x
14
+
15
+
16
+ def self.create_knockouts_from_ary!( games, round )
17
+ Game.create_from_ary!( games, round, true )
18
+ end
19
+
20
+ def self.create_from_ary!( games, round, knockout=false )
21
+ games.each_with_index do |values,index|
22
+
23
+ ## check if first value (that is, values[0]) is a numeric/number
24
+ ## if NOT add pos automatic (counting from 1 to n)
25
+
26
+ if values[0].kind_of? Numeric
27
+ pos = values[0]
28
+ offset = 1
29
+ else
30
+ pos = index
31
+ offset = 0
32
+ end
33
+
34
+ Game.create!(
35
+ :round => round,
36
+ :pos => pos,
37
+ :team1 => values[offset],
38
+ :score1 => values[offset+1][0],
39
+ :score2 => values[offset+1][1],
40
+ :score3 => values[offset+1][2],
41
+ :score4 => values[offset+1][3],
42
+ :score5 => values[offset+1][4],
43
+ :score6 => values[offset+1][5],
44
+ :team2 => values[offset+2],
45
+ :play_at => values[offset+3],
46
+ :group => values[offset+4], # Note: group is optional (may be null/nil)
47
+ :knockout => knockout )
48
+ end # each games
49
+ end
50
+
51
+ def self.create_pairs_from_ary_for_group!( pairs, group )
52
+
53
+ pairs.each do |pair|
54
+ game1_attribs = {
55
+ :round =>pair[0][5],
56
+ :pos =>pair[0][0],
57
+ :team1 =>pair[0][1],
58
+ :score1 =>pair[0][2][0],
59
+ :score2 =>pair[0][2][1],
60
+ :team2 =>pair[0][3],
61
+ :play_at =>pair[0][4],
62
+ :group =>group }
63
+
64
+ game2_attribs = {
65
+ :round =>pair[1][5],
66
+ :pos =>pair[1][0],
67
+ :team1 =>pair[1][1],
68
+ :score1 =>pair[1][2][0],
69
+ :score2 =>pair[1][2][1],
70
+ :team2 =>pair[1][3],
71
+ :play_at =>pair[1][4],
72
+ :group =>group }
73
+
74
+ game1 = Game.create!( game1_attribs )
75
+ game2 = Game.create!( game2_attribs )
76
+
77
+ # linkup games
78
+ game1.next_game_id = game2.id
79
+ game1.save!
80
+
81
+ game2.prev_game_id = game1.id
82
+ game2.save!
83
+ end # each pair
84
+ end
85
+
86
+ def self.create_knockout_pairs_from_ary!( pairs, round1, round2 )
87
+
88
+ pairs.each do |pair|
89
+ game1_attribs = {
90
+ :round =>round1,
91
+ :pos =>pair[0][0],
92
+ :team1 =>pair[0][1],
93
+ :score1 =>pair[0][2][0],
94
+ :score2 =>pair[0][2][1],
95
+ :team2 =>pair[0][3],
96
+ :play_at =>pair[0][4] }
97
+
98
+ game2_attribs = {
99
+ :round =>round2,
100
+ :pos =>pair[1][0],
101
+ :team1 =>pair[1][1],
102
+ :score1 =>pair[1][2][0],
103
+ :score2 =>pair[1][2][1],
104
+ :score3 =>pair[1][2][2],
105
+ :score4 =>pair[1][2][3],
106
+ :score5 =>pair[1][2][4],
107
+ :score6 =>pair[1][2][5],
108
+ :team2 =>pair[1][3],
109
+ :play_at =>pair[1][4],
110
+ :knockout =>true }
111
+
112
+ game1 = Game.create!( game1_attribs )
113
+ game2 = Game.create!( game2_attribs )
114
+
115
+ # linkup games
116
+ game1.next_game_id = game2.id
117
+ game1.save!
118
+
119
+ game2.prev_game_id = game1.id
120
+ game2.save!
121
+ end # each pair
122
+ end
123
+
124
+ def calc_toto12x
125
+ if score1.nil? || score2.nil?
126
+ self.toto12x = nil
127
+ elsif score1 == score2
128
+ self.toto12x = 'X'
129
+ elsif score1 > score2
130
+ self.toto12x = '1'
131
+ elsif score1 < score2
132
+ self.toto12x = '2'
133
+ end
134
+ end
135
+
136
+ end # class Game
137
+
138
+
139
+
140
+ end # module Models
141
+ end # module SportDB