Files
nuhodds_fi4/app/Filament/Widgets/Data/LoadEvents.php
T
2026-05-27 11:50:29 +02:00

394 lines
14 KiB
PHP

<?php
namespace App\Filament\Widgets\Data;
use App\Models\StatisticFixtures;
use App\Models\StatistikoDetail;
use App\Models\StatistikoEvent;
use App\Models\StatistikoPlayer;
class LoadEvents
{
public static $last_query = [];
public static function load($filter)
{
// 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;
$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 (! $team_id && $similar_past_matches != 'none') {
return [];
}
if ($statistic_type == 'none') {
$statistic_type = 'gls';
}
/**
* IF WITH SIMILAR_PAST_MATCHES
*
* 'none' => 'None',
* 'league' => 'League',
* 'all' => 'All Database',
*/
$handicap_range = 0;
$total_range = 0;
if ($similar_past_matches != 'none' && isset($res)) {
$res = StatisticFixtures::where('id', (int) $match_id)->first();
$exp_handicap = $res->exp_away - $res->exp_home;
$exp_total = $res->exp_away + $res->exp_home;
$handicap_range = [$exp_handicap - $accuracy, $exp_handicap + $accuracy];
$total_range = [$exp_total - $accuracy, $exp_total + $accuracy];
}
/**
* 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').')';
}
/**
* 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);
})
->orderBy('starttime', 'ASC')
->get();
if ($res == null) {
return [];
}
$dataset_home = [];
$dataset_away = [];
$results_per_event = [];
$labels = [];
$events = [];
$labels_since = [];
// win in favor (w) against (w2)
// draw in favor (d) against (d2)
// lost in favor (l) against (l2)
$results = ['w' => 0, 'd' => 0, 'l' => 0, 'w2' => 0, 'l2' => 0];
/**
* IF WITH PLAYER
*/
// $res = StatisticFixtures::with('home')->with('away')->where('id', (int) $match_id)->first();
// $event = $res->home->name.' vs '.$res->away->name;
// dd($event);
foreach ($res as $value) {
$labels[] = $value['starttime']->format('d.M.Y H:i');
$results_per_event[] = $value['gls_h'].' : '.$value['gls_a'];
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'];
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;
}
$results_per_event[] = $val_home.' : '.$val_away;
} 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'];
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['crds_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'];
} else {
$val_home = (int) $value['gls_a'];
$val_away = (int) $value['gls_h'];
}
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;
}
}
$data = [
'datasets' => [
[
'label' => 'In Favour',
'events' => $events,
'results_per_event' => $results_per_event,
'data' => $dataset_home,
'fill' => false,
'tension' => 0.5,
'borderColor' => '#36A2EB',
],
[
'label' => 'Against',
'events' => $events,
'results_per_event' => $results_per_event,
'data' => $dataset_away,
'fill' => false,
'tension' => 0.5,
'borderColor' => '#7ccf00',
],
],
'labels' => $labels,
'description' => $description ?? '',
'results' => $results,
];
self::$last_query = $data;
// dd($data);
return $data;
}
}