phpvms/app/Services/FlightService.php

361 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Contracts\Service;
use App\Exceptions\BidExistsForFlight;
use App\Exceptions\DuplicateFlight;
use App\Models\Bid;
use App\Models\Enums\Days;
use App\Models\Flight;
use App\Models\FlightFieldValue;
use App\Models\User;
use App\Repositories\FlightRepository;
use App\Repositories\NavdataRepository;
use App\Support\Units\Time;
use Illuminate\Support\Facades\Log;
class FlightService extends Service
{
private $airportSvc;
2018-08-27 00:40:04 +08:00
private $flightRepo;
private $navDataRepo;
private $userSvc;
/**
* FlightService constructor.
2018-08-27 00:40:04 +08:00
*
* @param AirportService $airportSvc
* @param FlightRepository $flightRepo
* @param NavdataRepository $navdataRepo
* @param UserService $userSvc
*/
public function __construct(
AirportService $airportSvc,
FlightRepository $flightRepo,
NavdataRepository $navdataRepo,
UserService $userSvc
2018-08-27 00:40:04 +08:00
) {
$this->airportSvc = $airportSvc;
$this->flightRepo = $flightRepo;
$this->navDataRepo = $navdataRepo;
$this->userSvc = $userSvc;
}
/**
* Create a new flight
*
* @param array $fields
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createFlight($fields)
{
$flightTmp = new Flight($fields);
if ($this->isFlightDuplicate($flightTmp)) {
throw new DuplicateFlight($flightTmp);
}
$fields = $this->transformFlightFields($fields);
$flight = $this->flightRepo->create($fields);
return $flight;
}
/**
* Update a flight with values from the given fields
*
* @param Flight $flight
* @param array $fields
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return \App\Models\Flight|mixed
*/
public function updateFlight($flight, $fields)
{
// apply the updates here temporarily, don't save
// the repo->update() call will actually do it
$flight->fill($fields);
if ($this->isFlightDuplicate($flight)) {
throw new DuplicateFlight($flight);
}
$fields = $this->transformFlightFields($fields);
$flight = $this->flightRepo->update($fields, $flight->id);
return $flight;
}
/**
* Check the fields for a flight and transform them
*
* @param array $fields
*
* @return array
*/
protected function transformFlightFields($fields)
{
if (array_key_exists('days', $fields) && filled($fields['days'])) {
$fields['days'] = Days::getDaysMask($fields['days']);
}
$fields['flight_time'] = Time::init($fields['minutes'], $fields['hours'])->getMinutes();
$fields['active'] = get_truth_state($fields['active']);
// Figure out a distance if not found
if (empty($fields['distance'])) {
$fields['distance'] = $this->airportSvc->calculateDistance(
$fields['dpt_airport_id'],
$fields['arr_airport_id']
);
}
return $fields;
}
/**
* Filter out any flights according to different settings
2018-08-27 00:40:04 +08:00
*
* @param $user
2018-08-27 00:40:04 +08:00
*
* @return FlightRepository
*/
public function filterFlights($user)
{
$where = [];
if (setting('pilots.only_flights_from_current', false)) {
$where['dpt_airport_id'] = $user->curr_airport_id;
}
return $this->flightRepo
->whereOrder($where, 'flight_number', 'asc');
}
/**
* Filter out subfleets to only include aircraft that a user has access to
2018-08-27 00:40:04 +08:00
*
* @param $user
* @param $flight
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function filterSubfleets($user, $flight)
{
$subfleets = $flight->subfleets;
2018-08-27 00:40:04 +08:00
/*
* Only allow aircraft that the user has access to in their rank
*/
if (setting('pireps.restrict_aircraft_to_rank', false)) {
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
$subfleets = $subfleets->filter(function ($subfleet, $i) use ($allowed_subfleets) {
if ($allowed_subfleets->contains($subfleet->id)) {
return true;
}
});
}
2018-08-27 00:40:04 +08:00
/*
* Only allow aircraft that are at the current departure airport
*/
if (setting('pireps.only_aircraft_at_dpt_airport', false)) {
foreach ($subfleets as $subfleet) {
$subfleet->aircraft = $subfleet->aircraft->filter(
function ($aircraft, $i) use ($flight) {
if ($aircraft->airport_id === $flight->dpt_airport_id) {
return true;
}
}
);
}
}
$flight->subfleets = $subfleets;
return $flight;
}
/**
* Check if this flight has a duplicate already
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
* @return bool
*/
public function isFlightDuplicate(Flight $flight)
{
$where = [
['id', '<>', $flight->id],
'airline_id' => $flight->airline_id,
'flight_number' => $flight->flight_number,
];
$found_flights = $this->flightRepo->findWhere($where);
if ($found_flights->count() === 0) {
return false;
}
// Find within all the flights with the same flight number
// Return any flights that have the same route code and leg
// If this list is > 0, then this has a duplicate
$found_flights = $found_flights->filter(function ($value, $key) use ($flight) {
2018-08-27 02:50:08 +08:00
return $flight->route_code === $value->route_code
&& $flight->route_leg === $value->route_leg;
});
2018-08-27 02:50:08 +08:00
return !($found_flights->count() === 0);
}
/**
* Delete a flight, and all the user bids, etc associated with it
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
* @throws \Exception
*/
public function deleteFlight(Flight $flight): void
{
$where = ['flight_id' => $flight->id];
2018-02-28 03:25:32 +08:00
Bid::where($where)->delete();
$flight->delete();
}
/**
* Update any custom PIREP fields
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
* @param array $field_values
*/
public function updateCustomFields(Flight $flight, array $field_values): void
{
foreach ($field_values as $fv) {
FlightFieldValue::updateOrCreate(
[
'flight_id' => $flight->id,
'name' => $fv['name'],
],
[
2018-08-27 00:40:04 +08:00
'value' => $fv['value'],
]
);
}
}
/**
* Return all of the navaid points as a collection
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Support\Collection
*/
public function getRoute(Flight $flight)
{
if (!$flight->route) {
return collect();
}
2018-08-27 02:50:08 +08:00
$route_points = array_map('strtoupper', explode(' ', $flight->route));
$route = $this->navDataRepo->findWhereIn('id', $route_points);
// Put it back into the original order the route is in
$return_points = [];
foreach ($route_points as $rp) {
$return_points[] = $route->where('id', $rp)->first();
}
return collect($return_points);
}
/**
* Allow a user to bid on a flight. Check settings and all that good stuff
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
* @param User $user
2018-08-27 00:40:04 +08:00
*
*@throws \App\Exceptions\BidExistsForFlight
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
2018-01-08 01:16:44 +08:00
public function addBid(Flight $flight, User $user)
{
2018-08-27 00:40:04 +08:00
// Get all of the bids for this user. See if they're allowed to have multiple
// bids
$bids = Bid::where('user_id', $user->id)->get();
if ($bids->count() > 0 && setting('bids.allow_multiple_bids') === false) {
throw new BidExistsForFlight('User "'.$user->ident.'" already has bids, skipping');
}
2018-08-27 00:40:04 +08:00
// Get all of the bids for this flight
$bids = Bid::where('flight_id', $flight->id)->get();
if ($bids->count() > 0) {
2018-08-27 00:40:04 +08:00
// Does the flight have a bid set?
if ($flight->has_bid === false) {
$flight->has_bid = true;
$flight->save();
}
2018-08-27 00:40:04 +08:00
// Check all the bids for one of this user
foreach ($bids as $bid) {
if ($bid->user_id === $user->id) {
Log::info('Bid exists, user='.$user->ident.', flight='.$flight->id);
return $bid;
}
}
2018-08-27 00:40:04 +08:00
// Check if the flight should be blocked off
if (setting('bids.disable_flight_on_bid') === true) {
throw new BidExistsForFlight($flight);
}
if (setting('bids.allow_multiple_bids') === false) {
throw new BidExistsForFlight($flight);
}
} else {
2018-08-27 02:51:47 +08:00
/* @noinspection NestedPositiveIfStatementsInspection */
if ($flight->has_bid === true) {
Log::info('Bid exists, flight='.$flight->id.'; no entry in bids table, cleaning up');
}
}
$bid = Bid::firstOrCreate([
'user_id' => $user->id,
'flight_id' => $flight->id,
]);
$flight->has_bid = true;
$flight->save();
return $bid;
}
/**
* Remove a bid from a given flight
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
* @param User $user
*/
public function removeBid(Flight $flight, User $user)
{
$bids = Bid::where([
'flight_id' => $flight->id,
2018-08-27 00:40:04 +08:00
'user_id' => $user->id,
])->get();
foreach ($bids as $bid) {
$bid->forceDelete();
}
2018-08-27 00:40:04 +08:00
// Only flip the flag if there are no bids left for this flight
$bids = Bid::where('flight_id', $flight->id)->get();
if ($bids->count() === 0) {
$flight->has_bid = false;
$flight->save();
}
}
}