<commit_before><?php

define('INSIDE', true);

$_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();

includeLang('simulator');
$_Lang['rows'] = '';
$_Lang['SimResult'] = '';

$TechEquivalents = [
    1 => 109,
    2 => 110,
    3 => 111,
    4 => 120,
    5 => 121,
    6 => 122,
    7 => 125,
    8 => 126,
    9 => 199,
];
$TechCount = count($TechEquivalents);
$MaxACSSlots = ACS_MAX_JOINED_FLEETS + 1;
$MaxStringLength = 30;

if(!empty($_POST['spyreport']))
{
    $_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) use ($TechEquivalents) {
            $safeKey = intval($key, 10);

            return [
                $value,
                $TechEquivalents[$safeKey],
            ];
        }
    );
    $_POST['def_ships'][1] = (isset($_POST['spyreport']['ships']) ? $_POST['spyreport']['ships'] : null);
    $_POST['spyreport'] = null;
}

if(isset($_POST['simulate']) && $_POST['simulate'] == 'yes')
{
    $Calculate = true;

    $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'],
        ],
    ];

    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;
                }
                if (!isset($inputMappingGroup['techsAccumulatorObject'][$userIdx])) {
                    $inputMappingGroup['techsAccumulatorObject'][$userIdx] = [];
                }

                $elementKey = World\Elements\getElementKey($elementId);
                $safeElementValue = intval($elementValue, 10);

                $inputMappingGroup['techsAccumulatorObject'][$userIdx][$elementId] = $safeElementValue;
                $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,
            ];
        }
    }

    foreach ($inputMappingGroups as $inputMappingGroup) {
        if (empty($_POST[$inputMappingGroup['fleetInputKey']])) {
            $Calculate = false;
            $BreakMSG = (
                $inputMappingGroup['fleetInputKey'] === 'atk_ships' ?
                    $_Lang['Break_noATKShips'] :
                    $_Lang['Break_noDEFShips']
            );

            break;
        }

        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 (
                    (
                        !World\Elements\isShip($elementId) &&
                        !World\Elements\isDefenseSystem($elementId)
                    ) ||
                    $safeElementCount <= 0
                ) {
                    continue;
                }
                if (!isset($inputMappingGroup['fleetAccumulatorObject'][$userIdx])) {
                    $inputMappingGroup['fleetAccumulatorObject'][$userIdx] = [];
                }

                $inputMappingGroup['fleetAccumulatorObject'][$userIdx][$elementId] = $safeElementCount;
            }

            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
                $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,
                ];
            }
        }
    }

    foreach ($AttackersData as $userSlotIdx => $userData) {
        if (empty($AttackingFleets[$userSlotIdx])) {
            unset($AttackingFleets[$userSlotIdx]);
            unset($AttackingTechs[$userSlotIdx]);
            unset($AttackersData[$userSlotIdx]);
        }
    }
    foreach ($DefendersData as $userSlotIdx => $userData) {
        if (empty($DefendingFleets[$userSlotIdx])) {
            unset($DefendingFleets[$userSlotIdx]);
            unset($DefendingTechs[$userSlotIdx]);
            unset($DefendersData[$userSlotIdx]);
        }
    }

    if (
        empty($AttackingFleets) ||
        empty($DefendingFleets)
    ) {
        $BreakMSG = (
            empty($AttackingFleets) ?
                $_Lang['Break_noATKShips'] :
                $_Lang['Break_noDEFShips']
        );

        $Calculate = false;<commit_msg>GH-91 Create shared user data object creator<commit_after><?php

define('INSIDE', true);

$_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();

includeLang('simulator');
$_Lang['rows'] = '';
$_Lang['SimResult'] = '';

$TechEquivalents = [
    1 => 109,
    2 => 110,
    3 => 111,
    4 => 120,
    5 => 121,
    6 => 122,
    7 => 125,
    8 => 126,
    9 => 199,
];
$TechCount = count($TechEquivalents);
$MaxACSSlots = ACS_MAX_JOINED_FLEETS + 1;
$MaxStringLength = 30;

if(!empty($_POST['spyreport']))
{
    $_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) use ($TechEquivalents) {
            $safeKey = intval($key, 10);

            return [
                $value,
                $TechEquivalents[$safeKey],
            ];
        }
    );
    $_POST['def_ships'][1] = (isset($_POST['spyreport']['ships']) ? $_POST['spyreport']['ships'] : null);
    $_POST['spyreport'] = null;
}

if(isset($_POST['simulate']) && $_POST['simulate'] == 'yes')
{
    $Calculate = true;

    $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;
                }
                if (!isset($inputMappingGroup['techsAccumulatorObject'][$userIdx])) {
                    $inputMappingGroup['techsAccumulatorObject'][$userIdx] = [];
                }

                $elementKey = World\Elements\getElementKey($elementId);
                $safeElementValue = intval($elementValue, 10);

                $inputMappingGroup['techsAccumulatorObject'][$userIdx][$elementId] = $safeElementValue;
                $userTechs[$elementKey] = $safeElementValue;
            }

            $inputMappingGroup['usersAccumulatorObject'][$userIdx] = $createSimulationUserData([
                'userSlotIdx' => $userSlotIdx,
                'usernamePrefix' => $inputMappingGroup['usernamePrefix'],
                'userKeyedTechs' => $userTechs,
            ]);
        }
    }

    foreach ($inputMappingGroups as $inputMappingGroup) {
        if (empty($_POST[$inputMappingGroup['fleetInputKey']])) {
            $Calculate = false;
            $BreakMSG = (
                $inputMappingGroup['fleetInputKey'] === 'atk_ships' ?
                    $_Lang['Break_noATKShips'] :
                    $_Lang['Break_noDEFShips']
            );

            break;
        }

        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 (
                    (
                        !World\Elements\isShip($elementId) &&
                        !World\Elements\isDefenseSystem($elementId)
                    ) ||
                    $safeElementCount <= 0
                ) {
                    continue;
                }
                if (!isset($inputMappingGroup['fleetAccumulatorObject'][$userIdx])) {
                    $inputMappingGroup['fleetAccumulatorObject'][$userIdx] = [];
                }

                $inputMappingGroup['fleetAccumulatorObject'][$userIdx][$elementId] = $safeElementCount;
            }

            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' => [],
                ]);
            }
        }
    }

    foreach ($AttackersData as $userSlotIdx => $userData) {
        if (empty($AttackingFleets[$userSlotIdx])) {
            unset($AttackingFleets[$userSlotIdx]);
            unset($AttackingTechs[$userSlotIdx]);
            unset($AttackersData[$userSlotIdx]);
        }
    }
    foreach ($DefendersData as $userSlotIdx => $userData) {
        if (empty($DefendingFleets[$userSlotIdx])) {
            unset($DefendingFleets[$userSlotIdx]);
            unset($DefendingTechs[$userSlotIdx]);
            unset($DefendersData[$userSlotIdx]);
        }
    }

    if (
        empty($AttackingFleets) ||
        empty($DefendingFleets)
    ) {
        $BreakMSG = (
            empty($AttackingFleets) ?
                $_Lang['Break_noATKShips'] :
                $_Lang['Break_noDEFShips']
        );

        $Calculate = false;
{'fraction_non_alphanumeric': 0.14842238676539257, 'fraction_numerical': 0.007737976119004736, 'mean_word_length': 1.9757840412862246, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '860940', 'n_tokens_mistral': 4540, 'n_tokens_neox': 4218, 'n_words': 569}