Skip to content

Commit

Permalink
Disallow researching when research lab is upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
rautamik committed Nov 17, 2024
1 parent 77bda98 commit 9c163a7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/Services/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,19 +376,27 @@ public static function objectRequirementsWithLevelsMet(string $machine_name, int
*/
public static function objectRequirementsMetWithQueue(string $machine_name, int $target_level, PlanetService $planet, PlayerService $player): bool
{
// Check the object's requirements against the existing objects
$object = self::getObjectByMachineName($machine_name);
$incompleteRequirements = self::filterCompletedRequirements($object->requirements, $planet, $player);

// Check object's previous levels against queued objects
if (!self::hasPreviousLevelsInQueue($target_level, $object, $planet, $player)) {
return false;
}

if (count($incompleteRequirements) === 0) {
// Disallow researching when Research Lab is upgrading
if ($object->type === GameObjectType::Research && $player->isBuildingObject('research_lab')) {
return false;
}

// Check object's requirements against built objects
$missingRequirements = self::filterCompletedRequirements($object->requirements, $planet, $player);

if (count($missingRequirements) === 0) {
return true;
}

return count(self::filterQueuedRequirements($incompleteRequirements, $planet, $player)) === 0;
// Check object's requirements against queued objects
return count(self::filterQueuedRequirements($missingRequirements, $planet, $player)) === 0;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/AccountTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,27 @@ protected function assertRequirementsNotMet(TestResponse $response, string $mach
}
}

protected function assertObjectNotInResearchQueue(TestResponse $response, string $machine_name, string $error_message = ''): void
{
// Get object name from machine name.
try {
$object = ObjectService::getObjectByMachineName($machine_name);
} catch (Exception $e) {
$this->fail('Failed to get object by machine name: ' . $machine_name . '. Error: ' . $e->getMessage());
}

// Check if cancel text is present on page.
try {
$response->assertDontSee('cancel ' . $object->title);
} catch (Exception $e) {
if (!empty($error_message)) {
$this->fail($error_message . '. Error: ' . $e->getMessage());
} else {
$this->fail('Object ' . $object->title . ' is in the research queue. Error: ' . $e->getMessage());
}
}
}

/**
* Add a resource build request to the current users current planet.
* @param string $machine_name
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/ResearchQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,30 @@ public function testResearchLabRequirement(): void
$response->assertStatus(200);
$this->assertObjectLevelOnPage($response, 'energy_technology', 1, 'Energy technology is not at level one 2 minutes after build request issued.');
}

/**
* Verify that ongoing upgrade of research lab prevents researching.
* @throws Exception
*/
public function testResearchLabUpgradingPreventsResearching(): void
{
// Add required resources for research to planet
$this->planetAddResources(new Resources(5000, 5000, 5000, 0));
$this->planetSetObjectLevel('research_lab', 1);

// Add Research Lab level 2 to build queue
$this->addFacilitiesBuildRequest('research_lab');
$response = $this->get('/facilities');
$response->assertStatus(200);
$this->assertObjectInQueue($response, 'research_lab', 2, 'Research Lab level 2 is not in build queue');

$this->assertThrows(
fn () => $this->addResearchBuildRequest('energy_technology'),
);

// Verify that Energy Technology is not in research queue
$response = $this->get('/research');
$response->assertStatus(200);
$this->assertObjectNotInResearchQueue($response, 'energy_technology', 'Energy Technology is in research queue but should not be added.');
}
}

0 comments on commit 9c163a7

Please sign in to comment.