From 3743b72f71f19bc6f4addec8a3b96a70e006083b Mon Sep 17 00:00:00 2001 From: salam Date: Thu, 21 May 2026 18:12:30 +0200 Subject: [PATCH] wip --- app/Actions/Statistics/LoadEvents.php | 25 -- app/Actions/Statistics/LoadEventsAspGoal.php | 279 +++++++++++++++++++ app/Filament/Widgets/MedianAverageWidget.php | 4 +- 3 files changed, 281 insertions(+), 27 deletions(-) create mode 100644 app/Actions/Statistics/LoadEventsAspGoal.php diff --git a/app/Actions/Statistics/LoadEvents.php b/app/Actions/Statistics/LoadEvents.php index e0bc9bda..d13ae0cd 100644 --- a/app/Actions/Statistics/LoadEvents.php +++ b/app/Actions/Statistics/LoadEvents.php @@ -50,15 +50,6 @@ class LoadEvents break; } - $accuracy = (float) $filter['accuracy']; - - $exp_home = $res['exp_home'] ?? 0; - $exp_home_from = $exp_home - $accuracy; - $exp_home_to = $exp_home + $accuracy; - $exp_away = $res['exp_away'] ?? 0; - $exp_away_from = $exp_away - $accuracy; - $exp_away_to = $exp_away + $accuracy; - $start = $filter['start_date'] ?? 0; $end = $filter['end_date'] ?? 0; @@ -67,10 +58,6 @@ class LoadEvents $statistic_type = $filter['statistic_type'] ?? 0; - // 'none' => 'None', - // 'league' => 'League', - // 'all' => 'All Database', - /** * STATTISTIC DATA */ @@ -78,7 +65,6 @@ class LoadEvents case 'home': $res = StatistikoEvent::where('league_id', $league_id) ->where('home_id', '=', $team_id) - // ->whereBetween('exp_home', [$exp_home_from, $exp_home_to]) ->when($start, function ($query) use ($start) { return $query->where('starttime', '>=', $start); })->when($end, function ($query) use ($end) { @@ -90,7 +76,6 @@ class LoadEvents case 'away': $res = StatistikoEvent::where('league_id', $league_id) ->where('away_id', '=', $team_id) - // ->whereBetween('exp_away', [$exp_away_from, $exp_away_to]) ->when($start, function ($query) use ($start) { return $query->where('starttime', '>=', $start); })->when($end, function ($query) use ($end) { @@ -104,9 +89,6 @@ class LoadEvents ->where(function ($query) use ($team_id) { $query->where('home_id', '=', $team_id)->orWhere('away_id', '=', $team_id); }) - // ->where(function ($query) use ($exp_home_from, $exp_home_to, $exp_away_from, $exp_away_to) { - // $query->whereBetween('exp_home', [$exp_home_from, $exp_home_to])->orWhereBetween('exp_away', [$exp_away_from, $exp_away_to]); - // }) ->when($start, function ($query) use ($start) { return $query->where('starttime', '>=', $start); })->when($end, function ($query) use ($end) { @@ -126,13 +108,6 @@ class LoadEvents foreach ($res as $value) { $labels[] = $value['starttime']->format('d. M.y'); - // dd($filter); - - // $tmp['exp_handicap'] = (float) $value['exp_handicap']; - // $tmp['exp_total'] = (float) $value['exp_total']; - // $tmp['exp_away'] = (float) $value['exp_away']; - // $tmp['exp_home'] = (float) $value['exp_home']; - if ($team_position == 'home') { switch ($statistic_type) { diff --git a/app/Actions/Statistics/LoadEventsAspGoal.php b/app/Actions/Statistics/LoadEventsAspGoal.php new file mode 100644 index 00000000..78c2fadd --- /dev/null +++ b/app/Actions/Statistics/LoadEventsAspGoal.php @@ -0,0 +1,279 @@ +where('home_id', $team_id)->first(); + break; + + case 'away': + $res = StatisticFixtures::where('league_id', $league_id)->where('away_id', $team_id)->first(); + break; + + default: + $res = StatisticFixtures::where('league_id', $league_id) + ->where(function ($query) use ($team_id) { + $query->where('home_id', '=', $team_id)->orWhere('away_id', '=', $team_id); + })->first(); + break; + } + + $start = $filter['start_date'] ?? 0; + + $end = $filter['end_date'] ?? 0; + + $accuracy = (float) $filter['accuracy']; + + $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]; + + $similar_past_matches = $filter['similar_past_matches'] ?? 0; + + switch ($similar_past_matches) { + case 'league': + $team_id = false; + break; + case 'all': + $league_id = false; + $team_id = false; + break; + default: + // none + $total_range = false; + $handicap_range = false; + break; + + } + + /** + * STATTISTIC DATA + */ + switch ($filter['team_position']) { + case 'home': + $res = StatistikoEvent::when($league_id, function ($query) use ($league_id) { + return $query->where('league_id', $league_id); + })->when($team_id, function ($query) use ($team_id) { + return $query->where('home_id', $team_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->where('starttime', '>=', $start); + })->when($end, function ($query) use ($end) { + return $query->where('starttime', '<=', $end); + }) + ->get(); + break; + + case 'away': + $res = StatistikoEvent::when($league_id, function ($query) use ($league_id) { + return $query->where('league_id', $league_id); + })->when($team_id, function ($query) use ($team_id) { + return $query->where('away_id', $team_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->where('starttime', '>=', $start); + })->when($end, function ($query) use ($end) { + return $query->where('starttime', '<=', $end); + }) + ->get(); + break; + + default: + $res = StatistikoEvent::when($league_id, function ($query) use ($league_id) { + return $query->where('league_id', $league_id); + })->when($team_id, function ($query) use ($team_id) { + $query->where('home_id', '=', $team_id)->orWhere('away_id', '=', $team_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->where('starttime', '>=', $start); + })->when($end, function ($query) use ($end) { + return $query->where('starttime', '<=', $end); + }) + ->get(); + + break; + } + + $datasets = []; + $labels = []; + + $statistic_type = $filter['statistic_type']; + if (! $statistic_type || $statistic_type == 'none') { + $statistic_type = 'gls'; + } + + foreach ($res as $value) { + $labels[] = $value['starttime']->format('d. M.y'); + + // dd($filter); + + // $tmp['exp_handicap'] = (float) $value['exp_handicap']; + // $tmp['exp_total'] = (float) $value['exp_total']; + // $tmp['exp_away'] = (float) $value['exp_away']; + // $tmp['exp_home'] = (float) $value['exp_home']; + + if ($team_position == 'home') { + + switch ($statistic_type) { + case 'gls': + $val = (int) $value['gls_h']; + break; + case 'crn': + $val = (int) $value['crn_h']; + break; + case 'fls': + $val = (int) $value['fls_h']; + break; + case 'ofs': + $val = (int) $value['ofs_h']; + break; + case 'crd': + $val = (int) $value['crd_h']; + break; + case 'snt': + $val = (int) $value['snt_h']; + break; + case 'tsh': + $val = (int) $value['tsh_h']; + break; + } + } elseif ($team_position == 'away') { + + switch ($statistic_type) { + case 'gls': + $val = (int) $value['gls_a']; + break; + case 'crn': + $val = (int) $value['crn_a']; + break; + case 'fls': + $val = (int) $value['fls_a']; + break; + case 'ofs': + $val = (int) $value['ofs_a']; + break; + case 'crd': + $val = (int) $value['crd_a']; + break; + case 'snt': + $val = (int) $value['snt_a']; + break; + case 'tsh': + $val = (int) $value['tsh_a']; + break; + } + } else { + + switch ($statistic_type) { + case 'gls': + if ($team_id == $value['home_id']) { + $val = (int) $value['gls_a']; + } else { + $val = (int) $value['gls_h']; + } + break; + case 'crn': + if ($team_id == $value['home_id']) { + $val = (int) $value['crn_a']; + } else { + $val = (int) $value['crn_h']; + } + break; + case 'fls': + if ($team_id == $value['home_id']) { + $val = (int) $value['fls_a']; + } else { + $val = (int) $value['fls_h']; + } + break; + case 'ofs': + if ($team_id == $value['home_id']) { + $val = (int) $value['ofs_a']; + } else { + $val = (int) $value['ofs_h']; + } + break; + case 'crd': + if ($team_id == $value['home_id']) { + $val = (int) $value['crd_a']; + } else { + $val = (int) $value['crd_h']; + } + break; + case 'snt': + if ($team_id == $value['home_id']) { + $val = (int) $value['snt_a']; + } else { + $val = (int) $value['snt_h']; + } + break; + case 'tsh': + if ($team_id == $value['home_id']) { + $val = (int) $value['tsh_a']; + } else { + $val = (int) $value['tsh_h']; + } + break; + } + } + + $datasets[] = $val; + } + + $data = [ + 'datasets' => [ + [ + 'label' => ucfirst($team_position), + 'data' => $datasets, + 'fill' => true, + 'tension' => 0.5, + 'borderColor' => '#36A2EB', + ], + ], + 'labels' => $labels, + ]; + + self::$last_query = $data; + + return $data; + } +} diff --git a/app/Filament/Widgets/MedianAverageWidget.php b/app/Filament/Widgets/MedianAverageWidget.php index 37c435be..a5b9ef7f 100644 --- a/app/Filament/Widgets/MedianAverageWidget.php +++ b/app/Filament/Widgets/MedianAverageWidget.php @@ -2,7 +2,7 @@ namespace App\Filament\Widgets; -use App\Actions\Statistics\LoadEvents; +use App\Actions\Statistics\LoadEventsAspGoal; use Filament\Widgets\Concerns\InteractsWithPageFilters; use Filament\Widgets\StatsOverviewWidget; use Filament\Widgets\StatsOverviewWidget\Stat; @@ -13,7 +13,7 @@ class MedianAverageWidget extends StatsOverviewWidget protected function getStats(): array { - $data = LoadEvents::load($this->filters); + $data = LoadEventsAspGoal::load($this->filters); if (! isset($data['datasets'][0])) { return [];