sportdb 1.9.0 → 1.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sportdb/calc.rb CHANGED
@@ -50,6 +50,12 @@ module StandingsHelper
50
50
 
51
51
 
52
52
  def self.calc( games, opts={} )
53
+
54
+ ##
55
+ # possible opts include:
56
+ # pts_won: 3 or 2 or n (default 3)
57
+ # pts_exclude_scorep false or true (default false) -- exclude penalty shotout scores (e.g. count a draw/tie - no winner)
58
+ #
53
59
  recs = calc_stats( games, opts )
54
60
 
55
61
  ## update pos (that is, ranking e.g. 1.,2., 3. etc.)
@@ -70,7 +76,7 @@ module StandingsHelper
70
76
 
71
77
  events.each do |event|
72
78
  puts " update standings for #{event.title}"
73
- recs = calc_stats( event.games )
79
+ recs = calc_stats( event.games, opts )
74
80
 
75
81
  recs.each do |team_key, rec|
76
82
  alltime_rec = alltime_recs[ team_key ] || Stats.new
@@ -129,7 +135,7 @@ module StandingsHelper
129
135
  ## update pos (that is, ranking e.g. 1.,2., 3. etc.)
130
136
  alltime_recs= update_ranking( alltime_recs )
131
137
 
132
- pp alltime_recs
138
+ ## pp alltime_recs
133
139
  alltime_recs
134
140
  end
135
141
 
@@ -144,6 +150,19 @@ module StandingsHelper
144
150
  # note:
145
151
  # returns stats records w/ stats records counter always set to one (recs==1)
146
152
 
153
+ ## todo/fix: find a better way to include logger (do NOT hardcode usage of root logger)!!!
154
+ logger = LogUtils::Logger.root
155
+
156
+
157
+ ## lets you pass in 2 as an alterantive, for example
158
+ pts_won = opts[:pts_won] || 3
159
+
160
+ ## lets you exclude penalty shootout (e.g. match gets scored as draw/tie 1 pt each)
161
+ # e.g. why? used for alltime standings formula in world cup, for example
162
+ # todo: check other standings - exclude penalty shootout too - e.g. championsleague ?? if yes - make it true as default??
163
+ pts_exclude_scorep = opts[:pts_exclude_scorep].present? ? opts[:pts_exclude_scorep] : false
164
+
165
+
147
166
  recs = {}
148
167
 
149
168
  games.each_with_index do |g,i| # note: index(i) starts w/ zero (0)
@@ -153,7 +172,7 @@ module StandingsHelper
153
172
  next
154
173
  end
155
174
  unless g.complete?
156
- puts " !!!! skipping match - scores incomplete"
175
+ logger.error "[StandingsHelper.calc_stats] skipping match #{g.team1.title} - #{g.team2.title} - scores incomplete #{g.score_str}"
157
176
  next
158
177
  end
159
178
 
@@ -166,16 +185,23 @@ module StandingsHelper
166
185
 
167
186
  rec1.played += 1
168
187
  rec2.played += 1
169
-
170
- if g.winner == 1
188
+
189
+ ## check - if winner (excludes penalty shootout scores in calc? start w/ extra time e.g winneret)
190
+ if pts_exclude_scorep
191
+ winner = g.winneret || g.winner90 ## if no extra time (et) score; try 90min (regular time score)
192
+ else
193
+ winner = g.winner ## note: might include penalty shoot scores
194
+ end
195
+
196
+ if winner == 1
171
197
  rec1.won += 1
172
198
  rec2.lost += 1
173
- rec1.pts += 3 # 3pts for win (hardcoded for now; change - use event setting?)
174
- elsif g.winner == 2
199
+ rec1.pts += pts_won
200
+ elsif winner == 2
175
201
  rec1.lost += 1
176
202
  rec2.won += 1
177
- rec2.pts += 3 # 3pts for win (hardcoded for now; change - use event setting?)
178
- else ## assume == 0 (drawn)
203
+ rec2.pts += pts_won
204
+ else ## assume drawn/tie (that is, 0)
179
205
  rec1.drawn += 1
180
206
  rec2.drawn += 1
181
207
  rec1.pts += 1
@@ -190,12 +216,12 @@ module StandingsHelper
190
216
 
191
217
  ## add overtime and penalty??
192
218
  ## - for now add only overtime if present
193
- rec1.goals_for += (g.score1et-g.score1) if g.score1et.present?
194
- rec1.goals_against += (g.score2et-g.score2) if g.score2et.present?
195
-
196
- rec2.goals_for += (g.score2et-g.score2) if g.score2et.present?
197
- rec2.goals_against += (g.score1et-g.score1) if g.score1et.present?
219
+
220
+ rec1.goals_for += (g.score1et-g.score1) if g.score1et.present?
221
+ rec1.goals_against += (g.score2et-g.score2) if g.score2et.present?
198
222
 
223
+ rec2.goals_for += (g.score2et-g.score2) if g.score2et.present?
224
+ rec2.goals_against += (g.score1et-g.score1) if g.score1et.present?
199
225
 
200
226
  recs[ g.team1.key ] = rec1
201
227
  recs[ g.team2.key ] = rec2
@@ -45,6 +45,25 @@ class Game < ActiveRecord::Base
45
45
  def draw? () winner == 0; end # use different name; use an alias (any better names more speaking???)
46
46
 
47
47
 
48
+
49
+ ## winner after extra time (will ignore possible penalty shootout; used for alltime standings in world cup calc, for example)
50
+ ## - also add winnerp nil,1,2 => nil -> no penalty shoutout (or no scores) -- needed for what?
51
+ def winneret
52
+ ## check for extra time
53
+ if score1et.present? && score2et.present?
54
+ if score1et > score2et
55
+ 1
56
+ elsif score1et < score2et
57
+ 2
58
+ else # assume score1et == score2et - draw
59
+ 0
60
+ end
61
+ else
62
+ nil # no extra time; note: return nil use winneret || winner90 to get result for both extra time or if not present regular time
63
+ end
64
+ end
65
+
66
+
48
67
  def calc_winner
49
68
  if score1.nil? || score2.nil?
50
69
  self.winner90 = nil
@@ -12,10 +12,10 @@ class EventStanding < ActiveRecord::Base
12
12
  belongs_to :event
13
13
 
14
14
  ## convenience helper; recalcs all records
15
- def self.recalc!() self.order(:id).each { |rec| rec.recalc! }; end
15
+ def self.recalc!( opts={} ) self.order(:id).each { |rec| rec.recalc!(opts) }; end
16
16
 
17
17
 
18
- def recalc!
18
+ def recalc!( opts={} )
19
19
  ## will calculate event standing e.g.
20
20
 
21
21
  ## calc points (pts) - loop over all group games/matches
@@ -26,7 +26,7 @@ class EventStanding < ActiveRecord::Base
26
26
  # skip knockout rounds - why? why not?
27
27
  # make it configure-able?
28
28
 
29
- recs = StandingsHelper.calc( event.games )
29
+ recs = StandingsHelper.calc( event.games, opts )
30
30
 
31
31
  ## - remove (if exit) old entries and add new entries
32
32
  entries.delete_all # note: assoc dependent set to :delete_all (defaults to :nullify)
@@ -12,16 +12,16 @@ class GroupStanding < ActiveRecord::Base
12
12
  belongs_to :group
13
13
 
14
14
  ## convenience helper; recalcs all records
15
- def self.recalc!() self.order(:id).each { |rec| rec.recalc! }; end
15
+ def self.recalc!( opts={} ) self.order(:id).each { |rec| rec.recalc!(opts) }; end
16
16
 
17
17
 
18
- def recalc!
18
+ def recalc!( opts={} )
19
19
  ## will calculate group standing e.g.
20
20
 
21
21
  ## calc points (pts) - loop over all group games/matches
22
22
  # group.games.each do |game|
23
23
  # end
24
- recs = StandingsHelper.calc( group.games )
24
+ recs = StandingsHelper.calc( group.games, opts )
25
25
 
26
26
  ## - remove (if exit) old entries and add new entries
27
27
  entries.delete_all # note: assoc dependent set to :delete_all (defaults to :nullify)
@@ -1,6 +1,6 @@
1
1
 
2
2
  module SportDb
3
- VERSION = '1.9.0'
3
+ VERSION = '1.9.1'
4
4
  end
5
5
 
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-13 00:00:00.000000000 Z
12
+ date: 2014-06-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: props
16
- requirement: &73799290 !ruby/object:Gem::Requirement
16
+ requirement: &78635870 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *73799290
24
+ version_requirements: *78635870
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: logutils
27
- requirement: &73798730 !ruby/object:Gem::Requirement
27
+ requirement: &78635550 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *73798730
35
+ version_requirements: *78635550
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: textutils
38
- requirement: &73860180 !ruby/object:Gem::Requirement
38
+ requirement: &78635320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *73860180
46
+ version_requirements: *78635320
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: worlddb
49
- requirement: &73859880 !ruby/object:Gem::Requirement
49
+ requirement: &78635060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.0.2
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *73859880
57
+ version_requirements: *78635060
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: tagutils
60
- requirement: &73859690 !ruby/object:Gem::Requirement
60
+ requirement: &78634820 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *73859690
68
+ version_requirements: *78634820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: persondb
71
- requirement: &73859420 !ruby/object:Gem::Requirement
71
+ requirement: &78634560 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *73859420
79
+ version_requirements: *78634560
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: activerecord-utils
82
- requirement: &73859210 !ruby/object:Gem::Requirement
82
+ requirement: &78634350 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *73859210
90
+ version_requirements: *78634350
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: fetcher
93
- requirement: &73858900 !ruby/object:Gem::Requirement
93
+ requirement: &78634090 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0.3'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *73858900
101
+ version_requirements: *78634090
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: gli
104
- requirement: &73858630 !ruby/object:Gem::Requirement
104
+ requirement: &78633850 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 2.5.6
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *73858630
112
+ version_requirements: *78633850
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: activerecord
115
- requirement: &73858440 !ruby/object:Gem::Requirement
115
+ requirement: &78653900 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :runtime
122
122
  prerelease: false
123
- version_requirements: *73858440
123
+ version_requirements: *78653900
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: rdoc
126
- requirement: &73858130 !ruby/object:Gem::Requirement
126
+ requirement: &78653640 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ~>
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '4.0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *73858130
134
+ version_requirements: *78653640
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: hoe
137
- requirement: &73857850 !ruby/object:Gem::Requirement
137
+ requirement: &78653390 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ~>
@@ -142,7 +142,7 @@ dependencies:
142
142
  version: '3.11'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *73857850
145
+ version_requirements: *78653390
146
146
  description: sportdb - sport.db command line tool
147
147
  email: opensport@googlegroups.com
148
148
  executables: