134 lines
5.6 KiB
PHP
134 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Selections;
|
|
|
|
use App\Filament\Pages\Selections\Helper\GetSeason;
|
|
use App\Models\SofaScorePlayerPerSeason;
|
|
use App\Models\StatistikoEvent;
|
|
|
|
class LoadPlayerSelection
|
|
{
|
|
public static function load(?int $team_id, ?string $team_position, ?int $sofascore_category_id, ?array $season, ?string $seasonmode)
|
|
{
|
|
$season = GetSeason::load($season, $seasonmode);
|
|
|
|
// $hash = hash('crc32', $team_id.$team_position.$sofascore_category_id.serialize($season));
|
|
// $cached_players = SofaScorePlayerCache::where('hash', $hash)->value('players');
|
|
// if ($cached_players) {
|
|
// return $cached_players;
|
|
// }
|
|
|
|
$matches = StatistikoEvent::whereIn('season', $season)
|
|
->where(function ($query) use ($team_id, $team_position) {
|
|
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);
|
|
}
|
|
})->pluck('match_id')->toArray();
|
|
|
|
$matches_count = count($matches);
|
|
|
|
$positions_map = LoadPositions::load();
|
|
|
|
$player = SofaScorePlayerPerSeason::with('details')->where('ss_team_id', $team_id)->whereIn('season', $season)->get();
|
|
|
|
// dd($player);
|
|
|
|
$arr = [];
|
|
foreach ($player as $val) {
|
|
if ($val->details) {
|
|
$player_id = $val->ss_player_id;
|
|
if (! isset($arr[$player_id])) {
|
|
$arr[$player_id]['player_name'] = $val->details->player_name;
|
|
$arr[$player_id]['events_played_home'] = $val->events_played_home;
|
|
$arr[$player_id]['events_played_away'] = $val->events_played_away;
|
|
$arr[$player_id]['minutes_played_home'] = $val->minutes_played_home;
|
|
$arr[$player_id]['minutes_played_away'] = $val->minutes_played_away;
|
|
$arr[$player_id]['preferred_foot'] = $val->details->preferred_foot ?? '';
|
|
$arr[$player_id]['player_position'] = $val->details->position ?? '';
|
|
$arr[$player_id]['color_level'] = round($matches_count / 3);
|
|
} else {
|
|
$arr[$player_id]['events_played_home'] += $val->events_played_home;
|
|
$arr[$player_id]['events_played_away'] += $val->events_played_away;
|
|
$arr[$player_id]['minutes_played_home'] += $val->minutes_played_home;
|
|
$arr[$player_id]['minutes_played_away'] += $val->minutes_played_away;
|
|
}
|
|
}
|
|
}
|
|
|
|
$result = [];
|
|
foreach ($arr as $player_id => $val) {
|
|
|
|
switch ($team_position) {
|
|
case 'home':
|
|
$events_played = $val['events_played_home'];
|
|
$minues_played = $val['minutes_played_home'];
|
|
break;
|
|
case 'away':
|
|
$events_played = $val['events_played_away'];
|
|
$minues_played = $val['minutes_played_away'];
|
|
break;
|
|
default:
|
|
$events_played = $val['events_played_home'] + $val['events_played_away'];
|
|
$minues_played = $val['minutes_played_home'] + $val['minutes_played_home'];
|
|
break;
|
|
}
|
|
|
|
$colorlevel = round($matches_count / 3);
|
|
|
|
if (in_array($val['player_position'], $positions_map['MIDFIELD'])) {
|
|
$position = 'MIDFIELD';
|
|
} elseif (in_array($val['player_position'], $positions_map['DEFENCE'])) {
|
|
$position = 'DEFENCE';
|
|
} elseif (in_array($val['player_position'], $positions_map['ATTACK'])) {
|
|
$position = 'ATTACK';
|
|
} elseif (in_array($val['player_position'], $positions_map['GOALKEEPER'])) {
|
|
$position = 'GOALKEEPER';
|
|
} else {
|
|
$position = 'NO SPECIFIC POSITION';
|
|
}
|
|
|
|
$infostring = '';
|
|
|
|
if ($colorlevel > 1 && $events_played < $colorlevel) {
|
|
$infostring = '<div class="w-full text-red-600 dark:text-red-500 cursor-pointer flex flex-row gap-1">';
|
|
} elseif ($events_played < $colorlevel * 2) {
|
|
$infostring = '<div class="w-full text-blue-600 dark:text-blue-500 cursor-pointer flex flex-row gap-1">';
|
|
} else {
|
|
$infostring = '<div class="w-full text-green-600 dark:text-green-500 cursor-pointer flex flex-row gap-1">';
|
|
}
|
|
$infostring .= '<div class="flex-1 font-bold">'.$val['player_name'].' '.$val['player_position'].'</div>';
|
|
$infostring .= '<div>'.$events_played.' Events,</div>';
|
|
$infostring .= '<div>'.$val['preferred_foot'].' foot,</div>';
|
|
if ($events_played) {
|
|
$infostring .= '<div>Av. on field '.round($minues_played / $events_played).'min</div>';
|
|
}
|
|
$infostring .= '</div>';
|
|
$result[$position][$player_id] = $infostring;
|
|
}
|
|
|
|
// if ($result) {
|
|
// SofaScorePlayerCache::updateOrCreate(['hash' => $hash], ['players' => $result]);
|
|
// }
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function convertToHoursMins(int $time, $format = '%02dh:%02dm')
|
|
{
|
|
if ($time < 1) {
|
|
return;
|
|
}
|
|
|
|
$hours = floor($time / 60);
|
|
$minutes = $time % 60;
|
|
|
|
return sprintf($format, $hours, $minutes);
|
|
}
|
|
}
|