From 96cccb89deff04f697872ef2e65d773984c641e2 Mon Sep 17 00:00:00 2001 From: Mika Rautanen Date: Mon, 18 Nov 2024 20:46:21 +0200 Subject: [PATCH] Disallow research lab ugrading when research in progress --- .../Abstracts/AbstractBuildingsController.php | 4 +++ app/Http/Traits/ObjectAjaxTrait.php | 5 ++++ app/Services/BuildingQueueService.php | 8 ++++++ app/Services/ObjectService.php | 5 ---- app/Services/PlayerService.php | 11 ++++++++ app/Services/ResearchQueueService.php | 14 +++++++++-- app/ViewModels/BuildingViewModel.php | 1 + .../views/ingame/facilities/index.blade.php | 25 +++++++++++-------- tests/AccountTestCase.php | 23 +---------------- tests/Feature/BuildQueueTest.php | 24 ++++++++++++++++++ tests/Feature/ResearchQueueTest.php | 6 ++--- 11 files changed, 82 insertions(+), 44 deletions(-) diff --git a/app/Http/Controllers/Abstracts/AbstractBuildingsController.php b/app/Http/Controllers/Abstracts/AbstractBuildingsController.php index 800669b4..5a84b4e9 100644 --- a/app/Http/Controllers/Abstracts/AbstractBuildingsController.php +++ b/app/Http/Controllers/Abstracts/AbstractBuildingsController.php @@ -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] = []; @@ -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; } diff --git a/app/Http/Traits/ObjectAjaxTrait.php b/app/Http/Traits/ObjectAjaxTrait.php index 84b41031..83b3e154 100644 --- a/app/Http/Traits/ObjectAjaxTrait.php +++ b/app/Http/Traits/ObjectAjaxTrait.php @@ -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: @@ -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([ diff --git a/app/Services/BuildingQueueService.php b/app/Services/BuildingQueueService.php index d79018a4..b325d4b1 100644 --- a/app/Services/BuildingQueueService.php +++ b/app/Services/BuildingQueueService.php @@ -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)) { diff --git a/app/Services/ObjectService.php b/app/Services/ObjectService.php index 5c694a2b..599ff025 100644 --- a/app/Services/ObjectService.php +++ b/app/Services/ObjectService.php @@ -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); diff --git a/app/Services/PlayerService.php b/app/Services/PlayerService.php index 80e30ee3..1583f072 100644 --- a/app/Services/PlayerService.php +++ b/app/Services/PlayerService.php @@ -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 * diff --git a/app/Services/ResearchQueueService.php b/app/Services/ResearchQueueService.php index 19f0086f..9968035d 100644 --- a/app/Services/ResearchQueueService.php +++ b/app/Services/ResearchQueueService.php @@ -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(); } @@ -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. diff --git a/app/ViewModels/BuildingViewModel.php b/app/ViewModels/BuildingViewModel.php index b77b918a..7ad6abaf 100644 --- a/app/ViewModels/BuildingViewModel.php +++ b/app/ViewModels/BuildingViewModel.php @@ -4,4 +4,5 @@ class BuildingViewModel extends QueueViewModel { + public bool $research_in_progress; } diff --git a/resources/views/ingame/facilities/index.blade.php b/resources/views/ingame/facilities/index.blade.php index 07be0134..f205db39 100644 --- a/resources/views/ingame/facilities/index.blade.php +++ b/resources/views/ingame/facilities/index.blade.php @@ -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 }}
@lang('Under construction')" + data-is-spaceprovider="" + data-progress="26" + data-start="1713521207" + data-end="1713604880" + data-total="61608" + title="{{ $building->object->title }}
@lang('Under construction')" @elseif (!$building->requirements_met) data-status="off" - title="{{ $building->object->title }}
@lang('Requirements are not met!')" + title="{{ $building->object->title }}
@lang('Requirements are not met!')" + @elseif ($building->research_in_progress && $building->object->machine_name == 'research_lab') + data-status="disabled" + title="{{ $building->object->title }}
@lang('Research is currently being carried out!')" @elseif (!$building->enough_resources) data-status="disabled" - title="{{ $building->object->title }}
@lang('Not enough resources!')" + title="{{ $building->object->title }}
@lang('Not enough resources!')" @elseif ($build_queue_max) data-status="disabled" - title="{{ $building->object->title }}
@lang('Queue is full')" + title="{{ $building->object->title }}
@lang('Queue is full')" @else data-status="on" - title="{{ $building->object->title }}" - @endif + title="{{ $building->object->title }}" + @endif > diff --git a/tests/AccountTestCase.php b/tests/AccountTestCase.php index bfd3b41c..30ac1524 100644 --- a/tests/AccountTestCase.php +++ b/tests/AccountTestCase.php @@ -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()); @@ -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 diff --git a/tests/Feature/BuildQueueTest.php b/tests/Feature/BuildQueueTest.php index 2ad1286f..2a56ef4b 100644 --- a/tests/Feature/BuildQueueTest.php +++ b/tests/Feature/BuildQueueTest.php @@ -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.'); + } } diff --git a/tests/Feature/ResearchQueueTest.php b/tests/Feature/ResearchQueueTest.php index 1d17d261..b4d3fc33 100644 --- a/tests/Feature/ResearchQueueTest.php +++ b/tests/Feature/ResearchQueueTest.php @@ -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.'); } }