Skip to content

Commit

Permalink
add winner n winner90
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Oct 28, 2013
1 parent 57205bc commit 0aa7889
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 60 deletions.
162 changes: 122 additions & 40 deletions lib/sportdb/models/game.rb
Expand Up @@ -11,7 +11,76 @@ class Game < ActiveRecord::Base

has_many :goals

before_save :calc_toto12x
before_save :calc_winner

def toto12x # old getter - do NOT use depreciated; gets removed; note: returns string; new getter winner returns int
puts "[SportDb::Models::Game] depreciated API toto12x; use [int] winner90 attrib/field"

## fix: use switch/when expr/stmt instead of ifs
value = winner90 # 1 0 2 1 => team 1 0 => draw 2 => team
if value == 0
'X'
elsif value == 1
'1'
elsif value == 2
'2'
elsif value == -1
nil # ??? - unknown -- include --??? why? why not??
else
nil
end
end


def winner1?() winner == 1; end
def winner2?() winner == 2; end
def draw? () winner == 0; end # use different name; use an alias (any better names more speaking???)


def calc_winner
if score1.nil? || score2.nil?
self.winner90 = nil
self.winner = nil
else
if score1 > score2
self.winner90 = 1
elsif score1 < score2
self.winner90 = 2
else # assume score1 == score2 - draw
self.winner90 = 0
end

## todo/fix:
# check for next-game/pre-game !!!
# use 1st leg and 2nd leg - use for winner too
# or add new winner_total or winner_aggregated method ???

## check for penalty - note: some games might only have penalty and no extra time (e.g. copa liberatadores)
if score1p.present? && score2p.present?
if score1p > score2p
self.winner = 1
elsif score1p < score2p
self.winner = 2
else
# issue warning! - should not happen; penalty goes on until winner found!
puts "*** warn: should not happen; penalty goes on until winner found"
end
## check for extra time
elsif score1et.present? && score2et.present?
if score1et > score2et
self.winner = 1
elsif score1et < score2et
self.winner = 2
else # assume score1et == score2et - draw
self.winner = 0
end
else
# assume no penalty and no extra time; same as 90min result
self.winner = self.winner90
end
end
end


### getter/setters for deprecated attribs (score3,4,5,6) n national

Expand Down Expand Up @@ -196,20 +265,6 @@ def self.create_knockout_pairs_from_ary!( pairs, round1, round2 )
end



def calc_toto12x
if score1.nil? || score2.nil?
self.toto12x = nil
elsif score1 == score2
self.toto12x = 'X'
elsif score1 > score2
self.toto12x = '1'
elsif score1 < score2
self.toto12x = '2'
end
end


def over? # game over?
play_at <= Time.now
end
Expand All @@ -218,33 +273,56 @@ def over? # game over?
def knockout?
knockout == true
end

def complete?
score1.present? && score2.present?
end


############# convenience helpers for styling
##

def team1_style_class
buf = ''
## NB: remove if calc?
buf << 'game-team-winner ' if complete? && (score1 > score2)
buf << 'game-team-draw ' if complete? && (score1 == score2)

### fix: loser
## - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals) ??
## use new winner_total method ??

if complete?
if winner1?
buf << 'game-team-winner '
elsif winner2?
buf << 'game-team-loser '
else # assume draw
buf << 'game-team-draw '
end
end

buf << 'game-knockout ' if knockout?
### fix: loser - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals)
buf << 'game-team-loser ' if complete? && (score1 < score2)
buf
end

def team2_style_class
buf = ''
## NB: remove if calc?
buf << 'game-team-winner ' if complete? && (score2 > score1)
buf << 'game-team-draw ' if complete? && (score2 == score1)

### fix: loser
## - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals) ??
## use new winner_total method ??

if complete?
if winner1?
buf << 'game-team-loser '
elsif winner2?
buf << 'game-team-winner '
else # assume draw
buf << 'game-team-draw '
end
end

buf << 'game-knockout ' if knockout?
### fix: loser - add method for checking winner/loser on ko pairs using (1st leg/2nd leg totals)
buf << 'game-team-loser ' if complete? && (score2 < score1)
buf
end

Expand All @@ -262,25 +340,29 @@ def play_at_str( format = nil )


def score_str
return ' - ' if score1.blank? && score2.blank?

if score1p.present? && score2p.present? # im Elfmeterschiessen i.E.?
"#{score1_str} : #{score2_str} / #{score1et} : #{score2et} n.V. / #{score1p} : #{score2p} i.E."
elsif score1et.present? && score2et.present? # nach Verlaengerung n.V.?
"#{score1_str} : #{score2_str} / #{score1et} : #{score2et} n.V."
else
"#{score1_str} : #{score2_str}"
end
end
## return ' - ' if score1.nil? && score2.nil?

def score1_str
if score1.blank? then '-' else score1.to_s end
end
# note: make after extra time optional;
# e.g. copa liberatadores only has regular time plus penalty, for example

def score2_str
if score2.blank? then '-' else score2.to_s end
buf = ""

buf << "#{score1_str} : #{score2_str}"
buf << " / #{score1et_str} : #{score2et_str} n.V." if score1et.present? || score2et.present?
buf << " / #{score1p_str} : #{score2p_str} i.E." if score1p.present? || score2p.present?

buf
end

def score1_str() score1.nil? ? '-' : score1.to_s; end
def score2_str() score2.nil? ? '-' : score2.to_s; end

def score1et_str() score1et.nil? ? '-' : score1et.to_s; end
def score2et_str() score2et.nil? ? '-' : score2et.to_s; end

def score1p_str() score1p.nil? ? '-' : score1p.to_s; end
def score2p_str() score2p.nil? ? '-' : score2p.to_s; end


end # class Game

Expand Down
9 changes: 3 additions & 6 deletions lib/sportdb/schema.rb
Expand Up @@ -194,12 +194,9 @@ def up
t.integer :score2ii # second third - team2 (opt)
t.references :next_game # for hinspiel bei rueckspiel in knockout game
t.references :prev_game



### todo> find a better name (toto is not international/english?)
## rename to score12x or pt12x or result12x
t.string :toto12x # 1,2,X,nil calculate on save

t.integer :winner # 1,2,0,nil calculate on save - "real" winner (after 90 or extra time or penalty, aggregated first+second leg?)
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!)

t.timestamps
end
Expand Down
13 changes: 6 additions & 7 deletions lib/sportdb/utils.rb
Expand Up @@ -427,8 +427,7 @@ def find_scores!( line )
## todo: how to handle game w/o extra time
# but w/ optional penalty ??? e.g. used in copa liberatores, for example
# retrun 0,0 or nil,nil for extra time score ?? or -1, -1 ??
# for now use -1,-1 - check if nil,nil will crash upstream/downstream code??
# todo/fix: better use nil, nil ??
# for now use nil,nil

if line =~ regex
logger.debug " score: >#{$1}-#{$2}<"
Expand All @@ -448,8 +447,8 @@ def find_scores!( line )

line.sub!( regex_et, '[SCORE.ET]' )

## check scores empty? - fill with -1,-1
scores += [-1,-1] if scores.size == 0
## check scores empty? - fill with nil,nil
scores += [nil,nil] if scores.size == 0

scores << $1.to_i
scores << $2.to_i
Expand All @@ -460,9 +459,9 @@ def find_scores!( line )

line.sub!( regex_p, '[SCORE.P]' )

## check scores empty? - fill with -1,-1
scores += [-1,-1] if scores.size == 0
scores += [-1,-1] if scores.size == 2
## check scores empty? - fill with nil,nil
scores += [nil,nil] if scores.size == 0
scores += [nil,nil] if scores.size == 2

scores << $1.to_i
scores << $2.to_i
Expand Down
39 changes: 38 additions & 1 deletion test/helper.rb
Expand Up @@ -9,8 +9,45 @@
# include MiniTest::Unit # lets us use TestCase instead of MiniTest::Unit::TestCase


## our own code
require 'pp'


# ruby gems

require 'active_record'

require 'worlddb'
require 'logutils'
require 'logutils/db' # NB: explict require required for LogDb (not automatic)

## our own code

require 'sportdb'


def setup_in_memory_db
# Database Setup & Config

db_config = {
adapter: 'sqlite3',
database: ':memory:'
}

pp db_config

ActiveRecord::Base.logger = Logger.new( STDOUT )
## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?

## NB: every connect will create a new empty in memory db
ActiveRecord::Base.establish_connection( db_config )


## build schema

LogDb.create
WorldDb.create
SportDb.create
end


setup_in_memory_db()
12 changes: 6 additions & 6 deletions test/test_scores.rb
Expand Up @@ -16,19 +16,19 @@ def test_scores
assert_equal [], parse_scores( '111:0' ) # do not support three digits

## penality only
assert_equal [-1,-1,-1,-1,3,4], parse_scores( '3-4iE' )
assert_equal [-1,-1,-1,-1,3,4], parse_scores( '3:4iE' )
assert_equal [nil,nil,nil,nil,3,4], parse_scores( '3-4iE' )
assert_equal [nil,nil,nil,nil,3,4], parse_scores( '3:4iE' )

## extra time only - allow ?? why not ?? only allow penalty w/ missing extra time?
## todo/fix: issue warning or error in parser!!!
assert_equal [-1,-1,3,4], parse_scores( '3-4nV' )
assert_equal [-1,-1,3,4], parse_scores( '3:4nV' )
assert_equal [nil,nil,3,4], parse_scores( '3-4nV' )
assert_equal [nil,nil,3,4], parse_scores( '3:4nV' )

assert_equal [1,1,3,4], parse_scores( '3:4nV 1:1' )
assert_equal [1,1,3,4], parse_scores( '1:1 3:4nV' )

assert_equal [1,1,-1,-1,3,4], parse_scores( '3:4iE 1:1' )
assert_equal [1,1,-1,-1,3,4], parse_scores( '1:1 3:4iE' )
assert_equal [1,1,nil,nil,3,4], parse_scores( '3:4iE 1:1' )
assert_equal [1,1,nil,nil,3,4], parse_scores( '1:1 3:4iE' )
end

private
Expand Down
46 changes: 46 additions & 0 deletions test/test_winner.rb
@@ -0,0 +1,46 @@
# encoding: utf-8

require 'helper'

class TestWinner < MiniTest::Unit::TestCase

def test_1_2
game = SportDb::Models::Game.new
game.score1 = 1
game.score2 = 2
game.calc_winner

assert_equal 2, game.winner90
assert_equal 2, game.winner
assert_equal true, game.winner2?
assert_equal false, game.winner1?
assert_equal false, game.draw?
end

def test_1_1
game = SportDb::Models::Game.new
game.score1 = 1
game.score2 = 1
game.calc_winner

assert_equal 0, game.winner90
assert_equal 0, game.winner
assert_equal false, game.winner1?
assert_equal false, game.winner2?
assert_equal true, game.draw?
end

def test_2_1
game = SportDb::Models::Game.new
game.score1 = 2
game.score2 = 1
game.calc_winner

assert_equal 1, game.winner90
assert_equal 1, game.winner
assert_equal true, game.winner1?
assert_equal false, game.winner2?
assert_equal false, game.draw?
end

end # class TestWinner

0 comments on commit 0aa7889

Please sign in to comment.