with('away')->where('id', $match_id)->first(); $home_id = $match->home_id; $away_id = $match->away_id; $res = StatistikoEvent::where('league_id', $league_id)->get(); $home_matches = []; $away_matches = []; $date_home = []; $date_away = []; $events_home = []; $events_away = []; $results_home = []; $results_away = []; $labels = []; foreach ($res as $val) { if ($val->home_id == $home_id or $val->away_id == $home_id) { $labels[] = round($val->starttime->diffInDays(Carbon::now())); $date_home[] = $val['starttime']->format('d. F Y H:i'); $results_home[] = $val['gls_h'] . ' : ' . $val['gls_a']; $events_home[] = $val->home->name . ' vs ' . $val->away->name; $home_matches[] = $val->match_id; } if ($val->home_id == $away_id or $val->away_id == $away_id) { $labels[] = round($val->starttime->diffInDays(Carbon::now())); $date_away[] = $val['starttime']->format('d. F Y H:i'); $results_away[] = $val['gls_h'] . ' : ' . $val['gls_a']; $events_away[] = $val->home->name . ' vs ' . $val->away->name; $away_matches[] = $val->match_id; } } $res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get(); $homes = self::filterMatches($res, $home_matches); $res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get(); $aways = self::filterMatches($res, $away_matches); self::shortenEnding($labels, $homes, $aways); $home_av = self::makeAv($homes); $away_av = self::makeAv($aways); $data = [ 'datasets' => [ [ 'label' => $match->home->name, 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $homes['all'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fb2c36', 'backgroundColor' => '#fb2c36', 'pointStyle' => $hasdots, ], [ 'label' => 'Average All', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $home_av['all'], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fb2c36', 'backgroundColor' => '#fb2c36', 'pointStyle' => false, 'borderDash' => [5, 5], ], [ 'label' => 'Defence', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $homes['def'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average Defence', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $home_av['def'], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'pointStyle' => false, 'hidden' => true, 'borderDash' => [5, 5], ], [ 'label' => 'Attak', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $homes['att'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average Attak', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $home_av['att'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, 'pointStyle' => false, 'hidden' => true, 'borderDash' => [5, 5], ], [ 'label' => 'Midfield', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $homes['mid'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average Midfield', 'playing_date' => $date_home, 'results_per_event' => $results_home, 'events' => $events_home, 'data' => $home_av['mid'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#c10007', 'backgroundColor' => '#c10007', 'fill' => false, 'pointStyle' => false, 'borderDash' => [5, 5], 'hidden' => true, ], [ 'label' => $match->away->name, 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $aways['all'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fdc700', 'backgroundColor' => '#fdc700', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average All', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $away_av['all'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#fdc700', 'backgroundColor' => '#fdc700', 'fill' => false, 'pointStyle' => false, 'borderDash' => [5, 5], 'hidden' => true, ], [ 'label' => 'Defence', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $aways['def'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average Defence', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $away_av['def'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, 'pointStyle' => false, 'borderDash' => [5, 5], 'hidden' => true, ], [ 'label' => 'Attak', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $aways['att'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average Attak', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $away_av['att'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, 'pointStyle' => false, 'borderDash' => [5, 5], 'hidden' => true, ], [ 'label' => 'Midfield', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $aways['mid'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, 'pointStyle' => $hasdots, 'hidden' => true, ], [ 'label' => 'Average Midfield', 'playing_date' => $date_away, 'results_per_event' => $results_away, 'events' => $events_away, 'data' => $away_av['mid'] ?? [], 'spanGaps' => true, 'tension' => 0.5, 'borderColor' => '#d08700', 'backgroundColor' => '#d08700', 'fill' => false, 'pointStyle' => false, 'borderDash' => [5, 5], 'hidden' => true, ], ], 'labels' => $labels, ]; return $data; } public static function filterMatches($res, array $matches) { if (!$res->count()) { return []; } $result = []; foreach ($res as $value) { $match_id = $value->match_id; if (! isset($result[$match_id])) { $result[$match_id]['all']['sum_min'] = 0; $result[$match_id]['all']['rating'] = 0; $result[$match_id]['att']['sum_min'] = 0; $result[$match_id]['att']['rating'] = 0; $result[$match_id]['mid']['sum_min'] = 0; $result[$match_id]['mid']['rating'] = 0; $result[$match_id]['def']['sum_min'] = 0; $result[$match_id]['def']['rating'] = 0; } $result[$match_id]['all']['sum_min'] += (int) $value->minutes_played; $result[$match_id]['all']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played; if ($value->player_position == 'M') { $result[$match_id]['mid']['sum_min'] += (int) $value->minutes_played; $result[$match_id]['mid']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played; } elseif ($value->player_position == 'F') { $result[$match_id]['att']['sum_min'] += (int) $value->minutes_played; $result[$match_id]['att']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played; } elseif ($value->player_position == 'D') { $result[$match_id]['def']['sum_min'] += (int) $value->minutes_played; $result[$match_id]['def']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played; } } $return = []; foreach ($matches as $matchid) { if (isset($result[$matchid])) { $return['all'][] = round($result[$matchid]['all']['rating'] / $result[$matchid]['all']['sum_min'], 1); if ($result[$matchid]['def']['sum_min']) { $return['def'][] = round($result[$matchid]['def']['rating'] / $result[$matchid]['def']['sum_min'], 1); } else { $return['def'][] = null; } if ($result[$matchid]['att']['sum_min']) { $return['att'][] = round($result[$matchid]['att']['rating'] / $result[$matchid]['att']['sum_min'], 1); } else { $return['att'][] = null; } if ($result[$matchid]['mid']['sum_min']) { $return['mid'][] = round($result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['sum_min'], 1); } else { $return['mid'][] = null; } } else { $return['all'][] = null; $return['def'][] = null; $return['att'][] = null; $return['mid'][] = null; } } // dd($return); // while (array_last($return['all']) == null) { // } return $return; } public static function shortenEnding(array &$labels, array &$homes, array &$aways) { // dd($labels, $homes, $aways); if (! isset($homes['all'])) { return; } $count_home = 0; $count_away = 0; for ($i = \count($homes['all']) - 1; $i > 0; $i--) { if ($homes['all'][$i] == null) { $count_home++; } else { break; } } for ($i = \count($aways['all']) - 1; $i > 0; $i--) { if ($aways['all'][$i] == null) { $count_away++; } else { break; } } $keeps = count($homes['all']) - min($count_home, $count_away) - 1; $homes['all'] = array_slice($homes['all'], 0, $keeps); $homes['def'] = array_slice($homes['def'], 0, $keeps); $homes['att'] = array_slice($homes['att'], 0, $keeps); $homes['mid'] = array_slice($homes['mid'], 0, $keeps); $aways['all'] = array_slice($aways['all'], 0, $keeps); $aways['def'] = array_slice($aways['def'], 0, $keeps); $aways['att'] = array_slice($aways['att'], 0, $keeps); $aways['mid'] = array_slice($aways['mid'], 0, $keeps); // $labels = array_slice($labels, 0, $keeps); $labels = range(1, $keeps); } public static function makeAv(array &$arr) { $all = array_sum($arr['all']) / count($arr['all']); $def = array_sum($arr['def']) / count($arr['def']); $mid = array_sum($arr['mid']) / count($arr['mid']); $att = array_sum($arr['att']) / count($arr['att']); $ret = []; $ret['all'] = array_fill(0, count($arr['all']), round($all, 2)); $ret['def'] = array_fill(0, count($arr['def']), round($def, 2)); $ret['mid'] = array_fill(0, count($arr['mid']), round($mid, 2)); $ret['att'] = array_fill(0, count($arr['att']), round($att, 2)); return $ret; } }