Skip to content

Commit

Permalink
GH-139 Create separate mission hold validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Dec 12, 2020
1 parent 9812215 commit 5fb3023
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
26 changes: 22 additions & 4 deletions fleet3.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,28 @@ function messageRed($Text, $Title)
}
}

if ($Fleet['Mission'] == 5) {
$missionHoldValidationResult = FlightControl\Utils\Validators\validateMissionHold([
'newFleet' => $Fleet,
]);

if (!$missionHoldValidationResult['isValid']) {
$firstValidationError = $missionHoldValidationResult['errors'][0];

$errorMessage = null;
switch ($firstValidationError['errorCode']) {
case 'INVALID_HOLD_TIME':
$errorMessage = $_Lang['fl3_Holding_BadTime'];
break;
default:
$errorMessage = $_Lang['fleet_generic_errors_unknown'];
break;
}

messageRed($errorMessage, $ErrorTitle);
}
}

// --- Check if Expeditions and HoldingTimes are Correct
$Throw = false;
$Fleet['StayTime'] = 0;
Expand All @@ -782,10 +804,6 @@ function messageRed($Text, $Title)
}
elseif($Fleet['Mission'] == 5)
{
if(!in_array($Fleet['HoldTime'], array(1, 2, 4, 8, 16, 32)))
{
$Throw = $_Lang['fl3_Holding_BadTime'];
}
$Fleet['StayTime'] = $Fleet['HoldTime'] * 3600;
}
if($Throw)
Expand Down
1 change: 1 addition & 0 deletions modules/flightControl/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
include($includePath . './utils/helpers/getFleetUnionJoinData.helper.php');
include($includePath . './utils/validators/fleetArray.validator.php');
include($includePath . './utils/validators/joinUnion.validator.php');
include($includePath . './utils/validators/missionHold.validator.php');

});

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

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

use UniEngine\Engine\Modules\FlightControl;

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

$newFleet = $props['newFleet'];

$availableHoldTimes = FlightControl\Utils\Helpers\getAvailableHoldTimes([]);

if (!(in_array($newFleet['HoldTime'], $availableHoldTimes))) {
return $isInvalid([
[ 'errorCode' => 'INVALID_HOLD_TIME', ],
]);
}

return $isValid();
}

?>

0 comments on commit 5fb3023

Please sign in to comment.