Skip to content

Commit

Permalink
Merge pull request #242 from mdziekon/gh-91-simulator-refactor-02
Browse files Browse the repository at this point in the history
GH-91 | Battle Simulator - Overall code refactor
  • Loading branch information
mdziekon authored Aug 2, 2022
2 parents d995eac + 46ea4ac commit beb2dc5
Show file tree
Hide file tree
Showing 37 changed files with 1,018 additions and 490 deletions.
1 change: 1 addition & 0 deletions admin/messagelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}

include($_EnginePath . 'modules/messages/_includes.php');
include($_EnginePath . 'modules/attackSimulator/_includes.php');

use UniEngine\Engine\Modules\Messages;

Expand Down
4 changes: 4 additions & 0 deletions includes/functions/TasksFunctions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

abstract class TaskType {
const UseSimulator = 'USE_SIMULATOR';
}

function Tasks_CheckUservar(&$UserVar)
{
if(empty($UserVar['tasks_done_parsed']))
Expand Down
7 changes: 7 additions & 0 deletions includes/unlocalised.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ function array_map_withkeys(array $inputArray, callable $callback) {
return array_map($callback, $inputArray, array_keys($inputArray));
}

/**
* Filters an array, allowing to access the key of each value
*/
function array_filter_withkeys(array $inputArray, callable $callback) {
return array_filter($inputArray, $callback, ARRAY_FILTER_USE_BOTH);
}

function array_find(array $inputArray, callable $callback) {
foreach ($inputArray as $item) {
if ($callback($item)) {
Expand Down
10 changes: 0 additions & 10 deletions language/en/simulator.lang
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ $_Lang['Morale_Level'] = 'Morale level';
$_Lang['Technology'] = 'Technologies';
$_Lang['Fleets'] = 'Fleet';
$_Lang['Defense'] = 'Defenses';
// FIXME: get this from a shared lang file
$_Lang['Techs'][1] = 'Weapons technology';
$_Lang['Techs'][2] = 'Shielding technology';
$_Lang['Techs'][3] = 'Armour technology';
$_Lang['Techs'][4] = 'Laser technology';
$_Lang['Techs'][5] = 'Ion technology';
$_Lang['Techs'][6] = 'Plasma technology';
$_Lang['Techs'][7] = 'Anti-matter technology';
$_Lang['Techs'][8] = 'Disintegration stream technology';
$_Lang['Techs'][9] = 'Graviton technology';

$_Lang['FillMyTechs'] = 'Fill with my tech levels';
$_Lang['FillMyFleets'] = 'Fill with my fleets (and defenses)';
Expand Down
9 changes: 0 additions & 9 deletions language/pl/simulator.lang
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ $_Lang['Morale_Level'] = 'Poziom morali';
$_Lang['Technology'] = 'Technologie';
$_Lang['Fleets'] = 'Flota';
$_Lang['Defense'] = 'Obrona';
$_Lang['Techs'][1] = 'Technologia bojowa';
$_Lang['Techs'][2] = 'Technologia ochronna';
$_Lang['Techs'][3] = 'Opancerzenie';
$_Lang['Techs'][4] = 'Technologia laserowa';
$_Lang['Techs'][5] = 'Technologia jonowa';
$_Lang['Techs'][6] = 'Technologia plazmowa';
$_Lang['Techs'][7] = 'Technologia Antymaterii';
$_Lang['Techs'][8] = 'Strumień Dezintegracyjny';
$_Lang['Techs'][9] = 'Rozwój grawitonów';

$_Lang['FillMyTechs'] = 'Wypełnij moim poziomem technologii';
$_Lang['FillMyFleets'] = 'Wypełnij moją flotą';
Expand Down
1 change: 1 addition & 0 deletions messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
include($_EnginePath.'common.php');

include($_EnginePath . 'modules/messages/_includes.php');
include($_EnginePath . 'modules/attackSimulator/_includes.php');

use UniEngine\Engine\Modules\Messages;

Expand Down
20 changes: 20 additions & 0 deletions modules/attackSimulator/_includes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// TODO: Migrate to IIFE once PHP 5 support is removed
call_user_func(function () {
global $_EnginePath;

$includePath = $_EnginePath . 'modules/attackSimulator/';

include($includePath . './components/MoraleInput/MoraleInput.component.php');
include($includePath . './components/MoraleInputsSection/MoraleInputsSection.component.php');
include($includePath . './components/ShipInput/ShipInput.component.php');
include($includePath . './components/TechInput/TechInput.component.php');
include($includePath . './components/TechInputsSection/TechInputsSection.component.php');
include($includePath . './components/UnitInputsSection/UnitInputsSection.component.php');

include($includePath . './utils/combatTechs.utils.php');

});

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

namespace UniEngine\Engine\Modules\AttackSimulator\Components\MoraleInput;

/**
* @param array $props
* @param number $props['slotIdx']
* @param enum ("attacker" | "defender") $props['columnType']
* @param number $props['initialValue']
*/
function render($props) {
$slotIdx = $props['slotIdx'];

$inputNamePrefix = (
$props['columnType'] === 'attacker' ?
'atk_morale' :
'def_morale'
);

$templateBodyProps = [
'prop_tabIndex' => (
$props['columnType'] === 'attacker' ?
'1' :
'2'
),
'prop_inputName' => "{$inputNamePrefix}[{$slotIdx}]",
'prop_initialValue' => $props['initialValue'],
];

$localTemplateLoader = createLocalTemplateLoader(__DIR__);
$componentHTML = parsetemplate(
$localTemplateLoader('body'),
$templateBodyProps
);

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

?>
7 changes: 7 additions & 0 deletions modules/attackSimulator/components/MoraleInput/body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<input
type="text"
tabindex="{prop_tabIndex}"
name="{prop_inputName}"
value="{prop_initialValue}"
autocomplete="off"
/>%
5 changes: 5 additions & 0 deletions modules/attackSimulator/components/MoraleInput/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,45 @@
<?php

namespace UniEngine\Engine\Modules\AttackSimulator\Components\MoraleInputsSection;

use UniEngine\Engine\Modules\AttackSimulator;

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

$slotIdx = $props['slotIdx'];
$input = &$props['input'];

$templateBodyProps = [
'lang_Morale' => $_Lang['Morale'],
'lang_Morale_Level' => $_Lang['Morale_Level'],

'prop_AttackerInput' => AttackSimulator\Components\MoraleInput\render([
'slotIdx' => $slotIdx,
'columnType' => 'attacker',
'initialValue' => $input['atk_morale'][$slotIdx],
])['componentHTML'],
'prop_DefenderInput' => AttackSimulator\Components\MoraleInput\render([
'slotIdx' => $slotIdx,
'columnType' => 'defender',
'initialValue' => $input['def_morale'][$slotIdx],
])['componentHTML'],
];

$localTemplateLoader = createLocalTemplateLoader(__DIR__);
$componentHTML = parsetemplate(
$localTemplateLoader('body'),
$templateBodyProps
);

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

?>
19 changes: 19 additions & 0 deletions modules/attackSimulator/components/MoraleInputsSection/body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<tr>
<th colspan="4" class="c">
{lang_Morale}
</th>
</tr>
<tr>
<th class="c pad5 ta_r r_l">
{lang_Morale_Level}:
</th>
<th class="c pad5 ta_l r_i">
{prop_AttackerInput}
</th>
<th class="c pad5 ta_r r_l">
{lang_Morale_Level}:
</th>
<th class="c pad5 ta_l r_i">
{prop_DefenderInput}
</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,47 @@
<?php

namespace UniEngine\Engine\Modules\AttackSimulator\Components\ShipInput;

/**
* @param array $props
* @param number $props['slotIdx']
* @param number $props['elementId']
* @param enum ("attacker" | "defender") $props['columnType']
* @param number $props['initialValue']
*/
function render($props) {
global $_Lang;

$slotIdx = $props['slotIdx'];
$elementId = $props['elementId'];

$inputNamePrefix = (
$props['columnType'] === 'attacker' ?
'atk_ships' :
'def_ships'
);

$templateBodyProps = [
'prop_tabIndex' => (
$props['columnType'] === 'attacker' ?
'1' :
'2'
),
'prop_inputName' => "{$inputNamePrefix}[{$slotIdx}][{$elementId}]",
'prop_initialValue' => $props['initialValue'],
'lang_Button_Min' => $_Lang['Button_Min'],
'lang_Button_Max' => $_Lang['Button_Max'],
];

$localTemplateLoader = createLocalTemplateLoader(__DIR__);
$componentHTML = parsetemplate(
$localTemplateLoader('body'),
$templateBodyProps
);

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

?>
11 changes: 11 additions & 0 deletions modules/attackSimulator/components/ShipInput/body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<input
type="text"
tabindex="{prop_tabIndex}"
name="{prop_inputName}"
value="{prop_initialValue}"
autocomplete="off"
class="pad2 fl"
/>
<span class="fr">
(<span class="clnOne point">{lang_Button_Min}</span> / <span class="maxOne point">{lang_Button_Max}</span>)
</span>
5 changes: 5 additions & 0 deletions modules/attackSimulator/components/ShipInput/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,43 @@
<?php

namespace UniEngine\Engine\Modules\AttackSimulator\Components\TechInput;

/**
* @param array $props
* @param number $props['slotIdx']
* @param number $props['elementId']
* @param enum ("attacker" | "defender") $props['columnType']
* @param number $props['initialValue']
*/
function render($props) {
$slotIdx = $props['slotIdx'];
$elementId = $props['elementId'];

$inputNamePrefix = (
$props['columnType'] === 'attacker' ?
'atk_techs' :
'def_techs'
);

$templateBodyProps = [
'prop_tabIndex' => (
$props['columnType'] === 'attacker' ?
'1' :
'2'
),
'prop_inputName' => "{$inputNamePrefix}[{$slotIdx}][{$elementId}]",
'prop_initialValue' => $props['initialValue'],
];

$localTemplateLoader = createLocalTemplateLoader(__DIR__);
$componentHTML = parsetemplate(
$localTemplateLoader('body'),
$templateBodyProps
);

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

?>
7 changes: 7 additions & 0 deletions modules/attackSimulator/components/TechInput/body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<input
type="text"
tabindex="{prop_tabIndex}"
name="{prop_inputName}"
value="{prop_initialValue}"
autocomplete="off"
/>
5 changes: 5 additions & 0 deletions modules/attackSimulator/components/TechInput/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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

?>
Loading

0 comments on commit beb2dc5

Please sign in to comment.