Files
nuhodds_fi4/app/Filament/Widgets/Charts/LoadPlayerWidgets.php
T
2026-10-15 16:29:48 +02:00

184 lines
5.9 KiB
PHP

<?php
namespace App\Filament\Widgets\Charts;
use App\Filament\Pages\Selections\Helper\GetSeason;
use App\Models\StatistikoDetail;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadPlayerWidgets
{
public static $last_query = [];
public static function load(array $filter, bool $hasdots)
{
// dd($filter);
// if (self::$last_query) {
// return self::$last_query;
// }
$team_position = $filter['team_position'];
$league_id = (int) $filter['league_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
$player_on_field = $filter['player_on_field'] ?? 'all';
$season = $filter['season'];
$season_mode = $filter['season_mode'];
$season = GetSeason::load($season, $season_mode);
// dd($player_on_field);
$statistic_type = $filter['statistic_type'] ?? 'gls';
$statistic_name = HelperStatisticType::get($filter);
if (! $league_id) {
return [];
}
if (! $team_id) {
return [];
}
if (! $ss_player_id) {
return [];
}
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
$player_name = StatistikoDetail::where('ss_player_id', (int) $ss_player_id)->value('player_name');
$matches_season = StatistikoEvent::whereIn('season', $season)
->where(function ($query) use ($team_id, $team_position) {
switch ($team_position) {
case 'home':
return $query->where('home_id', '=', $team_id);
case 'away':
return $query->where('away_id', '=', $team_id);
default:
return $query->where('home_id', '=', $team_id)
->orWhere('away_id', '=', $team_id);
}
})->pluck('match_id')->toArray();
// dd($matches_season);
$matches_played = StatistikoPlayer::query()
->whereIn('match_id', $matches_season)
->where('ss_player_id', $ss_player_id)
->when($player_on_field, function ($query) use ($player_on_field) {
// 'all' => 'All',
// 'lineup' => 'Lineup',
// 'substituted' => 'Substituted',
// 'notplayed' => 'Not Played',
switch ($player_on_field) {
case 'all':
return $query->where('minutes_played', '>', 0);
case 'lineup':
return $query->where('lineup', 'T')->where('minutes_played', '>', 0);
case 'substituted':
return $query->where('lineup', 'P')->where('minutes_played', '>', 0);
default:
return $query->where('minutes_played', '>', 0);
}
})
->pluck('match_id')->toArray();
$res = StatistikoPlayer::with('event')
->where('ss_player_id', (int) $ss_player_id)
->when($matches_played, function ($query) use ($matches_played, $player_on_field) {
if ($player_on_field == 'notplayed') {
return $query->whereNotIn('match_id', $matches_played);
} else {
return $query->whereIn('match_id', $matches_played);
}
})->get();
$tmp = [];
foreach ($res as $value) {
$val = 0;
$key = 0;
if (isset($value->event->starttime)) {
$key = (int) round($value->event->starttime->diffInDays(Carbon::now()));
switch ($statistic_type) {
case 'gls':
$val = (int) $value->goals;
break;
case 'crn':
$val = (int) 0;
break;
case 'fls':
$val = (int) $value->fouls;
break;
case 'ofs':
$val = (int) $value->offsides;
break;
case 'crd':
$val = (int) $value->card_yellow + $value->card_red + $value->card_yellowred;
break;
case 'snt':
$val = (int) $value->shots_on_target;
break;
case 'tsh':
$val = (int) $value->shots_off_target;
break;
}
$tmp[$key] = $val;
}
}
krsort($tmp);
$dataset = [];
$labels = [];
foreach ($tmp as $key => $value) {
$labels[] = $key;
$dataset[] = $value;
}
$avverage = array_sum($dataset) / count($dataset);
$dataset2 = array_fill(0, count($dataset), $avverage);
$data = [
'datasets' => [
[
'label' => ucfirst($statistic_name),
'data' => $dataset,
'tension' => 0.5,
'borderColor' => '#36A2EB',
'pointStyle' => $hasdots,
],
],
'labels' => $labels,
'player_name' => $player_name,
];
$data['datasets'][] = [
'label' => 'Average',
'data' => $dataset2,
'tension' => 0.5,
'borderColor' => '#ad46ff',
'backgroundColor' => '#ad46ff',
'borderDash' => [5, 5],
'pointStyle' => false,
'hidden' => false,
'average_team_home' => [],
'average_team_away' => [],
];
self::$last_query = $data;
return $data;
}
}