266 lines
9.7 KiB
PHP
266 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets\Data;
|
|
|
|
use App\Models\StatisticFixtures;
|
|
use App\Models\StatistikoEvent;
|
|
use App\Models\StatistikoPlayer;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class LoadPerformance
|
|
{
|
|
public static $last_query = [];
|
|
|
|
public static $events_data = [];
|
|
|
|
public static function load(array $filter, bool $hasdots)
|
|
{
|
|
// dd($filter);
|
|
|
|
$league_id = (int) $filter['league_id'] ?? 0;
|
|
|
|
$match_id = (int) $filter['match_id'] ?? 0;
|
|
|
|
$tm_player_id = (int) $filter['tm_player_id'] ?? 0;
|
|
|
|
if (! $league_id) {
|
|
return [];
|
|
}
|
|
if (! $match_id) {
|
|
return [];
|
|
}
|
|
|
|
$match = StatisticFixtures::with('home')->with('away')->where('id', $match_id)->first();
|
|
$home_id = $match->home_id;
|
|
$away_id = $match->away_id;
|
|
|
|
$res = StatistikoEvent::where('league_id', $league_id)->get();
|
|
|
|
$home_matches = [];
|
|
$away_matches = [];
|
|
$labels = [];
|
|
foreach ($res as $val) {
|
|
if ($val->home_id == $home_id or $val->away_id == $home_id or $val->home_id == $away_id or $val->away_id == $away_id) {
|
|
|
|
$labels[] = round($val->starttime->diffInDays(Carbon::now()));
|
|
|
|
$label_formatted[$val->match_id] = $val->starttime->format('d. F Y H:i');
|
|
|
|
if ($val->home_id == $home_id or $val->away_id == $home_id) {
|
|
$home_matches[] = $val->match_id;
|
|
} else {
|
|
$home_matches[] = null;
|
|
}
|
|
if ($val->home_id == $away_id or $val->away_id == $away_id) {
|
|
$away_matches[] = $val->match_id;
|
|
} else {
|
|
$away_matches[] = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// dd($away_matches);
|
|
|
|
$res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
|
|
$homes = self::filterMatches($res, $home_matches);
|
|
|
|
$res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value', 'player_position')->get();
|
|
$aways = self::filterMatches($res, $away_matches);
|
|
|
|
self::shortenEnding($labels, $homes, $aways);
|
|
|
|
$data = [
|
|
'datasets' => [
|
|
[
|
|
'label' => $match->home->name,
|
|
'data' => $homes['all'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#fb2c36',
|
|
'backgroundColor' => '#fb2c36',
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => 'Defence',
|
|
'data' => $homes['def'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#c10007',
|
|
'backgroundColor' => '#c10007',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => 'Attak',
|
|
'data' => $homes['att'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#c10007',
|
|
'backgroundColor' => '#c10007',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => 'Midfield',
|
|
'data' => $homes['mid'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#c10007',
|
|
'backgroundColor' => '#c10007',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => $match->away->name,
|
|
'data' => $aways['all'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#fdc700',
|
|
'backgroundColor' => '#fdc700',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => 'Defence',
|
|
'data' => $aways['def'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#d08700',
|
|
'backgroundColor' => '#d08700',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => 'Attak',
|
|
'data' => $aways['att'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#d08700',
|
|
'backgroundColor' => '#d08700',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
[
|
|
'label' => 'Midfield',
|
|
'data' => $aways['mid'] ?? [],
|
|
'spanGaps' => true,
|
|
'tension' => 0.5,
|
|
'borderColor' => '#d08700',
|
|
'backgroundColor' => '#d08700',
|
|
'fill' => false,
|
|
'pointStyle' => $hasdots,
|
|
],
|
|
],
|
|
'labels' => $labels,
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function filterMatches($res, array $matches)
|
|
{
|
|
if (!$res->count()) {
|
|
return [];
|
|
}
|
|
$result = [];
|
|
foreach ($res as $value) {
|
|
|
|
$match_id = $value->match_id;
|
|
|
|
if (! isset($result[$match_id])) {
|
|
$result[$match_id]['all']['sum_min'] = 0;
|
|
$result[$match_id]['all']['rating'] = 0;
|
|
$result[$match_id]['att']['sum_min'] = 0;
|
|
$result[$match_id]['att']['rating'] = 0;
|
|
$result[$match_id]['mid']['sum_min'] = 0;
|
|
$result[$match_id]['mid']['rating'] = 0;
|
|
$result[$match_id]['def']['sum_min'] = 0;
|
|
$result[$match_id]['def']['rating'] = 0;
|
|
}
|
|
|
|
$result[$match_id]['all']['sum_min'] += (int) $value->minutes_played;
|
|
$result[$match_id]['all']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
|
|
|
|
if ($value->player_position == 'M') {
|
|
$result[$match_id]['mid']['sum_min'] += (int) $value->minutes_played;
|
|
$result[$match_id]['mid']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
|
|
} elseif ($value->player_position == 'F') {
|
|
$result[$match_id]['att']['sum_min'] += (int) $value->minutes_played;
|
|
$result[$match_id]['att']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
|
|
} elseif ($value->player_position == 'D') {
|
|
$result[$match_id]['def']['sum_min'] += (int) $value->minutes_played;
|
|
$result[$match_id]['def']['rating'] += (int) $value->playerrating_value * (int) $value->minutes_played;
|
|
}
|
|
}
|
|
|
|
$return = [];
|
|
foreach ($matches as $matchid) {
|
|
if (isset($result[$matchid])) {
|
|
$return['all'][] = $result[$matchid]['all']['rating'] / $result[$matchid]['all']['sum_min'];
|
|
if ($result[$matchid]['def']['sum_min']) {
|
|
$return['def'][] = $result[$matchid]['def']['rating'] / $result[$matchid]['def']['sum_min'];
|
|
} else {
|
|
$return['def'][] = null;
|
|
}
|
|
if ($result[$matchid]['att']['sum_min']) {
|
|
$return['att'][] = $result[$matchid]['att']['rating'] / $result[$matchid]['att']['sum_min'];
|
|
} else {
|
|
$return['att'][] = null;
|
|
}
|
|
if ($result[$matchid]['mid']['sum_min']) {
|
|
$return['mid'][] = $result[$matchid]['mid']['rating'] / $result[$matchid]['mid']['sum_min'];
|
|
} else {
|
|
$return['mid'][] = null;
|
|
}
|
|
} else {
|
|
$return['all'][] = null;
|
|
$return['def'][] = null;
|
|
$return['att'][] = null;
|
|
$return['mid'][] = null;
|
|
}
|
|
}
|
|
|
|
// dd($return);
|
|
// while (array_last($return['all']) == null) {
|
|
// }
|
|
|
|
return $return;
|
|
}
|
|
|
|
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['all']) - 1; $i > 0; $i--) {
|
|
if ($homes['all'][$i] == null) {
|
|
$count_home++;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
for ($i = \count($aways['all']) - 1; $i > 0; $i--) {
|
|
if ($aways['all'][$i] == null) {
|
|
$count_away++;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
$keeps = count($homes['all']) - min($count_home, $count_away) - 1;
|
|
|
|
$homes['all'] = array_slice($homes['all'], 0, $keeps);
|
|
$homes['def'] = array_slice($homes['def'], 0, $keeps);
|
|
$homes['att'] = array_slice($homes['att'], 0, $keeps);
|
|
$homes['mid'] = array_slice($homes['mid'], 0, $keeps);
|
|
$aways['all'] = array_slice($aways['all'], 0, $keeps);
|
|
$aways['def'] = array_slice($aways['def'], 0, $keeps);
|
|
$aways['att'] = array_slice($aways['att'], 0, $keeps);
|
|
$aways['mid'] = array_slice($aways['mid'], 0, $keeps);
|
|
$labels = array_slice($labels, 0, $keeps);
|
|
}
|
|
}
|