From 4d81c52037d5a9cc20f9b9410e2e8fd1a695ee0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Thu, 28 Jul 2022 01:57:13 +0200 Subject: [PATCH] GH-91 Create shared user data object creator --- simulator.php | 66 ++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/simulator.php b/simulator.php index a774b478..3c6077a6 100644 --- a/simulator.php +++ b/simulator.php @@ -82,6 +82,30 @@ function ($value, $key) use ($TechEquivalents) { ], ]; + /** + * @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; @@ -106,21 +130,11 @@ function ($value, $key) use ($TechEquivalents) { $userTechs[$elementKey] = $safeElementValue; } - $inputMappingGroup['usersAccumulatorObject'][$userIdx] = [ - 'fleetRow' => [ - 'fleet_owner' => '0', - 'fleet_start_galaxy' => '0', - 'fleet_start_system' => '0', - 'fleet_start_planet' => '0', - ], - 'user' => array_merge( - [ - 'username' => "{$inputMappingGroup['usernamePrefix']}{$userSlotIdx}", - ], - $userTechs - ), - 'moraleData' => null, - ]; + $inputMappingGroup['usersAccumulatorObject'][$userIdx] = $createSimulationUserData([ + 'userSlotIdx' => $userSlotIdx, + 'usernamePrefix' => $inputMappingGroup['usernamePrefix'], + 'userKeyedTechs' => $userTechs, + ]); } } @@ -166,24 +180,12 @@ function ($value, $key) use ($TechEquivalents) { unset($inputMappingGroup['usersAccumulatorObject'][$userIdx]); } else if (empty($inputMappingGroup['techsAccumulatorObject'][$userIdx])) { // Fill user techs (with zeros) & details - $userTechs = []; - $inputMappingGroup['techsAccumulatorObject'][$userIdx] = []; - $inputMappingGroup['usersAccumulatorObject'][$userIdx] = [ - 'fleetRow' => [ - 'fleet_owner' => '0', - 'fleet_start_galaxy' => '0', - 'fleet_start_system' => '0', - 'fleet_start_planet' => '0', - ], - 'user' => array_merge( - [ - 'username' => "{$inputMappingGroup['usernamePrefix']}{$userSlotIdx}", - ], - $userTechs - ), - 'moraleData' => null, - ]; + $inputMappingGroup['usersAccumulatorObject'][$userIdx] = $createSimulationUserData([ + 'userSlotIdx' => $userSlotIdx, + 'usernamePrefix' => $inputMappingGroup['usernamePrefix'], + 'userKeyedTechs' => [], + ]); } } }