diff --git a/admin/messagelist.php b/admin/messagelist.php index 23fe891bb..a7071d979 100644 --- a/admin/messagelist.php +++ b/admin/messagelist.php @@ -15,6 +15,7 @@ } include($_EnginePath . 'modules/messages/_includes.php'); +include($_EnginePath . 'modules/attackSimulator/_includes.php'); use UniEngine\Engine\Modules\Messages; diff --git a/includes/functions/TasksFunctions.php b/includes/functions/TasksFunctions.php index 859177bc0..604e8efa8 100644 --- a/includes/functions/TasksFunctions.php +++ b/includes/functions/TasksFunctions.php @@ -1,5 +1,9 @@ diff --git a/modules/attackSimulator/components/MoraleInput/MoraleInput.component.php b/modules/attackSimulator/components/MoraleInput/MoraleInput.component.php new file mode 100644 index 000000000..32211cbba --- /dev/null +++ b/modules/attackSimulator/components/MoraleInput/MoraleInput.component.php @@ -0,0 +1,41 @@ + ( + $props['columnType'] === 'attacker' ? + '1' : + '2' + ), + 'prop_inputName' => "{$inputNamePrefix}[{$slotIdx}]", + 'prop_initialValue' => $props['initialValue'], + ]; + + $localTemplateLoader = createLocalTemplateLoader(__DIR__); + $componentHTML = parsetemplate( + $localTemplateLoader('body'), + $templateBodyProps + ); + + return [ + 'componentHTML' => $componentHTML, + ]; +} + +?> diff --git a/modules/attackSimulator/components/MoraleInput/body.tpl b/modules/attackSimulator/components/MoraleInput/body.tpl new file mode 100644 index 000000000..261d9bb11 --- /dev/null +++ b/modules/attackSimulator/components/MoraleInput/body.tpl @@ -0,0 +1,7 @@ +% diff --git a/modules/attackSimulator/components/MoraleInput/index.php b/modules/attackSimulator/components/MoraleInput/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/MoraleInput/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/components/MoraleInputsSection/MoraleInputsSection.component.php b/modules/attackSimulator/components/MoraleInputsSection/MoraleInputsSection.component.php new file mode 100644 index 000000000..a6f9d931b --- /dev/null +++ b/modules/attackSimulator/components/MoraleInputsSection/MoraleInputsSection.component.php @@ -0,0 +1,45 @@ + $_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, + ]; +} + +?> diff --git a/modules/attackSimulator/components/MoraleInputsSection/body.tpl b/modules/attackSimulator/components/MoraleInputsSection/body.tpl new file mode 100644 index 000000000..5e1811dfe --- /dev/null +++ b/modules/attackSimulator/components/MoraleInputsSection/body.tpl @@ -0,0 +1,19 @@ + + + {lang_Morale} + + + + + {lang_Morale_Level}: + + + {prop_AttackerInput} + + + {lang_Morale_Level}: + + + {prop_DefenderInput} + + diff --git a/modules/attackSimulator/components/MoraleInputsSection/index.php b/modules/attackSimulator/components/MoraleInputsSection/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/MoraleInputsSection/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/components/ShipInput/ShipInput.component.php b/modules/attackSimulator/components/ShipInput/ShipInput.component.php new file mode 100644 index 000000000..ebaa4ddd5 --- /dev/null +++ b/modules/attackSimulator/components/ShipInput/ShipInput.component.php @@ -0,0 +1,47 @@ + ( + $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, + ]; +} + +?> diff --git a/modules/attackSimulator/components/ShipInput/body.tpl b/modules/attackSimulator/components/ShipInput/body.tpl new file mode 100644 index 000000000..106870058 --- /dev/null +++ b/modules/attackSimulator/components/ShipInput/body.tpl @@ -0,0 +1,11 @@ + + + ({lang_Button_Min} / {lang_Button_Max}) + diff --git a/modules/attackSimulator/components/ShipInput/index.php b/modules/attackSimulator/components/ShipInput/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/ShipInput/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/components/TechInput/TechInput.component.php b/modules/attackSimulator/components/TechInput/TechInput.component.php new file mode 100644 index 000000000..56098a006 --- /dev/null +++ b/modules/attackSimulator/components/TechInput/TechInput.component.php @@ -0,0 +1,43 @@ + ( + $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, + ]; +} + +?> diff --git a/modules/attackSimulator/components/TechInput/body.tpl b/modules/attackSimulator/components/TechInput/body.tpl new file mode 100644 index 000000000..b41d77096 --- /dev/null +++ b/modules/attackSimulator/components/TechInput/body.tpl @@ -0,0 +1,7 @@ + diff --git a/modules/attackSimulator/components/TechInput/index.php b/modules/attackSimulator/components/TechInput/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/TechInput/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/components/TechInputsSection/TechInputsSection.component.php b/modules/attackSimulator/components/TechInputsSection/TechInputsSection.component.php new file mode 100644 index 000000000..79ea7614a --- /dev/null +++ b/modules/attackSimulator/components/TechInputsSection/TechInputsSection.component.php @@ -0,0 +1,81 @@ + $localTemplateLoader('body'), + 'techRow' => $localTemplateLoader('techRow'), + ]; + + $techRows = array_map_withkeys( + $combatTechs, + function ($elementId) use ($slotIdx, &$input, &$_Lang, &$tplBodyCache) { + $formInputValueAttacker = ( + isset($input['atk_techs'][$slotIdx][$elementId]) ? + $input['atk_techs'][$slotIdx][$elementId] : + null + ); + $formInputValueDefender = ( + isset($input['def_techs'][$slotIdx][$elementId]) ? + $input['def_techs'][$slotIdx][$elementId] : + null + ); + + $rowTemplateBodyProps = [ + 'prop_techName' => $_Lang['tech'][$elementId], + 'prop_AttackerInput' => AttackSimulator\Components\TechInput\render([ + 'slotIdx' => $slotIdx, + 'elementId' => $elementId, + 'columnType' => 'attacker', + 'initialValue' => $formInputValueAttacker, + ])['componentHTML'], + 'prop_DefenderInput' => AttackSimulator\Components\TechInput\render([ + 'slotIdx' => $slotIdx, + 'elementId' => $elementId, + 'columnType' => 'defender', + 'initialValue' => $formInputValueDefender, + ])['componentHTML'], + ]; + + return parsetemplate( + $tplBodyCache['techRow'], + $rowTemplateBodyProps + ); + } + ); + + $templateBodyProps = [ + 'lang_Technology' => $_Lang['Technology'], + 'lang_FillMyTechs' => $_Lang['FillMyTechs'], + 'lang_Fill_Clean' => $_Lang['Fill_Clean'], + + 'prop_techRowsHTML' => implode('', $techRows), + ]; + + $componentHTML = parsetemplate( + $tplBodyCache['body'], + $templateBodyProps + ); + + return [ + 'componentHTML' => $componentHTML, + ]; +} + +?> diff --git a/modules/attackSimulator/components/TechInputsSection/body.tpl b/modules/attackSimulator/components/TechInputsSection/body.tpl new file mode 100644 index 000000000..e1593145b --- /dev/null +++ b/modules/attackSimulator/components/TechInputsSection/body.tpl @@ -0,0 +1,26 @@ + + + {lang_Technology} + + + + + + {lang_FillMyTechs} + + / + + {lang_Fill_Clean} + + + + + {lang_FillMyTechs} + + / + + {lang_Fill_Clean} + + + +{prop_techRowsHTML} diff --git a/modules/attackSimulator/components/TechInputsSection/index.php b/modules/attackSimulator/components/TechInputsSection/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/TechInputsSection/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/components/TechInputsSection/techRow.tpl b/modules/attackSimulator/components/TechInputsSection/techRow.tpl new file mode 100644 index 000000000..635e19715 --- /dev/null +++ b/modules/attackSimulator/components/TechInputsSection/techRow.tpl @@ -0,0 +1,14 @@ + + + {prop_techName}: + + + {prop_AttackerInput} + + + {prop_techName}: + + + {prop_DefenderInput} + + diff --git a/modules/attackSimulator/components/UnitInputsSection/UnitInputsSection.component.php b/modules/attackSimulator/components/UnitInputsSection/UnitInputsSection.component.php new file mode 100644 index 000000000..bbc21dc91 --- /dev/null +++ b/modules/attackSimulator/components/UnitInputsSection/UnitInputsSection.component.php @@ -0,0 +1,130 @@ + $localTemplateLoader('mainFleetsBody'), + 'unitedFleetsBody' => $localTemplateLoader('unitedFleetsBody'), + 'defenseUnitRow' => $localTemplateLoader('defenseUnitRow'), + 'dualUnitRow' => $localTemplateLoader('dualUnitRow'), + ]; + + $unitRows = object_map( + $units, + function ($elementId) use ($slotIdx, &$input, &$_Lang, &$tplBodyCache) { + $formInputValueAttacker = ( + isset($input['atk_ships'][$slotIdx][$elementId]) ? + $input['atk_ships'][$slotIdx][$elementId] : + null + ); + $formInputValueDefender = ( + isset($input['def_ships'][$slotIdx][$elementId]) ? + $input['def_ships'][$slotIdx][$elementId] : + null + ); + + $rowTemplateBodyCommonProps = [ + 'prop_unitName' => $_Lang['tech'][$elementId], + + 'prop_DefenderInput' => AttackSimulator\Components\ShipInput\render([ + 'slotIdx' => $slotIdx, + 'elementId' => $elementId, + 'columnType' => 'defender', + 'initialValue' => $formInputValueDefender, + ])['componentHTML'], + ]; + + if (!hasAnyEngine($elementId)) { + return [ + parsetemplate( + $tplBodyCache['defenseUnitRow'], + $rowTemplateBodyCommonProps + ), + $elementId + ]; + } + + $rowTemplateBodyProps = [ + 'prop_AttackerInput' => AttackSimulator\Components\ShipInput\render([ + 'slotIdx' => $slotIdx, + 'elementId' => $elementId, + 'columnType' => 'attacker', + 'initialValue' => $formInputValueAttacker, + ])['componentHTML'], + ]; + + return [ + parsetemplate( + $tplBodyCache['dualUnitRow'], + array_merge( + $rowTemplateBodyCommonProps, + $rowTemplateBodyProps + ) + ), + $elementId + ]; + } + ); + $shipRows = array_filter_withkeys($unitRows, function ($elementHTML, $elementId) { + return World\Elements\isShip($elementId); + }); + $defenseRows = array_filter_withkeys($unitRows, function ($elementHTML, $elementId) { + return World\Elements\isDefenseSystem($elementId); + }); + + $templateBodyProps = [ + 'lang_Fleets' => $_Lang['Fleets'], + 'lang_Defense' => $_Lang['Defense'], + 'lang_FillMyFleets' => $_Lang['FillMyFleets'], + 'lang_Fill_Clean' => $_Lang['Fill_Clean'], + + 'prop_shipUnitRowsHTML' => implode('', $shipRows), + 'prop_defenseUnitRowsHTML' => implode('', $defenseRows), + ]; + + $bodyTpl = ( + $isMainFleetsSlot ? + $tplBodyCache['mainFleetsBody'] : + $tplBodyCache['unitedFleetsBody'] + ); + + $componentHTML = parsetemplate( + $bodyTpl, + $templateBodyProps + ); + + return [ + 'componentHTML' => $componentHTML, + ]; +} + +?> diff --git a/modules/attackSimulator/components/UnitInputsSection/defenseUnitRow.tpl b/modules/attackSimulator/components/UnitInputsSection/defenseUnitRow.tpl new file mode 100644 index 000000000..86a9ca569 --- /dev/null +++ b/modules/attackSimulator/components/UnitInputsSection/defenseUnitRow.tpl @@ -0,0 +1,11 @@ + + + - + + + {prop_unitName}: + + + {prop_DefenderInput} + + diff --git a/modules/attackSimulator/components/UnitInputsSection/dualUnitRow.tpl b/modules/attackSimulator/components/UnitInputsSection/dualUnitRow.tpl new file mode 100644 index 000000000..fc704974c --- /dev/null +++ b/modules/attackSimulator/components/UnitInputsSection/dualUnitRow.tpl @@ -0,0 +1,14 @@ + + + {prop_unitName}: + + + {prop_AttackerInput} + + + {prop_unitName}: + + + {prop_DefenderInput} + + diff --git a/modules/attackSimulator/components/UnitInputsSection/index.php b/modules/attackSimulator/components/UnitInputsSection/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/UnitInputsSection/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/components/UnitInputsSection/mainFleetsBody.tpl b/modules/attackSimulator/components/UnitInputsSection/mainFleetsBody.tpl new file mode 100644 index 000000000..b64020509 --- /dev/null +++ b/modules/attackSimulator/components/UnitInputsSection/mainFleetsBody.tpl @@ -0,0 +1,32 @@ + + + {lang_Fleets} + + + + + + {lang_FillMyFleets} + + / + + {lang_Fill_Clean} + + + + + {lang_FillMyFleets} + + / + + {lang_Fill_Clean} + + + +{prop_shipUnitRowsHTML} + + + {lang_Defense} + + +{prop_defenseUnitRowsHTML} diff --git a/modules/attackSimulator/components/UnitInputsSection/unitedFleetsBody.tpl b/modules/attackSimulator/components/UnitInputsSection/unitedFleetsBody.tpl new file mode 100644 index 000000000..812772503 --- /dev/null +++ b/modules/attackSimulator/components/UnitInputsSection/unitedFleetsBody.tpl @@ -0,0 +1,26 @@ + + + {lang_Fleets} + + + + + + {lang_FillMyFleets} + + / + + {lang_Fill_Clean} + + + + + {lang_FillMyFleets} + + / + + {lang_Fill_Clean} + + + +{prop_shipUnitRowsHTML} diff --git a/modules/attackSimulator/components/index.php b/modules/attackSimulator/components/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/components/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/index.php b/modules/attackSimulator/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/attackSimulator/utils/combatTechs.utils.php b/modules/attackSimulator/utils/combatTechs.utils.php new file mode 100644 index 000000000..65b8e94b1 --- /dev/null +++ b/modules/attackSimulator/utils/combatTechs.utils.php @@ -0,0 +1,36 @@ + 1, + 110 => 2, + 111 => 3, + 120 => 4, + 121 => 5, + 122 => 6, + 125 => 7, + 126 => 8, + 199 => 9, + ]; +} + +function getTechsList() { + return array_keys(_getTechsMapping()); +} + +function getTechPackedKey($elementId) { + return _getTechsMapping()[$elementId]; +} + +function getTechStandardKey($packedId) { + return array_flip(_getTechsMapping())[$packedId]; +} + +?> diff --git a/modules/attackSimulator/utils/index.php b/modules/attackSimulator/utils/index.php new file mode 100644 index 000000000..bc99142d1 --- /dev/null +++ b/modules/attackSimulator/utils/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/messages/utils/buildMessageDetails.utils.php b/modules/messages/utils/buildMessageDetails.utils.php index 7d48ebf31..99cdc72af 100644 --- a/modules/messages/utils/buildMessageDetails.utils.php +++ b/modules/messages/utils/buildMessageDetails.utils.php @@ -3,6 +3,7 @@ namespace UniEngine\Engine\Modules\Messages\Utils; use UniEngine\Engine\Modules\Messages; +use UniEngine\Engine\Modules\AttackSimulator; function _buildBasicMessageDetails($dbMessageData, $params) { global $_Lang; @@ -183,28 +184,7 @@ function _buildTypedSystemMessageContent($dbMessageData, $params = []) { function _buildBattleSimulationDetails($simulationDataString) { global $_Lang; - $SimTechs = [ - 109, - 110, - 111, - 120, - 121, - 122, - 125, - 126, - 199, - ]; - $SimTechsRep = [ - 109 => 1, - 110 => 2, - 111 => 3, - 120 => 4, - 121 => 5, - 122 => 6, - 125 => 7, - 126 => 8, - 199 => 9, - ]; + $combatTechs = AttackSimulator\Utils\CombatTechs\getTechsList(); $battleSimulationData = [ 'tech' => [], @@ -226,8 +206,10 @@ function _buildBattleSimulationDetails($simulationDataString) { $elementId = $simulationElement[0]; $elementData = $simulationElement[1]; - if (in_array($elementId, $SimTechs)) { - $battleSimulationData['tech'][$SimTechsRep[$elementId]] = $elementData; + if (in_array($elementId, $combatTechs)) { + $elementPackedKey = AttackSimulator\Utils\CombatTechs\getTechPackedKey($elementId); + + $battleSimulationData['tech'][$elementPackedKey] = $elementData; } else { $battleSimulationData['ships'][$elementId] = $elementData; } diff --git a/simulator.php b/simulator.php index 1de3cad65..38df57758 100644 --- a/simulator.php +++ b/simulator.php @@ -4,8 +4,11 @@ $_EnginePath = './'; include($_EnginePath.'common.php'); +include_once($_EnginePath . 'modules/attackSimulator/_includes.php'); +use UniEngine\Engine\Includes\Helpers\World; use UniEngine\Engine\Modules\Flights; +use UniEngine\Engine\Modules\AttackSimulator; loggedCheck(); @@ -13,19 +16,8 @@ $_Lang['rows'] = ''; $_Lang['SimResult'] = ''; -$TechEquivalents = array -( - 1 => 109, - 2 => 110, - 3 => 111, - 4 => 120, - 5 => 121, - 6 => 122, - 7 => 125, - 8 => 126, - 9 => 199, -); -$TechCount = count($TechEquivalents); +$combatTechs = AttackSimulator\Utils\CombatTechs\getTechsList(); + $MaxACSSlots = ACS_MAX_JOINED_FLEETS + 1; $MaxStringLength = 30; @@ -33,6 +25,17 @@ { $_POST['spyreport'] = json_decode(stripslashes($_POST['spyreport']), true); $_POST['def_techs'][1] = (isset($_POST['spyreport']['tech']) ? $_POST['spyreport']['tech'] : null); + $_POST['def_techs'][1] = object_map( + $_POST['def_techs'][1], + function ($value, $key) { + $safeKey = intval($key, 10); + + return [ + $value, + AttackSimulator\Utils\CombatTechs\getTechStandardKey($safeKey), + ]; + } + ); $_POST['def_ships'][1] = (isset($_POST['spyreport']['ships']) ? $_POST['spyreport']['ships'] : null); $_POST['spyreport'] = null; } @@ -41,234 +44,197 @@ { $Calculate = true; - if(!empty($_POST['atk_techs'])) - { - foreach($_POST['atk_techs'] as $User => $Vals) - { - $UserTemp = $User - 1; - foreach($TechEquivalents as $TechID => $TechKey) - { - if(!isset($Vals[$TechID]) || $Vals[$TechID] <= 0) - { - $Value = 0; + $AttackingTechs = []; + $DefendingTechs = []; + $AttackersData = []; + $DefendersData = []; + $AttackingFleets = []; + $DefendingFleets = []; + + $inputMappingGroups = [ + [ + 'techInputKey' => 'atk_techs', + 'fleetInputKey' => 'atk_ships', + 'moraleInputKey' => 'atk_morale', + 'techsAccumulatorObject' => &$AttackingTechs, + 'fleetAccumulatorObject' => &$AttackingFleets, + 'usersAccumulatorObject' => &$AttackersData, + 'usernamePrefix' => $_Lang['Attacker_Txt'], + ], + [ + 'techInputKey' => 'def_techs', + 'fleetInputKey' => 'def_ships', + 'moraleInputKey' => 'def_morale', + 'techsAccumulatorObject' => &$DefendingTechs, + 'fleetAccumulatorObject' => &$DefendingFleets, + 'usersAccumulatorObject' => &$DefendersData, + 'usernamePrefix' => $_Lang['Defender_Txt'], + ], + ]; + + /** + * @param array $params + * @param number $params['userSlotIdx'] + * @param string $params['usernamePrefix'] + * @param array $params['userKeyedTechs'] + */ + $createSimulationUserData = function ($params) { + return [ + 'fleetRow' => [ + 'fleet_owner' => '0', + 'fleet_start_galaxy' => '0', + 'fleet_start_system' => '0', + 'fleet_start_planet' => '0', + ], + 'user' => array_merge( + [ + 'username' => "{$params['usernamePrefix']}{$params['userSlotIdx']}", + ], + $params['userKeyedTechs'] + ), + 'moraleData' => null, + ]; + }; + + foreach ($inputMappingGroups as $inputMappingGroup) { + if (empty($_POST[$inputMappingGroup['techInputKey']])) { + continue; + } + + foreach ($_POST[$inputMappingGroup['techInputKey']] as $userSlotIdx => $userSlotData) { + $userIdx = $userSlotIdx - 1; + $userTechs = []; + + foreach ($userSlotData as $elementId => $elementValue) { + if (!World\Elements\isTechnology($elementId)) { + continue; } - else - { - $Value = intval($Vals[$TechID]); + if (!isset($inputMappingGroup['techsAccumulatorObject'][$userIdx])) { + $inputMappingGroup['techsAccumulatorObject'][$userIdx] = []; } - $AttackingTechs[$UserTemp][$TechKey] = $Value; + + $elementKey = World\Elements\getElementKey($elementId); + $safeElementValue = intval($elementValue, 10); + + $inputMappingGroup['techsAccumulatorObject'][$userIdx][$elementId] = $safeElementValue; + $userTechs[$elementKey] = $safeElementValue; } - $AttackersData[$UserTemp] = array - ( - 'username' => $_Lang['Attacker_Txt'].$User, - 'techs' => Array2String($AttackingTechs[$UserTemp]), - 'pos' => '0:0:0' - ); + + $inputMappingGroup['usersAccumulatorObject'][$userIdx] = $createSimulationUserData([ + 'userSlotIdx' => $userSlotIdx, + 'usernamePrefix' => $inputMappingGroup['usernamePrefix'], + 'userKeyedTechs' => $userTechs, + ]); } } - if(!empty($_POST['def_techs'])) - { - foreach($_POST['def_techs'] as $User => $Vals) - { - $UserTemp = $User - 1; - foreach($TechEquivalents as $TechID => $TechKey) - { - if(!isset($Vals[$TechID]) || $Vals[$TechID] <= 0) - { - $Value = 0; - } - else - { - $Value = intval($Vals[$TechID]); - } - $DefendingTechs[$UserTemp][$TechKey] = $Value; - } - $DefendersData[$UserTemp] = array - ( - 'username' => $_Lang['Defender_Txt'].$User, - 'techs' => Array2String($DefendingTechs[$UserTemp]), - 'pos' => '0:0:0' + + foreach ($inputMappingGroups as $inputMappingGroup) { + if (empty($_POST[$inputMappingGroup['fleetInputKey']])) { + $Calculate = false; + $BreakMSG = ( + $inputMappingGroup['fleetInputKey'] === 'atk_ships' ? + $_Lang['Break_noATKShips'] : + $_Lang['Break_noDEFShips'] ); + + break; } - } - if(!empty($_POST['atk_ships'])) - { - foreach($_POST['atk_ships'] as $User => $Vals) - { - $UserTemp = $User - 1; - foreach($Vals as $ID => $Count) - { - $Count = str_replace(array('.', ','), '', $Count); - if($Count > 0) - { - if(strlen($Count) > $MaxStringLength) - { - $Count = substr($Count, 0, $MaxStringLength); - } - $AttackingFleets[$UserTemp][$ID] = floor($Count); - } - } - if(empty($AttackingFleets[$UserTemp])) - { - unset($AttackingFleets[$UserTemp]); - unset($AttackingTechs[$UserTemp]); - unset($AttackersData[$UserTemp]); - } - else - { - if(empty($AttackersData[$UserTemp])) - { - foreach($TechEquivalents as $TechID => $TechKey) - { - if(!isset($Vals[$TechID]) || $Vals[$TechID] <= 0) - { - $Value = 0; - } - else - { - $Value = intval($Vals[$TechID]); - } - $AttackingTechs[$UserTemp][$TechKey] = $Value; - } - $AttackersData[$UserTemp] = array + + foreach ($_POST[$inputMappingGroup['fleetInputKey']] as $userSlotIdx => $userSlotData) { + $userIdx = $userSlotIdx - 1; + + foreach ($userSlotData as $elementId => $elementValue) { + $elementCount = str_replace([ '.', ',' ], '', $elementValue); + $elementCount = substr($elementCount, 0, $MaxStringLength); + $safeElementCount = floor($elementCount); + + if ( ( - 'username' => $_Lang['Attacker_Txt'].$User, - 'techs' => Array2String($AttackingTechs[$UserTemp]), - 'pos' => '0:0:0' - ); + !World\Elements\isShip($elementId) && + !World\Elements\isDefenseSystem($elementId) + ) || + $safeElementCount <= 0 + ) { + continue; } - } - } - } - else - { - $Calculate = false; - $BreakMSG = $_Lang['Break_noATKShips']; - } - if(!empty($_POST['def_ships'])) - { - foreach($_POST['def_ships'] as $User => $Vals) - { - $UserTemp = $User - 1; - foreach($Vals as $ID => $Count) - { - $Count = str_replace(array('.', ','), '', $Count); - if($Count > 0) - { - if(strlen($Count) > $MaxStringLength) - { - $Count = substr($Count, 0, $MaxStringLength); - } - $DefendingFleets[$UserTemp][$ID] = floor($Count); + if (!isset($inputMappingGroup['fleetAccumulatorObject'][$userIdx])) { + $inputMappingGroup['fleetAccumulatorObject'][$userIdx] = []; } + + $inputMappingGroup['fleetAccumulatorObject'][$userIdx][$elementId] = $safeElementCount; } - if(empty($DefendingFleets[$UserTemp])) - { - unset($DefendingFleets[$UserTemp]); - unset($DefendingTechs[$UserTemp]); - unset($DefendersData[$UserTemp]); - } - else - { - if(empty($DefendersData[$UserTemp])) - { - foreach($TechEquivalents as $TechID => $TechKey) - { - if(!isset($Vals[$TechID]) || $Vals[$TechID] <= 0) - { - $Value = 0; - } - else - { - $Value = intval($Vals[$TechID]); - } - $DefendingTechs[$UserTemp][$TechKey] = $Value; - } - $DefendersData[$UserTemp] = array - ( - 'username' => $_Lang['Defender_Txt'].$User, - 'techs' => Array2String($DefendingTechs[$UserTemp]), - 'pos' => '0:0:0' - ); - } + + if (empty($inputMappingGroup['fleetAccumulatorObject'][$userIdx])) { + // Unset user's techs & details + unset($inputMappingGroup['techsAccumulatorObject'][$userIdx]); + unset($inputMappingGroup['usersAccumulatorObject'][$userIdx]); + } else if (empty($inputMappingGroup['techsAccumulatorObject'][$userIdx])) { + // Fill user techs (with zeros) & details + $inputMappingGroup['techsAccumulatorObject'][$userIdx] = []; + $inputMappingGroup['usersAccumulatorObject'][$userIdx] = $createSimulationUserData([ + 'userSlotIdx' => $userSlotIdx, + 'usernamePrefix' => $inputMappingGroup['usernamePrefix'], + 'userKeyedTechs' => [], + ]); } } } - else - { - $Calculate = false; - $BreakMSG = $_Lang['Break_noDEFShips']; - } - if(isset($AttackersData)) - { - foreach($AttackersData as $UserTemp => $Data) - { - if(empty($AttackingFleets[$UserTemp])) - { - unset($AttackingFleets[$UserTemp]); - unset($AttackingTechs[$UserTemp]); - unset($AttackersData[$UserTemp]); - } + foreach ($AttackersData as $userSlotIdx => $userData) { + if (empty($AttackingFleets[$userSlotIdx])) { + unset($AttackingFleets[$userSlotIdx]); + unset($AttackingTechs[$userSlotIdx]); + unset($AttackersData[$userSlotIdx]); } } - if(isset($DefendersData)) - { - foreach($DefendersData as $UserTemp => $Data) - { - if(empty($DefendingFleets[$UserTemp])) - { - unset($DefendingFleets[$UserTemp]); - unset($DefendingTechs[$UserTemp]); - unset($DefendersData[$UserTemp]); - } + foreach ($DefendersData as $userSlotIdx => $userData) { + if (empty($DefendingFleets[$userSlotIdx])) { + unset($DefendingFleets[$userSlotIdx]); + unset($DefendingTechs[$userSlotIdx]); + unset($DefendersData[$userSlotIdx]); } } - if(empty($AttackingFleets) OR (empty($DefendingFleets))) - { - if(empty($AttackingFleets)) - { - $BreakMSG = $_Lang['Break_noATKShips']; - } - else if(empty($DefendingFleets)) - { - $BreakMSG = $_Lang['Break_noDEFShips']; - } + if ( + empty($AttackingFleets) || + empty($DefendingFleets) + ) { + $BreakMSG = ( + empty($AttackingFleets) ? + $_Lang['Break_noATKShips'] : + $_Lang['Break_noDEFShips'] + ); + $Calculate = false; } if (MORALE_ENABLED) { - if (!empty($AttackingFleets)) { - foreach ($AttackingFleets as $ThisUser => $ThisData) { - $ThisMoraleLevel = intval($_POST['atk_morale'][($ThisUser + 1)]); - $ThisMoraleLevel = keepInRange($ThisMoraleLevel, -100, 100); - - $AttackersData[$ThisUser]['morale'] = $ThisMoraleLevel; - - $moraleCombatModifiers = Flights\Utils\Modifiers\calculateMoraleCombatModifiers([ - 'moraleLevel' => $ThisMoraleLevel, - ]); - - $AttackingTechs[$ThisUser] = array_merge( - $AttackingTechs[$ThisUser], - $moraleCombatModifiers + foreach ($inputMappingGroups as $inputMappingGroup) { + $groupFleets = &$inputMappingGroup['fleetAccumulatorObject']; + $groupTechs = &$inputMappingGroup['techsAccumulatorObject']; + $groupUsersData = &$inputMappingGroup['usersAccumulatorObject']; + $inputKey = $inputMappingGroup['moraleInputKey']; + $moraleInputSlots = $_POST[$inputKey]; + + foreach ($groupFleets as $userSlotIdx => $userFleets) { + $userMoraleLevel = ( + isset($moraleInputSlots[($userSlotIdx + 1)]) ? + intval($moraleInputSlots[($userSlotIdx + 1)]) : + 0 ); - } - } - if (!empty($DefendingFleets)) { - foreach ($DefendingFleets as $ThisUser => $ThisData) { - $ThisMoraleLevel = intval($_POST['def_morale'][($ThisUser + 1)]); - $ThisMoraleLevel = keepInRange($ThisMoraleLevel, -100, 100); + $userMoraleLevel = keepInRange($userMoraleLevel, -100, 100); - $DefendersData[$ThisUser]['morale'] = $ThisMoraleLevel; + $groupUsersData[$userSlotIdx]['moraleData'] = [ + 'morale_level' => $userMoraleLevel, + 'morale_points' => 0, + ]; $moraleCombatModifiers = Flights\Utils\Modifiers\calculateMoraleCombatModifiers([ - 'moraleLevel' => $ThisMoraleLevel, + 'moraleLevel' => $userMoraleLevel, ]); - $DefendingTechs[$ThisUser] = array_merge( - $DefendingTechs[$ThisUser], + $groupTechs[$userSlotIdx] = array_merge( + $groupTechs[$userSlotIdx], $moraleCombatModifiers ); } @@ -293,8 +259,16 @@ $SimData['max_rounds'] = 0; $SimData['min_rounds'] = 99; - $SimData['total_lost_atk'] = array('met' => 0, 'cry' => 0, 'deu' => 0); - $SimData['total_lost_def'] = array('met' => 0, 'cry' => 0, 'deu' => 0); + $SimData['total_lost_atk'] = [ + 'met' => 0, + 'cry' => 0, + 'deu' => 0, + ]; + $SimData['total_lost_def'] = [ + 'met' => 0, + 'cry' => 0, + 'deu' => 0, + ]; $SimData['ship_lost_atk'] = 0; $SimData['ship_lost_def'] = 0; $SimData['ship_lost_atk_min'] = 99999999999999999999.0; @@ -302,41 +276,41 @@ $SimData['ship_lost_def_min'] = 99999999999999999999.0; $SimData['ship_lost_def_max'] = 0; - $TotalTime = 0; - for($i = 1; $i <= $Loop; $i += 1) - { - $Temp['ship_lost_atk'] = 0; - $Temp['ship_lost_def'] = 0; + $allSimulationsTotalTime = 0; + + for ( + $i = 1; + $i <= $Loop; + $i += 1 + ) { + $Temp = [ + 'ship_lost_atk' => 0, + 'ship_lost_def' => 0, + ]; - $StartTime = microtime(true); + $simulationStartTimestamp = microtime(true); - // Now start Combat calculations $Combat = Combat($AttackingFleets, $DefendingFleets, $AttackingTechs, $DefendingTechs, true); - $EndTime = microtime(true); - $TimeNow = $EndTime - $StartTime; - $TotalTime += $TimeNow; - $totaltime = sprintf('%0.6f', $TimeNow); + $simulationEndTimestamp = microtime(true); - $RoundsData = $Combat['rounds']; + $combatSimulationTime = $simulationEndTimestamp - $simulationStartTimestamp; + $combatSimulationTimeFormatted = sprintf('%0.6f', $combatSimulationTime); + $allSimulationsTotalTime += $combatSimulationTime; - $RoundCount = count($RoundsData) - 1; - if($RoundCount > $SimData['max_rounds']) - { + $RoundCount = count($Combat['rounds']) - 1; + + if ($RoundCount > $SimData['max_rounds']) { $SimData['max_rounds'] = $RoundCount; } - if($RoundCount < $SimData['min_rounds']) - { + if ($RoundCount < $SimData['min_rounds']) { $SimData['min_rounds'] = $RoundCount; } $SimData['rounds'] += $RoundCount; $Result = $Combat['result']; - $AtkShips = $Combat['AttackerShips']; - $DefShips = $Combat['DefenderShips']; $AtkLost = $Combat['AtkLose']; $DefLost = $Combat['DefLose']; - $DefSysLost = $Combat['DefSysLost']; $debrisRecoveryPercentages = [ 'ships' => ($_GameConfig['Fleet_Cdr'] / 100), @@ -354,11 +328,13 @@ $RealDebrisDeuteriumAtk = $attackersResourceLosses['realLoss']['deuterium']; if (!empty($AtkLost)) { - foreach ($AtkLost as $ID => $Count) { - if ($ID > 200 && $ID < 300) { - $SimData['ship_lost_atk'] += $Count; - $Temp['ship_lost_atk'] += $Count; + foreach ($AtkLost as $shipId => $shipCount) { + if (!World\Elements\isShip($shipId)) { + continue; } + + $SimData['ship_lost_atk'] += $shipCount; + $Temp['ship_lost_atk'] += $shipCount; } } @@ -377,9 +353,9 @@ $RealDebrisDeuteriumDef = $defendersResourceLosses['realLoss']['deuterium']; if (!empty($DefLost)) { - foreach ($DefLost as $ID => $Count) { - $SimData['ship_lost_def'] += $Count; - $Temp['ship_lost_def'] += $Count; + foreach ($DefLost as $shipId => $shipCount) { + $SimData['ship_lost_def'] += $shipCount; + $Temp['ship_lost_def'] += $shipCount; } } @@ -397,8 +373,7 @@ $defendersResourceLosses['recoverableLoss']['crystal'] ); - switch($Result) - { + switch ($Result) { case COMBAT_ATK: $SimData['atk_win'] += 1; break; @@ -410,8 +385,7 @@ break; } - switch($Result) - { + switch ($Result) { case COMBAT_ATK: $_Lang['Winner_Color'] = 'red'; $_Lang['Winner_Name'] = $_Lang['WonBy_Attacker']; @@ -430,77 +404,70 @@ 'totalDebris' => ($TotalLostMetal + $TotalLostCrystal), ]); - $ReportData = array(); - - $ReportData['init']['usr']['atk'] = $AttackersData; - $ReportData['init']['usr']['def'] = $DefendersData; - - $ReportData['init']['time'] = $totaltime; - $ReportData['init']['date'] = time(); - - $ReportData['init']['result'] = $Result; - $ReportData['init']['met'] = 0; - $ReportData['init']['cry'] = 0; - $ReportData['init']['deu'] = 0; - $ReportData['init']['deb_met'] = $TotalLostMetal; - $ReportData['init']['deb_cry'] = $TotalLostCrystal; - $ReportData['init']['moon_chance'] = $moonCreationRollResult['boundedMoonChance']; - $ReportData['init']['total_moon_chance'] = $moonCreationRollResult['totalMoonChance']; - $ReportData['init']['moon_created'] = false; - $ReportData['init']['moon_destroyed'] = false; - $ReportData['init']['moon_des_chance'] = '0'; - $ReportData['init']['fleet_destroyed'] = false; - $ReportData['init']['fleet_des_chance'] = '0'; - $ReportData['init']['planet_name'] = 'Planeta'; - $ReportData['init']['onMoon'] = false; - $ReportData['init']['atk_lost'] = $RealDebrisMetalAtk + $RealDebrisCrystalAtk + $RealDebrisDeuteriumAtk; - $ReportData['init']['def_lost'] = $RealDebrisMetalDef + $RealDebrisCrystalDef + $RealDebrisDeuteriumDef; - - foreach($RoundsData as $RoundKey => $RoundData) - { - foreach($RoundData as $MainKey => $RoundData2) - { - if(!empty($RoundData2['ships'])) - { - foreach($RoundData2['ships'] as $UserKey => $UserData) - { - $RoundsData[$RoundKey][$MainKey]['ships'][$UserKey] = Array2String($UserData); - } - } - } - } - $ReportData['rounds'] = $RoundsData; + $combatReportData = Flights\Utils\Factories\createCombatReportData([ + 'fleetRow' => [ + 'fleet_start_time' => time(), + ], + 'targetPlanet' => [ + 'name' => $_Lang['BR_Target_1'], + 'fleet_end_type' => 1, + ], + 'usersData' => [ + 'attackers' => $AttackersData, + 'defenders' => $DefendersData, + ], + 'combatData' => $Combat, + 'combatCalculationTime' => $combatSimulationTimeFormatted, + 'moraleData' => null, + 'totalResourcesPillage' => [ + 'metal' => 0, + 'crystal' => 0, + 'deuterium' => 0, + ], + 'resourceLosses' => [ + 'attackers' => $attackersResourceLosses, + 'defenders' => $defendersResourceLosses, + ], + 'moonCreationData' => [ + 'hasBeenCreated' => false, + 'normalizedChance' => $moonCreationRollResult['boundedMoonChance'], + 'totalChance' => $moonCreationRollResult['totalMoonChance'], + ], + 'moonDestructionData' => null, + ]); - $ReportID = CreateBattleReport($ReportData, array('atk' => $_User['id'], 'def' => 0), 0, true); + $ReportID = CreateBattleReport( + $combatReportData, + [ + 'atk' => $_User['id'], + 'def' => 0, + ], + 0, + true + ); $parse = $_Lang; $parse['id'] = $ReportID; $AllReports[] = $ReportID; - if($i == $Loop) - { - $parse['time'] = sprintf('%0.6f', $TotalTime); - } - else - { - $parse['time'] = $totaltime; - } - if($Temp['ship_lost_atk'] < $SimData['ship_lost_atk_min']) - { + $parse['time'] = ( + ($i == $Loop) ? + sprintf('%0.6f', $allSimulationsTotalTime) : + $combatSimulationTimeFormatted + ); + + if ($Temp['ship_lost_atk'] < $SimData['ship_lost_atk_min']) { $SimData['ship_lost_atk_min'] = $Temp['ship_lost_atk']; } - if($Temp['ship_lost_atk'] > $SimData['ship_lost_atk_max']) - { + if ($Temp['ship_lost_atk'] > $SimData['ship_lost_atk_max']) { $SimData['ship_lost_atk_max'] = $Temp['ship_lost_atk']; } - if($Temp['ship_lost_def'] < $SimData['ship_lost_def_min']) - { + if ($Temp['ship_lost_def'] < $SimData['ship_lost_def_min']) { $SimData['ship_lost_def_min'] = $Temp['ship_lost_def']; } - if($Temp['ship_lost_def'] > $SimData['ship_lost_def_max']) - { + if ($Temp['ship_lost_def'] > $SimData['ship_lost_def_max']) { $SimData['ship_lost_def_max'] = $Temp['ship_lost_def']; } } @@ -526,7 +493,7 @@ $_Lang['SimResult'] .= parsetemplate(gettemplate('simulator_result'), $parse); // Trigger Tasks Check - Tasks_TriggerTask($_User, 'USE_SIMULATOR'); + Tasks_TriggerTask($_User, TaskType::UseSimulator); } else { @@ -543,157 +510,87 @@ $TPL_Row = gettemplate('simulator_row'); $TPL_NoLeft = gettemplate('simulator_row_noleft'); -$Offsets; - -for($i = 1; $i <= $MaxACSSlots; $i += 1) -{ - $ThisSlot = array(); - $ThisSlot['SlotID'] = $i; - $ThisSlot['txt'] = ''; - - $InsertTabIndex1 = 1; - $InsertTabIndex2 = 1; - $parse = $_Lang; - $parse['i'] = $i; - if($i > 1) - { - $ThisSlot['SlotHidden'] = 'hide'; - } - - if(MORALE_ENABLED) - { - $parse['RowText'] = $_Lang['Morale']; - $ThisSlot['txt'] .= parsetemplate($TPL_SingleRow, $parse); - $parse['RowText'] = $_Lang['Morale_Level']; - $parse['RowInput'] = "%"; - $parse['RowText2'] = $_Lang['Morale_Level']; - $parse['RowInput2'] = "%"; - - $ThisSlot['txt'] .= parsetemplate($TPL_Row, $parse); - $InsertTabIndex1 += 1; - $InsertTabIndex2 += 1; - } - - $parse['RowText'] = $_Lang['Technology']; - $ThisSlot['txt'] .= parsetemplate($TPL_SingleRow, $parse); - $parse['RowText'] = ''.$_Lang['FillMyTechs'].' / '.$_Lang['Fill_Clean'].''; - $parse['RowText2'] = ''.$_Lang['FillMyTechs'].' / '.$_Lang['Fill_Clean'].''; - $ThisSlot['txt'] .= parsetemplate($TPL_NoBoth, $parse); - - for($techs = 1; $techs <= $TechCount; $techs += 1) - { - $ThisRow_InsertValue_Atk = isset($_POST['atk_techs'][$i][$techs]) ? $_POST['atk_techs'][$i][$techs] : null; - $ThisRow_InsertValue_Def = isset($_POST['def_techs'][$i][$techs]) ? $_POST['def_techs'][$i][$techs] : null; - - $parse['RowText'] = $_Lang['Techs'][$techs]; - $parse['RowInput'] = ""; - $parse['RowText2'] = $_Lang['Techs'][$techs]; - $parse['RowInput2'] = ""; - - $ThisSlot['txt'] .= parsetemplate($TPL_Row, $parse); - $InsertTabIndex1 += 1; - $InsertTabIndex2 += 1; - } - - $parse['RowText'] = $_Lang['Fleets']; - $ThisSlot['txt'] .= parsetemplate($TPL_SingleRow, $parse); - $parse['RowText'] = ''.$_Lang['FillMyFleets'].' / '.$_Lang['Fill_Clean'].''; - $parse['RowText2'] = ''.$_Lang['FillMyFleets'].' / '.$_Lang['Fill_Clean'].''; - $ThisSlot['txt'] .= parsetemplate($TPL_NoBoth, $parse); - - foreach($_Vars_ElementCategories['fleet'] as $Ships) - { - $ThisRow_InsertValue_Def = isset($_POST['def_ships'][$i][$Ships]) ? $_POST['def_ships'][$i][$Ships] : null; - - if (hasAnyEngine($Ships)) { - $ThisRow_InsertValue_Atk = isset($_POST['atk_ships'][$i][$Ships]) ? $_POST['atk_ships'][$i][$Ships] : null; - - $parse['RowText'] = $_Lang['tech'][$Ships]; - $parse['RowInput'] = " ({$_Lang['Button_Min']} / {$_Lang['Button_Max']})"; - $parse['RowText2'] = $_Lang['tech'][$Ships]; - $parse['RowInput2'] = " ({$_Lang['Button_Min']} / {$_Lang['Button_Max']})"; - - $ThisSlot['txt'] .= parsetemplate($TPL_Row, $parse); - $InsertTabIndex1 += 1; - $InsertTabIndex2 += 1; - } else { - $parse['RowText'] = '-'; - $parse['RowText2'] = $_Lang['tech'][$Ships]; - $parse['RowInput2'] = " ({$_Lang['Button_Min']} / {$_Lang['Button_Max']})"; - - $ThisSlot['txt'] .= parsetemplate($TPL_NoLeft, $parse); - $InsertTabIndex2 += 1; - } - } - - if($i == 1) - { - $parse['RowText'] = $_Lang['Defense']; - $ThisSlot['txt'] .= parsetemplate($TPL_SingleRow, $parse); - - foreach($_Vars_ElementCategories['defense'] as $Ships) - { - if(in_array($Ships, $_Vars_ElementCategories['rockets'])) - { - continue; - } +for ( + $slotIdx = 1; + $slotIdx <= $MaxACSSlots; + $slotIdx += 1 +) { + $thisSlotElements = [ + ( + MORALE_ENABLED ? + AttackSimulator\Components\MoraleInputsSection\render([ + 'slotIdx' => $slotIdx, + 'input' => &$_POST, + ])['componentHTML'] : + '' + ), + AttackSimulator\Components\TechInputsSection\render([ + 'slotIdx' => $slotIdx, + 'input' => &$_POST, + ])['componentHTML'], + AttackSimulator\Components\UnitInputsSection\render([ + 'slotIdx' => $slotIdx, + 'input' => &$_POST, + ])['componentHTML'], + ]; + $isMainFleetsSlot = ($slotIdx === 1); + + $thisSlotProps = [ + 'SlotID' => $slotIdx, + 'SlotInitiallyHidden' => ( + !$isMainFleetsSlot ? + 'hide' : + '' + ), + 'txt' => implode('', $thisSlotElements), + ]; + + $_Lang['rows'] .= parsetemplate($TPL_Slot, $thisSlotProps); +} - $ThisRow_InsertValue_Def = isset($_POST['def_ships'][$i][$Ships]) ? $_POST['def_ships'][$i][$Ships] : null; +$isUsingPrettyInputs = ($_User['settings_useprettyinputbox'] == 1); - $parse['RowText'] = '-'; - $parse['RowText2'] = $_Lang['tech'][$Ships]; - $parse['RowInput2'] = " ({$_Lang['Button_Min']} / {$_Lang['Button_Max']})"; +$ownTechLevels = object_map( + $combatTechs, + function ($elementId) use (&$_Planet, &$_User) { + $currentLevel = World\Elements\getElementCurrentLevel($elementId, $_Planet, $_User); - $ThisSlot['txt'] .= parsetemplate($TPL_NoLeft, $parse); - $InsertTabIndex2 += 1; - } + return [ + $currentLevel, + $elementId + ]; } - $Offsets[$i] = $InsertTabIndex1 - 1; - - $_Lang['rows'] .= parsetemplate($TPL_Slot, $ThisSlot); -} - -$_Lang['rows'] = preg_replace_callback( - '#\{REP1_O([0-9]{1,})_([0-9]{1,})\}#Ssi', - function ($matches) { - return ($matches[1] * 1000) + $matches[2]; - }, - $_Lang['rows'] -); -$_Lang['rows'] = preg_replace_callback( - '#\{REP2_O([0-9]{1,})_([0-9]{1,})\}#Ssi', - function ($matches) use ($Offsets) { - return ($matches[1] * 1000) + $Offsets[$matches[1]] + $matches[2]; - }, - $_Lang['rows'] ); -$_Lang['fill_with_mytechs'] = "var MyTechs = new Array();\nMyTechs[1] = ".(string)($_User['tech_weapons'] + 0).";\nMyTechs[3] = ".(string)($_User['tech_shielding'] + 0).";\nMyTechs[2] = ".(string)($_User['tech_armour'] + 0).";\nMyTechs[4] = ".(string)($_User['tech_laser'] + 0).";\nMyTechs[5] = ".(string)($_User['tech_ion'] + 0).";\nMyTechs[6] = ".(string)($_User['tech_plasma'] + 0).";\nMyTechs[7] = ".(string)($_User['tech_antimatter'] + 0).";\nMyTechs[8] = ".(string)($_User['tech_disintegration'] + 0).";\nMyTechs[9] = ".(string)($_User['tech_graviton'] + 0).";\n"; -$_Lang['fill_with_myfleets'] = "var MyFleets = new Array();\n"; - -$UsingPrettyInputBox = ($_User['settings_useprettyinputbox'] == 1 ? true : false); - -foreach($_Vars_ElementCategories['fleet'] as $ID) -{ - if($_Planet[$_Vars_GameElements[$ID]] > 0) - { - $_Lang['fill_with_myfleets'] .= "MyFleets[{$ID}] = '".($UsingPrettyInputBox === true ? prettyNumber($_Planet[$_Vars_GameElements[$ID]]) : $_Planet[$_Vars_GameElements[$ID]])."';\n"; +$fleetsAndDefenses = array_filter( + array_merge($_Vars_ElementCategories['fleet'], $_Vars_ElementCategories['defense']), + function ($elementId) { + return ( + World\Elements\isShip($elementId) || + World\Elements\isDefenseSystem($elementId) + ); } -} -foreach($_Vars_ElementCategories['defense'] as $ID) -{ - if(in_array($ID, $_Vars_ElementCategories['rockets'])) - { - continue; +); +$ownFleetsAndDefenses = object_map( + $fleetsAndDefenses, + function ($elementId) use (&$_Planet, &$_User, $isUsingPrettyInputs) { + $currentCount = World\Elements\getElementCurrentCount($elementId, $_Planet, $_User); + $currentCountDisplay = ( + $isUsingPrettyInputs ? + prettyNumber($currentCount) : + $currentCount + ); + + return [ + $currentCountDisplay, + $elementId + ]; } +); - if($_Planet[$_Vars_GameElements[$ID]] > 0) - { - $_Lang['fill_with_myfleets'] .= "MyFleets[{$ID}] = '".($UsingPrettyInputBox === true ? prettyNumber($_Planet[$_Vars_GameElements[$ID]]) : $_Planet[$_Vars_GameElements[$ID]])."';\n"; - } -} -$_Lang['fill_with_myfleets'] .= "\n"; -$_Lang['AllowPrettyInputBox'] = ($_User['settings_useprettyinputbox'] == 1 ? 'true' : 'false'); +$_Lang['fill_with_mytechs'] = json_encode($ownTechLevels); +$_Lang['fill_with_myfleets'] = json_encode($ownFleetsAndDefenses); +$_Lang['AllowPrettyInputBox'] = ($isUsingPrettyInputs ? 'true' : 'false'); //Display page $page = parsetemplate(gettemplate('simulator'), $_Lang); diff --git a/templates/default_template/simulator.tpl b/templates/default_template/simulator.tpl index 874918b4d..6829fd541 100644 --- a/templates/default_template/simulator.tpl +++ b/templates/default_template/simulator.tpl @@ -1,7 +1,7 @@ diff --git a/templates/default_template/simulator_slot.tpl b/templates/default_template/simulator_slot.tpl index 7562d8342..e753c3cbf 100644 --- a/templates/default_template/simulator_slot.tpl +++ b/templates/default_template/simulator_slot.tpl @@ -1 +1 @@ -{txt} +{txt}