-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSPAwardsPerformer.php
More file actions
81 lines (63 loc) · 2.8 KB
/
SPAwardsPerformer.php
File metadata and controls
81 lines (63 loc) · 2.8 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
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Modules\Awards\Awards;
use App\Contracts\Award;
use App\Models\UserAward;
use App\Models\Enums\PirepState;
use Illuminate\Support\Facades\Log;
class SPAwardsPerformer extends Award
{
public $name = 'SPAwards(Performer)';
public $param_description = 'The minimum average flight score required to receive this award';
public function check($minScore = null): bool
{
// Ensure parameter is provided and valid
if (is_null($minScore) || !is_numeric($minScore)) {
Log::error('SPAwards(Performer) | Invalid or missing minScore 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) $minScore)
->first();
if (!$award) {
Log::error("SPAwards(Performer) | No matching award found.");
return false;
}
$alreadyGranted = UserAward::where('user_id', $this->user->id)
->where('award_id', $award->id)
->exists();
$minScore = (float) $minScore;
// Retrieve average score from accepted PIREPs
$avgScore = $this->user->pireps()
->where('state', PirepState::ACCEPTED)
->avg('score');
// Handle pilots without any accepted PIREPs
if (is_null($avgScore)) {
Log::info("SPAwards(Performer) | Pilot (ID: {$this->user->id}) has no accepted flights yet.");
// If they previously had it, remove it
if ($alreadyGranted) {
$this->user->awards()->detach($award->id);
Log::info("SPAwards(Performer) | Award removed from Pilot (ID: {$this->user->id}) due to missing PIREPs.");
}
return false;
}
// Log for debugging
Log::info("SPAwards(Performer) | Pilot (ID: {$this->user->id}) average score: {$avgScore}, {$minScore} required.");
// Grant award if meets requirement
if ($avgScore >= $minScore) {
if (!$alreadyGranted) {
$this->user->awards()->attach($award->id);
Log::info("SPAwards(Performer) | Award granted to Pilot (ID: {$this->user->id}).");
return true;
}
Log::info("SPAwards(Performer) | Award already granted to Pilot (ID: {$this->user->id}). Skipping...");
return false;
}
// If falls below requirement, remove award if granted
if ($alreadyGranted) {
$this->user->awards()->detach($award->id);
Log::info("SPAwards(Performer) | Award removed from Pilot (ID: {$this->user->id}) due to score drop.");
}
return false;
}
}