Module: CbfCalendario::PartidaStats

Defined in:
lib/cbf_calendario/partida_stats.rb

Overview

Estatísticas derivadas dos registros da súmula (mesma lógica de show_game.rb).

Class Method Summary collapse

Class Method Details

.agregadas(jogo) ⇒ Hash



10
11
12
13
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/cbf_calendario/partida_stats.rb', line 10

def agregadas(jogo)
  regs = jogo['registros']
  regs = [] unless regs.is_a?(Array)

  por_tipo = regs.each_with_object(Hash.new(0)) { |r, h| h[r['tipo']] += 1 }

  mid = jogo.dig('mandante', 'id').to_s
  vid = jogo.dig('visitante', 'id').to_s

  gols = regs.select { |r| r['tipo'] == 'GOL' }
  gols_tipo = gols.each_with_object(Hash.new(0)) { |r, h| h[r['resultado'].to_s] += 1 }

  gols_m = gols.count { |r| r['clube_id'].to_s == mid }
  gols_v = gols.count { |r| r['clube_id'].to_s == vid }

  pens = regs.select { |r| r['tipo'] == 'PENALIDADE' }
  cartoes = pens.each_with_object(Hash.new(0)) { |r, h| h[r['resultado'].to_s] += 1 }

  {
    por_tipo_evento: por_tipo,
    gols_por_classificacao_sumula: gols_tipo,
    gols_mandante_em_eventos: gols_m,
    gols_visitante_em_eventos: gols_v,
    cartoes_por_resultado: cartoes,
    total_substituicoes_mandante: (jogo.dig('mandante', 'alteracoes') || []).size,
    total_substituicoes_visitante: (jogo.dig('visitante', 'alteracoes') || []).size
  }
end