Files
nuhodds_fi4/app/Actions/PlayerPerSeason.php
T
2026-09-11 10:36:16 +02:00

114 lines
5.6 KiB
PHP

<?php
namespace App\Actions;
use App\Models\SofaScorePlayerCache;
use App\Models\SofaScorePlayerPerSeason;
use App\Models\StatistikoDetail;
use App\Models\StatistikoPlayer;
class PlayerPerSeason
{
public static function run()
{
// when data changes clear player cache
SofaScorePlayerCache::truncate();
$res = StatistikoDetail::query()->where('is_active', 1)->select('ss_player_id', 'ss_team_id')->get();
foreach ($res as $val) {
$collection = [];
$res2 = StatistikoPlayer::with('event')->where('ss_player_id', $val->ss_player_id)->get();
foreach ($res2 as $val2) {
if (isset($val2->event->season)) {
if (! isset($collection['total'])) {
$collection['total']['events_played_home'] = 0;
$collection['total']['events_played_away'] = 0;
$collection['total']['minutes_played_home'] = 0;
$collection['total']['minutes_played_away'] = 0;
$collection['total']['goals'] = 0;
$collection['total']['fouls'] = 0;
$collection['total']['offsides'] = 0;
$collection['total']['shots_on_target'] = 0;
$collection['total']['shots_off_target'] = 0;
$collection['total']['cards'] = 0;
}
if (! isset($collection[$val2->event->season])) {
$collection[$val2->event->season]['events_played_home'] = 0;
$collection[$val2->event->season]['events_played_away'] = 0;
$collection[$val2->event->season]['minutes_played_home'] = 0;
$collection[$val2->event->season]['minutes_played_away'] = 0;
$collection[$val2->event->season]['goals'] = 0;
$collection[$val2->event->season]['fouls'] = 0;
$collection[$val2->event->season]['cards'] = 0;
$collection[$val2->event->season]['offsides'] = 0;
$collection[$val2->event->season]['shots_on_target'] = 0;
$collection[$val2->event->season]['shots_off_target'] = 0;
}
if ((int) $val2->minutes_played > 0) {
if ($val2->activeteam_id == $val2->event->home_id) {
$collection[$val2->event->season]['events_played_home'] += 1;
$collection[$val2->event->season]['minutes_played_home'] += $val2->minutes_played;
$collection['total']['events_played_home'] += 1;
$collection['total']['minutes_played_home'] += $val2->minutes_played;
} elseif ($val2->activeteam_id == $val2->event->away_id) {
$collection[$val2->event->season]['events_played_away'] += 1;
$collection[$val2->event->season]['minutes_played_away'] += $val2->minutes_played;
$collection['total']['minutes_played_away'] += 1;
$collection['total']['minutes_played_away'] += $val2->minutes_played;
}
$cards = $val2->card_yellow + $val2->card_yellowred + $val2->card_red;
$collection[$val2->event->season]['goals'] += $val2->goals;
$collection[$val2->event->season]['fouls'] += $val2->fouls;
$collection[$val2->event->season]['cards'] += $cards;
$collection[$val2->event->season]['offsides'] += $val2->offsides;
$collection[$val2->event->season]['shots_on_target'] += $val2->shots_on_target;
$collection[$val2->event->season]['shots_off_target'] += $val2->shots_off_target;
$collection['total']['goals'] += $val2->goals;
$collection['total']['fouls'] += $val2->fouls;
$collection['total']['cards'] += $cards;
$collection['total']['offsides'] += $val2->offsides;
$collection['total']['shots_on_target'] += $val2->shots_on_target;
$collection['total']['shots_off_target'] += $val2->shots_off_target;
}
}
}
foreach ($collection as $season => $row) {
if ($val->ss_team_id && $val->ss_player_id) {
SofaScorePlayerPerSeason::updateOrCreate(
[
'ss_player_id' => $val->ss_player_id,
'ss_team_id' => $val->ss_team_id,
'season' => $season,
],
[
'events_played_home' => $row['events_played_home'],
'events_played_away' => $row['events_played_away'],
'minutes_played_home' => $row['minutes_played_home'],
'minutes_played_away' => $row['minutes_played_away'],
'goals' => $row['goals'],
'fouls' => $row['fouls'],
'cards' => $row['cards'],
'offsides' => $row['offsides'],
'shots_on_target' => $row['shots_on_target'],
'shots_off_target' => $row['shots_off_target'],
]
);
}
}
}
}
}