From c050b3c9cd7bac203dd4ca90c3721f3216d03791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Thu, 14 Jul 2022 21:37:09 +0200 Subject: [PATCH] GH-230 Move noob protection infobox & effects into own component --- modules/overview/_includes.php | 4 + .../NoobProtectionInfoBox.component.php | 75 +++++++++++++++++++ .../NoobProtectionInfoBox.utils.php | 50 +++++++++++++ .../components/NoobProtectionInfoBox/body.tpl | 8 ++ .../NoobProtectionInfoBox/index.php | 5 ++ .../NoobProtectionInfoBox/turnOffMessage.tpl | 8 ++ .../effects/turnOffProtection.effect.php | 31 ++++++++ .../utils/helpers.utils.php | 14 ++++ .../NoobProtectionInfoBox/utils/index.php | 5 ++ overview.php | 24 +++--- 10 files changed, 209 insertions(+), 15 deletions(-) create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.component.php create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.utils.php create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/body.tpl create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/index.php create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/turnOffMessage.tpl create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/effects/turnOffProtection.effect.php create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/helpers.utils.php create mode 100644 modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/index.php diff --git a/modules/overview/_includes.php b/modules/overview/_includes.php index decf6aa6d..f0f15b3e5 100644 --- a/modules/overview/_includes.php +++ b/modules/overview/_includes.php @@ -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'); diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.component.php b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.component.php new file mode 100644 index 000000000..bb0b1a846 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.component.php @@ -0,0 +1,75 @@ + &$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, + ]; +} + +?> diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.utils.php b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.utils.php new file mode 100644 index 000000000..db0409e93 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/NoobProtectionInfoBox.utils.php @@ -0,0 +1,50 @@ + &$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, + ], + ]; +} + +?> diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/body.tpl b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/body.tpl new file mode 100644 index 000000000..3ec8d4634 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/body.tpl @@ -0,0 +1,8 @@ + + + {data_ProtectionCounterMessage} + + + +   + diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/index.php b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/turnOffMessage.tpl b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/turnOffMessage.tpl new file mode 100644 index 000000000..d6f30e75b --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/turnOffMessage.tpl @@ -0,0 +1,8 @@ + + + {NewUserProtection_Canceled} + + + +   + diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/effects/turnOffProtection.effect.php b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/effects/turnOffProtection.effect.php new file mode 100644 index 000000000..bc3e33b52 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/effects/turnOffProtection.effect.php @@ -0,0 +1,31 @@ + diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/helpers.utils.php b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/helpers.utils.php new file mode 100644 index 000000000..4638fbf77 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/helpers.utils.php @@ -0,0 +1,14 @@ + $props['currentTimestamp']); +} + +?> diff --git a/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/index.php b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/overview/screens/Overview/components/NoobProtectionInfoBox/utils/index.php @@ -0,0 +1,5 @@ + diff --git a/overview.php b/overview.php index f17d32d7f..88b79eb43 100644 --- a/overview.php +++ b/overview.php @@ -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'] = ''.$_Lang['NewUserProtection_Canceled'].' '; - } - else - { - $ProtectTimeLeft = $_User['NoobProtection_EndTime'] - $Now; - $parse['NewUserBox'] = InsertJavaScriptChronoApplet('newprotect', '', $ProtectTimeLeft).''.sprintf($_Lang['NewUserProtection_Text'], pretty_time($ProtectTimeLeft, true, 'dhms')).' '; - } + $parse['NewUserBox'] = $noobProtectionInfoBoxComponent['componentHTML']; + + if (!empty($noobProtectionInfoBoxComponent['globalJS'])) { + GlobalTemplate_AppendToAfterBody($noobProtectionInfoBoxComponent['globalJS']); } // --- Admin Info Box ------------------------------------------------------------------------------------