Skip to content

Commit

Permalink
Merge pull request #144 from mdziekon/mdziekon/gh-139/flight-control-…
Browse files Browse the repository at this point in the history
…refactor-part03

Flight Control refactor (part 03) - Alerts & Smart Fleets Blockade refactor
  • Loading branch information
mdziekon authored Dec 29, 2020
2 parents 9832fed + 4be9a5b commit 591eba4
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 294 deletions.
159 changes: 61 additions & 98 deletions ajax/galaxyfleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,110 +290,73 @@ function CreateReturn($ReturnCode)
}
}

// Fleet Blockade System
$BlockFleet = false;
$SFBSelectWhere[] = "(`Type` = 1 AND (`EndTime` > UNIX_TIMESTAMP() OR `PostEndTime` > UNIX_TIMESTAMP()))";
if($Mission == 6)
{
if($TargetUser > 0)
{
$SFBSelectWhere[] = "(`Type` = 2 AND `ElementID` = {$TargetUser} AND `EndTime` > UNIX_TIMESTAMP())";
}
$SFBSelectWhere[] = "(`Type` = 3 AND `ElementID` = {$TargetID} AND `EndTime` > UNIX_TIMESTAMP())";
}
$SFBSelectWhere[] = "(`Type` = 2 AND `ElementID` = {$_User['id']} AND `EndTime` > UNIX_TIMESTAMP())";
$SFBSelectWhere[] = "(`Type` = 3 AND `ElementID` = {$CurrentPlanet['id']} AND `EndTime` > UNIX_TIMESTAMP())";
$smartFleetsBlockadeStateValidationResult = FlightControl\Utils\Validators\validateSmartFleetsBlockadeState([
'timestamp' => $Time,
'fleetData' => [
'Mission' => $Mission,
],
'fleetOwnerDetails' => [
'userId' => $_User['id'],
'planetId' => $CurrentPlanet['id'],
],
'targetOwnerDetails' => (
$TargetUser > 0 ?
[
'userId' => $TargetUser,
'planetId' => $TargetID,
'onlinetime' => $HeDBRec['onlinetime'],
] :
null
),
'settings' => [
'idleTime' => $Protections['idleTime'],
],
]);

$SFBSelect = '';
$SFBSelect .= "SELECT `Type`, `BlockMissions`, `Reason`, `StartTime`, `EndTime`, `PostEndTime`, `ElementID`, `DontBlockIfIdle` FROM {{table}} WHERE `StartTime` <= UNIX_TIMESTAMP() AND ";
$SFBSelect .= implode(' OR ', $SFBSelectWhere);
$SFBSelect .= " ORDER BY `Type` ASC, `EndTime` DESC;";
if (!$smartFleetsBlockadeStateValidationResult['isValid']) {
$firstValidationError = $smartFleetsBlockadeStateValidationResult['errors'];

$SQLResult_SmartFleetBlockadeData = doquery($SFBSelect, 'smart_fleet_blockade');
$errorMessage = null;
switch ($firstValidationError['blockType']) {
case 'GLOBAL_ENDTIME':
CreateReturn('628');

if($SQLResult_SmartFleetBlockadeData->num_rows > 0)
{
while($GetSFBData = $SQLResult_SmartFleetBlockadeData->fetch_assoc())
{
$BlockedMissions = false;
if($GetSFBData['BlockMissions'] == '0')
{
$BlockedMissions = true;
$AllMissionsBlocked = true;
}
else
{
$BlockedMissions = explode(',', $GetSFBData['BlockMissions']);
}
break;
case 'GLOBAL_POSTENDTIME':
CreateReturn('635');

if($BlockedMissions === true OR in_array($Mission, $BlockedMissions))
{
if($GetSFBData['Type'] == 1)
{
// Global Blockade
if($GetSFBData['EndTime'] > $Time)
{
// Normal Blockade
if(!($GetSFBData['DontBlockIfIdle'] == 1 AND in_array($Mission, $_Vars_FleetMissions['military']) AND $TargetUser > 0 AND $HeDBRec['onlinetime'] <= ($Time - $Protections['idleTime'])))
{
CreateReturn('628');
}
}
else if($GetSFBData['PostEndTime'] > $Time)
{
// Post Blockade
if(in_array($Mission, $_Vars_FleetMissions['military']) AND $TargetUser > 0 AND
(
($AllMissionsBlocked !== true AND $HeDBRec['onlinetime'] > ($Time - $Protections['idleTime']) AND $HeDBRec['onlinetime'] < $GetSFBData['StartTime'])
OR
($AllMissionsBlocked === true AND $HeDBRec['onlinetime'] > ($Time - $Protections['idleTime']) AND $HeDBRec['onlinetime'] < $GetSFBData['EndTime'])
))
{
CreateReturn('635');
}
}
}
else if($GetSFBData['Type'] == 2)
{
// Per User Blockade
if($GetSFBData['ElementID'] == $_User['id'])
{
CreateReturn('636');
}
else
{
CreateReturn('637');
}
break;
case 'USER':
$errorDetails = $firstValidationError['details'];

if ($errorDetails['userId'] == $_User['id']) {
CreateReturn('636');
}
else if($GetSFBData['Type'] == 3)
{
// Per Planet Blockade
if($GetSFBData['ElementID'] == $CurrentPlanet['id'])
{
// SFB per Planet (your) is Active
if($CurrentPlanet['planet_type'] == 1)
{
CreateReturn('638');
}
else
{
CreateReturn('639');
}
}
else
{
// SFB per Planet (target) is Active
if($TargetPlanetType == 1)
{
CreateReturn('640');
}
else
{
CreateReturn('641');
}
}

CreateReturn('637');

break;
case 'PLANET':
$errorDetails = $firstValidationError['details'];

if ($errorDetails['planetId'] == $CurrentPlanet['id']) {
CreateReturn(
($CurrentPlanet['planet_type'] == 1) ?
'638' :
'639'
);
}
}

CreateReturn(
($TargetPlanetType == 1) ?
'640' :
'641'
);

break;
default:
CreateReturn('694');
break;
}
}

Expand Down
113 changes: 24 additions & 89 deletions ajax/sendmissiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,96 +219,31 @@ function CreateReturn($ReturnCode, $Update = '0')
}
}

// Fleet Blockade System
$SFBSelectWhere[] = "(`Type` = 1 AND (`EndTime` > UNIX_TIMESTAMP() OR `PostEndTime` > UNIX_TIMESTAMP()))";
if($PlanetData['id_owner'] > 0)
{
$SFBSelectWhere[] = "(`Type` = 2 AND `ElementID` = {$PlanetData['id_owner']} AND `EndTime` > UNIX_TIMESTAMP())";
}
$SFBSelectWhere[] = "(`Type` = 3 AND `ElementID` = {$PlanetData['id']} AND `EndTime` > UNIX_TIMESTAMP())";
$SFBSelectWhere[] = "(`Type` = 2 AND `ElementID` = {$_User['id']} AND `EndTime` > UNIX_TIMESTAMP())";
$SFBSelectWhere[] = "(`Type` = 3 AND `ElementID` = {$_Planet['id']} AND `EndTime` > UNIX_TIMESTAMP())";

$SFBSelect = '';
$SFBSelect .= "SELECT `Type`, `BlockMissions`, `Reason`, `StartTime`, `EndTime`, `PostEndTime`, `ElementID`, `DontBlockIfIdle` FROM {{table}} WHERE `StartTime` <= UNIX_TIMESTAMP() AND ";
$SFBSelect .= implode(' OR ', $SFBSelectWhere);
$SFBSelect .= " ORDER BY `Type` ASC, `EndTime` DESC;";

$SQLResult_GetSmartFleetBlockadeData = doquery($SFBSelect, 'smart_fleet_blockade');

if($SQLResult_GetSmartFleetBlockadeData->num_rows > 0)
{
while($GetSFBData = $SQLResult_GetSmartFleetBlockadeData->fetch_assoc())
{
$BlockedMissions = false;
if($GetSFBData['BlockMissions'] == '0')
{
$BlockedMissions = true;
$AllMissionsBlocked = true;
}
else
{
$BlockedMissions = explode(',', $GetSFBData['BlockMissions']);
}

if($BlockedMissions === true OR in_array($Mission, $BlockedMissions))
{
if($GetSFBData['Type'] == 1)
{
// Global Blockade
if($GetSFBData['EndTime'] > $Now)
{
// Normal Blockade
if(!($GetSFBData['DontBlockIfIdle'] == 1 AND in_array($Mission, $_Vars_FleetMissions['military']) AND $PlanetData['id_owner'] > 0 AND $HeDBRec['onlinetime'] <= ($Now - $Protections['idleTime'])))
{
$BlockFleet = true;
$BlockReason = $_Lang['SFB_Stop_GlobalBlockade'];
}
}
else if($GetSFBData['PostEndTime'] > $Now)
{
// Post Blockade
if(in_array($Mission, $_Vars_FleetMissions['military']) AND $PlanetData['id_owner'] > 0 AND
(
($AllMissionsBlocked !== true AND $HeDBRec['onlinetime'] > ($Now - $Protections['idleTime']) AND $HeDBRec['onlinetime'] < $GetSFBData['StartTime'])
OR
($AllMissionsBlocked === true AND $HeDBRec['onlinetime'] > ($Now - $Protections['idleTime']) AND $HeDBRec['onlinetime'] < $GetSFBData['EndTime'])
))
{
$BlockFleet = true;
$BlockReason = sprintf($_Lang['SFB_Stop_GlobalPostBlockade'], prettyDate('d m Y, H:i:s', $GetSFBData['PostEndTime'], 1));
}
}
}
else if($GetSFBData['Type'] == 2)
{
// Per User Blockade
$BlockFleet = true;
$BlockGivenReason = (empty($GetSFBData['Reason']) ? $_Lang['SFB_Stop_ReasonNotGiven'] : "\"{$GetSFBData['Reason']}\"");
$BlockReason = sprintf(($GetSFBData['ElementID'] == $_User['id'] ? $_Lang['SFB_Stop_UserBlockadeOwn'] : $_Lang['SFB_Stop_UserBlockade']), prettyDate('d m Y', $GetSFBData['EndTime'], 1), date('H:i:s', $GetSFBData['EndTime']), $BlockGivenReason);
}
else if($GetSFBData['Type'] == 3)
{
// Per Planet Blockade
$BlockFleet = true;
$BlockGivenReason = (empty($GetSFBData['Reason']) ? $_Lang['SFB_Stop_ReasonNotGiven'] : "\"{$GetSFBData['Reason']}\"");
if($GetSFBData['ElementID'] == $_Planet['id'])
{
$UseLangVar = ($_Planet['planet_type'] == 1 ? $_Lang['SFB_Stop_PlanetBlockadeOwn_Planet'] : $_Lang['SFB_Stop_PlanetBlockadeOwn_Moon']);
}
else
{
$UseLangVar = ($Type == 1 ? $_Lang['SFB_Stop_PlanetBlockade_Planet'] : $_Lang['SFB_Stop_PlanetBlockade_Moon']);
}
$BlockReason = sprintf($UseLangVar, prettyDate('d m Y', $GetSFBData['EndTime'], 1), date('H:i:s', $GetSFBData['EndTime']), $BlockGivenReason);
}
}
$smartFleetsBlockadeStateValidationResult = FlightControl\Utils\Validators\validateSmartFleetsBlockadeState([
'timestamp' => $Now,
'fleetData' => [
'Mission' => $Mission,
],
'fleetOwnerDetails' => [
'userId' => $_User['id'],
'planetId' => $_Planet['id'],
],
'targetOwnerDetails' => (
$PlanetData['id_owner'] > 0 ?
[
'userId' => $PlanetData['id_owner'],
'planetId' => $PlanetData['id'],
'onlinetime' => $HeDBRec['onlinetime'],
] :
null
),
'settings' => [
'idleTime' => $Protections['idleTime'],
],
]);

if($BlockFleet === true)
{
CreateReturn('626');
}
}
if (!$smartFleetsBlockadeStateValidationResult['isValid']) {
CreateReturn('626');
}

$CreateMIPAttack = '';
Expand Down
Loading

0 comments on commit 591eba4

Please sign in to comment.