Files
2026-10-15 16:29:48 +02:00

663 lines
26 KiB
PHP

<?php
namespace App\Filament\Widgets\Charts;
use App\Filament\Pages\Selections\Helper\GetSeason;
use App\Models\SofaScoreRosa;
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_id = (int) $filter['team_id'] ?? 0;
$team_position = $filter['team_position'];
$similar_past_matches = $filter['similar_past_matches'];
$accuracy = (float) $filter['accuracy'] ?? 0.5;
if (! $league_id) {
return [];
}
if (! $match_id) {
return [];
}
if (! $team_id && $similar_past_matches == 'none') {
return [];
}
$start = $filter['start_date'] ?? 0;
$end = $filter['end_date'] ?? 0;
$season_or_date = $filter['season_or_date'];
$season = $filter['season'];
$season_mode = $filter['season_mode'];
if ($season_or_date == 'season') {
$start = 0;
$end = 0;
}
$season = GetSeason::load($season, $season_mode);
$team_map = SofaScoreRosa::pluck('ss_team_name', 'ss_team_id')->toArray();
$match = StatisticFixtures::where('id', $match_id)->first();
if (! $match) {
return [];
}
$home_id = $match->home_id ?? 0;
$away_id = $match->away_id ?? 0;
$handicap_range = [];
$total_range = [];
if ($similar_past_matches != 'none') {
$team_position = '';
$exp_handicap = $match->exp_away - $match->exp_home;
$exp_total = $match->exp_away + $match->exp_home;
$handicap_range = [$exp_handicap - $accuracy, $exp_handicap + $accuracy];
$total_range = [$exp_total - $accuracy, $exp_total + $accuracy];
}
// $res = LoadEventsQuery::load($filter, $handicap_range, $total_range, $match, 1);
// $res = StatistikoEvent::where('league_id', $league_id)->get();
$res = StatistikoEvent::where(function ($query) use ($league_id, $similar_past_matches) {
switch ($similar_past_matches) {
case 'none':
case 'league':
return $query->where('league_id', $league_id);
}
})
->where(function ($query) use ($home_id, $away_id, $similar_past_matches) {
if ($similar_past_matches == 'none') {
return $query->where('home_id', $home_id)->orWhere('away_id', $home_id)->orWhere('home_id', $away_id)->orWhere('away_id', $away_id);
}
})
->when($handicap_range, function ($query) use ($handicap_range) {
return $query->whereBetween('exp_handicap', $handicap_range);
})->when($total_range, function ($query) use ($total_range) {
return $query->whereBetween('exp_total', $total_range);
})
->when($start, function ($query) use ($start) {
return $query->whereDate('starttime', '>=', $start);
})->when($end, function ($query) use ($end) {
return $query->whereDate('starttime', '<=', $end);
})
->when($season, function ($query) use ($season, $season_or_date, $similar_past_matches) {
if ($similar_past_matches == 'none' && $season_or_date == 'season') {
return $query->whereIn('season', $season);
}
})
->orderBy('starttime', 'ASC')
->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('Y-m-d H:i');
$results_home[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$home_matches[] = $val->match_id;
}
if ($val->away_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('Y-m-d H:i');
$results_away[] = $val['gls_h'].' : '.$val['gls_a'];
$events_away[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$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('Y-m-d H:i');
$results_home[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$home_matches[] = $val->match_id;
}
if ($val->home_id == $away_id) {
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
$date_away[] = $val['starttime']->format('Y-m-d H:i');
$results_away[] = $val['gls_h'].' : '.$val['gls_a'];
$events_away[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$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('Y-m-d H:i');
$results_home[] = $val['gls_h'].' : '.$val['gls_a'];
$events_home[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$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('Y-m-d H:i');
$results_away[] = $val['gls_h'].' : '.$val['gls_a'];
$events_away[] = $team_map[$val->home_id].' vs '.$team_map[$val->away_id];
$away_matches[] = $val->match_id;
}
break;
}
}
$res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'ss_player_id', 'activeteam_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$homes = self::filterMatches($res, $home_matches, $home_id);
// dd($homes['all']);
$res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'ss_player_id', 'activeteam_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$aways = self::filterMatches($res, $away_matches, $away_id);
// dd($aways['all']);
$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', 'ss_player_id', 'activeteam_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
$league = self::filterMatches($res, $league_matches);
$league_av = self::leagueAv($league, $length);
$data = [
'datasets' => [
[
/**
* HOME
*/
'label' => $team_map[$match->home_id],
'playing_date' => $date_home,
'results_per_event' => $results_home,
'events' => $events_home,
'data' => $homes['all'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#2b7fff',
'backgroundColor' => '#2b7fff',
'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' => '#2b7fff',
'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' => '#8ec5ff',
'backgroundColor' => '#8ec5ff',
'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' => '#8ec5ff',
'backgroundColor' => 'transparent',
'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' => '#8ec5ff',
'backgroundColor' => '#8ec5ff',
'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' => '#8ec5ff',
'backgroundColor' => 'transparent',
'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' => '#8ec5ff',
'backgroundColor' => '#8ec5ff',
'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' => '#8ec5ff',
'backgroundColor' => 'transparent',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
/**
* AWAY
*/
[
'label' => $team_map[$match->away_id],
'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' => false,
],
[
'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',
'fill' => false,
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => false,
],
[
'label' => 'Defence',
'playing_date' => $date_away,
'results_per_event' => $results_away,
'events' => $events_away,
'data' => $aways['def'] ?? [],
'spanGaps' => true,
'tension' => 0.5,
'borderColor' => '#7bf1a8',
'backgroundColor' => '#7bf1a8',
'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' => '#7bf1a8',
'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' => '#7bf1a8',
'backgroundColor' => '#7bf1a8',
'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' => '#7bf1a8',
'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' => '#7bf1a8',
'backgroundColor' => '#7bf1a8',
'fill' => false,
'pointStyle' => $hasdots,
'hidden' => true,
],
/**
* AVERAGE
*/
[
'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' => '#ffb900',
'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' => '#ffb900',
'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' => '#ffb900',
'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' => '#ffb900',
'pointStyle' => false,
'borderDash' => [5, 5],
'hidden' => true,
],
],
'labels' => $labels,
];
return $data;
}
public static function filterMatches($res, array $matches, $team_id = 0)
{
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']['ply'] = 0;
$result[$match_id]['all']['sum_min'] = 0;
$result[$match_id]['all']['rating'] = 0;
$result[$match_id]['att']['ply'] = 0;
$result[$match_id]['att']['sum_min'] = 0;
$result[$match_id]['att']['rating'] = 0;
$result[$match_id]['mid']['ply'] = 0;
$result[$match_id]['mid']['sum_min'] = 0;
$result[$match_id]['mid']['rating'] = 0;
$result[$match_id]['def']['ply'] = 0;
$result[$match_id]['def']['sum_min'] = 0;
$result[$match_id]['def']['rating'] = 0;
}
if ($team_id == 0 || $value->activeteam_id == $team_id) {
if ($value->minutes_played > 0) {
$result[$match_id]['all']['ply']++;
$result[$match_id]['all']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['all']['rating'] += (float) $value->playerrating_value;
if ($value->player_position == 'M') {
$result[$match_id]['mid']['ply']++;
$result[$match_id]['mid']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['mid']['rating'] += (float) $value->playerrating_value;
} elseif ($value->player_position == 'F') {
$result[$match_id]['att']['ply']++;
$result[$match_id]['att']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['att']['rating'] += (float) $value->playerrating_value;
} elseif ($value->player_position == 'D') {
$result[$match_id]['def']['ply']++;
$result[$match_id]['def']['sum_min'] += (int) $value->minutes_played;
$result[$match_id]['def']['rating'] += (float) $value->playerrating_value;
}
}
}
}
$return = [];
foreach ($matches as $matchid) {
if (isset($result[$matchid])) {
if (! $result[$matchid]['all']['ply']) {
$result[$matchid]['all']['ply'] = 1;
}
$return['all'][] = round($result[$matchid]['all']['rating'] / $result[$matchid]['all']['ply'], 1);
if ($result[$matchid]['def']['sum_min']) {
$return['def'][] = round($result[$matchid]['def']['rating'] / $result[$matchid]['def']['ply'], 1);
} else {
$return['def'][] = null;
}
if ($result[$matchid]['att']['sum_min']) {
$return['att'][] = round($result[$matchid]['att']['rating'] / $result[$matchid]['att']['ply'], 1);
} else {
$return['att'][] = null;
}
if ($result[$matchid]['mid']['sum_min']) {
$return['mid'][] = round($result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['ply'], 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;
}
}