lanedirt / OGameX

Open-source OGame redesign clone built with Laravel 11.x.
https://main.ogamex.dev
MIT License
34 stars 15 forks source link

[BUG] Energy requirement check does not work when upgrading Space Dock Building #208

Open alican22 opened 4 months ago

alican22 commented 4 months ago

Normally, I need to have at least 1.6K Energy to upgrade Space Dock. Screenshot 2567-05-26 at 12 50 12

But I can upgrade successfully with -59 Energy (minus) Screenshot 2567-05-26 at 12 50 25

lanedirt commented 4 months ago

Hi @alican22,

Thanks for the bug report. I checked the current code and it seems we are indeed missing a check for the energy cost.

The existing checks look like this now:

app/Http/Controllers/Abstracts/AbstractBuildingsController line 92:

  // Get object
  $object = $objects->getObjectByMachineName($object_machine_name);

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

  // Check requirements of this building
  $requirements_met = $objects->objectRequirementsMet($object_machine_name, $this->planet, $player);

  // Check if the current planet has enough resources to build this building.
  $enough_resources = $this->planet->hasResources($objects->getObjectPrice($object_machine_name, $this->planet));

The planet->hasResources() method which checks if the current planet has enough resources seems to be missing an energy requirement check:

    /**
     * Checks if this planet has equal or more than the requested resources.
     *
     * @param Resources $resources
     * Array with resources to check.
     *
     * @return bool
     */
    public function hasResources(Resources $resources): bool
    {
        if (!empty($resources->metal->get()) && $this->metal()->get() < $resources->metal->get()) {
            return false;
        }
        if (!empty($resources->crystal->get()) && $this->crystal()->get() < $resources->crystal->get()) {
            return false;
        }
        if (!empty($resources->deuterium->get()) && $this->deuterium()->get() < $resources->deuterium->get()) {
            return false;
        }

        return true;
    }

I'm not fully sure, but as far as I know the energy requirements works a little different than the normal resources. Normal resources (metal,crystal,deuterium) are deducted from the planet. But energy is more of a "check" if the total production energy production of the planet is at least X. So in your screenshot where it requires 1,6K energy it should not look at your current balance "-59" but rather it should look at the actual "current production" which is visible in the tooltip:

Screenshot 2024-05-26 at 12 39 33

I'm not entirely sure, but I think this is more or less how it should work.

Chewbaka69 commented 4 months ago

I check on the real Ogame and you are right, the check in on the total production and energy and not the current balance: image

Chewbaka69 commented 4 months ago

duplicate #174