Skip to content

Commit

Permalink
Merge pull request #98 from mdziekon/mdziekon/91/fleet-mission-attack…
Browse files Browse the repository at this point in the history
…-refactor-part07

Refactor fleet missions' code | Part 07 | Post-combat fleet updates
  • Loading branch information
mdziekon authored Apr 7, 2020
2 parents cd8027a + 1a6f70f commit 96130dc
Show file tree
Hide file tree
Showing 10 changed files with 815 additions and 587 deletions.
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

0 comments on commit 96130dc

Please sign in to comment.