Skip to content

Commit

Permalink
GH-139 Remove redundant fleet array parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed May 26, 2022
1 parent 9826ff9 commit d3a8842
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
26 changes: 9 additions & 17 deletions fleet1.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
$shipsDetails = [];

$Fleet = [
'count' => 0,
'array' => [],
'storage' => 0,
'FuelStorage' => 0,
];
Expand Down Expand Up @@ -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,
];
Expand Down
34 changes: 16 additions & 18 deletions fleet2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand All @@ -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'],
Expand Down
26 changes: 11 additions & 15 deletions fleet3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand All @@ -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'],
Expand Down

0 comments on commit d3a8842

Please sign in to comment.