290 lines
11 KiB
PHP
290 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Filament\Widgets\Data\HelperStatisticType;
|
|
use App\Filament\Widgets\Data\HelperTeamPosition;
|
|
use App\Filament\Widgets\Data\LoadEvents;
|
|
use App\Utilities\Round;
|
|
use Filament\Widgets\Concerns\InteractsWithPageFilters;
|
|
use Filament\Widgets\StatsOverviewWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Illuminate\Support\HtmlString;
|
|
use Livewire\Attributes\Reactive;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class StatsMedianAverage extends StatsOverviewWidget
|
|
{
|
|
use InteractsWithPageFilters;
|
|
|
|
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);
|
|
|
|
$team_id = (int) $this->filters['team_id'] ?? 0;
|
|
|
|
$season = $this->filterss['season'] ?? 0;
|
|
$season_or_date = $this->filters['season_or_date'];
|
|
$date_events = '';
|
|
switch ($season_or_date) {
|
|
case 'season':
|
|
if ($season)
|
|
$date_events = implode(',', $season);
|
|
else
|
|
$date_events = 'All Seasons' . $season_or_date;
|
|
break;
|
|
default:
|
|
$start = $this->filters['start_date'] ?? 0;
|
|
$end = $this->filters['end_date'] ?? 0;
|
|
|
|
$tmp = [];
|
|
if ($start) {
|
|
$tmp[] = 'From: ' . Carbon::createFromDate($start)->format('Y-m-d');
|
|
}
|
|
|
|
if ($end)
|
|
$tmp[] = 'To: ' . Carbon::createFromDate($end)->format('Y-m-d');
|
|
|
|
if ($tmp)
|
|
$date_events = implode('<br>', $tmp);
|
|
else
|
|
$date_events = 'All Dates';
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
// Log::debug($data['datasets']);
|
|
|
|
$team_position = HelperTeamPosition::get($this->filters);
|
|
|
|
if (! $data) {
|
|
return [];
|
|
}
|
|
|
|
|
|
$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 ($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']) && $team_id) {
|
|
// Log::debug($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 {
|
|
$win = 0;
|
|
$draw = 0;
|
|
$lost = 0;
|
|
}
|
|
|
|
if ($total_home && $team_id) {
|
|
$value_home = number_format($summe_home / $total_home, 1);
|
|
} else {
|
|
$value_home = 0;
|
|
}
|
|
if ($total_away && $team_id) {
|
|
$value_away = number_format($summe_away / $total_away, 1);
|
|
} else {
|
|
$value_away = 0;
|
|
}
|
|
|
|
$max_boxes = 3;
|
|
$average_team_home = 0;
|
|
$average_team_away = 0;
|
|
$average_league_home = 0;
|
|
$average_league_away = 0;
|
|
$expectations_home = 0;
|
|
$expectations_away = 0;
|
|
|
|
$expectations_home = $data['expectations_home'] ?? 0;
|
|
$expectations_away = $data['expectations_away'] ?? 0;
|
|
|
|
foreach ($data['datasets'] as $value) {
|
|
if ($value['label'] == 'Average Team') {
|
|
$average_team_home = $value['average_team_home'] ?? 0;
|
|
$average_team_away = $value['average_team_away'] ?? 0;
|
|
$max_boxes++;
|
|
}
|
|
if ($value['label'] == 'Average League') {
|
|
$average_league_home = $value['average_league_home'] ?? 0;
|
|
$average_league_away = $value['average_league_away'] ?? 0;
|
|
$max_boxes++;
|
|
}
|
|
}
|
|
|
|
$boxes = [];
|
|
|
|
// BOX 1
|
|
$boxes[] = Stat::make($statistic_type, self::template_2sp('expectations_home', 'expectations_away', 'Home', 'Away', $expectations_home, $expectations_away))
|
|
->description('Expected Values')
|
|
->extraAttributes([
|
|
'class' => 'box-yellow',
|
|
]);
|
|
|
|
if ($team_id) {
|
|
// BOX 2
|
|
|
|
$boxes[] = Stat::make('Total', self::template_1sp('number-of-events', $total_home, 'Events'))
|
|
->description(fn() => new HtmlString($date_events))
|
|
->extraAttributes([
|
|
'class' => 'box-info',
|
|
]);
|
|
|
|
// BOX 3
|
|
$boxes[] = Stat::make('Average ' . $statistic_type, self::template_2sp('avhome', 'avaway', $home_team, $away_team, $value_home, $value_away))
|
|
->description('Team ' . $team_position)
|
|
->extraAttributes([
|
|
'class' => 'box-success',
|
|
]);
|
|
// ->extraAttributes(['x-init' => "animate('avhome', $value_home); animate('avaway', $value_away)"]);
|
|
|
|
// BOX 4
|
|
// Log::debug([$average_team_home, $average_team_away]);
|
|
$boxes[] = Stat::make('Average ' . $statistic_type, self::template_2sp('av_team_home', 'av_team_away', $home_team, $away_team, $average_team_home, $average_team_away))
|
|
->description('Team Season')
|
|
->extraAttributes([
|
|
'class' => 'box-success',
|
|
]);
|
|
// ->extraAttributes(['x-init' => "animate('av_team_home', $average_team_home); animate('av_team_away', $average_team_away)"]);
|
|
|
|
// BOX 5
|
|
$boxes[] = Stat::make('Average ' . $statistic_type, self::template_2sp('av_league_home', 'av_league_away', 'Home', 'Away', $average_league_home, $average_league_away))
|
|
->description('League Season')
|
|
->extraAttributes([
|
|
'class' => 'box-success',
|
|
]);
|
|
// ->extraAttributes(['x-init' => "animate('av_league_home', $average_league_home); animate('av_league_away', $average_league_away)"]);
|
|
|
|
// BOX 6
|
|
$boxes[] = Stat::make('Odds', self::templateOdds($data, $win, $draw, $lost))
|
|
->description($team_position)
|
|
->extraAttributes([
|
|
'class' => 'box-purple',
|
|
]);
|
|
// ->extraAttributes(['x-init' => "animate('mehome', $median_home); animate('meaway', $median_away, 0)"]);
|
|
}
|
|
|
|
$this->columns = [
|
|
'default' => 1,
|
|
'sm' => 2,
|
|
'md' => 3,
|
|
'xl' => 6,
|
|
];
|
|
|
|
return $boxes;
|
|
}
|
|
|
|
protected function templateOdds(array &$data, float $win, float $draw, float $lost): HtmlString
|
|
{
|
|
if ($data['similar_past_matches'] != 'none') {
|
|
$name_win = '1 (' . $data['results']['w'] . ')';
|
|
$name_draw = 'X (' . $data['results']['d'] . ')';
|
|
$name_lost = '2 (' . $data['results']['l'] . ')';
|
|
} else {
|
|
$name_win = 'Win (' . $data['results']['w'] . ')';
|
|
$name_draw = 'Draw (' . $data['results']['d'] . ')';
|
|
$name_lost = 'Lost (' . $data['results']['l'] . ')';
|
|
}
|
|
|
|
return new HtmlString('<div class="grid grid-cols-3 justify-center">
|
|
<span class="text-center font-bold text-xl">' . number_format($win, 2) . '</span>
|
|
<span class="text-center font-bold text-xl">' . number_format($draw, 2) . '</span>
|
|
<span class="text-center font-bold text-xl">' . number_format($lost, 2) . '</span>
|
|
<span class="text-center text-xs text-gray-500 dark:text-gray-400">' . $name_win . '</span>
|
|
<span class="text-center text-xs text-gray-500 dark:text-gray-400">' . $name_draw . '</span>
|
|
<span class="text-center text-xs text-gray-500 dark:text-gray-400">' . $name_lost . '</span>
|
|
</div>');
|
|
}
|
|
|
|
protected function template_1sp(string $value_id, string $value, $text = 0): HtmlString
|
|
{
|
|
return new HtmlString('<div class="grid grid-cols-1 justify-center font-bold text-2xl">
|
|
<span class="text-center" id="' . $value_id . '">' . $value . '</span>
|
|
<span class="text-center text-xs text-gray-500 dark:text-gray-400">' . $text . '</span>
|
|
</div>');
|
|
}
|
|
|
|
protected function template_2sp(string $home_id, string $away_id, string $home_name, string $away_name, $val_home = 0, $val_away = 0): HtmlString
|
|
{
|
|
return new HtmlString('<div class="grid grid-cols-2 justify-center font-bold text-2xl">
|
|
<span class="text-center" id="' . $home_id . '">' . $val_home . '</span>
|
|
<span class="text-center" id="' . $away_id . '">' . $val_away . '</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>');
|
|
}
|
|
|
|
protected function getMedian(array $arr)
|
|
{
|
|
sort($arr);
|
|
|
|
$size = count($arr);
|
|
|
|
if ($size % 2 == 0) {
|
|
// even
|
|
$m = ($size) / 2;
|
|
$m1 = $m - 1;
|
|
$m2 = $m;
|
|
|
|
// dd($m1, $m2, $arr);
|
|
|
|
// dd('GERADE', $arr, $size, $m, $m1, $m2);
|
|
if (isset($arr[$m1]) && isset($arr[$m2])) {
|
|
return round(($arr[$m1] + $arr[$m2]) / 2, 2);
|
|
}
|
|
} else {
|
|
// odd (add 1 because array starts with 0)
|
|
$m = ceil($size / 2);
|
|
// dd($m, $arr);
|
|
// dd('UNGERADE', $m, $arr, $arr[$m]);
|
|
if (isset($arr[$m])) {
|
|
return $arr[$m];
|
|
}
|
|
}
|
|
}
|
|
}
|