115 lines
5.6 KiB
PHP
115 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets\Boxes;
|
|
|
|
use App\Filament\Pages\Selections\Helper\GetSeason;
|
|
use App\Filament\Widgets\Boxes\Helper\ExpectationAverage;
|
|
use App\Models\StatistikoEvent;
|
|
|
|
class AverageTeam
|
|
{
|
|
public static function load(array &$expectations, array $filter)
|
|
{
|
|
$tournament_id = (int) $filter['league_id'] ?? 0;
|
|
$team_position = $filter['team_position'];
|
|
$team_id = (int) $filter['team_id'] ?? 0;
|
|
$season = $filter['season'] ?? 0;
|
|
$season_or_date = $filter['season_or_date'] ?? 'season';
|
|
$start = $filter['start_date'] ?? 0;
|
|
$end = $filter['end_date'] ?? 0;
|
|
$seasonmode = $filter['seasonmode'];
|
|
|
|
if ($season_or_date != 'season') {
|
|
$season = 0;
|
|
} else {
|
|
$end = 0;
|
|
$start = 0;
|
|
$season = GetSeason::load($season, $seasonmode);
|
|
}
|
|
|
|
$active_season = GetSeason::active($seasonmode);
|
|
|
|
// Average made and got for team selected
|
|
// Box 1
|
|
// for selected Date or Time or All
|
|
// Box 2
|
|
// fixed to active season
|
|
// Box 3 wie 2
|
|
// fixed to active season but all league
|
|
$res = StatistikoEvent::where('league_id', $tournament_id)
|
|
->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) {
|
|
return $query->whereIn('season', $season);
|
|
})->when($team_id, function ($query) use ($team_position, $team_id) {
|
|
switch ($team_position) {
|
|
case 'home':
|
|
return $query->where('home_id', $team_id);
|
|
case 'away':
|
|
return $query->where('away_id', $team_id);
|
|
default:
|
|
return $query->orWhere('home_id', $team_id)
|
|
->orWhere('away_id', '=', $team_id);
|
|
}
|
|
})->get();
|
|
|
|
$result = [];
|
|
if ($res) {
|
|
|
|
// 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 $val) {
|
|
|
|
if ($team_position == 'home' && $team_id == $val->home_id) {
|
|
ExpectationAverage::filter($result, 'team', 'gls', $val->gls_h, $val->gls_a);
|
|
ExpectationAverage::filter($result, 'team', 'fls', $val->fls_h, $val->fls_a);
|
|
ExpectationAverage::filter($result, 'team', 'ofs', $val->ofs_h, $val->ofs_a);
|
|
ExpectationAverage::filter($result, 'team', 'crd', $val->crd_h, $val->crd_a);
|
|
ExpectationAverage::filter($result, 'team', 'snt', $val->snt_h, $val->snt_a);
|
|
ExpectationAverage::filter($result, 'team', 'tsh', $val->tsh_h, $val->tsh_a);
|
|
} elseif ($team_position == 'away' && $team_id == $val->away_id) {
|
|
ExpectationAverage::filter($result, 'team', 'gls', $val->gls_a, $val->gls_h);
|
|
ExpectationAverage::filter($result, 'team', 'fls', $val->fls_a, $val->fls_h);
|
|
ExpectationAverage::filter($result, 'team', 'ofs', $val->ofs_a, $val->ofs_h);
|
|
ExpectationAverage::filter($result, 'team', 'crd', $val->crd_a, $val->crd_h);
|
|
ExpectationAverage::filter($result, 'team', 'snt', $val->snt_a, $val->snt_h);
|
|
ExpectationAverage::filter($result, 'team', 'tsh', $val->tsh_a, $val->tsh_h);
|
|
} else {
|
|
ExpectationAverage::filter($result, 'team', 'gls', $val->gls_a, $val->gls_h);
|
|
ExpectationAverage::filter($result, 'team', 'fls', $val->fls_a, $val->fls_h);
|
|
ExpectationAverage::filter($result, 'team', 'ofs', $val->ofs_a, $val->ofs_h);
|
|
ExpectationAverage::filter($result, 'team', 'crd', $val->crd_a, $val->crd_h);
|
|
ExpectationAverage::filter($result, 'team', 'snt', $val->snt_a, $val->snt_h);
|
|
ExpectationAverage::filter($result, 'team', 'tsh', $val->tsh_a, $val->tsh_h);
|
|
}
|
|
}
|
|
|
|
$bets = ['gls', 'fls', 'ofs', 'crd', 'snt', 'tsh'];
|
|
if ($result) {
|
|
foreach ($bets as $bet) {
|
|
$expectations[$bet.'_team']['home'] = round(array_sum($result['team'][$bet]['home']) / \count($result['team'][$bet]['home']), 1);
|
|
$expectations[$bet.'_team']['away'] = round(array_sum($result['team'][$bet]['away']) / \count($result['team'][$bet]['away']), 1);
|
|
}
|
|
} else {
|
|
foreach ($bets as $bet) {
|
|
$expectations[$bet.'_team']['home'] = 0;
|
|
$expectations[$bet.'_team']['away'] = 0;
|
|
}
|
|
}
|
|
$expectations['gls_team']['name'] = 'Goals';
|
|
$expectations['fls_team']['name'] = 'Fouls';
|
|
$expectations['ofs_team']['name'] = 'Offsides';
|
|
$expectations['crd_team']['name'] = 'Cards';
|
|
$expectations['snt_team']['name'] = 'Shots on Target';
|
|
$expectations['tsh_team']['name'] = 'Total Shots';
|
|
}
|
|
}
|
|
}
|