Skip to content

Commit c050b3c

Browse files
committed
GH-230 Move noob protection infobox & effects into own component
1 parent de2ef2f commit c050b3c

File tree

10 files changed

+209
-15
lines changed

10 files changed

+209
-15
lines changed

modules/overview/_includes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
include($includePath . './screens/Overview/components/Morale/Morale.utils.php');
3232
include($includePath . './screens/Overview/components/NewMessagesInfo/NewMessagesInfo.component.php');
3333
include($includePath . './screens/Overview/components/NewSurveysInfo/NewSurveysInfo.component.php');
34+
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.component.php');
35+
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.utils.php');
36+
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/utils/helpers.utils.php');
37+
include($includePath . './screens/Overview/components/NoobProtectionInfoBox/utils/effects/turnOffProtection.effect.php');
3438
include($includePath . './screens/Overview/components/PlanetsListElement/PlanetsListElement.component.php');
3539
include($includePath . './screens/Overview/components/ResourcesTransport/ResourcesTransport.component.php');
3640
include($includePath . './screens/Overview/components/StatsList/StatsList.component.php');
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;
4+
5+
use UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;
6+
7+
/**
8+
* @param array $params
9+
* @param arrayRef $params['input']
10+
* @param arrayRef $params['user']
11+
* @param number $params['currentTimestamp']
12+
*/
13+
function render($props) {
14+
global $_Lang;
15+
16+
$input = &$props['input'];
17+
$user = &$props['user'];
18+
$currentTimestamp = $props['currentTimestamp'];
19+
20+
$isProtectedByNoobProtection = NoobProtectionInfoBox\Utils\isProtectedByNoobProtection([
21+
'user' => &$user,
22+
'currentTimestamp' => $currentTimestamp,
23+
]);
24+
25+
if (!$isProtectedByNoobProtection) {
26+
return [
27+
'componentHTML' => '',
28+
'globalJS' => '',
29+
];
30+
}
31+
32+
$effectsResult = NoobProtectionInfoBox\runEffects([
33+
'input' => &$input,
34+
'user' => &$user,
35+
'currentTimestamp' => $currentTimestamp,
36+
]);
37+
38+
$localTemplateLoader = createLocalTemplateLoader(__DIR__);
39+
40+
if (
41+
$effectsResult['isSuccess'] &&
42+
$effectsResult['payload']['protectionTurnedOff']
43+
) {
44+
$componentHTML = parsetemplate(
45+
$localTemplateLoader('turnOffMessage'),
46+
$_Lang
47+
);
48+
49+
return [
50+
'componentHTML' => $componentHTML,
51+
'globalJS' => '',
52+
];
53+
}
54+
55+
$protectionTimeLeft = $user['NoobProtection_EndTime'] - $currentTimestamp;
56+
57+
$componentHTML = parsetemplate(
58+
$localTemplateLoader('body'),
59+
[
60+
'data_ProtectionCounterMessage' => sprintf(
61+
$_Lang['NewUserProtection_Text'],
62+
pretty_time($protectionTimeLeft, true, 'dhms')
63+
)
64+
]
65+
);
66+
67+
$protectionCountdownJS = InsertJavaScriptChronoApplet('newprotect', '', $protectionTimeLeft);
68+
69+
return [
70+
'componentHTML' => $componentHTML,
71+
'globalJS' => $protectionCountdownJS,
72+
];
73+
}
74+
75+
?>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;
4+
5+
use UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox;
6+
7+
/**
8+
* @param array $params
9+
* @param arrayRef $params['input']
10+
* @param arrayRef $params['user']
11+
* @param number $params['currentTimestamp']
12+
*/
13+
function runEffects($props) {
14+
$input = &$props['input'];
15+
$user = &$props['user'];
16+
$currentTimestamp = $props['currentTimestamp'];
17+
18+
$isProtectedByNoobProtection = NoobProtectionInfoBox\Utils\isProtectedByNoobProtection([
19+
'user' => &$user,
20+
'currentTimestamp' => $currentTimestamp,
21+
]);
22+
23+
if (!$isProtectedByNoobProtection) {
24+
return [
25+
'isSuccess' => null,
26+
];
27+
}
28+
if (
29+
!isset($input['cancelprotection']) ||
30+
$input['cancelprotection'] != '1'
31+
) {
32+
return [
33+
'isSuccess' => null,
34+
];
35+
}
36+
37+
NoobProtectionInfoBox\Utils\Effects\turnOffProtection([
38+
'user' => &$user,
39+
'currentTimestamp' => $currentTimestamp,
40+
]);
41+
42+
return [
43+
'isSuccess' => true,
44+
'payload' => [
45+
'protectionTurnedOff' => true,
46+
],
47+
];
48+
}
49+
50+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<tr>
2+
<th class="c pad5 lime" colspan="3">
3+
{data_ProtectionCounterMessage}
4+
</th>
5+
</tr>
6+
<tr>
7+
<th style="visibility: hidden;">&nbsp;</th>
8+
</tr>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
header("Location: ../index.php");
4+
5+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<tr>
2+
<th class="c pad5 lime" colspan="3">
3+
{NewUserProtection_Canceled}
4+
</th>
5+
</tr>
6+
<tr>
7+
<th style="visibility: hidden;">&nbsp;</th>
8+
</tr>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox\Utils\Effects;
4+
5+
/**
6+
* @param array $params
7+
* @param arrayRef $params['user']
8+
* @param number $params['currentTimestamp']
9+
*/
10+
function turnOffProtection($props) {
11+
$user = &$props['user'];
12+
$currentTimestamp = $props['currentTimestamp'];
13+
14+
$protectionPropKey = 'NoobProtection_EndTime';
15+
16+
$user[$protectionPropKey] = $currentTimestamp;
17+
18+
$updateQuery = (
19+
"UPDATE {{table}} " .
20+
"SET " .
21+
"`{$protectionPropKey}` = {$currentTimestamp} " .
22+
"WHERE " .
23+
"`id` = {$user['id']} " .
24+
"LIMIT 1 " .
25+
";"
26+
);
27+
28+
doquery($updateQuery, 'users');
29+
}
30+
31+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NoobProtectionInfoBox\Utils;
4+
5+
/**
6+
* @param array $params
7+
* @param arrayRef $params['user']
8+
* @param number $params['currentTimestamp']
9+
*/
10+
function isProtectedByNoobProtection($props) {
11+
return ($props['user']['NoobProtection_EndTime'] > $props['currentTimestamp']);
12+
}
13+
14+
?>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
header("Location: ../index.php");
4+
5+
?>

overview.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,16 @@
6666
'user' => &$_User,
6767
])['componentHTML'];
6868

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

78-
$parse['NewUserBox'] = '<tr><th class="c pad5 lime" colspan="3">'.$_Lang['NewUserProtection_Canceled'].'</th></tr><tr><th style="visibility: hidden;">&nbsp;</th></tr>';
79-
}
80-
else
81-
{
82-
$ProtectTimeLeft = $_User['NoobProtection_EndTime'] - $Now;
83-
$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>';
84-
}
75+
$parse['NewUserBox'] = $noobProtectionInfoBoxComponent['componentHTML'];
76+
77+
if (!empty($noobProtectionInfoBoxComponent['globalJS'])) {
78+
GlobalTemplate_AppendToAfterBody($noobProtectionInfoBoxComponent['globalJS']);
8579
}
8680

8781
// --- Admin Info Box ------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)