Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor fleet missions' code | Part 07 | Post-combat fleet updates #98

Merged
merged 19 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
2d325c8
GH-91 Create helpers related to pillage storage of ships
mdziekon Apr 5, 2020
a072e7c
GH-91 Create calculatePillageStorage() util
mdziekon Apr 5, 2020
2441b95
GH-91 Write tests for calculatePillageStorage() util
mdziekon Apr 5, 2020
697fd54
GH-91 Don't calculate pillage storage manually in MissionCaseAttack
mdziekon Apr 5, 2020
63d3360
GH-91 Don't calculate pillage storage manually in MissionCaseDestruction
mdziekon Apr 5, 2020
64015cd
GH-91 Don't calculate pillage storage manually in MissionCaseGroupAttack
mdziekon Apr 5, 2020
d876b1a
GH-91 Create createFleetUpdateEntry() util
mdziekon Apr 5, 2020
979b7c6
GH-91 Use createFleetUpdateEntry() to create update entries in Missio…
mdziekon Apr 5, 2020
81efe07
GH-91 postCombatShips might be null when all ships have been destroyed
mdziekon Apr 5, 2020
9a834b6
GH-91 Create createFleetDevelopmentLogEntries() util
mdziekon Apr 5, 2020
210bd94
GH-91 Use createFleetDevelopmentLogEntries() to create fleet change d…
mdziekon Apr 5, 2020
6563da7
GH-91 Use createFleetUpdateEntry() to create update entries in Missio…
mdziekon Apr 5, 2020
3a70b6c
GH-91 Use createFleetDevelopmentLogEntries() to create fleet change d…
mdziekon Apr 6, 2020
4c074b8
GH-91 Simplify Deathstars counting
mdziekon Apr 6, 2020
7dff058
GH-91 Use createFleetUpdateEntry() to create update entries in Missio…
mdziekon Apr 7, 2020
f09b09e
GH-91 Use createFleetDevelopmentLogEntries() to create fleet change d…
mdziekon Apr 7, 2020
a170379
GH-91 Switch condition to fix bug with unwanted fleet deletion
mdziekon Apr 7, 2020
ec7beeb
GH-91 Support resourcesPillage in createFleetDevelopmentLogEntries()
mdziekon Apr 7, 2020
1a6f70f
GH-91 Let createFleetDevelopmentLogEntries() take care of pillage log…
mdziekon Apr 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 98 additions & 180 deletions includes/functions/MissionCaseAttack.php

Large diffs are not rendered by default.

307 changes: 115 additions & 192 deletions includes/functions/MissionCaseDestruction.php

Large diffs are not rendered by default.

381 changes: 166 additions & 215 deletions includes/functions/MissionCaseGroupAttack.php

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions includes/helpers/fleets/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ function getShipsStorageCapacity($shipID) {
return $_Vars_Prices[$shipID]['capacity'];
}

function canShipPillage($shipID) {
global $_Vars_Prices;

return (
!isset($_Vars_Prices[$shipID]['cantPillage']) ?
true :
($_Vars_Prices[$shipID]['cantPillage'] !== true)
);
}

function getShipsPillageStorageCapacity($shipID) {
return (
canShipPillage($shipID) ?
getShipsStorageCapacity($shipID) :
0
);
}

function getShipsUsedEngineData($shipID, $user) {
$engines = getShipsEngines($shipID);

Expand Down
3 changes: 3 additions & 0 deletions modules/flights/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
$includePath = $_EnginePath . 'modules/flights/';

include($includePath . './utils/calculations/calculatePillageFactor.utils.php');
include($includePath . './utils/calculations/calculatePillageStorage.utils.php');
include($includePath . './utils/calculations/calculateResourcesLoss.utils.php');
include($includePath . './utils/factories/createFleetDevelopmentLogEntries.utils.php');
include($includePath . './utils/factories/createFleetUpdateEntry.utils.php');
include($includePath . './utils/fleetCache/updateGalaxyDebris.utils.php');
include($includePath . './utils/fleetCache/updateUserStats.utils.php');
include($includePath . './utils/initializers/technologies.utils.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php

use PHPUnit\Framework\TestCase;

$_EnginePath = './';

define('INSIDE', true);
require_once $_EnginePath . 'common/_includes.php';
require_once $_EnginePath . 'includes/vars.php';
require_once $_EnginePath . 'includes/helpers/_includes.php';
require_once $_EnginePath . 'modules/flights/_includes.php';

use UniEngine\Engine\Modules\Flights\Utils\Calculations;


/**
* @group UniEngineTest
*/
class CalculatePillageStorageTestCase extends TestCase {
/**
* @test
*/
public function itShouldHandleEmptyShipsArray() {
$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '0',
'fleet_resource_crystal' => '0',
'fleet_resource_deuterium' => '0',
'fleet_array' => '',
],
'ships' => [],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
0,
$result
);
}

/**
* @test
*/
public function itShouldHandleRegularShipsArray() {
$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '0',
'fleet_resource_crystal' => '0',
'fleet_resource_deuterium' => '0',
'fleet_array' => '202,100;204,100',
],
'ships' => [
'202' => 100,
'204' => 120,
],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
(
(5000 * 100) +
(50 * 120)
),
$result
);
}

/**
* @test
* @backupGlobals enabled
*/
public function itShouldHandleShipsWithForbiddenPillageArray() {
global $_Vars_Prices;

$_Vars_Prices[204]['cantPillage'] = true;

$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '0',
'fleet_resource_crystal' => '0',
'fleet_resource_deuterium' => '0',
'fleet_array' => '202,100;204,100',
],
'ships' => [
'202' => 100,
'204' => 120,
],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
(
(5000 * 100) +
(0 * 120)
),
$result
);
}

/**
* @test
*/
public function itShouldHandleFleetsWithOccupiedSpace() {
$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '1000',
'fleet_resource_crystal' => '70',
'fleet_resource_deuterium' => '0',
'fleet_array' => '202,100;204,100',
],
'ships' => [
'202' => 100,
'204' => 120,
],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
(
(5000 * 100) +
(50 * 120) -
(1000 + 70 + 0)
),
$result
);
}

/**
* @test
*/
public function itShouldNotBlowUpWhenFleetRowDoesNotHaveAllResourceKeys() {
$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '1000',
'fleet_resource_deuterium' => '5',
'fleet_array' => '202,100;204,100',
],
'ships' => [
'202' => 100,
'204' => 120,
],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
(
(5000 * 100) +
(50 * 120) -
(1000 + 0 + 5)
),
$result
);
}

/**
* @test
*/
public function itShouldNotBlowUpWhenFleetRowHasUnknownResource() {
$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '1000',
'fleet_resource_unknown' => '5',
'fleet_array' => '202,100;204,100',
],
'ships' => [
'202' => 100,
'204' => 120,
],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
(
(5000 * 100) +
(50 * 120) -
(1000 + 0 + 0)
),
$result
);
}

/**
* @test
*/
public function itShouldNotBlowUpWhenUsingMixedDataTypes() {
$params = [
'fleetRow' => [
'fleet_id' => '42',
'fleet_resource_metal' => '1000',
'fleet_resource_crystal' => 70,
'fleet_resource_deuterium' => '5',
'fleet_array' => '202,100;204,100',
],
'ships' => [
'202' => '100',
204 => 120,
],
];

$result = Calculations\calculatePillageStorage($params);

$this->assertEquals(
(
(5000 * 100) +
(50 * 120) -
(1000 + 70 + 5)
),
$result
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace UniEngine\Engine\Modules\Flights\Utils\Calculations;

use UniEngine\Engine\Includes\Helpers\World\Resources;

/**
* @param array $params
* @param number $params['fleetRow']
* @param number $params['ships'] Ships that have survided the combat
*/
function calculatePillageStorage($params) {
$fleetRow = $params['fleetRow'];
$ships = $params['ships'];

$pillageStorage = 0;

foreach ($ships as $shipID => $shipCount) {
$shipPillageStorage = getShipsPillageStorageCapacity($shipID);

$pillageStorage += ($shipPillageStorage * $shipCount);
}

$pillagableResourceKeys = Resources\getKnownPillagableResourceKeys();

foreach ($pillagableResourceKeys as $resourceKey) {
$fleetResourcePropKey = "fleet_resource_{$resourceKey}";

if (!isset($fleetRow[$fleetResourcePropKey])) {
continue;
}

$pillageStorage -= $fleetRow[$fleetResourcePropKey];
}

return max($pillageStorage, 0);
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace UniEngine\Engine\Modules\Flights\Utils\Factories;

/**
* @param array $params
* @param array $params['originalShips'] Ships that have participated in the combat
* @param array $params['postCombatShips'] Ships that have survived the combat
* @param array $params['resourcesPillage']
* (default: `[]`)
*/
function createFleetDevelopmentLogEntries($params) {
$originalShips = $params['originalShips'];
$postCombatShips = (
isset($params['postCombatShips']) ?
$params['postCombatShips'] :
[]
);
$resourcesPillage = (
isset($params['resourcesPillage']) ?
$params['resourcesPillage'] :
[]
);

$entries = [];

foreach ($originalShips as $shipID => $shipOriginalCount) {
$shipPostCombatCount = (
isset($postCombatShips[$shipID]) ?
$postCombatShips[$shipID] :
0
);
$shipCountDifference = ($shipOriginalCount - $shipPostCombatCount);

if ($shipCountDifference <= 0) {
continue;
}

$entries[] = implode(',', [ $shipID, $shipCountDifference ]);
}

$pillagableResourceKeyMapping = [
'metal' => 'M',
'crystal' => 'C',
'deuterium' => 'D',
];

foreach ($resourcesPillage as $resourceKey => $pillagedAmount) {
if ($pillagedAmount <= 0) {
continue;
}
if (!isset($pillagableResourceKeyMapping[$resourceKey])) {
// No mapping available yet,
// probably because of a custom resource added to the game
continue;
}

$newEntryParams = [
$pillagableResourceKeyMapping[$resourceKey],
$pillagedAmount,
];
$entries[] = implode(',', $newEntryParams);
}

return $entries;
}

?>
Loading