Skip to content

Commit

Permalink
GH-139 Move union joining attempt calculation into an util
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed May 29, 2022
1 parent bd82c99 commit bb8cc7a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 30 deletions.
65 changes: 35 additions & 30 deletions fleet1.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,39 +122,38 @@
}
}

$_Lang['P_HideACSJoining'] = $Hide;
$GetACSData = intval($_POST['getacsdata']);
$SetPosNotEmpty = false;
if($GetACSData > 0)
{
$ACSData = doquery("SELECT `id`, `name`, `end_galaxy`, `end_system`, `end_planet`, `end_type`, `start_time` FROM {{table}} WHERE `id` = {$GetACSData};", 'acs', true);
if($ACSData['id'] == $GetACSData)
{
if($ACSData['start_time'] > $Now)
{
$SetPos['g'] = $ACSData['end_galaxy'];
$SetPos['s'] = $ACSData['end_system'];
$SetPos['p'] = $ACSData['end_planet'];
$SetPos['t'] = $ACSData['end_type'];

$SetPosNotEmpty = true;
$_Lang['P_HideACSJoining'] = '';
$_Lang['fl1_ACSJoiningFleet'] = sprintf($_Lang['fl1_ACSJoiningFleet'], $ACSData['name'], $ACSData['end_galaxy'], $ACSData['end_system'], $ACSData['end_planet']);
$_Lang['P_DisableCoordSel'] = 'disabled';
$_Lang['SelectedACSID'] = $GetACSData;
}
else
{
message($_Lang['fl1_ACSTimeUp'], $ErrorTitle, 'fleet.php', 3);
}
}
else
{
message($_Lang['fl1_ACSNoExist'], $ErrorTitle, 'fleet.php', 3);
$SetPos = [];

$inputJoinUnionId = intval($_POST['getacsdata']);

if ($inputJoinUnionId > 0) {
$joinUnionResult = FlightControl\Utils\Helpers\tryJoinUnion([
'unionId' => $inputJoinUnionId,
'currentTimestamp' => $Now,
]);

if (!$joinUnionResult['isSuccess']) {
$errorMessage = FlightControl\Utils\Errors\mapTryJoinUnionErrorToReadableMessage($joinUnionResult['error']);

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

$unionData = $joinUnionResult['payload']['unionData'];

$SetPos['g'] = $unionData['end_galaxy'];
$SetPos['s'] = $unionData['end_system'];
$SetPos['p'] = $unionData['end_planet'];
$SetPos['t'] = $unionData['end_type'];

$_Lang['fl1_ACSJoiningFleet'] = sprintf(
$_Lang['fl1_ACSJoiningFleet'],
$unionData['name'], $unionData['end_galaxy'], $unionData['end_system'], $unionData['end_planet']
);
$_Lang['P_DisableCoordSel'] = 'disabled';
$_Lang['SelectedACSID'] = $unionData['id'];
}

if($SetPosNotEmpty !== true)
if(empty($SetPos))
{
$SetPos['g'] = intval($_POST['galaxy']);
$SetPos['s'] = intval($_POST['system']);
Expand Down Expand Up @@ -300,6 +299,12 @@
$_Lang['P_HideNoFastLinks'] = '';
}

$_Lang['P_HideACSJoining'] = (
empty($_Lang['SelectedACSID']) ?
$Hide :
null
);

$Page = parsetemplate(gettemplate('fleet1_body'), $_Lang);
display($Page, $_Lang['fl_title']);

Expand Down
2 changes: 2 additions & 0 deletions modules/flightControl/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
include($includePath . './utils/helpers/getUserFleetSlotsCount.helper.php');
include($includePath . './utils/helpers/getValidMissionTypes.helper.php');
include($includePath . './utils/helpers/noobProtection.helper.php');
include($includePath . './utils/helpers/tryJoinUnion.helper.php');
include($includePath . './utils/inputs/normalizeFleetResources.inputs.php');
include($includePath . './utils/inputs/normalizeGobackFleetArray.inputs.php');
include($includePath . './utils/updaters/createUnionEntry.updaters.php');
Expand Down Expand Up @@ -107,6 +108,7 @@
include($includePath . './utils/errors/quantumGate.utils.php');
include($includePath . './utils/errors/smartFleetsBlockade.utils.php');
include($includePath . './utils/errors/targetOwner.utils.php');
include($includePath . './utils/errors/tryJoinUnion.utils.php');

});

Expand Down
25 changes: 25 additions & 0 deletions modules/flightControl/utils/errors/tryJoinUnion.utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace UniEngine\Engine\Modules\FlightControl\Utils\Errors;

/**
* @param object $error As returned by FlightControl\Utils\Helpers\tryJoinUnion
*/
function mapTryJoinUnionErrorToReadableMessage($error) {
global $_Lang;

$errorCode = $error['code'];

$knownErrorsByCode = [
'INVALID_UNION_ID' => $_Lang['fl1_ACSNoExist'],
'UNION_JOIN_TIME_EXPIRED' => $_Lang['fl1_ACSTimeUp'],
];

if (!isset($knownErrorsByCode[$errorCode])) {
return $_Lang['fleet_generic_errors_unknown'];
}

return $knownErrorsByCode[$errorCode];
}

?>
50 changes: 50 additions & 0 deletions modules/flightControl/utils/helpers/tryJoinUnion.helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace UniEngine\Engine\Modules\FlightControl\Utils\Helpers;

/**
* @param array $params
* @param number $params['unionId']
* @param number $params['currentTimestamp']
*/
function tryJoinUnion($params) {
$executor = function ($input, $resultHelpers) {
$unionId = $input['unionId'];
$currentTimestamp = $input['currentTimestamp'];

$fetchUnionDataQuery = (
"SELECT " .
"`id`, `name`, `start_time`, " .
"`end_galaxy`, `end_system`, `end_planet`, `end_type` " .
"FROM {{table}} " .
"WHERE " .
"`id` = {$unionId} " .
"LIMIT 1 " .
";"
);
$fetchUnionDataResult = doquery($fetchUnionDataQuery, 'acs', true);

if (!$fetchUnionDataResult) {
return $resultHelpers['createFailure']([
'code' => 'INVALID_UNION_ID',
]);
}

if ($fetchUnionDataResult['start_time'] <= $currentTimestamp) {
return $resultHelpers['createFailure']([
'code' => 'UNION_JOIN_TIME_EXPIRED',
]);
}

// TODO: To prevent union data leakage,
// check whether this union is accessible to this user

return $resultHelpers['createSuccess']([
'unionData' => $fetchUnionDataResult,
]);
};

return createFuncWithResultHelpers($executor)($params);
}

?>

0 comments on commit bb8cc7a

Please sign in to comment.