168 lines
6.2 KiB
PHP
168 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Filament\Pages\Selections\Helper\GetSeason;
|
|
use App\Filament\Pages\Selections\LoadPositions;
|
|
use App\Livewire\Mapper\LoadGridPositions;
|
|
use App\Models\SofaScorePlayerPerSeason;
|
|
use App\Models\StatistikoEvent;
|
|
use Filament\Widgets\Concerns\InteractsWithPageFilters;
|
|
use Livewire\Component;
|
|
|
|
class PlayerCalculator extends Component
|
|
{
|
|
use InteractsWithPageFilters;
|
|
|
|
public $grid = [];
|
|
|
|
public function mount(array $filters)
|
|
{
|
|
$pos = array_chunk(LoadGridPositions::load(), 5);
|
|
$pla = array_chunk(self::load($filters), 5);
|
|
|
|
$i = 1;
|
|
while ($v = array_shift($pos)) {
|
|
array_splice($pla, $i, 0, [$v]);
|
|
$i += 2;
|
|
}
|
|
$x = 1;
|
|
$y = 1;
|
|
foreach ($pla as $ele) {
|
|
foreach ($ele as $val) {
|
|
$tmp = [];
|
|
if (is_array($val)) {
|
|
foreach ($val as $key => $value) {
|
|
$tmp[$key] = $value;
|
|
}
|
|
$tmp['type'] = '';
|
|
} else {
|
|
$tmp['type'] = $val != 0 ? $val : '';
|
|
}
|
|
$tmp['x'] = 104 * $x;
|
|
$tmp['y'] = 104 * $y;
|
|
$this->grid[] = $tmp;
|
|
$x++;
|
|
if ($x > 10) {
|
|
$x = 1;
|
|
$y++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function load(array $filters)
|
|
{
|
|
$team_position = $filters['team_position'];
|
|
$season = $filters['season'];
|
|
$seasonmode = $filters['seasonmode'];
|
|
$team_id = (int) $filters['team_id'];
|
|
$season = GetSeason::load($season, $seasonmode);
|
|
|
|
$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');
|
|
|
|
$positions_map = LoadPositions::load();
|
|
|
|
$player = SofaScorePlayerPerSeason::with('details')->where('ss_team_id', $team_id)->whereIn('season', $season)->get();
|
|
|
|
$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';
|
|
$position_color = 'info';
|
|
} elseif (in_array($val['player_position'], $positions_map['DEFENCE'])) {
|
|
$position = 'DEFENCE';
|
|
$position_color = 'success';
|
|
} elseif (in_array($val['player_position'], $positions_map['ATTACK'])) {
|
|
$position = 'ATTACK';
|
|
$position_color = 'warning';
|
|
} elseif (in_array($val['player_position'], $positions_map['GOALKEEPER'])) {
|
|
$position = 'GOALKEEPER';
|
|
$position_color = 'gray';
|
|
} else {
|
|
$position = 'NO SPECIFIC POSITION';
|
|
$position_color = '';
|
|
}
|
|
|
|
$name = $val['player_name'];
|
|
$preferred_foot = $val['preferred_foot'] ?? '-';
|
|
$everage_minutes = round($minues_played / $events_played).' min';
|
|
if ($colorlevel > 1 && $events_played < $colorlevel) {
|
|
$color = 'danger';
|
|
} elseif ($events_played < $colorlevel * 2) {
|
|
$color = 'info';
|
|
} else {
|
|
$color = 'success';
|
|
}
|
|
|
|
$result[] = [
|
|
'player_id' => $player_id,
|
|
'name' => $name,
|
|
'position' => $position,
|
|
'position_color' => $position_color,
|
|
'color' => $color,
|
|
'info' => 'Ev '.$events_played.', '.$preferred_foot.', '.$everage_minutes,
|
|
];
|
|
}
|
|
|
|
$maxlength = 35;
|
|
$len = count($result);
|
|
if ($len < $maxlength) {
|
|
$result += array_fill($len, $maxlength - $len, [0, 0, 0, 0, 0, 0, 0]);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|