Skip to content

Commit

Permalink
GH-230 Move noob protection infobox & effects into own component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jul 14, 2022
1 parent de2ef2f commit c050b3c
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 15 deletions.
4 changes: 4 additions & 0 deletions modules/overview/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
include($includePath . './screens/Overview/components/Morale/Morale.utils.php');
include($includePath . './screens/Overview/components/NewMessagesInfo/NewMessagesInfo.component.php');
include($includePath . './screens/Overview/components/NewSurveysInfo/NewSurveysInfo.component.php');
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.component.php');
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.utils.php');
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/utils/helpers.utils.php');
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/utils/effects/turnOffProtection.effect.php');
include($includePath . './screens/Overview/components/PlanetsListElement/PlanetsListElement.component.php');
include($includePath . './screens/Overview/components/ResourcesTransport/ResourcesTransport.component.php');
include($includePath . './screens/Overview/components/StatsList/StatsList.component.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;

use UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;

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

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

$isProtectedByNoobProtection = NoobProtectionInfoBox\Utils\isProtectedByNoobProtection([
'user' => &$user,
'currentTimestamp' => $currentTimestamp,
]);

if (!$isProtectedByNoobProtection) {
return [
'componentHTML' => '',
'globalJS' => '',
];
}

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

$localTemplateLoader = createLocalTemplateLoader(__DIR__);

if (
$effectsResult['isSuccess'] &&
$effectsResult['payload']['protectionTurnedOff']
) {
$componentHTML = parsetemplate(
$localTemplateLoader('turnOffMessage'),
$_Lang
);

return [
'componentHTML' => $componentHTML,
'globalJS' => '',
];
}

$protectionTimeLeft = $user['NoobProtection_EndTime'] - $currentTimestamp;

$componentHTML = parsetemplate(
$localTemplateLoader('body'),
[
'data_ProtectionCounterMessage' => sprintf(
$_Lang['NewUserProtection_Text'],
pretty_time($protectionTimeLeft, true, 'dhms')
)
]
);

$protectionCountdownJS = InsertJavaScriptChronoApplet('newprotect', '', $protectionTimeLeft);

return [
'componentHTML' => $componentHTML,
'globalJS' => $protectionCountdownJS,
];
}

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

namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;

use UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;

/**
* @param array $params
* @param arrayRef $params['input']
* @param arrayRef $params['user']
* @param number $params['currentTimestamp']
*/
function runEffects($props) {
$input = &$props['input'];
$user = &$props['user'];
$currentTimestamp = $props['currentTimestamp'];

$isProtectedByNoobProtection = NoobProtectionInfoBox\Utils\isProtectedByNoobProtection([
'user' => &$user,
'currentTimestamp' => $currentTimestamp,
]);

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

NoobProtectionInfoBox\Utils\Effects\turnOffProtection([
'user' => &$user,
'currentTimestamp' => $currentTimestamp,
]);

return [
'isSuccess' => true,
'payload' => [
'protectionTurnedOff' => true,
],
];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<tr>
<th class="c pad5 lime" colspan="3">
{data_ProtectionCounterMessage}
</th>
</tr>
<tr>
<th style="visibility: hidden;">&nbsp;</th>
</tr>
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,8 @@
<tr>
<th class="c pad5 lime" colspan="3">
{NewUserProtection_Canceled}
</th>
</tr>
<tr>
<th style="visibility: hidden;">&nbsp;</th>
</tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox\Utils\Effects;

/**
* @param array $params
* @param arrayRef $params['user']
* @param number $params['currentTimestamp']
*/
function turnOffProtection($props) {
$user = &$props['user'];
$currentTimestamp = $props['currentTimestamp'];

$protectionPropKey = 'NoobProtection_EndTime';

$user[$protectionPropKey] = $currentTimestamp;

$updateQuery = (
"UPDATE {{table}} " .
"SET " .
"`{$protectionPropKey}` = {$currentTimestamp} " .
"WHERE " .
"`id` = {$user['id']} " .
"LIMIT 1 " .
";"
);

doquery($updateQuery, 'users');
}

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

namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox\Utils;

/**
* @param array $params
* @param arrayRef $params['user']
* @param number $params['currentTimestamp']
*/
function isProtectedByNoobProtection($props) {
return ($props['user']['NoobProtection_EndTime'] > $props['currentTimestamp']);
}

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

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

?>
24 changes: 9 additions & 15 deletions overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,16 @@
'user' => &$_User,
])['componentHTML'];

// --- New User Protection Box
if($_User['NoobProtection_EndTime'] > $Now)
{
if(isset($_GET['cancelprotection']) && $_GET['cancelprotection'] == '1')
{
$_User['NoobProtection_EndTime'] = $Now;
$Query_UpdateUser = "UPDATE {{table}} SET `NoobProtection_EndTime` = {$Now} WHERE `id` = {$_User['id']} LIMIT 1;";
doquery($Query_UpdateUser, 'users');
$noobProtectionInfoBoxComponent = Overview\Screens\Overview\Components\NoobProtectionInfoBox\render([
'input' => &$_GET,
'user' => &$_User,
'currentTimestamp' => $Now,
]);

$parse['NewUserBox'] = '<tr><th class="c pad5 lime" colspan="3">'.$_Lang['NewUserProtection_Canceled'].'</th></tr><tr><th style="visibility: hidden;">&nbsp;</th></tr>';
}
else
{
$ProtectTimeLeft = $_User['NoobProtection_EndTime'] - $Now;
$parse['NewUserBox'] = InsertJavaScriptChronoApplet('newprotect', '', $ProtectTimeLeft).'<tr><th class="c pad5 lime" colspan="3">'.sprintf($_Lang['NewUserProtection_Text'], pretty_time($ProtectTimeLeft, true, 'dhms')).'</th></tr><tr><th style="visibility: hidden;">&nbsp;</th></tr>';
}
$parse['NewUserBox'] = $noobProtectionInfoBoxComponent['componentHTML'];

if (!empty($noobProtectionInfoBoxComponent['globalJS'])) {
GlobalTemplate_AppendToAfterBody($noobProtectionInfoBoxComponent['globalJS']);
}

// --- Admin Info Box ------------------------------------------------------------------------------------
Expand Down

0 comments on commit c050b3c

Please sign in to comment.