Skip to content

Commit

Permalink
Merge pull request #404 from lanedirt/403-bug-the-game-crashes-when-a…
Browse files Browse the repository at this point in the history
…-battle-starts

The game crashes when a large battle starts
  • Loading branch information
lanedirt authored Oct 20, 2024
2 parents 2a74f0a + d05da59 commit 404b665
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/GameMissions/BattleEngine/BattleEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private function fightBattleRounds(BattleResult $result): array
private function attackUnit(bool $isAttacker, BattleResultRound $round, BattleUnit $attacker, BattleUnit $defender): bool
{
// Calculate the damage dealt by the attacker to the defender.
$damage = $attacker->currentAttackPower;
$damage = $attacker->attackPower;
$shieldAbsorption = 0;

if ($damage < (0.01 * $defender->currentShieldPoints)) {
Expand Down
27 changes: 0 additions & 27 deletions app/GameMissions/BattleEngine/BattleResultRound.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,6 @@
*/
class BattleResultRound
{
/*
"attackerLosses": {"4492924": {"203": "1"}},
"attackerLossesInThisRound": {"4492924": {"203": "1"}},
"defenderLosses": [{"401": "15", "402": "45", "404": "1"}],
"defenderLossesInThisRound": [{"401": "15", "402": "45", "404": "1"}],
"statistic": {
"hitsAttacker": "361",
"hitsDefender": "66",
"absorbedDamageAttacker": "4448",
"absorbedDamageDefender": "3772",
"fullStrengthAttacker": "590656",
"fullStrengthDefender": "16740"
},
"c": {
"4492924":
{
@foreach ($attacker_units_start->units as $unit)
"{{ $unit->unitObject->id }}": {{ $unit->amount }},
@endforeach
}
},
"defenderShips": [{
@foreach ($defender_units_start->units as $unit)
"{{ $unit->unitObject->id }}": {{ $unit->amount }},
@endforeach
}] */

/**
* @var UnitCollection Unit losses of the attacker player until now which includes previous rounds.
* TODO: now this only works for a single attacker. Support for multiple attackers should be added later.
Expand Down
10 changes: 2 additions & 8 deletions app/GameMissions/BattleEngine/BattleUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class BattleUnit
*/
public int $originalShieldPoints;

/**
* @var int The original attack power of the unit.
*/
public int $originalAttackPower;

/**
* @var int The current health points of the unit. Hull plating = structural integrity / 10.
*/
Expand All @@ -44,7 +39,7 @@ class BattleUnit
/**
* @var int The attack power of the unit. This is the amount of damage the unit deals to another unit in a single round of combat.
*/
public int $currentAttackPower;
public int $attackPower;

/**
* Create a new BattleUnit object.
Expand All @@ -66,8 +61,7 @@ public function __construct(UnitObject $unitObject, int $structuralIntegrity, in
$this->originalShieldPoints = $shieldPoints;
$this->currentShieldPoints = $shieldPoints;

$this->originalAttackPower = $attackPower;
$this->currentAttackPower = $attackPower;
$this->attackPower = $attackPower;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
ogame-app:
volumes:
- ./:/var/www
- ./php/local.ini:/.usr/local/etc/php/conf.d/local.ini
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
build:
context: .
dockerfile: Dockerfile
Expand Down
2 changes: 1 addition & 1 deletion php/local.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
upload_max_filesize=40M
post_max_size=40M
memory_limit=128M
memory_limit=1024M
70 changes: 70 additions & 0 deletions tests/Feature/FleetDispatch/FleetDispatchAttackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,74 @@ public function testDispatchFleetAttackerLossDebrisFieldCreated(): void
$this->assertEquals($battleReport->debris['crystal'], $debrisResources->crystal->get(), 'Debris field crystal in database does not match battle report.');
$this->assertEquals($battleReport->debris['deuterium'], $debrisResources->deuterium->get(), 'Debris field deuterium in database does not match battle report.');
}

/**
* Assert that a battle with a large amount of units works correctly and creates a debris field.
*/
public function testLargeScaleAttackWithDebrisField(): void
{
// Set time to static time 2024-01-01
$startTime = Carbon::create(2024, 1, 1, 0, 0, 0);
Carbon::setTestNow($startTime);

// Prepare attacker fleet
$this->planetAddUnit('cruiser', 700000);
$this->planetAddUnit('battle_ship', 100000);

$unitCollection = new UnitCollection();
$unitCollection->addUnit($this->planetService->objects->getUnitObjectByMachineName('cruiser'), 700000);
$unitCollection->addUnit($this->planetService->objects->getUnitObjectByMachineName('battle_ship'), 100000);

// Send fleet to a nearby foreign planet
$foreignPlanet = $this->sendMissionToOtherPlayer($unitCollection, new Resources(0, 0, 0, 0));

// Prepare defender units
$foreignPlanet->addUnit('plasma_turret', 20000);
$foreignPlanet->addUnit('rocket_launcher', 100000);

// Ensure that there is no debris field on the foreign planet
$debrisFieldService = resolve(DebrisFieldService::class);
if ($debrisFieldService->loadForCoordinates($foreignPlanet->getPlanetCoordinates())) {
$debrisFieldService->delete();
}

// Get fleet mission
$fleetMissionService = resolve(FleetMissionService::class, ['player' => $this->planetService->getPlayer()]);
$fleetMission = $fleetMissionService->getActiveFleetMissionsForCurrentPlayer()->first();
$fleetMissionId = $fleetMission->id;

// Calculate fleet mission duration
$fleetMissionDuration = $fleetMissionService->calculateFleetMissionDuration($this->planetService, $foreignPlanet->getPlanetCoordinates(), $unitCollection);

// Set time to fleet mission duration + 1 second
$fleetParentTime = $startTime->copy()->addSeconds($fleetMissionDuration + 1);
Carbon::setTestNow($fleetParentTime);

// Reload application to make sure the planet is not cached
$this->reloadApplication();

// Trigger the update logic
$response = $this->get('/overview');
$response->assertStatus(200);

// Assert that the fleet mission is processed without errors
$fleetMission = $fleetMissionService->getFleetMissionById($fleetMissionId, false);
$this->assertTrue($fleetMission->processed == 1, 'Large-scale fleet mission was not processed successfully.');

// Get the battle report
$battleReport = BattleReport::orderBy('id', 'desc')->first();
$this->assertNotNull($battleReport, 'Battle report was not created for large-scale attack.');

// Assert that the battle report contains debris field information
$this->assertNotEmpty($battleReport->debris, 'Battle report does not contain debris field information for large-scale attack.');
$this->assertGreaterThan(0, $battleReport->debris['metal'] + $battleReport->debris['crystal'] + $battleReport->debris['deuterium'], 'Debris field in battle report is empty for large-scale attack.');

// Assert that a debris field was actually created in the database
$debrisFieldService = resolve(DebrisFieldService::class);
$debrisFieldExists = $debrisFieldService->loadForCoordinates($foreignPlanet->getPlanetCoordinates());
$this->assertTrue($debrisFieldExists, 'Debris field was not created in the database for large-scale attack.');

$debrisResources = $debrisFieldService->getResources();
$this->assertGreaterThan(0, $debrisResources->metal->get() + $debrisResources->crystal->get() + $debrisResources->deuterium->get(), 'Debris field resources are empty for large-scale attack.');
}
}

0 comments on commit 404b665

Please sign in to comment.