Skip to content

Commit

Permalink
Merge pull request #233 from mdziekon/gh-230-planet-name-change-refactor
Browse files Browse the repository at this point in the history
GH-230 | Overview - Planet name change screen refactor
  • Loading branch information
mdziekon authored Jul 4, 2022
2 parents 35cfd8a + 41cbccf commit ccf1db0
Show file tree
Hide file tree
Showing 19 changed files with 336 additions and 97 deletions.
4 changes: 4 additions & 0 deletions common/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

include($includePath . './exceptions.php');

include($includePath . './components/GalaxyPlanetLink/GalaxyPlanetLink.component.php');

include($includePath . './modules/uni/utils/isEmailDomainBanned.util.php');

include($includePath . './utils/routing/galaxy.routing.php');

?>
26 changes: 26 additions & 0 deletions common/components/GalaxyPlanetLink/GalaxyPlanetLink.component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace UniEngine\Engine\Common\Components\GalaxyPlanetLink;

use UniEngine\Engine\Common\Utils\Routing;

/**
* @param array $props
* @param array $props['coords']
* @param number $props['coords']['galaxy']
* @param number $props['coords']['system']
* @param number $props['coords']['planet']
*/
function render($props) {
$linkText = "[{$props['coords']['galaxy']}:{$props['coords']['system']}:{$props['coords']['planet']}]";

return buildDOMElementHTML([
'tagName' => 'a',
'contentHTML' => $linkText,
'attrs' => [
'href' => Routing\getGalaxyTargetUrl($props['coords']),
],
]);
}

?>
5 changes: 5 additions & 0 deletions common/components/GalaxyPlanetLink/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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

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

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

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

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

?>
23 changes: 23 additions & 0 deletions common/utils/routing/galaxy.routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace UniEngine\Engine\Common\Utils\Routing;

/**
* @param array $params
* @param number $params['galaxy']
* @param number $params['system']
* @param number $params['planet']
*/
function getGalaxyTargetUrl($params) {
return buildHref([
'path' => 'galaxy.php',
'query' => [
'mode' => '3',
'galaxy' => $params['galaxy'],
'system' => $params['system'],
'planet' => $params['planet'],
],
]);
}

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

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

?>
5 changes: 5 additions & 0 deletions modules/overview/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
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');

});

?>
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',
],
];
}

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

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

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

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

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

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

/**
* @param object $error As returned by Overview\Screens\PlanetNameChange\Utils\Validators\validateNewName
*/
function mapValidateNewNameErrorToReadableMessage($error) {
global $_Lang;

$errorCode = $error['code'];

$knownErrorsByCode = [
'NEW_NAME_EMPTY' => $_Lang['RenamePlanet_0Lenght'],
'NEW_NAME_SAME_AS_OLD' => $_Lang['RenamePlanet_SameName'],
'NEW_NAME_TOO_SHORT' => $_Lang['RenamePlanet_TooShort'],
'NEW_NAME_TOO_LONG' => $_Lang['RenamePlanet_TooLong'],
'NEW_NAME_INVALID' => $_Lang['RenamePlanet_BadSigns'],
];

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

return $knownErrorsByCode[$errorCode];
}

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

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

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

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

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

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

/**
* @param array $params
* @param array $params['input']
* @param string $params['input']['newName']
* @param arrayRef $params['planet']
*/
function validateNewName($params) {
$planet = &$params['planet'];

$executor = function ($input, $resultHelpers) use (&$planet) {
$newName = $input['newName'];

$currentName = $planet['name'];

$MIN_LENGTH = 3;
$MAX_LENGTH = 20;

if (empty($newName)) {
return $resultHelpers['createFailure']([
'code' => 'NEW_NAME_EMPTY',
]);
}
if ($newName == $currentName) {
return $resultHelpers['createFailure']([
'code' => 'NEW_NAME_SAME_AS_OLD',
]);
}

$newNameLength = strlen($newName);

if ($newNameLength < $MIN_LENGTH) {
return $resultHelpers['createFailure']([
'code' => 'NEW_NAME_TOO_SHORT',
'params' => [
'minLength' => $MIN_LENGTH
],
]);
}
if ($newNameLength > $MAX_LENGTH) {
return $resultHelpers['createFailure']([
'code' => 'NEW_NAME_TOO_LONG',
'params' => [
'maxLength' => $MAX_LENGTH
],
]);
}
if (!preg_match(REGEXP_PLANETNAME_ABSOLUTE, $newName)) {
return $resultHelpers['createFailure']([
'code' => 'NEW_NAME_INVALID',
]);
}

return $resultHelpers['createSuccess']([]);
};

return createFuncWithResultHelpers($executor)($params['input']);
}

?>
Loading

0 comments on commit ccf1db0

Please sign in to comment.