Skip to content

Commit

Permalink
Disallow research lab ugrading when research in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rautamik committed Nov 18, 2024
1 parent 9c163a7 commit 96cccb8
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public function index(Request $request, PlayerService $player): View
$build_active = $build_full_queue->getCurrentlyBuildingFromQueue();
$build_queue = $build_full_queue->getQueuedFromQueue();

// Research Lab upgrading is disallowed when researching is ongoing
$research_in_progress = $player->isResearching();

$buildings = [];
foreach ($this->objects as $key_row => $objects_row) {
$buildings[$key_row] = [];
Expand Down Expand Up @@ -114,6 +117,7 @@ public function index(Request $request, PlayerService $player): View
$view_model->requirements_met = $requirements_met;
$view_model->enough_resources = $enough_resources;
$view_model->currently_building = ($build_active !== null && $build_active->object->machine_name === $object->machine_name);
$view_model->research_in_progress = $research_in_progress;

$buildings[$key_row][$object->id] = $view_model;
}
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Traits/ObjectAjaxTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ public function ajaxHandler(Request $request, PlayerService $player): JsonRespon
$production_time = '';
$production_datetime = '';
$research_lab_upgrading = false;
$research_in_progress = false;
switch ($object->type) {
case GameObjectType::Building:
case GameObjectType::Station:
$production_time = AppUtil::formatTimeDuration($planet->getBuildingConstructionTime($object->machine_name));
$production_datetime = AppUtil::formatDateTimeDuration($planet->getBuildingConstructionTime($object->machine_name));

// Research Lab upgrading is disallowed when research is in progress
$research_in_progress = $player->isResearching();
break;
case GameObjectType::Ship:
case GameObjectType::Defense:
Expand Down Expand Up @@ -158,6 +162,7 @@ public function ajaxHandler(Request $request, PlayerService $player): JsonRespon
'max_build_amount' => $max_build_amount,
'current_amount' => $current_amount,
'research_lab_upgrading' => $research_lab_upgrading,
'research_in_progress' => $research_in_progress,
]);

return response()->json([
Expand Down
8 changes: 8 additions & 0 deletions app/Services/BuildingQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ public function start(PlanetService $planet, int $time_start = 0): void
continue;
}

// Sanity check: check if the Research Lab is tried to upgrade when research is in progress
if ($object->machine_name === 'research_lab' && $planet->getPlayer()->isResearching()) {
// Error, cancel build queue item.
$this->cancel($planet, $queue_item->id, $queue_item->object_id);

continue;
}

// Sanity check: check if the planet has enough resources. If not,
// then cancel build request.
if (!$planet->hasResources($price)) {
Expand Down
5 changes: 0 additions & 5 deletions app/Services/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,6 @@ public static function objectRequirementsMetWithQueue(string $machine_name, int
return false;
}

// 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);

Expand Down
11 changes: 11 additions & 0 deletions app/Services/PlayerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,17 @@ public function getResearchArray(): array
return $array;
}

/**
* Get is the player researching any tech or not
*
* @return bool
*/
public function isResearching(): bool
{
$research_queue = resolve('OGame\Services\ResearchQueueService');
return (bool) $research_queue->activeResearchQueueItemCount($this);
}

/**
* Get is the player researching the tech or not
*
Expand Down
14 changes: 12 additions & 2 deletions app/Services/ResearchQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,17 @@ public function retrieveQueue(PlanetService $planet): ResearchQueueListViewModel
public function activeResearchQueueItemCount(PlayerService $player, int $tech_id = 0): int
{
// Fetch queue items from model
return $this->model
return $this->model::query()
->join('planets', 'research_queues.planet_id', '=', 'planets.id')
->join('users', 'planets.user_id', '=', 'users.id')
->where([
['users.id', $player->getId()],
['research_queues.object_id', $tech_id],
['research_queues.processed', 0],
['research_queues.canceled', 0],
])
->when($tech_id, function ($q) use ($tech_id) {
return $q->where('research_queues.object_id', '=', $tech_id);
})
->count();
}

Expand Down Expand Up @@ -267,6 +269,14 @@ public function start(PlayerService $player, int $time_start = 0): void
break;
}

// Sanity check: check if the Research Lab is being upgraded
if ($player->isBuildingObject('research_lab')) {
// Error, cancel research queue item.
$this->cancel($player, $queue_item->id, $queue_item->object_id);

continue;
}

// Sanity check: check if the target level as stored in the database
// is 1 higher than the current level. If not, then it means something
// is wrong.
Expand Down
1 change: 1 addition & 0 deletions app/ViewModels/BuildingViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

class BuildingViewModel extends QueueViewModel
{
public bool $research_in_progress;
}
25 changes: 14 additions & 11 deletions resources/views/ingame/facilities/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,28 @@
data-ipi-hint="ipiTechnology{{ $building->object->class_name }}"
@if ($building->currently_building)
data-status="active"
data-is-spaceprovider=""
data-progress="26"
data-start="1713521207"
data-end="1713604880"
data-total="61608"
title="{{ $building->object->title }}<br/>@lang('Under construction')"
data-is-spaceprovider=""
data-progress="26"
data-start="1713521207"
data-end="1713604880"
data-total="61608"
title="{{ $building->object->title }}<br/>@lang('Under construction')"
@elseif (!$building->requirements_met)
data-status="off"
title="{{ $building->object->title }}<br/>@lang('Requirements are not met!')"
title="{{ $building->object->title }}<br/>@lang('Requirements are not met!')"
@elseif ($building->research_in_progress && $building->object->machine_name == 'research_lab')
data-status="disabled"
title="{{ $building->object->title }}<br/>@lang('Research is currently being carried out!')"
@elseif (!$building->enough_resources)
data-status="disabled"
title="{{ $building->object->title }}<br/>@lang('Not enough resources!')"
title="{{ $building->object->title }}<br/>@lang('Not enough resources!')"
@elseif ($build_queue_max)
data-status="disabled"
title="{{ $building->object->title }}<br/>@lang('Queue is full')"
title="{{ $building->object->title }}<br/>@lang('Queue is full')"
@else
data-status="on"
title="{{ $building->object->title }}"
@endif
title="{{ $building->object->title }}"
@endif
>

<span class="icon sprite sprite_medium medium {{ $building->object->class_name }}">
Expand Down
23 changes: 1 addition & 22 deletions tests/AccountTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ protected function assertObjectNotInQueue(TestResponse $response, string $machin

// Check if cancel text is present on page.
try {
$response->assertDontSee('Cancel production of ' . $object->title);
$response->assertDontSee(['Cancel production of ' . $object->title, 'cancel ' . $object->title]);
} catch (Exception $e) {
if (!empty($error_message)) {
$this->fail($error_message . '. Error: ' . $e->getMessage());
Expand Down Expand Up @@ -502,27 +502,6 @@ 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
24 changes: 24 additions & 0 deletions tests/Feature/BuildQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,28 @@ public function testBuildingProductionTimeHighSpeed(): void
$building_construction_time = $this->planetService->getBuildingConstructionTime('metal_mine');
$this->assertEquals(1, $building_construction_time);
}

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

// Add Energy Technology to research queue
$this->addResearchBuildRequest('energy_technology');
$response = $this->get('/research');
$response->assertStatus(200);
$this->assertObjectInQueue($response, 'energy_technology', 1, 'Energy Technology level 1 is not in research queue');

$this->addFacilitiesBuildRequest('research_lab');

// Verify that Research Lab is not in build queue
$response = $this->get('/facilities');
$response->assertStatus(200);
$this->assertObjectNotInQueue($response, 'research_lab', 'Research lab is in build queue but should not be added.');
}
}
6 changes: 2 additions & 4 deletions tests/Feature/ResearchQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,11 @@ public function testResearchLabUpgradingPreventsResearching(): void
$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'),
);
$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.');
$this->assertObjectNotInQueue($response, 'energy_technology', 'Energy Technology is in research queue but should not be added.');
}
}

0 comments on commit 96cccb8

Please sign in to comment.