Skip to content

Commit

Permalink
GH-139 Reuse validateFleetArray in fleet2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Dec 7, 2020
1 parent d60fcce commit 1af17ed
Showing 1 changed file with 47 additions and 44 deletions.
91 changes: 47 additions & 44 deletions fleet2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1af17ed

Please sign in to comment.