Files
nuhodds_fi4/app/Filament/Widgets/Data/LoadEvents.php
T
2026-08-29 08:44:30 +02:00

407 lines
14 KiB
PHP

<?php
namespace App\Filament\Widgets\Data;
use App\Filament\Widgets\Data\LoadEvents\LoadEventsAverages;
use App\Filament\Widgets\Data\LoadEvents\LoadEventsQuery;
use App\Filament\Widgets\Data\LoadEvents\LoadExpectations;
use App\Models\StatisticFixtures;
use Illuminate\Support\Carbon;
class LoadEvents
{
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;
$statistic_type = $filter['statistic_type'] ?? 'gls';
$team_position = $filter['team_position'];
$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';
}
/**
* IF WITH SIMILAR_PAST_MATCHES
*
* 'none' => 'None',
* 'league' => 'League',
* 'all' => 'All Database',
*/
$handicap_range = [];
$total_range = [];
$match = StatisticFixtures::with('home')->with('away')->where('id', (int) $match_id)->first();
if (! $match) {
return [];
}
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];
$home_team = $match->home->ss_team_name;
$away_team = $match->away->ss_team_name;
} else {
$home_team = 'In Favour';
$away_team = 'Against';
}
/**
* STATTISTIC DATA
*/
$res = LoadEventsQuery::load($filter, $handicap_range, $total_range, $match);
$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];
if (!$team_id || !\in_array($team_position, ['home', 'away'])) {
$team_position = 'all';
}
if (!isset($res['all'])) {
return [];
}
$expectations = LoadExpectations::load($res['all'], $match, $statistic_type);
if ($team_id) {
foreach ($res[$team_position] as $value) {
$labels[] = round($value['starttime']->diffInDays(Carbon::now()));
$playing_date[] = $value['starttime']->format('Y-m-d H:i');
$results_per_event[] = $value['val_home'] . ' : ' . $value['val_away'];
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;
}
$val_home = (int) $value['val_home'];
$val_away = (int) $value['val_away'];
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
}
} 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;
}
$val_home = (int) $value['val_home'];
$val_away = (int) $value['val_away'];
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_home'], 1);
$dataset_exp_away[] = (float) round($value['exp_away'], 1);
}
} 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;
}
$val_home = (int) $value['val_away'];
$val_away = (int) $value['val_home'];
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_away'], 1);
$dataset_exp_away[] = (float) round($value['exp_home'], 1);
}
} else {
// dd($team_id, $value['home_id'], $value['away_id']);
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;
}
$val_home = (int) $value['val_away'];
$val_away = (int) $value['val_home'];
if ($statistic_type == 'gls') {
$dataset_exp_home[] = (float) round($value['exp_away'], 1);
$dataset_exp_away[] = (float) round($value['exp_home'], 1);
}
}
}
if (isset($val_home) && isset($val_away)) {
$events[] = $value['home'] . ' vs ' . $value['away'];
$dataset_home[] = $val_home;
$dataset_away[] = $val_away;
}
}
if ($similar_past_matches != 'none') {
$label1 = 'Home';
$label2 = 'Away';
} else {
$label1 = 'In Favour';
$label2 = 'Against';
}
} else {
$label1 = $match->home->ss_team_name . ' +';
$label2 = $match->home->ss_team_name . ' -';
$label3 = $match->away->ss_team_name . ' +';
$label4 = $match->away->ss_team_name . ' -';
$dataset_home2 = [];
$dataset_away2 = [];
foreach ($res[$team_position] as $value) {
$labels[] = round($value['starttime']->diffInDays(Carbon::now()));
// Home playing HOME done and commited
if ($match->home_id == $value['home_id']) {
$dataset_home[] = (int) $value['val_home'];
$dataset_away[] = (int) $value['val_away'];
} else if ($match->home_id == $value['away_id']) {
$dataset_home[] = (int) $value['val_away'];
$dataset_away[] = (int) $value['val_home'];
}
// Home playing AWAY done and commited
else if ($match->away_id == $value['home_id']) {
$dataset_home2[] = (int) $value['val_home'];
$dataset_away2[] = (int) $value['val_away'];
} else if ($match->away_id == $value['away_id']) {
$dataset_home2[] = (int) $value['val_away'];
$dataset_away2[] = (int) $value['val_home'];
}
}
$n = max(count($dataset_home), count($dataset_away), count($dataset_away2), count($dataset_home2));
$f = array_first($labels);
$step = round($f / $n);
$labels = [];
for ($i = $f; $i >= 0; $i -= $step) {
$labels[] = $i;
}
}
// dd($events, count($dataset_home), count($dataset_away), count($dataset_home2), count($dataset_away2));
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,
'expectations_home' => $expectations['home'],
'expectations_away' => $expectations['away'],
];
if ($team_id && $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,
];
}
if (isset($dataset_home2) && isset($dataset_away2)) {
$data['datasets'][] = [
'label' => $label3,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_home2,
'tension' => 0.5,
'borderColor' => '#00c950',
'backgroundColor' => '#00c950',
'pointStyle' => $hasdots,
'hidden' => true,
];
$data['datasets'][] = [
'label' => $label4,
'events' => $events,
'playing_date' => $playing_date,
'results_per_event' => $results_per_event,
'data' => $dataset_away2,
'tension' => 0.5,
'borderColor' => '#2b7fff',
'backgroundColor' => '#2b7fff',
'pointStyle' => $hasdots,
'hidden' => true,
];
}
// Log::debug($results);
/**
* LEAGUE AVERAGES
*/
if ($team_id && $similar_past_matches == 'none') {
LoadEventsAverages::load($data, $filter, $labels, $events);
}
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);
}
}