Files
nuhodds_fi4/app/Filament/Pages/Statistic.php
T
2026-05-29 16:38:15 +02:00

286 lines
13 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Actions\LoadStatisticFixturesLeaguesOptions;
use App\Actions\LoadStatisticFixturesMatchOptions;
use App\Filament\Widgets\ChartEvent;
use App\Filament\Widgets\ChartEventMoAv;
use App\Filament\Widgets\ChartEventPerf;
use App\Filament\Widgets\ChartEventPerfMoAv;
use App\Filament\Widgets\ChartPlayer;
use App\Filament\Widgets\ChartPlayerMoAv;
use App\Filament\Widgets\ChartPlayerPerf;
use App\Filament\Widgets\ChartPlayerPerfMoAv;
use App\Filament\Widgets\StatsMedianAverage;
use App\Models\Element;
use App\Models\StatisticFixtures;
use App\Models\StatistikoDetail;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Slider;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HtmlString;
class Statistic extends BaseDashboard
{
use HasFiltersForm;
protected static string|BackedEnum|null $navigationIcon = 'phosphor-chart-line-up';
protected ?string $pollingInterval = null;
protected static ?string $title = 'Statistic';
protected int|string|array $columnSpan = [
'md' => 2,
'xl' => 3,
];
public function getHeading(): string|Htmlable|null
{
return '';
}
public function filtersForm(Schema $schema): Schema
{
return $schema
->components([
Grid::make([
'default' => 2,
'sm' => 2,
'md' => 3,
'lg' => 4,
])
->columnSpanFull()
->extraAttributes([
'class' => 'p-5 bg-blue-50 dark:bg-blue-950 rounded-lg border border-blue-200 dark:border-blue-800',
])
->schema([
Select::make('league_id')
->prefixIconColor('primary')
->prefixIcon('phosphor-trophy')
->options(fn () => LoadStatisticFixturesLeaguesOptions::load(1))
->live()
->label('League')
->afterStateUpdated(function (callable $set) {
$set('match_id', null);
}),
Select::make('match_id')
->prefixIconColor('primary')
->prefixIcon('phosphor-calendar-check')
->options(function (Get $get) {
return LoadStatisticFixturesMatchOptions::load($get('league_id'));
})
->afterStateUpdated(function (callable $set) {
$set('team_id', null);
$set('tm_player_id', null);
})
->live()
->label('Events'),
Select::make('team_id')
->prefixIconColor(fn (Get $get): string => $get('similar_past_matches') == 'none' ? 'primary' : '')
->prefixIcon('phosphor-users-three')
->disabled(fn (Get $get) => $get('similar_past_matches') != 'none')
->options(function (Get $get) {
if ($get('match_id')) {
$res = StatisticFixtures::where('id', $get('match_id'))->get();
$arr = [];
foreach ($res as $value) {
$arr[] = $value->home_id;
$arr[] = $value->away_id;
}
return Element::query()->whereIn('id', $arr)->pluck('name', 'id');
}
})
->afterStateUpdated(function (callable $set) {
$set('tm_player_id', null);
})
->live()
->label('Team'),
Select::make('team_position')
->prefixIconColor(fn (Get $get): string => $get('similar_past_matches') == 'none' ? 'primary' : '')
->prefixIcon('phosphor-map-pin')
->disabled(fn (Get $get) => $get('similar_past_matches') != 'none')
->default('all')
->selectablePlaceholder(false)
->options([
'all' => 'All',
'home' => 'Home',
'away' => 'Away',
]),
DatePicker::make('start_date')
->native(false)
->prefixIconColor('primary')
->prefixIcon('phosphor-calendar')
->live()
->suffixAction(
Action::make('clear_start')
->icon('phosphor-x')
->color('gray')
->action(fn (Set $set) => $set('start_date', null))
),
DatePicker::make('end_date')
->native(false)
->prefixIconColor('primary')
->prefixIcon('phosphor-calendar')
->live()
->suffixAction(
Action::make('clear_end')
->icon('phosphor-x')
->color('gray')
->action(fn (Set $set) => $set('end_date', null))
),
Select::make('statistic_type')
->columnSpan([
'default' => 1,
'xl' => 2,
])
->prefixIconColor('primary')
->prefixIcon('phosphor-dots-three-outline')
->default('none')
->selectablePlaceholder(false)
->options([
'gls' => 'Goals',
'crn' => 'Corner',
'fls' => 'Fouls Commited',
'ofs' => 'Offside',
'crd' => 'Cards',
'snt' => 'Shots On Target',
'tsh' => 'Total shots',
]),
Select::make('similar_past_matches')
->prefixIconColor('primary')
->prefixIcon('phosphor-approximate-equals')
->suffixAction(
Action::make('iconInfo')
->color('gray')
->icon('phosphor-question')
->tooltip(fn () => $this->tooltip_similar_past_matches())
)
->default('none')
->live()
->selectablePlaceholder(false)
->afterStateUpdated(function (callable $set) {
$set('team_id', null);
$set('tm_player_id', null);
})
->options([
'none' => 'None',
'league' => 'League',
'all' => 'All Database',
]),
Slider::make('accuracy')
->label('Accuracy max: 0.25, steps: 0.025')
->hiddenLabel()
->disabled(fn (Get $get) => $get('similar_past_matches') == 'none')
// ->fillTrack()
->range(minValue: 0, maxValue: 0.25)
->step(0.025)
->decimalPlaces(1)
->decimalPlaces(3)
->tooltips()
// ->pips()
->live(),
Select::make('tm_player_id')
->label('Player')
->disabled(fn (Get $get) => $get('similar_past_matches') != 'none')
->prefixIconColor(fn (Get $get): string => $get('similar_past_matches') == 'none' ? 'primary' : '')
->prefixIcon('icon-player')
->suffixAction(
Action::make('copyCostToPrice')
->action(function (callable $set) {
$set('tm_player_id', null);
$set('player_on_field', null);
})
->color('gray')
->icon('phosphor-x'),
)
->preload()
->options(function (Get $get) {
return StatistikoDetail::where('txodds_id', $get('team_id'))->pluck('name', 'tm_player_id');
})
->live(),
Select::make('player_on_field')
->label('Time on field')
->prefixIcon('phosphor-watch')
->default('all')
->disabled(fn (Get $get) => ! $get('tm_player_id') or $get('similar_past_matches') != 'none')
->prefixIconColor(fn (Get $get): string => ($get('tm_player_id') and $get('similar_past_matches') == 'none') ? 'primary' : '')
->selectablePlaceholder(false)
->options([
'all' => 'All',
'lineup' => 'Lineup',
'substituted' => 'Substituted',
])
->live(),
]),
]);
}
public function getFooterWidgetsColumns(): int|array
{
return [
'default' => 1,
'md' => 2,
// 'lg' => 3,
];
}
protected function getHeaderWidgets(): array
{
return [
// Events
// ChartEvent::make(),
// ChartEventMoAv::make(),
// Players
// ChartPlayer::make(),
// ChartPlayerMoAv::make(),
];
}
protected function getFooterWidgets(): array
{
return [
StatsMedianAverage::make(),
// Events
ChartEvent::make(),
ChartEventMoAv::make(),
ChartEventPerf::make(),
ChartEventPerfMoAv::make(),
// Players
ChartPlayer::make(),
ChartPlayerMoAv::make(),
// ChartPlayerPerf::make(),
// ChartPlayerPerfMoAv::make(),
];
}
protected function tooltip_similar_past_matches(): HtmlString
{
$str = '<div class="grid grid-cols-4 gap-4 max-w-lg">';
$str .= '<div class="font-bold">None</div>';
$str .= '<div class="col-span-3">Only selected team within selected league is considered.</div>';
$str .= '<div class="font-bold">League</div>';
$str .= '<div class="col-span-3">All events in seleceted league are considered, filtered expected goal handicap and total parameter in the range of <span class="text-blue-500">Accuracy Slider</span> setting.</div>';
$str .= '<div class="font-bold">All Database</div>';
$str .= '<div class="col-span-3">Eintire database is considered, filtered expected goal handicap and total parameter in the range of the <br><span class="text-blue-500">Accuracy Slider</span> setting.</div>';
$str .= '</div>';
return new HtmlString($str);
}
}