Class: Score

Inherits:
Object
  • Object
show all
Defined in:
lib/score-formats.rb,
lib/score-formats/score.rb,
lib/score-formats/printer.rb

Overview

note: make Score top-level and use like Date - yes, yes, yes - why? why not?

Constant Summary collapse

SCORE_SPLIT_RE =
%r{^  [ ]*
                        ([0-9]+)
[ ]*
[:x–-]      ## note: allow some unicode dashes too
[ ]*
                        ([0-9]+)
[ ]*  $}xi

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ Score

Returns a new instance of Score.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/score-formats/score.rb', line 75

def initialize( *values )
  ## note: for now always assumes integers
  ##  todo/check - check/require integer args - why? why not?

  ### todo/fix: add more init options
  ##   allow kwargs (keyword args) via hash - why? why not?
  ##     use kwargs for "perfect" init where you can only set the half time (ht) score
  ##          or only the penalty or other "edge" cases
  ##   allow int pairs e.g. [1,2], [2,2]
  ##   allow values array MUST be of size 8 (or 4 or 6) - why? why not?

  raise ArgumentError, "expected even integer number (pairs), but got #{values.size}"   if values.size % 2 == 1

  if values.size == 2
    @score1   = values[0]    # full time (ft) score
    @score2   = values[1]

    @score1i  = @score2i  = nil
    @score1et = @score2et = nil
    @score1p  = @score2p  = nil
  else
    @score1i  = values[0]    # half time (ht) score
    @score2i  = values[1]

    @score1   = values[2]    # full time (ft) score
    @score2   = values[3]

    @score1et = values[4]    # extra time (et) score
    @score2et = values[5]

    @score1p  = values[6]    # penalty (p) score
    @score2p  = values[7]
  end
end

Instance Attribute Details

#score1Object (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score1
  @score1
end

#score1etObject (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score1et
  @score1et
end

#score1iObject (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score1i
  @score1i
end

#score1pObject (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score1p
  @score1p
end

#score2Object (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score2
  @score2
end

#score2etObject (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score2et
  @score2et
end

#score2iObject (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score2i
  @score2i
end

#score2pObject (readonly)

half time (ht) score



42
43
44
# File 'lib/score-formats/score.rb', line 42

def score2p
  @score2p
end

Class Method Details

.find!(line, lang: ScoreFormats.lang) ⇒ Object



66
67
68
# File 'lib/score-formats.rb', line 66

def self.find!( line, lang: ScoreFormats.lang )
  ScoreFormats.find!( line, lang: lang )
end

.parse(line, lang: ScoreFormats.lang) ⇒ Object



62
63
64
# File 'lib/score-formats.rb', line 62

def self.parse( line, lang: ScoreFormats.lang )
  ScoreFormats.parse( line, lang: lang )
end

.split(str) ⇒ Object

note: return array of two integers or empty array



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/score-formats/score.rb', line 14

def self.split( str )   ## note: return array of two integers or empty array
  ## e.g. allow/support
  ##      1-1 or 1 - 1      - "english" style
  ##      1:1               - "german / deutsch" style
  ##      1x1 1X1           - "brazil - português / portuguese" style

  ## note: add unicode "fancy" dash too (e.g. –)
  ## add some more - why? why not?

  if m=SCORE_SPLIT_RE.match(str)
    [m[1].to_i, m[2].to_i]
  else
    # no match - warn if str is NOT empty? why? why not?

    if str.empty? || ['-', '-:-', '?'].include?( str )
      ## do NOT warn for known "good" empty scores for now - why? why not?
      ##   add some more?? use Score.empty? or such - why? why not?
    else
      puts "!! WARN - cannot match (split) score format >#{str}<"
    end

    []
  end
end

Instance Method Details

#etObject Also known as: extra_time

e.g. 90+15mins



52
# File 'lib/score-formats/score.rb', line 52

def et()  [@score1et, @score2et]; end

#et?Boolean Also known as: extra_time?

Returns:

  • (Boolean)


65
# File 'lib/score-formats/score.rb', line 65

def et?()  @score1et && @score2et; end

#format_de(format = :default) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/score-formats/printer.rb', line 51

def format_de( format=:default )
  ## note: format gets ignored for now (only one available)

  buf = String.new('')
  ## note: also allow (minimal) scores only with a.e.t. (and no full time)
  if ft? || et?        # 2-2 (1-1) n.V. 5-1 i.E.
    if et?
      buf << "#{@score1et}:#{@score2et}"
    end
    if ft?
      if buf.empty?
        buf << " #{@score1}:#{@score2}"
        ## note:
        ##   avoid 0-0 (0-0)
        ##  only print if score1 & score2 NOT 0-0
        if ht? && ft != [0,0]
          buf << " (#{@score1i}:#{@score2i})"
        end
      else  ## assume pen. and/or a.e.t.
        buf << " (#{@score1}:#{@score2}"
        if ht? && ft != [0,0]
          buf << ", #{@score1i}:#{@score2i}"
        end
        buf << ")"
      end
    end
    if et?
      buf << " n.V."
    end
    if p?
      buf << " #{@score1p}:#{@score2p} i.E."
    end
  else # assume empty / unknown score
    buf << '-'
  end
  buf.strip
end

#format_en(format = :default) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/score-formats/printer.rb', line 14

def format_en( format=:default )
  ## note: format gets ignored for now (only one available)

  buf = String.new
  ## note:  allow (minimal) scores only with a.e.t. (and no full time)
  ###       allow (minimal) score only with pen. too
  if ft? || et? || p?
    if p?
      buf << "#{@score1p}-#{@score2p} pen."
    end
    if et?
      buf << " #{@score1et}-#{@score2et} a.e.t."
    end
    if ft?
       if buf.empty?
         buf << " #{@score1}-#{@score2}"
         ## note:
         ##   avoid 0-0 (0-0)
         ##  only print if score1 & score2 NOT 0-0
         if ht? && ft != [0,0]
           buf << " (#{@score1i}-#{@score2i})"
         end
       else  ## assume pen. and/or a.e.t.
         buf << " (#{@score1}-#{@score2}"
         if ht? && ft != [0,0]
           buf << ", #{@score1i}-#{@score2i}"
         end
         buf << ")"
       end
    end
  else # assume empty / unknown score
     buf << '-'
  end
  buf.strip
end

#ftObject Also known as: full_time

alternate accessor via array e.g. ft and ft



50
# File 'lib/score-formats/score.rb', line 50

def ft()  [@score1,   @score2];   end

#ft?Boolean Also known as: full_time?

todo/check: allow one part missing why? why not?

e.g.  1-nil  or nil-1  - why? why not?

Returns:

  • (Boolean)


63
# File 'lib/score-formats/score.rb', line 63

def ft?()  @score1   && @score2;   end

#htObject Also known as: half_time

e.g. 45 mins



51
# File 'lib/score-formats/score.rb', line 51

def ht()  [@score1i,  @score2i];  end

#ht?Boolean Also known as: half_time?

Returns:

  • (Boolean)


64
# File 'lib/score-formats/score.rb', line 64

def ht?()  @score1i  && @score2i;  end

#pObject Also known as: pen, penalties

e.g. note - starts “fresh” score from 0-0



53
# File 'lib/score-formats/score.rb', line 53

def p()   [@score1p,  @score2p];  end

#p?Boolean Also known as: pen?, penalties?

Returns:

  • (Boolean)


66
# File 'lib/score-formats/score.rb', line 66

def p?()   @score1p  && @score2p;  end

#to_aObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/score-formats/score.rb', line 152

def to_a
  ## pairs with values
  pairs = []
  ## note: allow 1-nil, nil-1 for now in pairs (or use && and NOT ||) - why? why not?
  pairs << [@score1i,  @score2i]   if @score1i  || @score2i
  pairs << [@score1,   @score2]    if @score1   || @score2
  pairs << [@score1et, @score2et]  if @score1et || @score2et
  pairs << [@score1p,  @score2p]   if @score1p  || @score2p

  if pairs.empty?
    pairs   # e.g. return []
  elsif pairs.size == 1
    pairs[0]  # return single pair "unwrapped" e.g. [0,1] instead of [[0,1]] - why? why not?
  else
    pairs
  end
end

#to_formatted_s(format = :default, lang: ScoreFormats.lang) ⇒ Object Also known as: to_s



3
4
5
6
7
8
9
# File 'lib/score-formats/printer.rb', line 3

def to_formatted_s( format=:default, lang: ScoreFormats.lang )
  ## note: format gets ignored for now (only one available)
  case lang.to_sym
  when :de   then   format_de( format )
  else              format_en( format ) # note: for now always fallback to english
  end
end

#to_h(format = :default) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/score-formats/score.rb', line 112

def to_h( format = :default )
   case format.to_sym
   when :default, :std
     ## check/todo:  only add entries if ft, ht, etc. have values (non-null) or always - why? why not?
     h = {}
     h[:ht] = [@score1i,  @score2i]   if @score1i  || @score2i
     h[:ft] = [@score1,   @score2]    if @score1   || @score2
     h[:et] = [@score1et, @score2et]  if @score1et || @score2et
     h[:p]  = [@score1p,  @score2p]   if @score1p  || @score2p
     h
   when :db
     ## use a "flat" structure with "internal" std names
     { score1i:  @score1i,   score2i:  @score2i,
       score1:   @score1,    score2:   @score2,
       score1et: @score1et,  score2et: @score2et,
       score1p:  @score1p,   score2p:  @score2p
     }
   else
     puts "!! ERROR: unknown score to_h format >#{format}<"
     exit 1
   end
end

#valuesObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/score-formats/score.rb', line 136

def values
  ## todo/ fix: always return complete array
  ##  e.g. [score1i, score2i, score1, score2, score1et, score2et, score1p, score2p]

  ## 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 nil,nil
  score = []
  score += [@score1i,  @score2i]     if @score1p || @score2p || @score1et || @score2et || @score1 || score2 || score1i || score2i
  score += [@score1,   @score2]      if @score1p || @score2p || @score1et || @score2et || @score1 || score2
  score += [@score1et, @score2et]    if @score1p || @score2p || @score1et || @score2et
  score += [@score1p,  @score2p]     if @score1p || @score2p
  score
end