Class: Players

Inherits:
Object
  • Object
show all
Defined in:
lib/fbtxt-pp/models/players.rb

Instance Method Summary collapse

Constructor Details

#initializePlayers

Returns a new instance of Players.



126
127
128
129
# File 'lib/fbtxt-pp/models/players.rb', line 126

def initialize
   @recs   = []
   @lookup = {}
end

Instance Method Details

#_add(new_rec) ⇒ Object

note:

PAK Nam Chol  - North Korea  is two players with same name in the same team!!
                 with different jersey number 4/14


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/fbtxt-pp/models/players.rb', line 154

def _add( new_rec )
   @recs << new_rec

   new_rec.keys.each do |key|
      rec  =  @lookup[ key ]
      if rec.nil?
         @lookup[ key ] = new_rec
      else
        if key == 'paknamchol'      ||
           key == 'deniskovalevich'
           ## ignore for now
           ##   fix later (use/prefer numeric ids!!!)
        else
          raise ArgumentError,
            "duplicate player records  #{rec.pretty_inspect} == #{new_rec.pretty_inspect}"
        end
      end
   end
end

#_add_bookings(bookings, type:) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fbtxt-pp/models/players.rb', line 224

def _add_bookings( bookings, type: )
   bookings.each do |b|

       ## note - ignores cards coach and stuff for now upstream!!!
       player = find( b['name'])
       assert( player, "booking player not found; sorry- #{b.pretty_inspect}" )

       minute =  b['minute']

       case type.to_sym
       when :y   then  player.add_yellow( minute: minute )
       when :r   then  player.add_red( minute: minute )
       when :yr  then  player.add_yellowred( minute: minute )
       else
           raise ArgumentError,
             "expected :y|:r|:yr - unknown card type for booking: #{b.pretty_inspect}"
       end
   end
end

#add_bench(recs) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/fbtxt-pp/models/players.rb', line 140

def add_bench( recs )
   recs.each do |h|
      rec = Player.build( h )
      rec.status = 2
     _add( rec )
   end
end

#add_red(bookings) ⇒ Object



222
# File 'lib/fbtxt-pp/models/players.rb', line 222

def add_red( bookings)        _add_bookings( bookings, type: 'r' ); end

#add_starter(recs) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/fbtxt-pp/models/players.rb', line 132

def add_starter( recs )
   recs.each do |h|
      rec = Player.build( h )
      rec.status = 1  ## set status to starter flag (1)
     _add( rec )
   end
end

#add_subs(subs) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fbtxt-pp/models/players.rb', line 182

def add_subs( subs )
     subs.each do |sub|

       player_off = find( sub['off'] )
       player_on  = find( sub['on'] )

       minute = sub['minute']

       if player_off.nil?
          puts "!! player_off not found in:"
          pp sub
          puts "---"
          pp @recs
          exit 1
       end

       if player_on.nil?
          ### quick fix for N.N.
          if sub['on'] == 'N.N.'
            player_on = Player.build( { 'name' => 'N.N.'} )
          else
            puts "!! player_on not found in:"
            pp sub
            puts "---"
            pp @recs
            exit 1
          end
       end

       assert( player_off && player_on,
               "subs player_off or player_on not found; sorry" )

       player_off.add_sub( player: player_on,
                           minute: minute )
     end
end

#add_yellow(bookings) ⇒ Object



220
# File 'lib/fbtxt-pp/models/players.rb', line 220

def add_yellow( bookings)     _add_bookings( bookings, type: 'y' ); end

#add_yellowred(bookings) ⇒ Object



221
# File 'lib/fbtxt-pp/models/players.rb', line 221

def add_yellowred( bookings)  _add_bookings( bookings, type: 'yr' ); end

#find(q) ⇒ Object



175
176
177
178
# File 'lib/fbtxt-pp/models/players.rb', line 175

def find( q )
   key = slugify( q )
   @lookup[ key ]
end

#lineupObject

todo/check add alias (or rename) starter ?? add new subs to status == 2 - why? why not?



250
251
252
253
# File 'lib/fbtxt-pp/models/players.rb', line 250

def lineup
   recs = @recs.select { |rec| rec.starter? }
   recs
end

#sizeObject



272
# File 'lib/fbtxt-pp/models/players.rb', line 272

def size() @recs.size; end