-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-224 Move vacation enabling checks to an util
- Loading branch information
Showing
5 changed files
with
147 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
modules/settings/utils/errorMappers/tryEnableVacation.errorMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
88
modules/settings/utils/helpers/tryEnableVacation.helper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters