-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSPAwardsNightowl.php
More file actions
70 lines (54 loc) · 2.49 KB
/
SPAwardsNightowl.php
File metadata and controls
70 lines (54 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Modules\Awards\Awards;
use App\Contracts\Award;
use App\Models\UserAward;
use App\Models\Enums\PirepState;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
class SPAwardsNightowl extends Award
{
public $name = 'SPAwards(Nightowl)';
public $param_description = 'The number of night landings required for this award, between 22:00–06:00 UTC';
public function check($requiredCount = null): bool
{
// Ensure parameter is provided and valid
if (is_null($requiredCount) || !is_numeric($requiredCount)) {
Log::error('SPAwards(Nightowl) | Invalid or missing parameter.');
return false;
}
// Check if the award is already granted
$award = \App\Models\Award::where('ref_model', get_class($this))
->where('ref_model_params', (string) $requiredCount)
->first();
if (!$award) {
Log::error("SPAwards(Nightowl) | No matching award found.");
return false;
}
$alreadyGranted = UserAward::where('user_id', $this->user->id)
->where('award_id', $award->id)
->exists();
if ($alreadyGranted) {
Log::info("SPAwards(Nightowl) | Award already granted to Pilot (ID: {$this->user->id}). Skipping...");
return false;
}
$requiredCount = (int) $requiredCount;
// Retrieve all accepted PIREPs of the user
$pireps = $this->user->pireps()
->where('state', PirepState::ACCEPTED) // only count accepted PIREPs
->get(['id', 'block_on_time', 'created_at']);
// Count flights where the block-on time was between 22:00 and 06:00 UTC
$nightLandings = $pireps->filter(function ($pirep) {
// Use block_on_time if available, otherwise fallback to created_at
$landingTime = $pirep->block_on_time
? Carbon::parse($pirep->block_on_time)
: Carbon::parse($pirep->created_at);
$hour = $landingTime->copy()->utc()->hour;
// Night hours between 22:00–06:00 UTC
return ($hour >= 22 || $hour < 6);
})->count();
// Log for debugging
Log::info("SPAwards(Nightowl) | Pilot (ID: {$this->user->id}) has {$nightLandings} night landings, {$requiredCount} required.");
// Return true if the required number of night landings is reached
return $nightLandings >= $requiredCount;
}
}