From d3a8842f026a42cc6547d5ef35887df6863f855f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Thu, 26 May 2022 02:37:51 +0200 Subject: [PATCH] GH-139 Remove redundant fleet array parsing --- fleet1.php | 26 +++++++++----------------- fleet2.php | 34 ++++++++++++++++------------------ fleet3.php | 26 +++++++++++--------------- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/fleet1.php b/fleet1.php index 9090715a..36f5915e 100644 --- a/fleet1.php +++ b/fleet1.php @@ -29,7 +29,7 @@ $shipsDetails = []; $Fleet = [ - 'count' => 0, + 'array' => [], 'storage' => 0, 'FuelStorage' => 0, ]; @@ -103,30 +103,22 @@ message($errorMessage, $ErrorTitle, 'fleet.php', 3); } - foreach ($_POST['ship'] as $ShipID => $ShipCount) { - $ShipID = intval($ShipID); - $ShipCount = floor(str_replace('.', '', $ShipCount)); + $Fleet['array'] = $fleetArrayParsingResult['payload']['parsedFleet']; - if ($ShipCount <= 0) { - continue; - } - - $Fleet['array'][$ShipID] = $ShipCount; - $Fleet['count'] += $ShipCount; - - $allShipsOfTypeStorage = getShipsStorageCapacity($ShipID) * $ShipCount; + foreach ($Fleet['array'] as $shipId => $shipCount) { + $allShipsOfTypeStorage = getShipsStorageCapacity($shipId) * $shipCount; - if (canShipPillage($ShipID)) { + if (canShipPillage($shipId)) { $Fleet['storage'] += $allShipsOfTypeStorage; } else { $Fleet['FuelStorage'] += $allShipsOfTypeStorage; } - $shipSpeed = getShipsCurrentSpeed($ShipID, $_User); - $shipConsumption = getShipsCurrentConsumption($ShipID, $_User); - $allShipsConsumption = ($shipConsumption * $ShipCount); + $shipSpeed = getShipsCurrentSpeed($shipId, $_User); + $shipConsumption = getShipsCurrentConsumption($shipId, $_User); + $allShipsConsumption = ($shipConsumption * $shipCount); - $shipsDetails[$ShipID] = [ + $shipsDetails[$shipId] = [ 'speed' => $shipSpeed, 'totalConsumptionOfShipType' => (string) $allShipsConsumption, ]; diff --git a/fleet2.php b/fleet2.php index 5fd06f1f..eb58ad95 100644 --- a/fleet2.php +++ b/fleet2.php @@ -157,22 +157,24 @@ } // Parse Fleet Array -$Fleet['count'] = 0; -$Fleet['storage'] = 0; -$Fleet['FuelStorage'] = 0; +$Fleet = [ + 'array' => [], + 'count' => 0, + 'storage' => 0, + 'FuelStorage' => 0, +]; -$Fleet['array'] = String2Array($_POST['FleetArray']); -$FleetArray = []; +$inputFleetArray = String2Array($_POST['FleetArray']); if ( - empty($Fleet['array']) || - !is_array($Fleet['array']) + empty($inputFleetArray) || + !is_array($inputFleetArray) ) { message($_Lang['fl1_NoShipsGiven'], $ErrorTitle, 'fleet.php', 3); } $fleetArrayParsingResult = FlightControl\Utils\Validators\parseFleetArray([ - 'fleet' => $Fleet['array'], + 'fleet' => $inputFleetArray, 'planet' => &$_Planet, 'isFromDirectUserInput' => false, ]); @@ -184,24 +186,20 @@ message($errorMessage, $ErrorTitle, 'fleet.php', 3); } -foreach ($Fleet['array'] as $ShipID => $ShipCount) { - $ShipID = intval($ShipID); - $ShipCount = floor($ShipCount); - $FleetArray[$ShipID] = $ShipCount; - $Fleet['count'] += $ShipCount; +$Fleet['array'] = $fleetArrayParsingResult['payload']['parsedFleet']; + +foreach ($Fleet['array'] as $shipID => $shipCount) { + $Fleet['count'] += $shipCount; - $allShipsOfTypeStorage = getShipsStorageCapacity($ShipID) * $ShipCount; + $allShipsOfTypeStorage = getShipsStorageCapacity($shipID) * $shipCount; - if (canShipPillage($ShipID)) { + if (canShipPillage($shipID)) { $Fleet['storage'] += $allShipsOfTypeStorage; } else { $Fleet['FuelStorage'] += $allShipsOfTypeStorage; } } -$Fleet['array'] = $FleetArray; -unset($FleetArray); - $AvailableMissions = FlightControl\Utils\Helpers\getValidMissionTypes([ 'targetCoordinates' => $Target, 'fleetShips' => $Fleet['array'], diff --git a/fleet3.php b/fleet3.php index 0e9a358a..44a58633 100644 --- a/fleet3.php +++ b/fleet3.php @@ -173,23 +173,23 @@ function messageRed($Text, $Title) } // --- Parse Fleet Array +$Fleet['array'] = []; $Fleet['count'] = 0; $Fleet['storage'] = 0; $Fleet['FuelStorage'] = 0; $Fleet['TotalResStorage'] = 0; -$Fleet['array'] = String2Array($_POST['FleetArray']); -$FleetArray = []; +$inputFleetArray = String2Array($_POST['FleetArray']); if ( - empty($Fleet['array']) || - !is_array($Fleet['array']) + empty($inputFleetArray) || + !is_array($inputFleetArray) ) { messageRed($_Lang['fl1_NoShipsGiven'], $ErrorTitle); } $fleetArrayParsingResult = FlightControl\Utils\Validators\parseFleetArray([ - 'fleet' => $Fleet['array'], + 'fleet' => $inputFleetArray, 'planet' => &$_Planet, 'isFromDirectUserInput' => false, ]); @@ -201,24 +201,20 @@ function messageRed($Text, $Title) messageRed($errorMessage, $ErrorTitle); } -foreach ($Fleet['array'] as $ShipID => $ShipCount) { - $ShipID = intval($ShipID); - $ShipCount = floor($ShipCount); - $FleetArray[$ShipID] = $ShipCount; - $Fleet['count'] += $ShipCount; +$Fleet['array'] = $fleetArrayParsingResult['payload']['parsedFleet']; - $allShipsOfTypeStorage = getShipsStorageCapacity($ShipID) * $ShipCount; +foreach ($Fleet['array'] as $shipID => $shipCount) { + $Fleet['count'] += $shipCount; - if (canShipPillage($ShipID)) { + $allShipsOfTypeStorage = getShipsStorageCapacity($shipID) * $shipCount; + + if (canShipPillage($shipID)) { $Fleet['storage'] += $allShipsOfTypeStorage; } else { $Fleet['FuelStorage'] += $allShipsOfTypeStorage; } } -$Fleet['array'] = $FleetArray; -unset($FleetArray); - $validMissionTypes = FlightControl\Utils\Helpers\getValidMissionTypes([ 'targetCoordinates' => $Target, 'fleetShips' => $Fleet['array'],