Skip to content

Commit

Permalink
GH-139 Create union join validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Dec 10, 2020
1 parent febd7f2 commit 0ab1ac0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/flightControl/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
include($includePath . './utils/helpers/getAvailableSpeeds.helper.php');
include($includePath . './utils/helpers/getFleetUnionJoinData.helper.php');
include($includePath . './utils/validators/fleetArray.validator.php');
include($includePath . './utils/validators/joinUnion.validator.php');

});

Expand Down
80 changes: 80 additions & 0 deletions modules/flightControl/utils/validators/joinUnion.validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace UniEngine\Engine\Modules\FlightControl\Utils\Validators;

use UniEngine\Engine\Modules\FlightControl;

/**
* @param array $props
* @param array $props['newFleet']
* @param array $props['timestamp']
* @param ref $props['user']
* @param ref $props['destinationEntry']
*/
function validateJoinUnion ($props) {
$isValid = function () {
return [
'isValid' => true,
];
};
$isInvalid = function ($errors) {
return [
'isValid' => false,
'errors' => $errors,
];
};

$newFleet = $props['newFleet'];
$timestamp = $props['timestamp'];
$user = &$props['user'];
$destinationEntry = &$props['destinationEntry'];

if (!($newFleet['ACS_ID'] > 0)) {
return $isInvalid([
[ 'errorCode' => 'INVALID_UNION_ID', ],
]);
}

$unionJoinData = FlightControl\Utils\Helpers\getFleetUnionJoinData([
'newFleet' => $newFleet,
]);

if (!($unionJoinData)) {
return $isInvalid([
[ 'errorCode' => 'UNION_NOT_FOUND', ],
]);
}

if (
!(
$unionJoinData['owner_id'] == $user['id'] ||
strstr($unionJoinData['users'], "|{$user['id']}|") !== false
)
) {
return $isInvalid([
[ 'errorCode' => 'USER_CANT_JOIN', ],
]);
}

if (!($unionJoinData['end_target_id'] == $destinationEntry['id'])) {
return $isInvalid([
[ 'errorCode' => 'INVALID_DESTINATION_COORDINATES', ],
]);
}

if (!($unionJoinData['fleets_count'] < ACS_MAX_JOINED_FLEETS)) {
return $isInvalid([
[ 'errorCode' => 'UNION_JOINED_FLEETS_COUNT_EXCEEDED', ],
]);
}

if (!($unionJoinData['start_time'] > $timestamp)) {
return $isInvalid([
[ 'errorCode' => 'UNION_JOIN_TIME_EXCEEDED', ],
]);
}

return $isValid();
}

?>

0 comments on commit 0ab1ac0

Please sign in to comment.