Skip to content

Commit

Permalink
fix building upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
g5bot committed Dec 18, 2023
1 parent 1e4c4a5 commit 83e3516
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Component/Building/BuildingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ private function updateWorkerAndMaxBev(BuildingInterface $building, ColonyInterf
$host->setMaxBev($host->getMaxBev() - $building->getHousing());
}

public function remove(PlanetFieldInterface $field): void
public function remove(PlanetFieldInterface $field, bool $isDueToUpgrade = false): void
{
$building = $field->getBuilding();
if ($building === null) {
return;
}

if (!$building->isRemovable()) {
if (!$isDueToUpgrade && !$building->isRemovable()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Component/Building/BuildingManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function deactivate(PlanetFieldInterface $field): void;
/**
* Deconstructs the building on the given field
*/
public function remove(PlanetFieldInterface $field): void;
public function remove(PlanetFieldInterface $fieldbool, bool $isDueToUpgrade = false): void;

/**
* Finishes the buildprocess for the building on the given field
Expand Down
8 changes: 5 additions & 3 deletions src/Module/Colony/Lib/BuildingAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,21 @@ public function deactivate(PlanetFieldInterface $field, GameControllerInterface

public function remove(
PlanetFieldInterface $field,
GameControllerInterface $game
GameControllerInterface $game,
bool $isDueToUpgrade = false
): void {

if (!$field->hasBuilding()) {
return;
}

$building = $field->getBuilding();

if (!$building->isRemovable()) {
if (!$isDueToUpgrade && !$building->isRemovable()) {
return;
}

$this->buildingManager->remove($field);
$this->buildingManager->remove($field, $isDueToUpgrade);

$game->addInformationf(
_('%s auf Feld %d wurde demontiert'),
Expand Down
4 changes: 2 additions & 2 deletions src/Module/Colony/Lib/BuildingActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Stu\Module\Colony\Lib;

use Stu\Module\Control\GameControllerInterface;
use Stu\Orm\Entity\ColonyInterface;
use Stu\Orm\Entity\PlanetFieldInterface;

interface BuildingActionInterface
Expand All @@ -14,6 +13,7 @@ public function deactivate(PlanetFieldInterface $field, GameControllerInterface

public function remove(
PlanetFieldInterface $field,
GameControllerInterface $game
GameControllerInterface $game,
bool $isDueToUpgrade = false
): void;
}
127 changes: 127 additions & 0 deletions tests/Component/Building/BuildingManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,133 @@ public function testRemoveRemovesExpectDeactivationWhenActive(): void
$this->buildingManager->remove($field);
}

public function testRemoveExpectRemovalWhenUpgrade(): void
{
$field = $this->mock(PlanetFieldInterface::class);
$building = $this->mock(BuildingInterface::class);
$host = $this->mock(ColonyInterface::class);
$function = $this->mock(BuildingFunctionInterface::class);
$buildingAction = $this->mock(BuildingActionHandlerInterface::class);

$currentStorage = 555;
$storage = 44;
$currentEps = 33;
$eps = 22;
$functionId = 123;
$buildingWorkers = 123;

$field->shouldReceive('getBuilding')
->withNoArgs()
->twice()
->andReturn($building);
$field->shouldReceive('getHost')
->withNoArgs()
->twice()
->andReturn($host);
$field->shouldReceive('clearBuilding')
->withNoArgs()
->once();
$field->shouldReceive('isUnderConstruction')
->withNoArgs()
->once()
->andReturnFalse();
$field->shouldReceive('isActivateable')
->withNoArgs()
->once()
->andReturnTrue();
$field->shouldReceive('isActive')
->withNoArgs()
->once()
->andReturnTrue();
$field->shouldReceive('setActive')
->with(false)
->once();

$building->shouldReceive('getStorage')
->withNoArgs()
->once()
->andReturn($storage);
$building->shouldReceive('getEpsStorage')
->withNoArgs()
->once()
->andReturn($eps);
$building->shouldReceive('getWorkers')
->withNoArgs()
->once()
->andReturn($buildingWorkers);
$building->shouldReceive('getHousing')
->withNoArgs()
->once()
->andReturn(100);
$building->shouldReceive('getFunctions')
->withNoArgs()
->once()
->andReturn(new ArrayCollection([$function]));

$function->shouldReceive('getFunction')
->withNoArgs()
->once()
->andReturn($functionId);

$this->buildingFunctionActionMapper->shouldReceive('map')
->with($functionId)
->once()
->andReturn($buildingAction);

$buildingAction->shouldReceive('destruct')
->with($functionId, $host)
->once();

$host->shouldReceive('getMaxStorage')
->withNoArgs()
->once()
->andReturn($currentStorage);
$host->shouldReceive('getMaxEps')
->withNoArgs()
->once()
->andReturn($currentEps);
$host->shouldReceive('setMaxStorage')
->with($currentStorage - $storage)
->once()
->andReturnSelf();
$host->shouldReceive('setMaxEps')
->with($currentEps - $eps)
->once();
$host->shouldReceive('getWorkless')
->withNoArgs()
->once()
->andReturn(0);
$host->shouldReceive('setWorkless')
->with($buildingWorkers)
->once();
$host->shouldReceive('getWorkers')
->withNoArgs()
->once()
->andReturn($buildingWorkers);
$host->shouldReceive('setWorkers')
->with(0)
->once();
$host->shouldReceive('getMaxBev')
->withNoArgs()
->once()
->andReturn(200);
$host->shouldReceive('setMaxBev')
->with(100)
->once();

$this->planetFieldRepository->shouldReceive('save')
->with($field)
->twice();
$this->colonyRepository->shouldReceive('save')
->with($host)
->twice();
$this->buildingPostAction->shouldReceive('handleDeactivation')
->with($building, $host)
->once();

$this->buildingManager->remove($field, true);
}

public function testFinishFailsIfNoBuildingAvailable(): void
{
$field = $this->mock(PlanetFieldInterface::class);
Expand Down

0 comments on commit 83e3516

Please sign in to comment.