Class: MatchReport

Inherits:
Object show all
Defined in:
lib/fifadat/models/match_report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ MatchReport

Returns a new instance of MatchReport.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fifadat/models/match_report.rb', line 134

def initialize
    @goals1 =  [], @goals2 = []
    @penalties = nil   ## or use [] - why? why not?


    @lineup1 = [], @lineup2 = []
    @bench1  = [], @bench2  = []

    @formation1 = nil, @formation2 = nil

    @yellow1  = []
    @yellow2  = []
    @yellowred1 = []
    @yellowred2  = []
    @red1  = []
    @red2  = []

    @subs1  = [],       @subs2  = []

    @officials = nil
end

Instance Attribute Details

#bench1 ⇒ Object

Returns the value of attribute bench1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def bench1
  @bench1
end

#bench2 ⇒ Object

Returns the value of attribute bench2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def bench2
  @bench2
end

#formation1 ⇒ Object

Returns the value of attribute formation1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def formation1
  @formation1
end

#formation2 ⇒ Object

Returns the value of attribute formation2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def formation2
  @formation2
end

#goals1 ⇒ Object

Returns the value of attribute goals1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def goals1
  @goals1
end

#goals2 ⇒ Object

Returns the value of attribute goals2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def goals2
  @goals2
end

#lineup1 ⇒ Object

Returns the value of attribute lineup1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def lineup1
  @lineup1
end

#lineup2 ⇒ Object

Returns the value of attribute lineup2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def lineup2
  @lineup2
end

#officials ⇒ Object

Returns the value of attribute officials.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def officials
  @officials
end

#penalties ⇒ Object

Returns the value of attribute penalties.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def penalties
  @penalties
end

#red1 ⇒ Object

Returns the value of attribute red1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def red1
  @red1
end

#red2 ⇒ Object

Returns the value of attribute red2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def red2
  @red2
end

#subs1 ⇒ Object

Returns the value of attribute subs1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def subs1
  @subs1
end

#subs2 ⇒ Object

Returns the value of attribute subs2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def subs2
  @subs2
end

#yellow1 ⇒ Object

Returns the value of attribute yellow1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def yellow1
  @yellow1
end

#yellow2 ⇒ Object

Returns the value of attribute yellow2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def yellow2
  @yellow2
end

#yellowred1 ⇒ Object

Returns the value of attribute yellowred1.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def yellowred1
  @yellowred1
end

#yellowred2 ⇒ Object

Returns the value of attribute yellowred2.



122
123
124
# File 'lib/fifadat/models/match_report.rb', line 122

def yellowred2
  @yellowred2
end

Class Method Details

.build(live, timeline = nil) ⇒ Object



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
49
50
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fifadat/models/match_report.rb', line 18

def self.build( live, timeline=nil )
      _players1 = Player._build_ary( live['HomeTeam']['Players'] )
      _players2 = Player._build_ary( live['AwayTeam']['Players'] )

      players  = Players.new      ## lookup all players (team1+team2)

      players.add( _players1 )
      players.add( _players2 )

      players1 = Players.new      ## lookup players by team1/team2 only

      players1.add( _players1 )
      players2 = Players.new
      players2.add( _players2 )


      o = new
      ###

      ##  add goals

      o.goals1 = Goal._build_ary( live['HomeTeam']['Goals'], players: players )
      o.goals2 = Goal._build_ary( live['AwayTeam']['Goals'], players: players )


      ##########

      ##   add penalty kicks / penalties

      ##     note - requires timeline  !!!

      if live['ResultType'] == 2     ## win on pens

         if timeline.nil?
            ##

            team1_code = live['HomeTeam']['Abbreviation']
            team2_code = live['AwayTeam']['Abbreviation']
            puts "!! ERROR - no timeline for match #{team1_code} v #{team2_code} with win on penalties!!"
            exit 1
         else
            ### get timeline with penalty shoot-out details

            ##     note - requires team1/2_ids!!!

            team1_id = live['HomeTeam']['IdTeam']
            team2_id = live['AwayTeam']['IdTeam']

            pens = Penalty._build_ary( timeline['Event'],
                                         players: players,
                                         team1_id: team1_id,
                                         team2_id: team2_id )
            ## pp pens


            o.penalties = pens
         end
      end


      #######

      ##  add lineup/bench

      o.formation1 = live['HomeTeam']['Tactics']   ## e.g. "Tactics": "3-1-4-2"

      o.lineup1    = players1.lineup   ## starter XI (only excludes bench)

      o.bench1     = players1.bench    ## substitutes


      o.formation2 = live['AwayTeam']['Tactics']
      o.lineup2    = players2.lineup
      o.bench2     = players2.bench


      #####

      ## add bookings (yellow/red/red-yellow cards)

      players1.add_bookings( live['HomeTeam']['Bookings'])
      players2.add_bookings( live['AwayTeam']['Bookings'])



      ##

      ##  maybe collect cards on demand (not ahead of time) - why? why not?

      yellow1    = players1.yellowcards
      red1       = players1.redcards
      yellowred1 = players1.yellowredcards

      o.yellow1     = yellow1    unless yellow1.empty?
      o.red1        = red1       unless red1.empty?
      o.yellowred1  = yellowred1 unless yellowred1.empty?

      yellow2    = players2.yellowcards
      red2       = players2.redcards
      yellowred2 = players2.yellowredcards

      o.yellow2     = yellow2    unless yellow2.empty?
      o.red2        = red2       unless red2.empty?
      o.yellowred2  = yellowred2 unless yellowred2.empty?


      ## add substitutions (off/on - in/out)

      o.subs1   =  Sub._build_ary( live['HomeTeam']['Substitutions'], players: players1 )
      o.subs2   =  Sub._build_ary( live['AwayTeam']['Substitutions'], players: players2 )


      ## add referees

      officials = Official._build_ary( live['Officials'] )

      if officials.empty?
         puts "!! WARN no refs / officials found"
      else
         o.officials = officials
      end

      o
end

Instance Method Details

#as_json ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fifadat/models/match_report.rb', line 171

def as_json(*)
      h = {}

       if goals1 || goals2
         h['goals1']  = (goals1 || []).as_json
         h['goals2']  = (goals2 || []).as_json
       end

       h['penalities'] = penalties.as_json    if penalties

       h['formation1'] = formation1    if formation1
       h['lineup1']    = lineup1.as_json
       h['bench1']     = bench1.as_json

       h['formation2'] = formation2    if formation2
       h['lineup2']    = lineup2.as_json
       h['bench2']     = bench2.as_json

       h['yellow1']     = yellow1.as_json    if yellow1 && !yellow1.empty?
       h['yellow2']     = yellow2.as_json    if yellow2 && !yellow2.empty?
       h['yellowred1']  = yellowred1.as_json   if yellowred1 && !yellowred1.empty?
       h['yellowred2']  = yellowred2.as_json   if yellowred2 && !yellowred2.empty?
       h['red1']        = red1.as_json          if red1 && !red1.empty?
       h['red2']        = red2.as_json          if red2 && !red2.empty?

       h['subs1']     = subs1.as_json
       h['subs2']     = subs2.as_json

       h['referees'] = officials.as_json    if  officials

       h
end

#sentoff1 ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/fifadat/models/match_report.rb', line 156

def sentoff1
   ## pp self

   sentoff1 =  (red1||[]) + (yellowred1||[])
   ## pp red1

   ## pp yellowred1

   ## pp sentoff1

   sentoff1.sort { |l,r|  l.minute <=> r.minute }  ## sort by minute

end

#sentoff2 ⇒ Object



164
165
166
167
# File 'lib/fifadat/models/match_report.rb', line 164

def sentoff2
   sentoff2 =  (red2||[]) + (yellowred2||[])
   sentoff2.sort { |l,r|  l.minute <=> r.minute }  ## sort by minute

end