From 989f3522355bd34d8b569bf87a9033d41e19ac7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Mon, 13 Jun 2022 21:53:18 +0200 Subject: [PATCH] GH-224 Move vacation enabling checks to an util --- modules/settings/_includes.php | 3 + .../tryEnableVacation.errorMapper.php | 38 ++++++++ modules/settings/utils/helpers/index.php | 5 ++ .../helpers/tryEnableVacation.helper.php | 88 +++++++++++++++++++ settings.php | 63 +++---------- 5 files changed, 147 insertions(+), 50 deletions(-) create mode 100644 modules/settings/utils/errorMappers/tryEnableVacation.errorMapper.php create mode 100644 modules/settings/utils/helpers/index.php create mode 100644 modules/settings/utils/helpers/tryEnableVacation.helper.php diff --git a/modules/settings/_includes.php b/modules/settings/_includes.php index ec5689a00..cc5689325 100644 --- a/modules/settings/_includes.php +++ b/modules/settings/_includes.php @@ -8,9 +8,12 @@ include($includePath . './utils/content/prepareChangeProcessEmails.content.php'); + include($includePath . './utils/errorMappers/tryEnableVacation.errorMapper.php'); include($includePath . './utils/errorMappers/validatePasswordChange.errorMapper.php'); include($includePath . './utils/errorMappers/validateEmailChange.errorMapper.php'); + include($includePath . './utils/helpers/tryEnableVacation.helper.php'); + include($includePath . './utils/queries/createEmailChangeProcessEntry.query.php'); include($includePath . './utils/queries/getMovingFleetsCount.query.php'); include($includePath . './utils/queries/getUserWithEmailAddress.query.php'); diff --git a/modules/settings/utils/errorMappers/tryEnableVacation.errorMapper.php b/modules/settings/utils/errorMappers/tryEnableVacation.errorMapper.php new file mode 100644 index 000000000..c151d6c7c --- /dev/null +++ b/modules/settings/utils/errorMappers/tryEnableVacation.errorMapper.php @@ -0,0 +1,38 @@ + $_Lang['Vacation_24hNotPassed'], + 'VACATION_CANNOT_START_FLYING_FLEETS' => $_Lang['Vacation_FlyingFleets'], + 'VACATION_CANNOT_START_DEVELOPMENT' => function ($params) use (&$_Lang) { + $blockingPlanets = array_map_withkeys($params['blockingPlanets'], function ($planet) { + return "{$planet['name']} [{$planet['galaxy']}:{$planet['system']}:{$planet['planet']}]"; + }); + $blockingPlanetLabels = implode(', ', $blockingPlanets); + + return sprintf($_Lang['Vacation_CannotBuildOrRes'], $blockingPlanetLabels); + }, + ]; + + if (!isset($knownErrorsByCode[$errorCode])) { + return $_Lang['fleet_generic_errors_unknown']; + } + + if (is_callable($knownErrorsByCode[$errorCode])) { + return $knownErrorsByCode[$errorCode]($errorParams); + } + + return $knownErrorsByCode[$errorCode]; +} + +?> diff --git a/modules/settings/utils/helpers/index.php b/modules/settings/utils/helpers/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/settings/utils/helpers/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/settings/utils/helpers/tryEnableVacation.helper.php b/modules/settings/utils/helpers/tryEnableVacation.helper.php new file mode 100644 index 000000000..ee7f6b835 --- /dev/null +++ b/modules/settings/utils/helpers/tryEnableVacation.helper.php @@ -0,0 +1,88 @@ + 'VACATION_MODE_NOT_AVAILABLE_YET', + 'params' => [ + 'nextAvailableAt' => $nextVacationAvailableAt, + ], + ]); + } + + $movingFleetsCount = Settings\Utils\Queries\getMovingFleetsCount([ 'userId' => $userId ]); + + if ($movingFleetsCount > 0) { + return $resultHelpers['createFailure']([ + 'code' => 'VACATION_CANNOT_START_FLYING_FLEETS', + ]); + } + + $userPlanets = doquery("SELECT * FROM {{table}} WHERE `id_owner` = {$userId};", 'planets'); + $updateResults = [ + 'planets' => [], + ]; + $planetLabelDetails = []; + $blockingPlanets = []; + + mapQueryResults($userPlanets, function ($planet) use (&$user, $currentTimestamp, &$updateResults, &$planetLabelDetails, &$blockingPlanets) { + $planetId = $planet['id']; + $planetLabelDetails[$planetId] = [ + 'name' => $planet['name'], + 'galaxy' => $planet['galaxy'], + 'system' => $planet['system'], + 'planet' => $planet['planet'], + ];; + + $hasPlanetBeenUpdated = HandlePlanetUpdate($planet, $user, $currentTimestamp, true) === true; + + if ($hasPlanetBeenUpdated) { + $updateResults['planets'][] = $planet; + } + if ( + $planet['buildQueue_firstEndTime'] > 0 || + $planet['shipyardQueue'] != 0 + ) { + $blockingPlanets[$planetId] = $planetLabelDetails[$planetId]; + } + }); + + HandlePlanetUpdate_MultiUpdate($updateResults, $user); + + if ($user['techQueue_EndTime'] > 0) { + $techPlanetId = $user['techQueue_Planet']; + $blockingPlanets[$techPlanetId] = $planetLabelDetails[$techPlanetId]; + } + + if (!empty($blockingPlanets)) { + return $resultHelpers['createFailure']([ + 'code' => 'VACATION_CANNOT_START_DEVELOPMENT', + 'params' => [ + 'blockingPlanets' => $blockingPlanets, + ], + ]); + } + + return $resultHelpers['createSuccess']([]); + }; + + return createFuncWithResultHelpers($executor)($params); +} + +?> diff --git a/settings.php b/settings.php index 861983bed..640dfa907 100644 --- a/settings.php +++ b/settings.php @@ -672,57 +672,20 @@ $_Lang['SetActiveMarker'] = '04'; if((isset($_POST['vacation_activate']) && $_POST['vacation_activate'] == 'on') || (isset($_POST['delete_activate']) && $_POST['delete_activate'] == 'on')) { - if(isset($_POST['vacation_activate']) && $_POST['vacation_activate'] == 'on') - { - $allowVacation = true; - if(($_User['vacation_leavetime'] + TIME_DAY) >= $Now) - { - $allowVacation = false; - $WarningMsgs[] = $_Lang['Vacation_24hNotPassed']; - } - - $movingFleetsCount = Settings\Utils\Queries\getMovingFleetsCount([ 'userId' => $_User['id'] ]); - - if ($movingFleetsCount > 0) { - $allowVacation = false; - $WarningMsgs[] = $_Lang['Vacation_FlyingFleets']; - } - - if($allowVacation === true) - { - // Update All Planets/Moons before VacationMode (don't do that if previous conditions are not fulfilled) - $SQLResult_AllPlanets = doquery("SELECT * FROM {{table}} WHERE `id_owner` = {$_User['id']};", 'planets'); - - $Results['planets'] = array(); - while($PlanetsData = $SQLResult_AllPlanets->fetch_assoc()) - { - // Update Planet - Building Queue - $GeneratePlanetName[$PlanetsData['id']] = "{$PlanetsData['name']} [{$PlanetsData['galaxy']}:{$PlanetsData['system']}:{$PlanetsData['planet']}]"; - - if(HandlePlanetUpdate($PlanetsData, $_User, $Now, true) === true) - { - $Results['planets'][] = $PlanetsData; - } - if($PlanetsData['buildQueue_firstEndTime'] > 0 OR $PlanetsData['shipyardQueue'] != 0) - { - $FoundBlockingPlanets[$PlanetsData['id']] = $GeneratePlanetName[$PlanetsData['id']]; - } - } - HandlePlanetUpdate_MultiUpdate($Results, $_User); - if($_User['techQueue_EndTime'] > 0) - { - $FoundBlockingPlanets[$PlanetsData['id']] = $GeneratePlanetName[$_User['techQueue_Planet']]; - } - - if(!empty($FoundBlockingPlanets)) - { - $allowVacation = false; - $WarningMsgs[] = sprintf($_Lang['Vacation_CannotBuildOrRes'], implode(', ', $FoundBlockingPlanets)); - } - } + if ( + isset($_POST['vacation_activate']) && + $_POST['vacation_activate'] == 'on' + ) { + $tryEnableVacationResult = Settings\Utils\Helpers\tryEnableVacation([ + 'user' => &$_User, + 'currentTimestamp' => $Now, + ]); - if($allowVacation === true) - { + if (!$tryEnableVacationResult['isSuccess']) { + $WarningMsgs[] = Settings\Utils\ErrorMappers\mapTryEnableVacationErrorToReadableMessage( + $tryEnableVacationResult['error'] + ); + } else { $ChangeSet['is_onvacation'] = '1'; $ChangeSet['vacation_starttime'] = $Now; $ChangeSet['vacation_endtime'] = $Now + (MAXVACATIONS_REG * TIME_DAY);