Files
nuhodds_fi4/app/Filament/Widgets/Data/LoadPerformance.php
T
2026-05-30 17:18:21 +02:00

238 lines
8.7 KiB
PHP

<?php
namespace App\Filament\Widgets\Data;
use App\Models\StatisticFixtures;
use App\Models\StatistikoDetail;
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)
{
// dd($filter);
$league_id = (int) $filter['league_id'] ?? 0;
$match_id = (int) $filter['match_id'] ?? 0;
$tm_player_id = (int) $filter['tm_player_id'] ?? 0;
if (! $league_id) {
return [];
}
if (! $match_id) {
return [];
}
$match = StatisticFixtures::with('home')->with('away')->where('id', $match_id)->first();
$home_id = $match->home_id;
$away_id = $match->away_id;
$res = StatistikoEvent::where('league_id', $league_id)->get();
$home_matches = [];
$away_matches = [];
$labels = [];
foreach ($res as $val) {
if ($val->home_id == $home_id or $val->away_id == $home_id or $val->home_id == $away_id or $val->away_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$label_formatted[$val->match_id] = $val->starttime->format('d. F Y H:i');
if ($val->home_id == $home_id or $val->away_id == $home_id) {
$home_matches[] = $val->match_id;
} else {
$home_matches[] = null;
}
if ($val->home_id == $away_id or $val->away_id == $away_id) {
$away_matches[] = $val->match_id;
} else {
$away_matches[] = null;
}
}
}
// dd($away_matches);
$res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$homes = self::filterMatches($res, $home_matches);
$res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$aways = self::filterMatches($res, $away_matches);
$data = [
'datasets' => [
[
'label' => $match->home->name,
'data' => $homes['all'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => '#fb2c36',
],
[
'label' => 'Defence',
'data' => $homes['def'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#c10007',
'backgroundColor' => '#c10007',
'fill' => false,
],
[
'label' => 'Attak',
'data' => $homes['att'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#c10007',
'backgroundColor' => '#c10007',
'fill' => false,
],
[
'label' => 'Midfield',
'data' => $homes['mid'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#c10007',
'backgroundColor' => '#c10007',
'fill' => false,
],
[
'label' => $match->away->name,
'data' => $aways['all'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#fdc700',
'backgroundColor' => '#fdc700',
'fill' => false,
],
[
'label' => 'Defence',
'data' => $aways['def'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#d08700',
'backgroundColor' => '#d08700',
'fill' => false,
],
[
'label' => 'Attak',
'data' => $aways['att'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#d08700',
'backgroundColor' => '#d08700',
'fill' => false,
],
[
'label' => 'Midfield',
'data' => $aways['mid'],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#d08700',
'backgroundColor' => '#d08700',
'fill' => false,
],
],
'labels' => $labels,
];
return $data;
/**
* IF WITH PLAYER
*/
// $matches_played = 0;
// $description = '';
// if ($tm_player_id) {
// $matches_played = StatistikoPlayer::where('tm_player_id', (int) $tm_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');
// }
// })
// ->pluck('match_id')->toArray();
// if (! $matches_played) {
// return [];
// }
// $description = ' (with '.StatistikoDetail::where('tm_player_id', (int) $tm_player_id)->value('name').')';
// }
}
public static function filterMatches($res, $matches)
{
$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'] += $value->minutes_played;
$result[$match_id]['all']['rating'] += $value->playerrating_value * $value->minutes_played;
if ($value->player_position == 'M') {
$result[$match_id]['mid']['sum_min'] += $value->minutes_played;
$result[$match_id]['mid']['rating'] += $value->playerrating_value * $value->minutes_played;
} elseif ($value->player_position == 'F') {
$result[$match_id]['att']['sum_min'] += $value->minutes_played;
$result[$match_id]['att']['rating'] += $value->playerrating_value * $value->minutes_played;
} elseif ($value->player_position == 'D') {
$result[$match_id]['def']['sum_min'] += $value->minutes_played;
$result[$match_id]['def']['rating'] += $value->playerrating_value * $value->minutes_played;
}
}
$return = [];
foreach ($matches as $matchid) {
if (isset($result[$matchid])) {
$return['all'][] = $result[$matchid]['all']['rating'] / $result[$matchid]['all']['sum_min'];
if ($result[$matchid]['def']['sum_min']) {
$return['def'][] = $result[$matchid]['def']['rating'] / $result[$matchid]['def']['sum_min'];
} else {
$return['def'][] = null;
}
if ($result[$matchid]['att']['sum_min']) {
$return['att'][] = $result[$matchid]['att']['rating'] / $result[$matchid]['att']['sum_min'];
} else {
$return['att'][] = null;
}
if ($result[$matchid]['mid']['sum_min']) {
$return['mid'][] = $result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['sum_min'];
} else {
$return['mid'][] = null;
}
} else {
$return['all'][] = null;
$return['def'][] = null;
$return['att'][] = null;
$return['mid'][] = null;
}
}
return $return;
}
}