Allow "Log 1st Flight" achievement to be awarded even if flight is upcoming
This commit is contained in:
@@ -3,19 +3,35 @@
|
|||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Console\Attributes\Argument;
|
||||||
use Illuminate\Console\Attributes\Description;
|
use Illuminate\Console\Attributes\Description;
|
||||||
use Illuminate\Console\Attributes\Signature;
|
use Illuminate\Console\Attributes\Signature;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
#[Signature('app:recalculate-achivements')]
|
#[Signature('app:recalculate-achievements {username? : The username of a single user to recalculate}')]
|
||||||
#[Description('Recalculate achievements for all users')]
|
#[Description('Recalculate achievements for all users, or a single user by username')]
|
||||||
class RecalculateAchievements extends Command
|
class RecalculateAchievements extends Command
|
||||||
{
|
{
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$users = User::all();
|
$username = $this->argument('username');
|
||||||
foreach ($users as $user) {
|
|
||||||
|
if ($username) {
|
||||||
|
$user = User::where('name', $username)->first();
|
||||||
|
|
||||||
|
if (! $user) {
|
||||||
|
$this->error("No user found with username [{$username}].");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$user->calculateAchievements();
|
$user->calculateAchievements();
|
||||||
|
$this->info("Recalculated achievements for {$username}.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$users = User::all();
|
||||||
|
$this->withProgressBar($users, fn (User $user) => $user->calculateAchievements());
|
||||||
|
$this->newLine();
|
||||||
|
$this->info("Recalculated achievements for {$users->count()} users.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class AchievementService
|
|||||||
* @var Collection<int,UserFlight> $flights
|
* @var Collection<int,UserFlight> $flights
|
||||||
*/
|
*/
|
||||||
private Collection $flights;
|
private Collection $flights;
|
||||||
|
private int $loggedFlightCount;
|
||||||
|
|
||||||
public function calculate(User $user): void
|
public function calculate(User $user): void
|
||||||
{
|
{
|
||||||
@@ -64,20 +65,24 @@ class AchievementService
|
|||||||
'arrivalAirport.region.continent',
|
'arrivalAirport.region.continent',
|
||||||
])->where('departure_date', '<=', now('UTC'))->get();
|
])->where('departure_date', '<=', now('UTC'))->get();
|
||||||
|
|
||||||
|
$this->loggedFlightCount = $user->flights()->count();
|
||||||
|
|
||||||
foreach ($this->checkers as $checkerClass) {
|
foreach ($this->checkers as $checkerClass) {
|
||||||
$checker = new $checkerClass($this);
|
$checker = new $checkerClass($this);
|
||||||
$checker->check($user);
|
$checker->check($user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Collection<int,UserFlight>
|
|
||||||
*/
|
|
||||||
public function getFlights(): Collection
|
public function getFlights(): Collection
|
||||||
{
|
{
|
||||||
return $this->flights;
|
return $this->flights;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLoggedFlightCount(): int
|
||||||
|
{
|
||||||
|
return $this->loggedFlightCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
// Award / revoke
|
// Award / revoke
|
||||||
|
|||||||
@@ -15,6 +15,11 @@ abstract class BaseChecker implements AchievementCheckerInterface
|
|||||||
return $this->service->getFlights();
|
return $this->service->getFlights();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function allLoggedFlightsCount(): int
|
||||||
|
{
|
||||||
|
return $this->service->getLoggedFlightCount();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve an achievement ID from its internal name.
|
* Resolve an achievement ID from its internal name.
|
||||||
* Results are cached on the service so repeated lookups are free.
|
* Results are cached on the service so repeated lookups are free.
|
||||||
|
|||||||
@@ -15,10 +15,11 @@ class GeneralFlyingChecker extends BaseChecker
|
|||||||
*/
|
*/
|
||||||
$flights = $this->flights();
|
$flights = $this->flights();
|
||||||
$count = $flights->count();
|
$count = $flights->count();
|
||||||
|
$allLoggedFlightsCount = $this->allLoggedFlightsCount();
|
||||||
|
|
||||||
// --- Boolean achievements ---
|
// --- Boolean achievements ---
|
||||||
|
|
||||||
$this->awardIf($count >= 1, 'general_flying.first_flight');
|
$this->awardIf($allLoggedFlightsCount >= 1, 'general_flying.first_flight');
|
||||||
|
|
||||||
$this->awardIf(
|
$this->awardIf(
|
||||||
$flights->contains(fn ($f) => $f->isDomestic()),
|
$flights->contains(fn ($f) => $f->isDomestic()),
|
||||||
|
|||||||
Reference in New Issue
Block a user