Skip to content

Commit

Permalink
Merge pull request #140 from mdziekon/mdziekon/gh-139/flight-control-…
Browse files Browse the repository at this point in the history
…refactor-part01

Flight Control refactor (part 01) - Fleet array validation
  • Loading branch information
mdziekon authored Dec 7, 2020
2 parents 4848ee2 + 9f89f41 commit ae3dfbd
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 142 deletions.
104 changes: 59 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,64 @@
}

// 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 = getShipsStorageCapacity($ShipID) * $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
102 changes: 54 additions & 48 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 @@ -227,60 +231,62 @@
$Fleet['storage'] = 0;
$Fleet['FuelStorage'] = 0;

$Fleet['array'] = explode(';', $_POST['FleetArray']);
$Fleet['array'] = String2Array($_POST['FleetArray']);
$FleetArray = array();
if(!empty($Fleet['array']) AND (array)$Fleet['array'] === $Fleet['array'])
{
foreach($Fleet['array'] as $ShipData)
{
$ShipData = explode(',', $ShipData);
$ShipID = intval($ShipData[0]);
if(in_array($ShipID, $_Vars_ElementCategories['fleet']))
{
if(!empty($_Vars_Prices[$ShipID]['engine']))
{
$ShipCount = floor($ShipData[1]);
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['fl2_ShipCountCantBe0'], $ErrorTitle, 'fleet.php', 3);
}
}
else
{
message($_Lang['fl1_CantSendUnflyable'], $ErrorTitle, 'fleet.php', 3);
}

if (
!empty($Fleet['array']) &&
is_array($Fleet['array'])
) {
$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 = getShipsStorageCapacity($ShipID) * $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
105 changes: 56 additions & 49 deletions fleet3.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

include($_EnginePath.'common.php');

include($_EnginePath . 'modules/flightControl/_includes.php');

use UniEngine\Engine\Modules\FlightControl;
use UniEngine\Engine\Modules\Flights;

loggedCheck();
Expand Down Expand Up @@ -45,7 +48,6 @@ function messageRed($Text, $Title)
$Target['planet'] = intval($_POST['planet']);
$Target['type'] = intval($_POST['planettype']);
$Fleet['Speed'] = floatval($_POST['speed']);
$Fleet['array'] = explode(';', $_POST['FleetArray']);
$Fleet['UseQuantum'] = (isset($_POST['usequantumgate']) && $_POST['usequantumgate'] == 'on' ? true : false);
$Fleet['resources'] = array('metal' => $_POST['resource1'], 'crystal' => $_POST['resource2'], 'deuterium' => $_POST['resource3']);
$Fleet['ExpeTime'] = intval($_POST['expeditiontime']);
Expand Down Expand Up @@ -405,60 +407,65 @@ function messageRed($Text, $Title)
$Fleet['FuelStorage'] = 0;
$Fleet['TotalResStorage'] = 0;

$Fleet['array'] = String2Array($_POST['FleetArray']);
$FleetArray = array();
if(!empty($Fleet['array']) AND (array)$Fleet['array'] === $Fleet['array'])
{
foreach($Fleet['array'] as $ShipData)
{
$ShipData = explode(',', $ShipData);
$ShipID = intval($ShipData[0]);
if(in_array($ShipID, $_Vars_ElementCategories['fleet']))
{
if(!empty($_Vars_Prices[$ShipID]['engine']))
{
$ShipCount = floor($ShipData[1]);
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;
}
$FleetRemover[] = "`{$_Vars_GameElements[$ShipID]}` = `{$_Vars_GameElements[$ShipID]}` - {$ShipCount}";
}
else
{
messageRed($_Lang['fl1_NoEnoughShips'], $ErrorTitle);
}
}
else
{
messageRed($_Lang['fl2_ShipCountCantBe0'], $ErrorTitle);
}
}
else
{
messageRed($_Lang['fl1_CantSendUnflyable'], $ErrorTitle);
}

if (
!empty($Fleet['array']) &&
is_array($Fleet['array'])
) {
$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
{
messageRed($_Lang['fl1_BadShipGiven'], $ErrorTitle);

messageRed($errorMessage, $ErrorTitle);
}

foreach ($Fleet['array'] as $ShipID => $ShipCount) {
$ShipID = intval($ShipID);
$ShipCount = floor($ShipCount);
$FleetArray[$ShipID] = $ShipCount;
$Fleet['count'] += $ShipCount;

$ThisStorage = getShipsStorageCapacity($ShipID) * $ShipCount;

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

$planetElementKey = _getElementPlanetKey($ShipID);
$FleetRemover[] = "`{$planetElementKey}` = `{$planetElementKey}` - {$ShipCount}";
}
}
else
{
} else {
messageRed($_Lang['fl2_FleetArrayPostEmpty'], $ErrorTitle);
}

if($Fleet['count'] <= 0)
{
messageRed($_Lang['fl2_ZeroShips'], $ErrorTitle);
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!';

?>
Loading

0 comments on commit ae3dfbd

Please sign in to comment.