124 lines
5.0 KiB
PHP
124 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Filament\Widgets\Data\HelperStatisticType;
|
|
use App\Filament\Widgets\Data\LoadEvents;
|
|
use App\Utilities\Round;
|
|
use Filament\Widgets\StatsOverviewWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Illuminate\Support\HtmlString;
|
|
use Livewire\Attributes\Reactive;
|
|
|
|
class StatsMedianAverage extends StatsOverviewWidget
|
|
{
|
|
protected ?string $pollingInterval = null;
|
|
|
|
protected int|array|null $columns = null;
|
|
|
|
#[Reactive]
|
|
public ?array $filters = [];
|
|
|
|
public ?int $count;
|
|
|
|
public function mount(array $filters)
|
|
{
|
|
$this->filters = $filters;
|
|
}
|
|
|
|
protected function getStats(): array
|
|
{
|
|
$data = LoadEvents::load($this->filters, false);
|
|
|
|
// dd($data);
|
|
|
|
$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'];
|
|
}
|
|
|
|
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),
|
|
];
|
|
}
|
|
|
|
sort($arr_home);
|
|
sort($arr_away);
|
|
|
|
if ($total_home % 2 == 0) {
|
|
// even
|
|
$m = $total_home / 2;
|
|
$m1 = floor($m);
|
|
$m2 = ceil($m);
|
|
$median_home = round(($arr_home[$m1] + $arr_home[$m2]) / 2, 2);
|
|
$m = $total_away / 2;
|
|
$m1 = floor($m);
|
|
$m2 = ceil($m);
|
|
$median_away = round(($arr_away[$m1] + $arr_away[$m2]) / 2, 2);
|
|
|
|
} else {
|
|
// odd
|
|
$m = $total_home / 2;
|
|
$median_home = $arr_home[$m];
|
|
$m = floor($total_away / 2);
|
|
$median_away = $arr_away[$m];
|
|
}
|
|
|
|
$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;
|
|
|
|
$value_home = number_format($summe_home / $total_home, 1);
|
|
$value_away = number_format($summe_away / $total_away, 1);
|
|
|
|
return [
|
|
Stat::make('Number of events', new HtmlString('<div id="number-of-events">'.$total_home.'</div>'))->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' ? 'Win' : 'Win: '.$data['results']['w'], new HtmlString('<div id="number-of-win">'.$win.'</div>'))->extraAttributes(['x-init' => "animate('number-of-win', $win)"]),
|
|
Stat::make($data['similar_past_matches'] != 'none' ? 'Draw' : 'Draw: '.$data['results']['d'], new HtmlString('<div id="number-of-draw">'.$draw.'</div>'))->extraAttributes(['x-init' => "animate('number-of-draw', $draw)"]),
|
|
Stat::make($data['similar_past_matches'] != 'none' ? 'Lost' : 'Lost: '.$data['results']['l'], new HtmlString('<div id="number-of-lost">'.$lost.'</div>'))->extraAttributes(['x-init' => "animate('number-of-lost', $lost)"]),
|
|
];
|
|
}
|
|
|
|
protected function template(string $home_id, string $away_id, string $home_name, string $away_name): HtmlString
|
|
{
|
|
return new HtmlString('<div class="grid grid-cols-2 justify-center font-bold text-4xl">
|
|
<span class="text-center" id="'.$home_id.'">0</span>
|
|
<span class="text-center" id="'.$away_id.'">0</span>
|
|
<span class="text-center text-xs text-gray-500 dark:text-gray-400">'.$home_name.'</span>
|
|
<span class="text-center text-xs text-gray-500 dark:text-gray-400">'.$away_name.'</span>
|
|
</div>');
|
|
}
|
|
}
|