-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSPAwardsDistance.php
More file actions
57 lines (44 loc) · 1.92 KB
/
SPAwardsDistance.php
File metadata and controls
57 lines (44 loc) · 1.92 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
<?php
namespace Modules\Awards\Awards;
use App\Contracts\Award;
use App\Models\UserAward;
use App\Models\Enums\PirepState;
use Illuminate\Support\Facades\Log;
class SPAwardsDistance extends Award
{
public $name = 'SPAwards(Distance)';
public $param_description = 'The total distance flown in nautical miles at which to give this award';
public function check($distance = null): bool
{
// Ensure parameter is provided and valid
if (is_null($distance) || !is_numeric($distance)) {
Log::error('SPAwards(Distance) | Invalid or missing distance 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) $distance)
->first();
if (!$award) {
Log::error("SPAwards(Distance) | 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(Distance) | Award already granted to Pilot (ID: {$this->user->id}). Skipping...");
return false;
}
// Convert parameter to float
$requiredDistance = (float) $distance;
// Sum up all accepted PIREP distances for the user
$totalDistance = $this->user->pireps()
->where('state', PirepState::ACCEPTED) // only count accepted PIREPs
->sum('distance');
// Log for debugging
Log::info("SPAwards(Distance) | Pilot (ID: {$this->user->id}) has flown {$totalDistance} nm, {$requiredDistance} nm needed.");
// Check if the pilot meets or exceeds the required total distance
return $totalDistance >= $requiredDistance;
}
}