Skip to content

Commit

Permalink
GH-139 Move unions list fetcher into an util
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed May 30, 2022
1 parent 33d9017 commit c599193
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
31 changes: 18 additions & 13 deletions fleet2.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,27 @@
'isUnionMissionAllowed' => false,
]);

$joinableUnionsList = [];

if (
in_array(Flights\Enums\FleetMission::Attack, $AvailableMissions) &&
$targetPlanetDetails['id'] > 0
) {
$SQLResult_CheckACS = doquery(
"SELECT * FROM {{table}} WHERE (`users` LIKE '%|{$_User['id']}|%' OR `owner_id` = {$_User['id']}) AND `end_target_id` = {$targetPlanetDetails['id']} AND `start_time` > UNIX_TIMESTAMP();",
'acs'
);
$joinableUnionFlights = FlightControl\Utils\Fetchers\fetchJoinableUnionFlights([
'userId' => $_User['id'],
'targetPlanetId' => $targetPlanetDetails['id'],
]);

if($SQLResult_CheckACS->num_rows > 0)
{
while($ACSData = $SQLResult_CheckACS->fetch_assoc())
{
$ACSData['fleets_count'] += 1;
$ACSList[$ACSData['id']] = "{$ACSData['name']} ({$_Lang['fl_acs_fleets']}: {$ACSData['fleets_count']})";
}
$joinableUnionsList = object_map($joinableUnionFlights, function ($unionFlight) use (&$_Lang) {
$joinedFleetsCount = $unionFlight['fleets_count'] + 1;

return [
"{$unionFlight['name']} ({$_Lang['fl_acs_fleets']}: {$joinedFleetsCount})",
$unionFlight['id']
];
});

if (!empty($joinableUnionsList)) {
$AvailableMissions[] = Flights\Enums\FleetMission::UnitedAttack;
}
}
Expand Down Expand Up @@ -453,8 +458,8 @@
if (in_array(Flights\Enums\FleetMission::UnitedAttack, $AvailableMissions)) {
$_Lang['CreateACSList'] = '';

foreach ($ACSList as $ID => $Name) {
$_Lang['CreateACSList'] .= '<option value="'.$ID.'" '.($inputJoinUnionId == $ID ? 'selected' : '').'>'.$Name.'</option>';
foreach ($joinableUnionsList as $unionId => $unionDisplayData) {
$_Lang['CreateACSList'] .= '<option value="'.$unionId.'" '.($inputJoinUnionId == $unionId ? 'selected' : '').'>'.$unionDisplayData.'</option>';
}
} else {
$_Lang['P_HideACSJoinList'] = $Hide;
Expand Down
1 change: 1 addition & 0 deletions modules/flightControl/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
include($includePath . './utils/factories/createUnionInvitationMessage.factory.php');
include($includePath . './utils/fetchers/fetchActiveSmartFleetsBlockadeEntries.fetcher.php');
include($includePath . './utils/fetchers/fetchBashValidatorFlightLogEntries.fetcher.php');
include($includePath . './utils/fetchers/fetchJoinableUnionFlights.fetcher.php');
include($includePath . './utils/fetchers/fetchMultiDeclaration.fetcher.php');
include($includePath . './utils/fetchers/fetchPlanetOwnerDetails.fetcher.php');
include($includePath . './utils/fetchers/fetchSavedShortcuts.fetcher.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace UniEngine\Engine\Modules\FlightControl\Utils\Fetchers;

/**
* @param array $props
* @param number $props['userId']
* @param number $props['targetPlanetId']
*/
function fetchJoinableUnionFlights($props) {
$userId = $props['userId'];
$targetPlanetId = $props['targetPlanetId'];

$query = (
"SELECT " .
"* " .
"FROM {{table}} " .
"WHERE " .
"( " .
"`users` LIKE '%|{$userId}|%' OR " .
"`owner_id` = {$userId} " .
") AND " .
"`end_target_id` = {$targetPlanetId} AND " .
"`start_time` > UNIX_TIMESTAMP() " .
";"
);
$queryResult = doquery($query, 'acs');

return mapQueryResults($queryResult, function ($entry) {
return $entry;
});
}

?>

0 comments on commit c599193

Please sign in to comment.