-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from mdziekon/mdziekon/91/fleet-mission-attack…
…-refactor-part07 Refactor fleet missions' code | Part 07 | Post-combat fleet updates
- Loading branch information
Showing
10 changed files
with
815 additions
and
587 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 223 additions & 0 deletions
223
modules/flights/utils/calculations/calculatePillageStorage.utils.UETest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
modules/flights/utils/calculations/calculatePillageStorage.utils.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
?> |
68 changes: 68 additions & 0 deletions
68
modules/flights/utils/factories/createFleetDevelopmentLogEntries.utils.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
?> |
Oops, something went wrong.