Files
2026-10-04 15:25:03 +02:00

92 lines
2.4 KiB
PHP

<?php
namespace App\Filament\Widgets\Charts;
use App\Models\StatistikoDetail;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadPlayerPerformance
{
public static $last_query = [];
public static function load($filter, bool $hasdots)
{
// if (self::$last_query) {
// return self::$last_query;
// }
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
$statistic_type = $filter['statistic_type'] ?? 'gls';
$player_on_field = $filter['player_on_field'];
if (! $league_id) {
return [];
}
if (! $team_id) {
return [];
}
if (! $ss_player_id) {
return [];
}
$player_name = StatistikoDetail::where('ss_player_id', (int) $ss_player_id)->value('player_name');
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
$matches_played = StatistikoPlayer::with('event')
->where('ss_player_id', (int) $ss_player_id)
->when($player_on_field, function ($query) use ($player_on_field) {
switch ($player_on_field) {
case 'lineup':
return $query->where('lineup', 'T');
case 'substituted':
return $query->where('lineup', 'P');
}
})->get();
$tmp = [];
foreach ($matches_played as $value) {
$val = $value->playerrating_value;
$key = $value->event ? round($value->event->starttime->diffInDays(Carbon::now())) : 0;
$tmp["$key"] = $val;
}
krsort($tmp);
$dataset = [];
$labels = [];
foreach ($tmp as $key => $value) {
$labels[] = $key;
$dataset[] = $value;
}
// $labels = array_keys($dataset);
$data = [
'datasets' => [
[
'label' => 'Player Rating',
'data' => $dataset,
'tension' => 0.5,
'borderColor' => '#36A2EB',
'pointStyle' => $hasdots,
],
],
'labels' => $labels,
'player_name' => $player_name,
];
self::$last_query = $data;
return $data;
}
}