filters = $filters; } protected function getStats(): array { $data = LoadEvents::load($this->filters, false); if (!$data) return []; $arr_home = []; $arr_away = []; $this->columns = [ 'default' => 1, 'sm' => 2, 'md' => 3, 'lg' => 6, ]; $statistic_type = HelperStatisticType::get($this->filters); if (! isset($data['datasets'][0])) { $data['datasets'][0]['data'] = []; $data['datasets'][1]['data'] = []; $total_home = 0; $total_away = 0; $summe_home = 0; $summe_away = 0; $home_team = ''; $away_team = ''; } else { $arr_home = $data['datasets'][0]['data']; $arr_away = $data['datasets'][1]['data']; $total_home = count($arr_home); $total_away = count($arr_away); $summe_home = array_sum($arr_home); $summe_away = array_sum($arr_away); $home_team = $data['home_team']; $away_team = $data['away_team']; } // dd($arr_home); // if (! $total_home) { // return [ // Stat::make('Number of events', 0), // Stat::make('Average ' . $statistic_type, self::template(0, 0, $home_team, $away_team)), // Stat::make('Median ' . $statistic_type, self::template(0, 0, $home_team, $away_team)), // Stat::make('Win', 0), // Stat::make('Draw', 0), // Stat::make('Lost', 0), // ]; // } if ($arr_home) $median_home = self::getMedian($arr_home); else $median_home = 0; if ($arr_away) $median_away = self::getMedian($arr_away); else $median_away = 0; if (isset($data['results'])) { $win = $data['results']['w'] ? number_format(Round::odd($total_home / $data['results']['w']), 2) : 0; $draw = $data['results']['d'] ? number_format(Round::odd($total_home / $data['results']['d']), 2) : 0; $lost = $data['results']['l'] ? number_format(Round::odd($total_home / $data['results']['l']), 2) : 0; } else { return []; } if ($total_home) $value_home = number_format($summe_home / $total_home, 1); else $value_home = 0; if ($total_away) $value_away = number_format($summe_away / $total_away, 1); else $value_away = 0; // dd($value_home); if ($data['similar_past_matches'] != 'none') $fixtotwo = true; else $fixtotwo = false; return [ Stat::make('Number of events', new HtmlString('
' . $total_home . '
'))->extraAttributes(['x-init' => "animate('number-of-events', $total_home)"]), Stat::make('Average ' . $statistic_type, self::template('avhome', 'avaway', $home_team, $away_team))->extraAttributes(['x-init' => "animate('avhome', $value_home); animate('avaway', $value_away)"]), Stat::make('Median ' . $statistic_type, self::template('mehome', 'meaway', $home_team, $away_team))->extraAttributes(['x-init' => "animate('mehome', $median_home); animate('meaway', $median_away)"]), Stat::make($data['similar_past_matches'] != 'none' ? '1' : 'Win: ' . $data['results']['w'], new HtmlString('
' . $win . '
'))->extraAttributes(['x-init' => "animate('number-of-win', $win, 500, $fixtotwo)"]), Stat::make($data['similar_past_matches'] != 'none' ? 'X' : 'Draw: ' . $data['results']['d'], new HtmlString('
' . $draw . '
'))->extraAttributes(['x-init' => "animate('number-of-draw', $draw, 500, $fixtotwo)"]), Stat::make($data['similar_past_matches'] != 'none' ? '2' : 'Lost: ' . $data['results']['l'], new HtmlString('
' . $lost . '
'))->extraAttributes(['x-init' => "animate('number-of-lost', $lost, 500, $fixtotwo)"]), ]; } protected function template(string $home_id, string $away_id, string $home_name, string $away_name): HtmlString { return new HtmlString('
0 0 ' . $home_name . ' ' . $away_name . '
'); } protected function getMedian(array $arr) { sort($arr); $size = count($arr); if ($size % 2 == 0) { // even $m = ($size) / 2; $m1 = $m - 1; $m2 = $m; // dd('GERADE', $arr, $size, $m, $m1, $m2); return round(($arr[$m1] + $arr[$m2]) / 2, 2); } else { // odd (add 1 because array starts with 0) $m = ($size - 1) / 2; // dd('UNGERADE', $m, $arr, $arr[$m]); return $arr[$m]; } } }