Skip to content

Commit

Permalink
GH-139 Use generic fleet array validator in fleet1.php
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Dec 7, 2020
1 parent fa1a8ef commit 7808ca2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 45 deletions.
103 changes: 58 additions & 45 deletions fleet1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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['gobackUsed']) || $_POST['gobackUsed'] != '1'))
Expand Down Expand Up @@ -91,54 +95,63 @@
}

// Management of ShipsList
if(!empty($_POST['ship']))
{
foreach($_POST['ship'] as $ShipID => $ShipCount)
{
if (!empty($_POST['ship'])) {
$fleetArrayValidationResult = FlightControl\Utils\Validators\validateFleetArray([
'fleet' => $_POST['ship'],
'planet' => &$_Planet,
'isFromDirectUserInput' => true,
]);

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;
}

message($errorMessage, $ErrorTitle, 'fleet.php', 3);
}

foreach ($_POST['ship'] as $ShipID => $ShipCount) {
$ShipID = intval($ShipID);
if(in_array($ShipID, $_Vars_ElementCategories['fleet']))
{
if(!empty($_Vars_Prices[$ShipID]['engine']))
{
$ShipCount = floor(str_replace('.', '', $ShipCount));
if($ShipCount > 0)
{
if($_Planet[$_Vars_GameElements[$ShipID]] >= $ShipCount)
{
$Fleet['array'][$ShipID] = $ShipCount;
$Fleet['count'] += $ShipCount;
$ThisStorage = $_Vars_Prices[$ShipID]['capacity'] * $ShipCount;
if($ShipID != 210)
{
$Fleet['storage'] += $ThisStorage;
}
else
{
$Fleet['FuelStorage'] += $ThisStorage;
}
$speedalls[$ShipID] = getShipsCurrentSpeed($ShipID, $_User);
$shipConsumption = getShipsCurrentConsumption($ShipID, $_User);
$allShipsConsumption = ($shipConsumption * $ShipCount);

// TODO: Check if that "+1" is correct
$FleetHiddenBlock .= "<input type=\"hidden\" id=\"consumption{$ShipID}\" value=\"".((string)($allShipsConsumption + 1))."\" />";
$FleetHiddenBlock .= "<input type=\"hidden\" id=\"speed{$ShipID}\" value=\"{$speedalls[$ShipID]}\" />";
}
else
{
message($_Lang['fl1_NoEnoughShips'], $ErrorTitle, 'fleet.php', 3);
}
}
}
else
{
message($_Lang['fl1_CantSendUnflyable'], $ErrorTitle, 'fleet.php', 3);
}
$ShipCount = floor(str_replace('.', '', $ShipCount));

if ($ShipCount <= 0) {
continue;
}
else
{
message($_Lang['fl1_BadShipGiven'], $ErrorTitle, 'fleet.php', 3);

$Fleet['array'][$ShipID] = $ShipCount;
$Fleet['count'] += $ShipCount;
$ThisStorage = $_Vars_Prices[$ShipID]['capacity'] * $ShipCount;

if ($ShipID != 210) {
$Fleet['storage'] += $ThisStorage;
} else {
$Fleet['FuelStorage'] += $ThisStorage;
}

$speedalls[$ShipID] = getShipsCurrentSpeed($ShipID, $_User);
$shipConsumption = getShipsCurrentConsumption($ShipID, $_User);
$allShipsConsumption = ($shipConsumption * $ShipCount);

// TODO: Check if that "+1" is correct
$FleetHiddenBlock .= "<input type=\"hidden\" id=\"consumption{$ShipID}\" value=\"".((string)($allShipsConsumption + 1))."\" />";
$FleetHiddenBlock .= "<input type=\"hidden\" id=\"speed{$ShipID}\" value=\"{$speedalls[$ShipID]}\" />";
}
}

Expand Down
4 changes: 4 additions & 0 deletions language/en/fleet.lang
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,8 @@ $_Lang['CannotUseQuantumGateTill'] = 'You cannot perform a space j
$_Lang['GateReadyToUse'] = 'Quantum Gate is ready for space jump';
$_Lang['GateReadyToUseIn'] = 'Quantum Gate will be ready for space jump in';

// Generic translation strings
$_Lang['fleet_generic_errors_unknown'] = 'Unknown error';
$_Lang['fleet_generic_errors_invalidshipcount'] = 'Invalid ship count provided!';

?>
4 changes: 4 additions & 0 deletions language/pl/fleet.lang
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,8 @@ $_Lang['CannotUseQuantumGateTill'] = 'Nie możesz teraz wykonać S
$_Lang['GateReadyToUse'] = 'Brama Kwantowa gotowa do skoku';
$_Lang['GateReadyToUseIn'] = 'Brama Kwantowa gotowa do skoku za';

// Generic translation strings
$_Lang['fleet_generic_errors_unknown'] = 'Nieznany błąd';
$_Lang['fleet_generic_errors_invalidshipcount'] = 'Podano niepoprawną liczbę statków!';

?>

0 comments on commit 7808ca2

Please sign in to comment.