Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/Models/Vatsim/NetworkAircraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Airfield\Airfield;
use App\Models\Hold\NavaidNetworkAircraft;
use App\Models\Navigation\Navaid;
use App\Services\LocationService;
use App\Models\Stand\Stand;
use App\Models\Stand\StandAssignment;
use App\Models\User\User;
Expand All @@ -17,6 +18,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Location\Coordinate;
use Location\Distance\Haversine;

class NetworkAircraft extends Model
{
Expand Down Expand Up @@ -168,4 +170,23 @@ public function aircraft(): BelongsTo
{
return $this->belongsTo(Aircraft::class);
}

public function isNearDestination(float $thresholdNauticalMiles = 5.0): bool
{
if (!$this->destinationAirfield?->latitude || !$this->destinationAirfield?->longitude) {
return false;
}

$distanceToAirfieldInNm = LocationService::metersToNauticalMiles(
$this->latLong->getDistance($this->destinationAirfield->coordinate, new Haversine())
);

return $distanceToAirfieldInNm <= $thresholdNauticalMiles;
}

public function hasLanded(): bool
{
$isOnGround = $this->groundspeed < 50;
return $isOnGround && $this->isNearDestination();
}
Comment on lines +173 to +191
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls a quick test for this as well

}
18 changes: 12 additions & 6 deletions app/Services/Stand/StandOccupationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,22 @@ private function occupyStands(Collection $standsToOccupy): void
}

/**
* Given some stands that have been recently occupied, remove any conflicting stand assignments.
* Given some stands that have been recently occupied, remove any conflicting stand assignments, unless the
* aircraft has already landed.
*/
private function deleteConflictingAssignmentsFollowingOccupation(Collection $newOccupations): void
{
StandAssignment::whereNotIn('callsign', $newOccupations->pluck('callsign'))
$conflictingAssignments = StandAssignment::with('aircraft')
->whereNotIn('callsign', $newOccupations->pluck('callsign'))
->whereIn('stand_id', $newOccupations->pluck('stand_id'))
->get()
->each(function (StandAssignment $assignment) {
$this->assignmentsService->deleteStandAssignment($assignment);
});
->get();

foreach ($conflictingAssignments as $assignment) {
if ($assignment->aircraft && $assignment->aircraft->hasLanded()) {
continue;
}
$this->assignmentsService->deleteStandAssignment($assignment);
}
}

/**
Expand Down
150 changes: 150 additions & 0 deletions tests/app/Services/Stand/StandOccupationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,156 @@ public function testItReturnsClosestOccupiedStandIfMultipleInContention()
$this->assertEquals($newStand->id, $aircraft->occupiedStand->first()->id);
}

public function testItDoesNotDeleteConflictingAssignmentIfAssignedAircraftHasLanded()
{
Event::fake();

NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.47187222,
'longitude' => -0.48601389,
'groundspeed' => 10,
'altitude' => 0,
]
);

NetworkAircraftService::createOrUpdateNetworkAircraft(
'RYR787',
[
'latitude' => 51.47187222,
'longitude' => -0.48601389,
'groundspeed' => 0,
'altitude' => 0,
]
);

$this->addStandAssignment('BAW221', 2);
$this->service->setOccupiedStands();

$this->assertDatabaseHas('stand_assignments',['callsign' => 'BAW221', 'stand_id' => 2]);
}

public function testItDeletesConflictingAssignmentIfAssignedAircraftHasNotLanded()
{
Event::fake();

NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.47187222,
'longitude' => -0.48601389,
'groundspeed' => 250,
'altitude' => 0,
]
);

NetworkAircraftService::createOrUpdateNetworkAircraft(
'RYR787',
[
'latitude' => 51.47187222,
'longitude' => -0.48601389,
'groundspeed' => 0,
'altitude' => 0,
]
);

$this->addStandAssignment('BAW221', 2);
$this->service->setOccupiedStands();

Event::assertDispatched(fn(StandUnassignedEvent $event) => $event->getCallsign() === 'BAW221');
$this->assertDatabaseMissing('stand_assignments',['callsign' => 'BAW221']);
}

public function testIsNearDestinationReturnsFalseIfNoDestinationAirfield()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'XXXX',
'latitude' => 51.47187222,
'longitude' => -0.48601389,
]
);

$this->assertFalse($aircraft->isNearDestination());
}

public function testIsNearDestinationReturnsTrueIfWithinThreshold()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.47187222,
'longitude' => -0.48601389,
]
);

$this->assertTrue($aircraft->isNearDestination());
}

public function testIsNearDestinationReturnsFalseIfOutsideThreshold()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.646099,
'longitude' => 0.151667,
]
);

$this->assertFalse($aircraft->isNearDestination());
}

public function testHasLandedReturnsTrueIfOnGroundAndNearDestination()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.47187222,
'longitude' => -0.48601389,
'groundspeed' => 10,
]
);

$this->assertTrue($aircraft->hasLanded());
}

public function testHasLandedReturnsFalseIfAirborneAndNearDestination()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.47187222,
'longitude' => -0.48601389,
'groundspeed' => 250,
]
);

$this->assertFalse($aircraft->hasLanded());
}

public function testHasLandedReturnsFalseIfOnGroundButNotNearDestination()
{
$aircraft = NetworkAircraftService::createOrUpdateNetworkAircraft(
'BAW221',
[
'planned_destairport' => 'EGLL',
'latitude' => 51.646099,
'longitude' => 0.151667,
'groundspeed' => 10,
]
);

$this->assertFalse($aircraft->hasLanded());
}

private function addStandAssignment(string $callsign, int $standId): StandAssignment
{
NetworkAircraftService::createPlaceholderAircraft($callsign);
Expand Down
Loading