Files
nuhodds_fi4/app/Filament/Widgets/Data/LoadEvents.php
T
2026-06-18 12:15:43 +02:00

704 lines
28 KiB
PHP

<?php
namespace App\Filament\Widgets\Data;
use App\Models\CompetitionGroup;
use App\Models\StatisticFixtures;
use App\Models\StatistikoDetail;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
use Illuminate\Support\Carbon;
class LoadEvents
{
public static $last_query = [];
public static function load(array $filter, bool $hasdots)
{
// dd($filter);
// if (self::$last_query) {
// return self::$last_query;
// }
$league_id = (int) $filter['league_id'] ?? 0;
$match_id = (int) $filter['match_id'] ?? 0;
$team_id = (int) $filter['team_id'] ?? 0;
$season_or_date = $filter['season_or_date'] ?? 'season';
$season = $filter['season'] ?? 0;
$tm_player_id = (int) $filter['tm_player_id'] ?? 0;
$start = $filter['start_date'] ?? 0;
$end = $filter['end_date'] ?? 0;
$statistic_type = $filter['statistic_type'];
$team_position = $filter['team_position'];
$player_on_field = $filter['player_on_field'];
$accuracy = (float) $filter['accuracy'] ?? 0.5;
$similar_past_matches = $filter['similar_past_matches'];
if (! $league_id) {
return [];
}
if (! $match_id) {
return [];
}
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
switch ($season_or_date) {
case 'season':
$start = 0;
$end = 0;
break;
default:
$season = 0;
break;
}
/**
* IF WITH SIMILAR_PAST_MATCHES
*
* 'none' => 'None',
* 'league' => 'League',
* 'all' => 'All Database',
*/
$handicap_range = 0;
$total_range = 0;
if ($similar_past_matches != 'none') {
$team_position = '';
$match = StatisticFixtures::with('home')->with('away')->where('id', (int) $match_id)->first();
$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];
$home_team = $match->home->name;
$away_team = $match->away->name;
} else {
$home_team = 'In Favour';
$away_team = 'Against';
}
/**
* 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');
case 'notplayed':
return $query->where('minutes_played', 0);
}
})
->pluck('match_id')->toArray();
// dd($matches_played);
$description = ' with ' . StatistikoDetail::where('tm_player_id', (int) $tm_player_id)->value('name') . ' (' . \count($matches_played) . ')';
if (! $matches_played) {
$matches_played[] = -1;
}
}
/**
* STATTISTIC DATA
*/
$res = StatistikoEvent::with('home')->with('away')
->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 ($team_id, $team_position, $similar_past_matches) {
if ($similar_past_matches == 'none') {
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);
}
}
})->when($matches_played, function ($query) use ($matches_played) {
return $query->whereIn('match_id', $matches_played);
})->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->where('starttime', '>=', $start);
})->when($end, function ($query) use ($end) {
return $query->where('starttime', '<=', $end);
})->when($season , function ($query) use ($season) {
return $query->whereIn('season', $season);
})
->orderBy('starttime', 'ASC')
->get();
if ($res == null) {
return [];
}
$dataset_home = [];
$dataset_away = [];
$dataset_exp_home = [];
$dataset_exp_away = [];
$results_per_event = [];
$playing_date = [];
$labels = [];
$events = [];
$results = ['w' => 0, 'd' => 0, 'l' => 0, 'w2' => 0, 'l2' => 0];
foreach ($res as $value) {
// dd($value);
$labels[] = round($value['starttime']->diffInDays(Carbon::now()));
$playing_date[] = $value['starttime']->format('d. F Y H:i');
$results_per_event[] = $value['gls_h'] . ' : ' . $value['gls_a'];
if ($similar_past_matches != 'none') {
switch ($value['result_code']) {
case '1':
$results['w']++;
$results['l2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['l']++;
$results['w2']++;
break;
}
switch ($statistic_type) {
case 'gls':
$val_home = (int) $value['gls_h'];
$val_away = (int) $value['gls_a'];
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
break;
case 'crn':
$val_home = (int) $value['crn_h'];
$val_away = (int) $value['crn_a'];
break;
case 'fls':
$val_home = (int) $value['fls_h'];
$val_away = (int) $value['fls_a'];
break;
case 'ofs':
$val_home = (int) $value['ofs_h'];
$val_away = (int) $value['ofs_a'];
break;
case 'crd':
$val_home = (int) $value['crd_h'];
$val_away = (int) $value['crd_a'];
break;
case 'snt':
$val_home = (int) $value['snt_h'];
$val_away = (int) $value['snt_a'];
break;
case 'tsh':
$val_home = (int) $value['tsh_h'];
$val_away = (int) $value['tsh_a'];
break;
}
} else {
if ($team_position == 'home') {
switch ($value['result_code']) {
case '1':
$results['w']++;
$results['l2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['l']++;
$results['w2']++;
break;
}
switch ($statistic_type) {
case 'gls':
$val_home = (int) $value['gls_h'];
$val_away = (int) $value['gls_a'];
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
break;
case 'crn':
$val_home = (int) $value['crn_h'];
$val_away = (int) $value['crn_a'];
break;
case 'fls':
$val_home = (int) $value['fls_h'];
$val_away = (int) $value['fls_a'];
break;
case 'ofs':
$val_home = (int) $value['ofs_h'];
$val_away = (int) $value['ofs_a'];
break;
case 'crd':
$val_home = (int) $value['crd_h'];
$val_away = (int) $value['crd_a'];
break;
case 'snt':
$val_home = (int) $value['snt_h'];
$val_away = (int) $value['snt_a'];
break;
case 'tsh':
$val_home = (int) $value['tsh_h'];
$val_away = (int) $value['tsh_a'];
break;
}
} elseif ($team_position == 'away') {
switch ($value['result_code']) {
case '1':
$results['l']++;
$results['w2']++;
break;
case 'X':
$results['d']++;
break;
case '2':
$results['w']++;
$results['l2']++;
break;
}
switch ($statistic_type) {
case 'gls':
$val_home = (int) $value['gls_a'];
$val_away = (int) $value['gls_h'];
$dataset_exp_home[] = (float) round($value['exp_away'], 1);
$dataset_exp_away[] = (float) round($value['exp_home'], 1);
break;
case 'crn':
$val_home = (int) $value['crn_a'];
$val_away = (int) $value['crn_h'];
break;
case 'fls':
$val_home = (int) $value['fls_a'];
$val_away = (int) $value['fls_h'];
break;
case 'ofs':
$val_home = (int) $value['ofs_a'];
$val_away = (int) $value['ofs_h'];
break;
case 'crd':
$val_home = (int) $value['crd_a'];
$val_away = (int) $value['crd_h'];
break;
case 'snt':
$val_home = (int) $value['snt_a'];
$val_away = (int) $value['snt_h'];
break;
case 'tsh':
$val_home = (int) $value['tsh_a'];
$val_away = (int) $value['tsh_h'];
break;
}
} else {
switch ($value['result_code']) {
case '1':
if ($team_id == $value['home_id']) {
$results['l2']++;
$results['w']++;
} else {
$results['w2']++;
$results['l']++;
}
break;
case 'X':
$results['d']++;
break;
case '2':
if ($team_id == $value['home_id']) {
$results['l']++;
$results['w2']++;
} else {
$results['l2']++;
$results['w']++;
}
break;
}
switch ($statistic_type) {
case 'gls':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['gls_h'];
$val_away = (int) $value['gls_a'];
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
} else {
$val_home = (int) $value['gls_a'];
$val_away = (int) $value['gls_h'];
$dataset_exp_home[] = (float) round($value['exp_away'], 1);
$dataset_exp_away[] = (float) round($value['exp_home'], 1);
}
break;
case 'crn':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['crn_h'];
$val_away = (int) $value['crn_a'];
} else {
$val_home = (int) $value['crn_a'];
$val_away = (int) $value['crn_h'];
}
break;
case 'fls':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['fls_h'];
$val_away = (int) $value['fls_a'];
} else {
$val_home = (int) $value['fls_a'];
$val_away = (int) $value['fls_h'];
}
break;
case 'ofs':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['ofs_h'];
$val_away = (int) $value['ofs_a'];
} else {
$val_home = (int) $value['ofs_a'];
$val_away = (int) $value['ofs_h'];
}
break;
case 'crd':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['crd_h'];
$val_away = (int) $value['crd_a'];
} else {
$val_home = (int) $value['crd_a'];
$val_away = (int) $value['crd_h'];
}
break;
case 'snt':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['snt_h'];
$val_away = (int) $value['snt_a'];
} else {
$val_home = (int) $value['snt_a'];
$val_away = (int) $value['snt_h'];
}
break;
case 'tsh':
if ($team_id == $value['home_id']) {
$val_home = (int) $value['tsh_h'];
$val_away = (int) $value['tsh_a'];
} else {
$val_home = (int) $value['tsh_a'];
$val_away = (int) $value['tsh_h'];
}
break;
}
}
}
if (isset($val_home) && isset($val_away)) {
$events[] = $value->home->name . ' vs ' . $value->away->name;
$dataset_home[] = $val_home;
$dataset_away[] = $val_away;
}
}
if ($similar_past_matches != 'none') {
$label1 = 'Home';
$label2 = 'Away';
} else {
$label1 = 'In Favour';
$label2 = 'Against';
}
self::shortenEnding($labels, $dataset_home, $dataset_away);
$data = [
'datasets' => [
[
'label' => $label1,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_home,
'tension' => 0.5,
'borderColor' => '#fb2c36',
'backgroundColor' => '#fb2c36',
'pointStyle' => $hasdots,
'fill' => [
'target' => 1,
'above' => '#fb2c3680',
'below' => '#fdc70080',
],
],
[
'label' => $label2,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_away,
'tension' => 0.5,
'borderColor' => '#fdc700',
'backgroundColor' => '#fdc700',
'pointStyle' => $hasdots,
'fill' => false,
'hidden' => true,
],
],
'labels' => $labels,
'description' => $description ?? '',
'results' => $results,
'home_team' => $home_team,
'away_team' => $away_team,
'similar_past_matches' => $similar_past_matches,
];
if ($statistic_type == 'gls') {
$data['datasets'][] = [
'label' => 'Goals ' . $label1,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_exp_home,
'tension' => 0.5,
'borderColor' => '#00c950',
'backgroundColor' => '#00c950',
'pointStyle' => $hasdots,
'hidden' => true,
];
$data['datasets'][] = [
'label' => 'Goals ' . $label2,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_exp_away,
'tension' => 0.5,
'borderColor' => '#2b7fff',
'backgroundColor' => '#2b7fff',
'pointStyle' => $hasdots,
'hidden' => true,
];
}
/**
* LEAGUE AVERAGES
*/
if ($similar_past_matches == 'none') {
$res = CompetitionGroup::with('country')->where('id', $league_id)->first();
$season_mode = $res->country->season_mode;
if ($season_mode == 'fullyear') {
$season = date('Y');
} else {
$season = date('y') - 1 . '/' . date('Y');
}
$res = StatistikoEvent::where('league_id', $league_id)
->where('season', $season)
->orderBy('starttime', 'ASC')
->get();
if ($res) {
$media_team_h = [];
$media_team_a = [];
$media_league_h = [];
$media_league_a = [];
// 1) la media dei falli di tutte le partite giocate dal Flamengo
// (esempio, giocate 6 partite con falli 17-11-15-14-15-12, la media è 14)
// 2) la media dei falli di tutte le partite giocate nel campionato Brasiliano
// (esempio 6 giornate giocate = 60 partite quindi 120 squadre, quindi 120 dati tipo 17-12-12-15-8-15-13-21-...,
// quindi la media di questi 120 dati)
foreach ($res as $value) {
if ($team_id == $value['home_id'] or $team_id == $value['away_id']) {
switch ($statistic_type) {
case 'gls':
$media_team_h[] = (int) $value['gls_h'];
$media_team_a[] = (int) $value['gls_a'];
break;
case 'crn':
$media_team_h[] = (int) $value['crn_h'];
$media_team_a[] = (int) $value['crn_a'];
break;
case 'fls':
$media_team_h[] = (int) $value['fls_h'];
$media_team_a[] = (int) $value['fls_a'];
break;
case 'ofs':
$media_team_h[] = (int) $value['ofs_h'];
$media_team_a[] = (int) $value['ofs_a'];
break;
case 'crd':
$media_team_h[] = (int) $value['crd_h'];
$media_team_a[] = (int) $value['crd_a'];
break;
case 'snt':
$media_team_h[] = (int) $value['snt_h'];
$media_team_a[] = (int) $value['snt_a'];
break;
case 'tsh':
$media_team_h[] = (int) $value['tsh_h'];
$media_team_a[] = (int) $value['tsh_a'];
break;
}
}
switch ($statistic_type) {
case 'gls':
$media_league_h[] = (int) $value['gls_h'];
$media_league_a[] = (int) $value['gls_a'];
break;
case 'crn':
$media_league_h[] = (int) $value['crn_h'];
$media_league_a[] = (int) $value['crn_a'];
break;
case 'fls':
$media_league_h[] = (int) $value['fls_h'];
$media_league_a[] = (int) $value['fls_a'];
break;
case 'ofs':
$media_league_h[] = (int) $value['ofs_h'];
$media_league_a[] = (int) $value['ofs_a'];
break;
case 'crd':
$media_league_h[] = (int) $value['crd_h'];
$media_league_a[] = (int) $value['crd_a'];
break;
case 'snt':
$media_league_h[] = (int) $value['snt_h'];
$media_league_a[] = (int) $value['snt_a'];
break;
case 'tsh':
$media_league_h[] = (int) $value['tsh_h'];
$media_league_a[] = (int) $value['tsh_a'];
break;
}
}
$average_team = 0;
$average_league = 0;
if ($team_id && $media_team_h){
$sum = array_sum($media_team_h) + array_sum($media_team_a);
$count = count($media_team_h) + count($media_team_a);
$average_team = round($sum / $count, 2);
$average_team_h = round(array_sum($media_team_h) / count($media_team_h), 2);
$average_team_a = round(array_sum($media_team_a) / count($media_team_a), 2);
$average_league_h = $media_league_h ? round(array_sum($media_league_h) / \count($media_league_h), 2):0;
$average_league_a = $media_league_a ? round(array_sum($media_league_a) / \count($media_league_a), 2):0;
$media_league = array_merge($media_league_a, $media_league_h);
$average_league = $media_league ? round(array_sum($media_league) / \count($media_league), 2) : 0;
}
$data_team = array_fill(0, count($labels), $average_team);
$data_league = array_fill(0, count($labels), $average_league);
if ($average_team) {
$data['datasets'][] = [
'label' => 'Average Team',
'events' => $events,
'data' => $data_team,
'tension' => 0.5,
'borderColor' => '#ad46ff',
'backgroundColor' => '#ad46ff',
'borderDash' => [5, 5],
'pointStyle' => false,
'hidden' => false,
'average_team_h' => $average_team_h,
'average_team_a' => $average_team_a,
];
}
if ($average_league){
$data['datasets'][] = [
'label' => 'Average League',
'events' => $events,
'data' => $data_league,
'tension' => 0.5,
'borderColor' => '#00bba7',
'backgroundColor' => '#00bba7',
'borderDash' => [5, 5],
'pointStyle' => false,
'hidden' => false,
'average_league_h' => $average_league_h,
'average_league_a' => $average_league_a,
];
}
}
}
self::$last_query = $data;
// dd($data);
return $data;
}
public static function shortenEnding(array &$labels, array &$homes, array &$aways)
{
if (! isset($homes['all'])) {
return;
}
$count_home = 0;
$count_away = 0;
for ($i = \count($homes) - 1; $i > 0; $i--) {
if ($homes[$i] == null) {
$count_home++;
} else {
break;
}
}
for ($i = \count($aways) - 1; $i > 0; $i--) {
if ($aways[$i] == null) {
$count_away++;
} else {
break;
}
}
$keeps = count($homes['all']) - min($count_home, $count_away) - 1;
$homes = array_slice($homes, 0, $keeps);
$aways = array_slice($aways, 0, $keeps);
$labels = array_slice($labels, 0, $keeps);
}
}