69 lines
2.6 KiB
PHP
69 lines
2.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 chche
|
|
SofaScorePlayerCache::truncate();
|
|
|
|
$res = StatistikoDetail::query()->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[$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;
|
|
}
|
|
|
|
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;
|
|
} else if ($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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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'],
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|