Added achievement data
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Achievement;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Alliance;
|
||||
|
||||
class AirlineObserver
|
||||
{
|
||||
public function created(Airline $airline): void
|
||||
{
|
||||
if ($airline->alliance_id !== null) {
|
||||
$this->syncAllianceThresholds();
|
||||
}
|
||||
}
|
||||
|
||||
public function updated(Airline $airline): void
|
||||
{
|
||||
if ($airline->wasChanged('alliance_id')) {
|
||||
$this->syncAllianceThresholds();
|
||||
}
|
||||
}
|
||||
|
||||
public function deleted(Airline $airline): void
|
||||
{
|
||||
$this->syncAllianceThresholds();
|
||||
}
|
||||
|
||||
private function syncAllianceThresholds(): void
|
||||
{
|
||||
Alliance::withCount('airlines')->each(function (Alliance $alliance) {
|
||||
Achievement::where('internal_name', "airlines_alliances.all_{$alliance->internal_name}")
|
||||
->update(['threshold' => $alliance->airlines_count]);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\UserFlight;
|
||||
|
||||
class FlightObserver
|
||||
{
|
||||
/**
|
||||
* Recalculate after a flight is created.
|
||||
*/
|
||||
public function created(UserFlight $flight): void
|
||||
{
|
||||
$flight->user->calculateAchievements();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate after a flight is updated.
|
||||
* Cabin class, flight type, airline, etc. may have changed,
|
||||
* which could unlock or revoke achievements.
|
||||
*/
|
||||
public function updated(UserFlight $flight): void
|
||||
{
|
||||
$flight->user->calculateAchievements();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate after a flight is deleted.
|
||||
* Previously earned achievements may no longer be valid.
|
||||
*/
|
||||
public function deleted(UserFlight $flight): void
|
||||
{
|
||||
$flight->user->calculateAchievements();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user