Skip to content

Commit

Permalink
GH-230 Move Planet name change screen's code completely to its submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jul 4, 2022
1 parent a2d261d commit 41cbccf
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 55 deletions.
2 changes: 2 additions & 0 deletions modules/overview/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
include($includePath . './screens/FirstLogin/utils/effects/updateUserOnFirstLogin.effect.php');
include($includePath . './screens/FirstLogin/utils/helpers/getReferrerTasksData.helper.php');

include($includePath . './screens/PlanetNameChange/PlanetNameChange.screen.php');
include($includePath . './screens/PlanetNameChange/PlanetNameChange.utils.php');
include($includePath . './screens/PlanetNameChange/utils/errorMappers/validateNewName.errorMapper.php');
include($includePath . './screens/PlanetNameChange/utils/validators/validateNewName.validator.php');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace UniEngine\Engine\Modules\Overview\Screens\PlanetNameChange;

use UniEngine\Engine\Common;
use UniEngine\Engine\Modules\Overview\Screens\PlanetNameChange;

/**
* @param array $params
* @param arrayRef $params['input']
* @param arrayRef $params['user']
* @param arrayRef $params['planet']
*/
function render($props) {
global $_Lang;

$input = &$props['input'];
$user = &$props['user'];
$planet = &$props['planet'];

$effectsResult = PlanetNameChange\runEffects([
'input' => &$input,
'user' => &$user,
'planet' => &$planet,
]);

$screenTitle = $_Lang['Rename_TitleMain'];
$localTemplateLoader = createLocalTemplateLoader(__DIR__);

$isOnPlanet = $planet['planet_type'] == 1;
$galaxyPlanetLink = Common\Components\GalaxyPlanetLink\render([
'coords' => $planet,
]);

$tplBodyParams = [
'Rename_CurrentName' => sprintf(
$_Lang['Rename_CurrentName'],
(
$isOnPlanet ?
$_Lang['Rename_Planet'] :
$_Lang['Rename_Moon']
)
),
'Rename_Ins_CurrentName' => "{$planet['name']} {$galaxyPlanetLink}",

'Rename_Ins_MsgHide' => (
$effectsResult['isSuccess'] === null ?
'style="display: none;"' :
''
),
'Rename_Ins_MsgColor' => (
$effectsResult['isSuccess'] !== null ?
$effectsResult['payload']['color'] :
''
),
'Rename_Ins_MsgTxt' => (
$effectsResult['isSuccess'] !== null ?
$effectsResult['payload']['message'] :
''
),
];
$tplBodyParams = array_merge($_Lang, $tplBodyParams);

$componentHTML = parsetemplate(
$localTemplateLoader('body'),
$tplBodyParams
);

display($componentHTML, $screenTitle);
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace UniEngine\Engine\Modules\Overview\Screens\PlanetNameChange;

use UniEngine\Engine\Modules\Overview\Screens\PlanetNameChange;

/**
* @param array $params
* @param arrayRef $params['input']
* @param arrayRef $params['user']
* @param arrayRef $params['planet']
*/
function runEffects($props) {
global $_Lang;

$input = &$props['input'];
$user = &$props['user'];
$planet = &$props['planet'];

if (
!isset($input['action']) ||
$input['action'] != 'do'
) {
return [
'isSuccess' => null,
];
}

$newName = (
isset($input['set_newname']) ?
trim($input['set_newname']) :
''
);

$nameChangeValidationResult = PlanetNameChange\Utils\Validators\validateNewName([
'input' => [
'newName' => $newName,
],
'planet' => &$planet,
]);

if (!$nameChangeValidationResult['isSuccess']) {
$errorMessage = PlanetNameChange\Utils\ErrorMappers\mapValidateNewNameErrorToReadableMessage(
$nameChangeValidationResult['error']
);

return [
'isSuccess' => false,
'payload' => [
'message' => $errorMessage,
'color' => 'red',
],
];
}

$planet['name'] = $newName;

doquery("UPDATE {{table}} SET `name` = '{$newName}' WHERE `id` = {$user['current_planet']} LIMIT 1;", 'planets');

return [
'isSuccess' => true,
'payload' => [
'message' => $_Lang['RenamePlanet_NameSaved'],
'color' => 'lime',
],
];
}

?>
59 changes: 4 additions & 55 deletions overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,63 +39,12 @@
switch($mode)
{
case 'rename':
// --- Rename Planet Page ---
$parse = $_Lang;

$parse['Rename_Ins_MsgHide'] = 'style="display: none;"';
$parse['Rename_Ins_MsgTxt'] = '';

if($_Planet['planet_type'] == 1)
{
$parse['Rename_CurrentName'] = sprintf($parse['Rename_CurrentName'], $parse['Rename_Planet']);
}
else
{
$parse['Rename_CurrentName'] = sprintf($parse['Rename_CurrentName'], $parse['Rename_Moon']);
}

if (
isset($_POST['action']) &&
$_POST['action'] == 'do'
) {
// User wants to change planets name
$NewName = trim($_POST['set_newname']);

$nameChangeValidationResult = Overview\Screens\PlanetNameChange\Utils\Validators\validateNewName([
'input' => [
'newName' => $NewName,
],
'planet' => &$_Planet,
]);

if (!$nameChangeValidationResult['isSuccess']) {
$errorMessage = Overview\Screens\PlanetNameChange\Utils\ErrorMappers\mapValidateNewNameErrorToReadableMessage(
$nameChangeValidationResult['error']
);

$parse['Rename_Ins_MsgColor'] = 'red';
$parse['Rename_Ins_MsgTxt'] = $errorMessage;
} else {
$_Planet['name'] = $NewName;
doquery("UPDATE {{table}} SET `name` = '{$NewName}' WHERE `id` = {$_User['current_planet']} LIMIT 1;", 'planets');

$parse['Rename_Ins_MsgColor'] = 'lime';
$parse['Rename_Ins_MsgTxt'] = $_Lang['RenamePlanet_NameSaved'];
}
}

if ($parse['Rename_Ins_MsgTxt'] !== '') {
$parse['Rename_Ins_MsgHide'] = '';
}

$galaxyPlanetLink = Common\Components\GalaxyPlanetLink\render([
'coords' => $_Planet,
Overview\Screens\PlanetNameChange\render([
'input' => &$_POST,
'user' => &$_User,
'planet' => &$_Planet,
]);

$parse['Rename_Ins_CurrentName'] = "{$_Planet['name']} {$galaxyPlanetLink}";

$page = parsetemplate(gettemplate('overview_rename'), $parse);
display($page, $_Lang['Rename_TitleMain']);
break;
case 'abandon':
// --- Abandon Colony ---
Expand Down

0 comments on commit 41cbccf

Please sign in to comment.