From 1af17edda4fb6040d854cf238acd14c29e9737f1 Mon Sep 17 00:00:00 2001 From: Michal Dziekonski Date: Mon, 7 Dec 2020 02:15:04 +0100 Subject: [PATCH] GH-139 Reuse validateFleetArray in fleet2 --- fleet2.php | 91 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/fleet2.php b/fleet2.php index fc0934c7b..1c6e61556 100644 --- a/fleet2.php +++ b/fleet2.php @@ -5,6 +5,10 @@ $_EnginePath = './'; include($_EnginePath.'common.php'); +include($_EnginePath . 'modules/flightControl/_includes.php'); + +use UniEngine\Engine\Modules\FlightControl; + loggedCheck(); if((!isset($_POST['sending_fleet']) || $_POST['sending_fleet'] != '1') && (!isset($_POST['fromEnd']) || $_POST['fromEnd'] != '1')) @@ -234,55 +238,54 @@ !empty($Fleet['array']) && is_array($Fleet['array']) ) { - foreach($Fleet['array'] as $ShipID => $ShipCount) - { - $ShipID = intval($ShipID); - if(in_array($ShipID, $_Vars_ElementCategories['fleet'])) - { - if(!empty($_Vars_Prices[$ShipID]['engine'])) - { - $ShipCount = floor($ShipCount); - if($ShipCount > 0) - { - if($_Planet[$_Vars_GameElements[$ShipID]] >= $ShipCount) - { - $FleetArray[$ShipID] = $ShipCount; - $Fleet['count'] += $ShipCount; - $ThisStorage = $_Vars_Prices[$ShipID]['capacity'] * $ShipCount; - if($ShipID != 210) - { - $Fleet['storage'] += $ThisStorage; - } - else - { - $Fleet['FuelStorage'] += $ThisStorage; - } - } - else - { - message($_Lang['fl1_NoEnoughShips'], $ErrorTitle, 'fleet.php', 3); - } - } - else - { - message($_Lang['fleet_generic_errors_invalidshipcount'], $ErrorTitle, 'fleet.php', 3); - } - } - else - { - message($_Lang['fl1_CantSendUnflyable'], $ErrorTitle, 'fleet.php', 3); - } + $fleetArrayValidationResult = FlightControl\Utils\Validators\validateFleetArray([ + 'fleet' => $Fleet['array'], + 'planet' => &$_Planet, + 'isFromDirectUserInput' => false, + ]); + + if (!$fleetArrayValidationResult['isValid']) { + $firstValidationError = $fleetArrayValidationResult['errors'][0]; + + $errorMessage = null; + switch ($firstValidationError['errorCode']) { + case 'INVALID_SHIP_ID': + $errorMessage = $_Lang['fl1_BadShipGiven']; + break; + case 'SHIP_WITH_NO_ENGINE': + $errorMessage = $_Lang['fl1_CantSendUnflyable']; + break; + case 'INVALID_SHIP_COUNT': + $errorMessage = $_Lang['fleet_generic_errors_invalidshipcount']; + break; + case 'SHIP_COUNT_EXCEEDS_AVAILABLE': + $errorMessage = $_Lang['fl1_NoEnoughShips']; + break; + default: + $errorMessage = $_Lang['fleet_generic_errors_unknown']; + break; } - else - { - message($_Lang['fl1_BadShipGiven'], $ErrorTitle, 'fleet.php', 3); + + message($errorMessage, $ErrorTitle, 'fleet.php', 3); + } + + foreach ($Fleet['array'] as $ShipID => $ShipCount) { + $ShipID = intval($ShipID); + $ShipCount = floor($ShipCount); + $FleetArray[$ShipID] = $ShipCount; + $Fleet['count'] += $ShipCount; + $ThisStorage = $_Vars_Prices[$ShipID]['capacity'] * $ShipCount; + + if ($ShipID != 210) { + $Fleet['storage'] += $ThisStorage; + } else { + $Fleet['FuelStorage'] += $ThisStorage; } } -} -else -{ +} else { message($_Lang['fl2_FleetArrayPostEmpty'], $ErrorTitle, 'fleet.php', 3); } + if($Fleet['count'] <= 0) { message($_Lang['fl2_ZeroShips'], $ErrorTitle, 'fleet.php', 3);