Skip to content

Commit

Permalink
[GH-91] Move ship input rendering to a component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jul 22, 2022
1 parent 471d2b5 commit 3fd70aa
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
13 changes: 13 additions & 0 deletions modules/attackSimulator/_includes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

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

include($includePath . './components/ShipInput/ShipInput.component.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");

?>
5 changes: 5 additions & 0 deletions modules/attackSimulator/components/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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

?>
5 changes: 5 additions & 0 deletions modules/attackSimulator/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

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

?>
25 changes: 21 additions & 4 deletions simulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

$_EnginePath = './';
include($_EnginePath.'common.php');
include_once($_EnginePath . 'modules/attackSimulator/_includes.php');

use UniEngine\Engine\Modules\Flights;

use UniEngine\Engine\Includes\Helpers\World;
use UniEngine\Engine\Modules\AttackSimulator;

loggedCheck();

Expand Down Expand Up @@ -576,15 +577,31 @@ function ($value, $key) use ($TechEquivalents) {
$ThisRow_InsertValue_Atk = isset($_POST['atk_ships'][$i][$Ships]) ? $_POST['atk_ships'][$i][$Ships] : null;

$parse['RowText'] = $_Lang['tech'][$Ships];
$parse['RowInput'] = "<input type=\"text\" tabindex=\"1\" name=\"atk_ships[{$i}][{$Ships}]\" value=\"{$ThisRow_InsertValue_Atk}\" 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>";
$parse['RowInput'] = AttackSimulator\Components\ShipInput\render([
'slotIdx' => $i,
'elementId' => $Ships,
'columnType' => 'attacker',
'initialValue' => $ThisRow_InsertValue_Atk,
])['componentHTML'];

$parse['RowText2'] = $_Lang['tech'][$Ships];
$parse['RowInput2'] = "<input type=\"text\" tabindex=\"2\" name=\"def_ships[{$i}][{$Ships}]\" value=\"{$ThisRow_InsertValue_Def}\" 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>";
$parse['RowInput2'] = AttackSimulator\Components\ShipInput\render([
'slotIdx' => $i,
'elementId' => $Ships,
'columnType' => 'defender',
'initialValue' => $ThisRow_InsertValue_Def,
])['componentHTML'];

$ThisSlot['txt'] .= parsetemplate($TPL_Row, $parse);
} else {
$parse['RowText'] = '-';
$parse['RowText2'] = $_Lang['tech'][$Ships];
$parse['RowInput2'] = "<input type=\"text\" tabindex=\"2\" name=\"def_ships[{$i}][{$Ships}]\" value=\"{$ThisRow_InsertValue_Def}\" 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>";
$parse['RowInput2'] = AttackSimulator\Components\ShipInput\render([
'slotIdx' => $i,
'elementId' => $Ships,
'columnType' => 'defender',
'initialValue' => $ThisRow_InsertValue_Def,
])['componentHTML'];

$ThisSlot['txt'] .= parsetemplate($TPL_NoLeft, $parse);
}
Expand Down

0 comments on commit 3fd70aa

Please sign in to comment.