Skip to content

Commit

Permalink
Merge pull request #443 from rautamik/bugfix/lab-upgrading-disallow-r…
Browse files Browse the repository at this point in the history
…esearching

Bugfix/lab upgrading disallow researching
  • Loading branch information
lanedirt authored Nov 22, 2024
2 parents d78566b + eee8add commit ad69e30
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 127 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 All @@ -93,9 +96,10 @@ public function index(Request $request, PlayerService $player): View

// Get current level of building
$current_level = $this->planet->getObjectLevel($object_machine_name);
$next_level = $current_level + 1;

// Check requirements of this building
$requirements_met = ObjectService::objectRequirementsMet($object_machine_name, $this->planet, $player);
$requirements_met = ObjectService::objectRequirementsMetWithQueue($object_machine_name, $next_level, $this->planet, $player);

// Check if the current planet has enough resources to build this building.
$enough_resources = $this->planet->hasResources(ObjectService::getObjectPrice($object_machine_name, $this->planet));
Expand All @@ -113,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
2 changes: 1 addition & 1 deletion app/Http/Controllers/Abstracts/AbstractUnitsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function index(Request $request, PlayerService $player): View
$amount = $planet->getObjectAmount($object->machine_name);

// Check requirements of this building
$requirements_met = ObjectService::objectRequirementsMet($object->machine_name, $planet, $player, 0, false);
$requirements_met = ObjectService::objectRequirementsMet($object->machine_name, $planet, $player);

// Check if the current planet has enough resources to build this building.
$enough_resources = $planet->hasResources(ObjectService::getObjectPrice($object->machine_name, $planet));
Expand Down
31 changes: 19 additions & 12 deletions app/Http/Controllers/ResearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use OGame\Services\PlanetService;
use OGame\Services\PlayerService;
use OGame\Services\ResearchQueueService;
use OGame\ViewModels\BuildingViewModel;
use OGame\ViewModels\ResearchViewModel;

class ResearchController extends OGameController
{
Expand All @@ -38,6 +38,7 @@ public function __construct(ResearchQueueService $queue)
{
$this->route_view_index = 'research.index';
$this->queue = $queue;

parent::__construct();
}

Expand All @@ -63,10 +64,13 @@ public function index(PlayerService $player): View

$count = 0;

// Parse build queue for this planet
// Parse research queue for this planet
$research_full_queue = $this->queue->retrieveQueue($planet);
$build_active = $research_full_queue->getCurrentlyBuildingFromQueue();
$build_queue = $research_full_queue->getQueuedFromQueue();
$research_active = $research_full_queue->getCurrentlyBuildingFromQueue();
$research_queue = $research_full_queue->getQueuedFromQueue();

// Researching is disallowed when Research Lab is upgrading
$research_lab_upgrading = $player->isBuildingObject('research_lab');

$research = [];
foreach ($screen_objects as $key_row => $objects_row) {
Expand All @@ -75,22 +79,24 @@ public function index(PlayerService $player): View

$object = ObjectService::getResearchObjectByMachineName($object_machine_name);

// Get current level of building
// Get current level of technology
$current_level = $player->getResearchLevel($object->machine_name);
$next_level = $current_level + 1;

// Check requirements of this building
$requirements_met = ObjectService::objectRequirementsMet($object->machine_name, $planet, $player);
// Check requirements of this technology
$requirements_met = ObjectService::objectRequirementsMetWithQueue($object->machine_name, $next_level, $planet, $player);

// Check if the current planet has enough resources to build this building.
// Check if the current planet has enough resources to research this technology.
$enough_resources = $planet->hasResources(ObjectService::getObjectPrice($object->machine_name, $planet));

$view_model = new BuildingViewModel();
$view_model = new ResearchViewModel();
$view_model->object = $object;
$view_model->current_level = $current_level;
$view_model->requirements_met = $requirements_met;
$view_model->count = $count;
$view_model->enough_resources = $enough_resources;
$view_model->currently_building = (!empty($build_active) && $build_active->object->machine_name == $object->machine_name);
$view_model->currently_building = (!empty($research_active) && $research_active->object->machine_name === $object->machine_name);
$view_model->research_lab_upgrading = $research_lab_upgrading;

$research[$key_row][$object->id] = $view_model;
}
Expand All @@ -106,9 +112,10 @@ public function index(PlayerService $player): View
'planet_id' => $planet->getPlanetId(),
'planet_name' => $planet->getPlanetName(),
'research' => $research,
'build_active' => $build_active,
'build_queue' => $build_queue,
'build_active' => $research_active,
'build_queue' => $research_queue,
'build_queue_max' => $build_queue_max,
'research_lab_upgrading' => $research_lab_upgrading,
]);
}

Expand Down
12 changes: 11 additions & 1 deletion app/Http/Traits/ObjectAjaxTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function ajaxHandler(Request $request, PlayerService $player): JsonRespon
$next_level = $current_level + 1;

// Check requirements of this object
$requirements_met = ObjectService::objectRequirementsMet($object->machine_name, $planet, $player);
$requirements_met = ObjectService::objectRequirementsMetWithQueue($object->machine_name, $next_level, $planet, $player);

$price = ObjectService::getObjectPrice($object->machine_name, $planet);

Expand All @@ -52,11 +52,16 @@ public function ajaxHandler(Request $request, PlayerService $player): JsonRespon
// Switch
$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 All @@ -66,6 +71,9 @@ public function ajaxHandler(Request $request, PlayerService $player): JsonRespon
case GameObjectType::Research:
$production_time = AppUtil::formatTimeDuration($planet->getTechnologyResearchTime($object->machine_name));
$production_datetime = AppUtil::formatDateTimeDuration($planet->getTechnologyResearchTime($object->machine_name));

// Researching is disallowed when Research Lab is upgrading
$research_lab_upgrading = $player->isBuildingObject('research_lab');
break;
default:
// Unknown object type, throw error.
Expand Down Expand Up @@ -153,6 +161,8 @@ public function ajaxHandler(Request $request, PlayerService $player): JsonRespon
'max_storage' => $max_storage,
'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
26 changes: 17 additions & 9 deletions app/Services/BuildingQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ public function add(PlanetService $planet, int $building_id): void
// Check if user satisfies requirements to build this object.
$building = ObjectService::getObjectById($building_id);

// Check if user satisfies requirements to build this object.
// TODO: refactor throw exception into a more user-friendly message.
$requirements_met = ObjectService::objectRequirementsMet($building->machine_name, $planet, $planet->getPlayer());
if (!$requirements_met) {
throw new Exception('Requirements not met to build this object.');
}

// @TODO: add checks that current logged in user is owner of planet
// and is able to add this object to the building queue.
$current_level = $planet->getObjectLevel($building->machine_name);
Expand All @@ -93,6 +86,13 @@ public function add(PlanetService $planet, int $building_id): void
$amount = $this->activeBuildingQueueItemCount($planet, $building->id);
$next_level = $current_level + $amount + 1;

// Check if user satisfies requirements to build this object.
// TODO: refactor throw exception into a more user-friendly message.
$requirements_met = ObjectService::objectRequirementsMetWithQueue($building->machine_name, $next_level, $planet, $planet->getPlayer());
if (!$requirements_met) {
throw new Exception('Requirements not met to build this object.');
}

$queue = new BuildingQueue();
$queue->planet_id = $planet->getPlanetId();
$queue->object_id = $building->id;
Expand Down 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 All @@ -227,7 +235,7 @@ public function start(PlanetService $planet, int $time_start = 0): void

// Sanity check: check if the building requirements are still met. If not,
// then cancel build request.
if (!ObjectService::objectRequirementsMet($object->machine_name, $planet, $planet->getPlayer(), $queue_item->object_level_target, false)) {
if (!ObjectService::objectRequirementsWithLevelsMet($object->machine_name, $queue_item->object_level_target, $planet, $planet->getPlayer())) {
$this->cancel($planet, $queue_item->id, $queue_item->object_id);

continue;
Expand Down Expand Up @@ -341,7 +349,7 @@ public function cancelItemMissingRequirements(PlanetService $planet): void
foreach ($build_queue_items as $build_queue_item) {
$object = ObjectService::getObjectById($build_queue_item->object_id);

if (!ObjectService::objectRequirementsMet($object->machine_name, $planet, $planet->getPlayer(), $build_queue_item->object_level_target)) {
if (!ObjectService::objectRequirementsMetWithQueue($object->machine_name, $build_queue_item->object_level_target, $planet, $planet->getPlayer())) {
$this->cancel($planet, $build_queue_item->id, $object->id);
break;
}
Expand Down
Loading

0 comments on commit ad69e30

Please sign in to comment.