Files
nuhodds_fi4/app/Livewire/PlayerCalculator.php
T
2026-09-11 20:03:27 +02:00

88 lines
2.9 KiB
PHP

<?php
namespace App\Livewire;
use App\Filament\Pages\Selections\LoadPositions;
use App\Models\SofaScorePlayerPerSeason;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use Livewire\Component;
class PlayerCalculator extends Component
{
use InteractsWithPageFilters;
public $players = [];
public function mount(array $filters)
{
$this->players = self::load($filters);
}
public static function load(array $filters)
{
$team_id = (int) $filters['team_id'];
$positions_map = LoadPositions::load();
$player = SofaScorePlayerPerSeason::with('details')->where('season', '=', 'total')->where('ss_team_id', '=', $team_id)->get();
$result = [];
foreach ($player as $val) {
$tmp = [];
$tmp['id'] = $val->ss_player_id;
$tmp['name'] = $val->details->player_name;
$tmp['pos_ori'] = $val->details->position;
$tmp['pos'] = '';
$tmp['foot'] = $val->details->preferred_foot;
$tmp['tot_evt'] = $val->total_events;
$tmp['tot_min'] = $val->total_min;
$tmp['ave_min'] = $val->average_min;
$tmp['gls'] = $val->goals;
$tmp['fls'] = $val->fouls;
$tmp['ofs'] = $val->offsides;
$tmp['crd'] = $val->cards;
$tmp['snt'] = $val->shots_on_target;
$tmp['tsh'] = $val->shots_off_target;
$level = round($val->total_events / 3);
if ($val->total_events) {
if ($level > 1 && $val->total_events < $level) {
$tmp['color_evt'] = 'danger';
} elseif ($val->total_events < $level * 2) {
$tmp['color_evt'] = 'info';
} else {
$tmp['color_evt'] = 'success';
}
}
if (in_array($val->details->position, $positions_map['MIDFIELD'])) {
$tmp['area'] = 'midfield';
$tmp['color_pos'] = 'info';
} elseif (in_array($val->details->position, $positions_map['DEFENCE'])) {
$tmp['area'] = 'defence';
$tmp['color_pos'] = 'success';
} elseif (in_array($val->details->position, $positions_map['ATTACK'])) {
$tmp['area'] = 'attack';
$tmp['color_pos'] = 'warning';
} elseif (in_array($val->details->position, $positions_map['GOALKEEPER'])) {
$tmp['area'] = 'goalkeeper';
$tmp['color_pos'] = 'gray';
} else {
$tmp['area'] = 'defence';
$tmp['color_pos'] = 'success';
}
$result[] = $tmp;
}
// dd($result);
$pos = array_column($result, 'pos');
$area = array_column($result, 'area');
array_multisort($area, SORT_ASC, $pos, SORT_ASC, $result);
return $result;
}
}