<commit_before><?php

namespace UniEngine\Engine\Modules\FlightControl\Components\FlightsList\Utils;

use UniEngine\Engine\Includes\Helpers\Common\Collections;
use UniEngine\Engine\Modules\Flights;


// TODO: Reuse, same code as in FriendlyAcsList
function _getOwnFleetBehaviorDetails($params) {
    global $_Lang;

    $fleetEntry = $params['fleetEntry'];
    $currentTimestamp = $params['currentTimestamp'];

    if ($fleetEntry['fleet_start_time'] >= $currentTimestamp) {
        return [
            'behavior' => $_Lang['fl_get_to_ttl'],
            'behaviorTxt' => $_Lang['fl_get_to'],
        ];
    }

    if (
        $fleetEntry['fleet_end_stay'] > 0 &&
        $fleetEntry['fleet_end_stay'] > $currentTimestamp
    ) {
        $isMissionExpedition = $fleetEntry['fleet_mission'] == Flights\Enums\FleetMission::Expedition;

        return [
            'behavior' => (
                $isMissionExpedition ?
                    $_Lang['fl_explore_to_ttl'] :
                    $_Lang['fl_stay_to_ttl']
            ),
            'behaviorTxt' => (
                $isMissionExpedition ?
                    $_Lang['fl_explore_to'] :
                    $_Lang['fl_stay_to']
            ),
        ];
    }

    if ($fleetEntry['fleet_end_time'] > $currentTimestamp) {
        return [
            'behavior' => $_Lang['fl_back_to_ttl'],
            'behaviorTxt' => $_Lang['fl_back_to'],
        ];
    }

    return [
        'behavior' => $_Lang['fl_cameback_ttl'],
        'behaviorTxt' => $_Lang['fl_cameback'],
    ];
}

// TODO: this should most likely be a component
//  Arguments
//      - $params (Object)
//          - elementNo (Number)
//          - fleetEntry (Object)
//          - acsMainFleets (Record<mainFleetId: string, object>)
//          - currentTimestamp (Number)
//          - acsUnionsExtraSquads (Record<mainFleetId: string, object>)
//          - relatedAcsFleets (Array<object>)
//          - isJoiningThisUnion (Boolean)
//
function buildOwnListElement($params) {
    global $_Lang;

    $elementNo = $params['elementNo'];
    $fleetEntry = $params['fleetEntry'];
    $acsMainFleets = $params['acsMainFleets'];
    $currentTimestamp = $params['currentTimestamp'];
    $acsUnionsExtraSquads = $params['acsUnionsExtraSquads'];
    $relatedAcsFleets = $params['relatedAcsFleets'];
    $isJoiningThisUnion = $params['isJoiningThisUnion'];

    $fleetShipsRowTpl = gettemplate('fleet_fdetail');
    $fleetUnionSquadMainTpl = gettemplate('fleet_faddinfo');
    $fleetResourcesRowTpl = gettemplate('fleet_fresinfo');
    $fleetOrdersRetreatTpl = gettemplate('fleet_orders_retreat');
    $fleetOrdersCreateUnionTpl = gettemplate('fleet_orders_acs');
    $fleetOrdersJoinUnionTpl = gettemplate('fleet_orders_jointoacs');

    $fleetResourcesRowTpl = str_replace(
        [
            'TitleMain',
            'TitleMetal',
            'TitleCrystal',
            'TitleDeuterium',
        ],
        [
            $_Lang['fl_fleetinfo_resources'],
            $_Lang['Metal'],
            $_Lang['Crystal'],
            $_Lang['Deuterium'],
        ],
        $fleetResourcesRowTpl
    );

    $fleetId = $fleetEntry['fleet_id'];
    $fleetShips = String2Array($fleetEntry['fleet_array']);
    $fleetMissionType = $fleetEntry['fleet_mission'];
    $flightElapsedTime = ($currentTimestamp - $fleetEntry['fleet_send_time']);
    $flightTimeRemaining = ($fleetEntry['fleet_start_time'] - $currentTimestamp);
    $flightStayRemaining = ($fleetEntry['fleet_end_stay'] - $currentTimestamp);
    $flightComeBackRemaining = ($fleetEntry['fleet_end_time'] - $currentTimestamp);
    $isMainAcsFleet = !empty($acsMainFleets[$fleetId]);
    $hasUnionExtraSquad = !empty($acsUnionsExtraSquads[$fleetId]);
    $isFleetMissionNotCalculated = ($fleetEntry['fleet_mess'] == 0);
    $isNotCalculatedStationMission = (
        $fleetMissionType === Flights\Enums\FleetMission::Station &&
        $isFleetMissionNotCalculated
    );
    $isStillProtectingHoldMission = (
        !$isFleetMissionNotCalculated &&
        $fleetMissionType === Flights\Enums\FleetMission::Hold &&
        $flightStayRemaining > 0
    );
    $isRetreatStillAvailable = (
        $isFleetMissionNotCalculated ||
        $isStillProtectingHoldMission
    );

    $fleetMissionDisplayType = (
        (
            $isMainAcsFleet &&
            $acsMainFleets[$fleetId]['hasJoinedFleets']
        ) ?
            Flights\Enums\FleetMission::UnitedAttack :
            $fleetMissionType
    );

    $thisRelatedAcsFleetEntry = (
        $fleetMissionType === Flights\Enums\FleetMission::UnitedAttack ?
            array_find($relatedAcsFleets, function ($relatedAcsFleet) use ($fleetId) {
                return $fleetId === $relatedAcsFleet['fleetId'];
            }) :
            null
    );
    $supportingMainFleetId = (
        !empty($thisRelatedAcsFleetEntry) ?
            $thisRelatedAcsFleetEntry['mainFleetId'] :
            null
    );
    $thisUnionMainFleetId = (
        $isMainAcsFleet ?
            $fleetId :
            $supportingMainFleetId
    );

    $behaviorDetails = _getOwnFleetBehaviorDetails($params);
    $extraShipsInUnion = [];
    $extraShipsInUnionCount = 0;

    if ($hasUnionExtraSquad) {
        foreach ($acsUnionsExtraSquads[$fleetId] as $extraSquad) {
            if (empty($extraSquad['array'])) {
                continue;
            }

            foreach ($extraSquad['array'] as $shipId => $shipCount) {
                if (empty($extraShipsInUnion[$shipId])) {
                    $extraShipsInUnion[$shipId] = 0;
                }

                $extraShipsInUnion[$shipId] += $shipCount;
                $extraShipsInUnionCount += $shipCount;
            }
        }
    }

    $availableFleetOrders = Collections\compact([
        (
            $isRetreatStillAvailable ?
                parsetemplate(
                    $fleetOrdersRetreatTpl,
                    [
                        'FleetID' => $fleetEntry['fleet_id'],
                        'ButtonText' => (
                            $isFleetMissionNotCalculated ?
                                $_Lang['fl_sback'] :
                                $_Lang['fl_back_from_stay']
                        ),
                    ]
                ) :
                null
        ),
        (
            (
                $isFleetMissionNotCalculated &&
                $fleetMissionType === Flights\Enums\FleetMission::Attack
            ) ?
                parsetemplate(
                    $fleetOrdersCreateUnionTpl,
                    [
                        'FleetID' => $fleetEntry['fleet_id'],
                        'ButtonText' => $_Lang['fl_associate'],
                    ]
                ) :
                null<commit_msg>GH-139 Adjust comparison operators since we can't guarantee strict equality yet<commit_after><?php

namespace UniEngine\Engine\Modules\FlightControl\Components\FlightsList\Utils;

use UniEngine\Engine\Includes\Helpers\Common\Collections;
use UniEngine\Engine\Modules\Flights;


// TODO: Reuse, same code as in FriendlyAcsList
function _getOwnFleetBehaviorDetails($params) {
    global $_Lang;

    $fleetEntry = $params['fleetEntry'];
    $currentTimestamp = $params['currentTimestamp'];

    if ($fleetEntry['fleet_start_time'] >= $currentTimestamp) {
        return [
            'behavior' => $_Lang['fl_get_to_ttl'],
            'behaviorTxt' => $_Lang['fl_get_to'],
        ];
    }

    if (
        $fleetEntry['fleet_end_stay'] > 0 &&
        $fleetEntry['fleet_end_stay'] > $currentTimestamp
    ) {
        $isMissionExpedition = $fleetEntry['fleet_mission'] == Flights\Enums\FleetMission::Expedition;

        return [
            'behavior' => (
                $isMissionExpedition ?
                    $_Lang['fl_explore_to_ttl'] :
                    $_Lang['fl_stay_to_ttl']
            ),
            'behaviorTxt' => (
                $isMissionExpedition ?
                    $_Lang['fl_explore_to'] :
                    $_Lang['fl_stay_to']
            ),
        ];
    }

    if ($fleetEntry['fleet_end_time'] > $currentTimestamp) {
        return [
            'behavior' => $_Lang['fl_back_to_ttl'],
            'behaviorTxt' => $_Lang['fl_back_to'],
        ];
    }

    return [
        'behavior' => $_Lang['fl_cameback_ttl'],
        'behaviorTxt' => $_Lang['fl_cameback'],
    ];
}

// TODO: this should most likely be a component
//  Arguments
//      - $params (Object)
//          - elementNo (Number)
//          - fleetEntry (Object)
//          - acsMainFleets (Record<mainFleetId: string, object>)
//          - currentTimestamp (Number)
//          - acsUnionsExtraSquads (Record<mainFleetId: string, object>)
//          - relatedAcsFleets (Array<object>)
//          - isJoiningThisUnion (Boolean)
//
function buildOwnListElement($params) {
    global $_Lang;

    $elementNo = $params['elementNo'];
    $fleetEntry = $params['fleetEntry'];
    $acsMainFleets = $params['acsMainFleets'];
    $currentTimestamp = $params['currentTimestamp'];
    $acsUnionsExtraSquads = $params['acsUnionsExtraSquads'];
    $relatedAcsFleets = $params['relatedAcsFleets'];
    $isJoiningThisUnion = $params['isJoiningThisUnion'];

    $fleetShipsRowTpl = gettemplate('fleet_fdetail');
    $fleetUnionSquadMainTpl = gettemplate('fleet_faddinfo');
    $fleetResourcesRowTpl = gettemplate('fleet_fresinfo');
    $fleetOrdersRetreatTpl = gettemplate('fleet_orders_retreat');
    $fleetOrdersCreateUnionTpl = gettemplate('fleet_orders_acs');
    $fleetOrdersJoinUnionTpl = gettemplate('fleet_orders_jointoacs');

    $fleetResourcesRowTpl = str_replace(
        [
            'TitleMain',
            'TitleMetal',
            'TitleCrystal',
            'TitleDeuterium',
        ],
        [
            $_Lang['fl_fleetinfo_resources'],
            $_Lang['Metal'],
            $_Lang['Crystal'],
            $_Lang['Deuterium'],
        ],
        $fleetResourcesRowTpl
    );

    $fleetId = $fleetEntry['fleet_id'];
    $fleetShips = String2Array($fleetEntry['fleet_array']);
    $fleetMissionType = $fleetEntry['fleet_mission'];
    $flightElapsedTime = ($currentTimestamp - $fleetEntry['fleet_send_time']);
    $flightTimeRemaining = ($fleetEntry['fleet_start_time'] - $currentTimestamp);
    $flightStayRemaining = ($fleetEntry['fleet_end_stay'] - $currentTimestamp);
    $flightComeBackRemaining = ($fleetEntry['fleet_end_time'] - $currentTimestamp);
    $isMainAcsFleet = !empty($acsMainFleets[$fleetId]);
    $hasUnionExtraSquad = !empty($acsUnionsExtraSquads[$fleetId]);
    $isFleetMissionNotCalculated = ($fleetEntry['fleet_mess'] == 0);
    $isNotCalculatedStationMission = (
        $fleetMissionType == Flights\Enums\FleetMission::Station &&
        $isFleetMissionNotCalculated
    );
    $isStillProtectingHoldMission = (
        !$isFleetMissionNotCalculated &&
        $fleetMissionType == Flights\Enums\FleetMission::Hold &&
        $flightStayRemaining > 0
    );
    $isRetreatStillAvailable = (
        $isFleetMissionNotCalculated ||
        $isStillProtectingHoldMission
    );

    $fleetMissionDisplayType = (
        (
            $isMainAcsFleet &&
            $acsMainFleets[$fleetId]['hasJoinedFleets']
        ) ?
            Flights\Enums\FleetMission::UnitedAttack :
            $fleetMissionType
    );

    $thisRelatedAcsFleetEntry = (
        $fleetMissionType == Flights\Enums\FleetMission::UnitedAttack ?
            array_find($relatedAcsFleets, function ($relatedAcsFleet) use ($fleetId) {
                return $fleetId == $relatedAcsFleet['fleetId'];
            }) :
            null
    );
    $supportingMainFleetId = (
        !empty($thisRelatedAcsFleetEntry) ?
            $thisRelatedAcsFleetEntry['mainFleetId'] :
            null
    );
    $thisUnionMainFleetId = (
        $isMainAcsFleet ?
            $fleetId :
            $supportingMainFleetId
    );

    $behaviorDetails = _getOwnFleetBehaviorDetails($params);
    $extraShipsInUnion = [];
    $extraShipsInUnionCount = 0;

    if ($hasUnionExtraSquad) {
        foreach ($acsUnionsExtraSquads[$fleetId] as $extraSquad) {
            if (empty($extraSquad['array'])) {
                continue;
            }

            foreach ($extraSquad['array'] as $shipId => $shipCount) {
                if (empty($extraShipsInUnion[$shipId])) {
                    $extraShipsInUnion[$shipId] = 0;
                }

                $extraShipsInUnion[$shipId] += $shipCount;
                $extraShipsInUnionCount += $shipCount;
            }
        }
    }

    $availableFleetOrders = Collections\compact([
        (
            $isRetreatStillAvailable ?
                parsetemplate(
                    $fleetOrdersRetreatTpl,
                    [
                        'FleetID' => $fleetEntry['fleet_id'],
                        'ButtonText' => (
                            $isFleetMissionNotCalculated ?
                                $_Lang['fl_sback'] :
                                $_Lang['fl_back_from_stay']
                        ),
                    ]
                ) :
                null
        ),
        (
            (
                $isFleetMissionNotCalculated &&
                $fleetMissionType == Flights\Enums\FleetMission::Attack
            ) ?
                parsetemplate(
                    $fleetOrdersCreateUnionTpl,
                    [
                        'FleetID' => $fleetEntry['fleet_id'],
                        'ButtonText' => $_Lang['fl_associate'],
                    ]
                ) :
                null
{'fraction_non_alphanumeric': 0.12333210468116476, 'fraction_numerical': 0.0011057869517139699, 'mean_word_length': 2.1143250688705235, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '6081324', 'n_tokens_mistral': 4487, 'n_tokens_neox': 4139, 'n_words': 493}