Skip to content

Commit

Permalink
GH-224 Move vacation enabling checks to an util
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 13, 2022
1 parent 1cf6817 commit 989f352
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 50 deletions.
3 changes: 3 additions & 0 deletions modules/settings/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Utils\ErrorMappers;

/**
* @param object $error As returned by Settings\Utils\Helpers\tryEnableVacation
*/
function mapTryEnableVacationErrorToReadableMessage($error) {
global $_Lang;

$errorCode = $error['code'];
$errorParams = $error['params'];

$knownErrorsByCode = [
'VACATION_MODE_NOT_AVAILABLE_YET' => $_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];
}

?>
5 changes: 5 additions & 0 deletions modules/settings/utils/helpers/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

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

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

use UniEngine\Engine\Modules\Settings;

/**
* @param array $params
* @param arrayRef $params['user']
* @param number $params['currentTimestamp']
*/
function tryEnableVacation($params) {
$executor = function ($input, $resultHelpers) {
$user = &$input['user'];
$currentTimestamp = $input['currentTimestamp'];

$userId = $user['id'];
$nextVacationAvailableAt = ($user['vacation_leavetime'] + TIME_DAY);

if ($currentTimestamp <= $nextVacationAvailableAt) {
return $resultHelpers['createFailure']([
'code' => '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);
}

?>
63 changes: 13 additions & 50 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 989f352

Please sign in to comment.