Files
nuhodds_fi4/app/Filament/Widgets/Data/LoadPerformance.php
T
2026-08-03 20:53:41 +02:00

591 lines
24 KiB
PHP

<?php
namespace App\Filament\Widgets\Data;
use App\Models\StatisticFixtures;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadPerformance
{
public static $last_query = [];
public static $events_data = [];
public static function load(array $filter, bool $hasdots)
{
$league_id = (int) $filter['league_id'] ?? 0;
$match_id = (int) $filter['match_id'] ?? 0;
$team_position = $filter['team_position'];
$team_id = (int) $filter['team_id'] ?? 0;
if (! $league_id) {
return [];
}
if (! $match_id) {
return [];
}
$match = StatisticFixtures::with('home')->with('away')->where('id', $match_id)->first();
if (! $match) {
return [];
}
$home_id = $match->home_id ?? 0;
$away_id = $match->away_id ?? 0;
$res = StatistikoEvent::where('league_id', $league_id)->get();
$home_matches = [];
$away_matches = [];
$league_matches = [];
$date_home = [];
$date_away = [];
$events_home = [];
$events_away = [];
$results_home = [];
$results_away = [];
$labels = [];
foreach ($res as $val) {
$league_matches[] = $val->match_id;
switch ($team_position) {
case 'home':
if ($val->home_id == $home_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_home[] = $val['starttime']->format('d. F Y H:i');
$results_home[] = $val['gls_h'] . ' : ' . $val['gls_a'];
$events_home[] = $val->home->ss_team_name . ' vs ' . $val->away->ss_team_name;
$home_matches[] = $val->match_id;
}
if ($val->away_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('d. F Y H:i');
$results_away[] = $val['gls_h'] . ' : ' . $val['gls_a'];
$events_away[] = $val->home->ss_team_name . ' vs ' . $val->away->ss_team_name;
$away_matches[] = $val->match_id;
}
break;
case 'away':
if ($val->away_id == $home_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_home[] = $val['starttime']->format('d. F Y H:i');
$results_home[] = $val['gls_h'] . ' : ' . $val['gls_a'];
$events_home[] = $val->home->name . ' vs ' . $val->away->name;
$home_matches[] = $val->match_id;
}
if ($val->home_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('d. F Y H:i');
$results_away[] = $val['gls_h'] . ' : ' . $val['gls_a'];
$events_away[] = $val->home->ss_team_name . ' vs ' . $val->away->ss_team_name;
$away_matches[] = $val->match_id;
}
break;
default:
if ($val->home_id == $home_id or $val->away_id == $home_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_home[] = $val['starttime']->format('d. F Y H:i');
$results_home[] = $val['gls_h'] . ' : ' . $val['gls_a'];
$events_home[] = $val->home->ss_team_name . ' vs ' . $val->away->ss_team_name;
$home_matches[] = $val->match_id;
}
if ($val->home_id == $away_id or $val->away_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('d. F Y H:i');
$results_away[] = $val['gls_h'] . ' : ' . $val['gls_a'];
$events_away[] = $val->home->ss_team_name . ' vs ' . $val->away->ss_team_name;
$away_matches[] = $val->match_id;
}
break;
}
}
// dd($home_matches);
$res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$homes = self::filterMatches($res, $home_matches);
// dd($homes);
$res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$aways = self::filterMatches($res, $away_matches);
$c_home = isset($homes['all']) ? count($homes['all']) : 0;
$c_away = isset($aways['all']) ? count($aways['all']) : 0;
$length = max($c_home, $c_away);
self::shortenEnding($labels, $homes, $aways);
$home_av = self::makeAv($homes, $length);
$away_av = self::makeAv($aways, $length);
$res = StatistikoPlayer::whereIn('match_id', $league_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$league = self::filterMatches($res, $league_matches);
$league_av = self::leagueAv($league, $length);
$data = [
'datasets' => [
[
'label' => $match->home->name,
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => '#fb2c36',
'pointStyle' => $hasdots,
],
[
'label' => 'Average Team',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['all'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => '#fb2c36',
'pointStyle' => false,
'borderDash' => [5, 5],
],
[
'label' => 'Defence',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#ff6900',
'backgroundColor' => '#ff6900',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Defence',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['def'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#ff6900',
'backgroundColor' => '#ff6900',
'pointStyle' => false,
'hidden' => true,
'borderDash' => [5, 5],
],
[
'label' => 'Attak',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#ff6900',
'backgroundColor' => '#ff6900',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Attak',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#ff6900',
'backgroundColor' => '#ff6900',
'fill' => false,
'pointStyle' => false,
'hidden' => true,
'borderDash' => [5, 5],
],
[
'label' => 'Midfield',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#ff6900',
'backgroundColor' => '#ff6900',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Midfield',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $home_av['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#ff6900',
'backgroundColor' => '#ff6900',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => $match->away->name,
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#00c950',
'backgroundColor' => '#00c950',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Team',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#00c950',
'backgroundColor' => '#00c950',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Defence',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#009966',
'backgroundColor' => '#009966',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Defence',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#009966',
'backgroundColor' => '#009966',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Attak',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#009966',
'backgroundColor' => '#009966',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Attak',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['att'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#009966',
'backgroundColor' => '#009966',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Midfield',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#009966',
'backgroundColor' => '#009966',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
[
'label' => 'Average Midfield',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $away_av['mid'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#009966',
'backgroundColor' => '#009966',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average League',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['all'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#00a6f4',
'backgroundColor' => '#00a6f4',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average Defence',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['def'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#155dfc',
'backgroundColor' => '#155dfc',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average Attak',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['att'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#155dfc',
'backgroundColor' => '#155dfc',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
[
'label' => 'Average Midfield',
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $league_av['mid'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#155dfc',
'backgroundColor' => '#155dfc',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
],
'labels' => $labels,
];
return $data;
}
public static function filterMatches($res, array $matches)
{
if (!$res->count()) {
return ['all', 'def', 'att', 'mid'];
}
$result = [];
foreach ($res as $value) {
$match_id = $value->match_id;
if (! isset($result[$match_id])) {
$result[$match_id]['all']['sum_min'] = 0;
$result[$match_id]['all']['rating'] = 0;
$result[$match_id]['att']['sum_min'] = 0;
$result[$match_id]['att']['rating'] = 0;
$result[$match_id]['mid']['sum_min'] = 0;
$result[$match_id]['mid']['rating'] = 0;
$result[$match_id]['def']['sum_min'] = 0;
$result[$match_id]['def']['rating'] = 0;
}
$result[$match_id]['all']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['all']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
if ($value->player_position == 'M') {
$result[$match_id]['mid']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['mid']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
} elseif ($value->player_position == 'F') {
$result[$match_id]['att']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['att']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
} elseif ($value->player_position == 'D') {
$result[$match_id]['def']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['def']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
}
}
$return = [];
foreach ($matches as $matchid) {
if (isset($result[$matchid])) {
$return['all'][] = round($result[$matchid]['all']['rating'] / $result[$matchid]['all']['sum_min'], 1);
if ($result[$matchid]['def']['sum_min']) {
$return['def'][] = round($result[$matchid]['def']['rating'] / $result[$matchid]['def']['sum_min'], 1);
} else {
$return['def'][] = null;
}
if ($result[$matchid]['att']['sum_min']) {
$return['att'][] = round($result[$matchid]['att']['rating'] / $result[$matchid]['att']['sum_min'], 1);
} else {
$return['att'][] = null;
}
if ($result[$matchid]['mid']['sum_min']) {
$return['mid'][] = round($result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['sum_min'], 1);
} else {
$return['mid'][] = null;
}
} else {
$return['all'][] = null;
$return['def'][] = null;
$return['att'][] = null;
$return['mid'][] = null;
}
}
// dd($return);
// while (array_last($return['all']) == null) {
// }
return $return;
}
public static function shortenEnding(array &$labels, array &$homes, array &$aways)
{
// dd($labels, $homes, $aways);
if (! isset($homes['all'])) {
return;
}
$count_home = 0;
$count_away = 0;
if (isset($homes['all'])) {
for ($i = \count($homes['all']) - 1; $i > 0; $i--) {
if ($homes['all'][$i] == null) {
$count_home++;
} else {
break;
}
}
}
if (isset($aways['all'])) {
for ($i = \count($aways['all']) - 1; $i > 0; $i--) {
if ($aways['all'][$i] == null) {
$count_away++;
} else {
break;
}
}
}
$keeps = count($homes['all']) - min($count_home, $count_away) - 1;
if (isset($homes['all'])) {
$homes['all'] = array_slice($homes['all'], 0, $keeps);
$homes['def'] = array_slice($homes['def'], 0, $keeps);
$homes['att'] = array_slice($homes['att'], 0, $keeps);
$homes['mid'] = array_slice($homes['mid'], 0, $keeps);
}
if (isset($aways['all'])) {
$aways['all'] = array_slice($aways['all'], 0, $keeps);
$aways['def'] = array_slice($aways['def'], 0, $keeps);
$aways['att'] = array_slice($aways['att'], 0, $keeps);
$aways['mid'] = array_slice($aways['mid'], 0, $keeps);
}
// $labels = array_slice($labels, 0, $keeps);
$labels = range(1, $keeps);
}
public static function makeAv(array &$arr, int $length)
{
if (isset($arr['all']) && count($arr['all'])) {
$all = isset($arr['all']) ? array_sum($arr['all']) / count($arr['all']) : 1;
$def = isset($arr['def']) ? array_sum($arr['def']) / count($arr['def']) : 1;
$mid = isset($arr['mid']) ? array_sum($arr['mid']) / count($arr['mid']) : 1;
$att = isset($arr['att']) ? array_sum($arr['att']) / count($arr['att']) : 1;
$ret = [];
$ret['all'] = array_fill(0, $length, round($all, 2));
$ret['def'] = array_fill(0, $length, round($def, 2));
$ret['mid'] = array_fill(0, $length, round($mid, 2));
$ret['att'] = array_fill(0, $length, round($att, 2));
} else {
$ret = [];
$ret['all'] = [];
$ret['def'] = [];
$ret['mid'] = [];
$ret['att'] = [];
}
return $ret;
}
public static function leagueAv(array &$arr, int $len)
{
if (isset($arr['all'])) {
$all = array_sum($arr['all']) / count($arr['all']);
$def = array_sum($arr['def']) / count($arr['def']);
$mid = array_sum($arr['mid']) / count($arr['mid']);
$att = array_sum($arr['att']) / count($arr['att']);
} else {
$all = 0;
$def = 0;
$mid = 0;
$att = 0;
}
$ret = [];
$ret['all'] = array_fill(0, $len, round($all, 2));
$ret['def'] = array_fill(0, $len, round($def, 2));
$ret['mid'] = array_fill(0, $len, round($mid, 2));
$ret['att'] = array_fill(0, $len, round($att, 2));
return $ret;
}
}