Skip to content

Commit

Permalink
GH-230 Move morale rendering code to own component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jul 12, 2022
1 parent 59bf5a7 commit 7ec348b
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 74 deletions.
2 changes: 2 additions & 0 deletions modules/overview/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

include($includePath . './screens/Overview/components/AdminAlerts/AdminAlerts.component.php');
include($includePath . './screens/Overview/components/EmailChangeInfo/EmailChangeInfo.component.php');
include($includePath . './screens/Overview/components/Morale/Morale.component.php');
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/PlanetsListElement/PlanetsListElement.component.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

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

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

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

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

$localTemplateLoader = createLocalTemplateLoader(__DIR__);

$moraleLevelColor = null;

if ($user['morale_level'] > 0) {
$moraleLevelColor = 'lime';
} else if ($user['morale_level'] == 0) {
$moraleLevelColor = '';
} else if ($user['morale_level'] > -50) {
$moraleLevelColor = 'orange';
} else {
$moraleLevelColor = 'red';
}

$moraleStatusData = Morale\Utils\getMoraleStatusData([
'user' => &$user,
'currentTimestamp' => $currentTimestamp,
]);

$tplBodyParams = [
'Insert_Morale_Level' => $user['morale_level'],
'Insert_Morale_Color' => $moraleLevelColor,
'Insert_Morale_Status' => $moraleStatusData['text'],
'Insert_Morale_Points' => sprintf(
$_Lang['Box_Morale_Points'],
prettyNumber($user['morale_points'])
),
];
$tplBodyParams = array_merge($_Lang, $tplBodyParams);

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

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

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

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

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

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

$userMoraleLevel = $user['morale_level'];

if ($userMoraleLevel == 0) {
return [
'text' => $_Lang['Box_Morale_NoChanges'],
'globalJS' => null,
];
}

$moraleSentiment = (
$userMoraleLevel > 0 ?
'Pos' :
'Neg'
);
$moraleDroptimeRemaining = $user['morale_droptime'] - $currentTimestamp;

if ($moraleDroptimeRemaining > 0) {
return [
'text' => sprintf(
$_Lang["Box_Morale_DropStartIn_{$moraleSentiment}"],
pretty_time(
$moraleDroptimeRemaining,
true,
'D'
)
),
'globalJS' => InsertJavaScriptChronoApplet(
'morale',
'',
$user['morale_droptime'],
true
),
];
}

$moraleDropInterval = (
$userMoraleLevel > 0 ?
MORALE_DROPINTERVAL_POSITIVE :
MORALE_DROPINTERVAL_NEGATIVE
);
$moraleDropNextTimepoint = (
($user['morale_lastupdate'] == 0) ?
($user['morale_droptime'] + $moraleDropInterval) :
($user['morale_lastupdate'] + $moraleDropInterval)
);

return [
'text' => sprintf(
$_Lang["Box_Morale_Dropping_{$moraleSentiment}"],
pretty_time(
$moraleDropNextTimepoint - $currentTimestamp,
true,
'D'
)
),
'globalJS' => InsertJavaScriptChronoApplet(
'morale',
'',
$moraleDropNextTimepoint,
true
),
];
}

?>
18 changes: 18 additions & 0 deletions modules/overview/screens/Overview/components/Morale/body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<table width="100%" align="center">
<tr>
<td class="c pad3">
<b class="fl">{Box_Morale_Header}</b>
<a class="fr" href="morale_info.php" title="{Box_Morale_InfoTitle}">(?)</a>
</td>
</tr>
<tr>
<th>
<b class="{Insert_Morale_Color}">{Insert_Morale_Level}%</b><br/>
{Insert_Morale_Status}
</th>
</tr>
<tr>
<th>{Insert_Morale_Points}</th>
</tr>
</table>
<br/>
5 changes: 5 additions & 0 deletions modules/overview/screens/Overview/components/Morale/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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

?>
71 changes: 9 additions & 62 deletions overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,72 +171,19 @@
$parse['LastStatsRecount'] = date('d.m.Y H:i:s', $_GameConfig['last_update']);

// --- MoraleSystem Box ---
if(MORALE_ENABLED)
{
if (MORALE_ENABLED) {
Morale_ReCalculate($_User);
$UserMoraleLevel = $_User['morale_level'];

$parse['Insert_Morale_Level'] = $UserMoraleLevel;
if($UserMoraleLevel > 0)
{
$parse['Insert_Morale_Color'] = 'lime';
}
else if($UserMoraleLevel < 0)
{
if($UserMoraleLevel <= -50)
{
$parse['Insert_Morale_Color'] = 'red';
}
else
{
$parse['Insert_Morale_Color'] = 'orange';
}
}
$moraleComponent = Overview\Screens\Overview\Components\Morale\render([
'user' => &$_User,
'currentTimestamp' => $Now,
]);

if($UserMoraleLevel == 0)
{
$parse['Insert_Morale_Status'] = $_Lang['Box_Morale_NoChanges'];
}
else
{
if($UserMoraleLevel > 0)
{
$Temp_MoraleStatus = 'Pos';
}
else
{
$Temp_MoraleStatus = 'Neg';
}
if($_User['morale_droptime'] > $Now)
{
GlobalTemplate_AppendToAfterBody(InsertJavaScriptChronoApplet('morale', '', $_User['morale_droptime'], true));
$parse['Insert_Morale_Status'] = sprintf($_Lang['Box_Morale_DropStartIn_'.$Temp_MoraleStatus], pretty_time($_User['morale_droptime'] - $Now, true, 'D'));
}
else
{
if($UserMoraleLevel > 0)
{
$Temp_MoraleDropInterval = MORALE_DROPINTERVAL_POSITIVE;
}
else
{
$Temp_MoraleDropInterval = MORALE_DROPINTERVAL_NEGATIVE;
}
if($_User['morale_lastupdate'] == 0)
{
$Temp_MoraleNextDrop = $_User['morale_droptime'] + $Temp_MoraleDropInterval;
}
else
{
$Temp_MoraleNextDrop = $_User['morale_lastupdate'] + $Temp_MoraleDropInterval;
}
GlobalTemplate_AppendToAfterBody(InsertJavaScriptChronoApplet('morale', '', $Temp_MoraleNextDrop, true));
$parse['Insert_Morale_Status'] = sprintf($_Lang['Box_Morale_Dropping_'.$Temp_MoraleStatus], pretty_time($Temp_MoraleNextDrop - $Now, true, 'D'));
}
}
$_Lang['Box_Morale_Points'] = sprintf($_Lang['Box_Morale_Points'], prettyNumber($_User['morale_points']));
$parse['Insert_MoraleBox'] = $moraleComponent['componentHTML'];

$parse['Insert_MoraleBox'] = parsetemplate(gettemplate('overview_body_morale'), $parse);
if (!empty($moraleComponent['globalJS'])) {
GlobalTemplate_AppendToAfterBody($moraleComponent['globalJS']);
}
}

// --- Get Register Date -
Expand Down
12 changes: 0 additions & 12 deletions templates/default_template/overview_body_morale.tpl

This file was deleted.

0 comments on commit 7ec348b

Please sign in to comment.