From 1868089004296a1fc7b8408afecd5b6a0a415044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Tom=C3=A1s?= Date: Sat, 27 Jan 2024 09:02:21 +0000 Subject: [PATCH] subtract resources from player when upgrading building --- lib/galaxies/accounts.ex | 20 +++++++++++++++---- lib/galaxies/planet.ex | 10 +++++++++- lib/galaxies_web/live/facilities_live.ex | 15 +++++++++++++- lib/galaxies_web/live/resources_live.ex | 15 +++++++++++++- ...> 20240124123636_create_planets_table.exs} | 2 +- 5 files changed, 54 insertions(+), 8 deletions(-) rename priv/repo/migrations/{20240124123636_add_player_planets.exs => 20240124123636_create_planets_table.exs} (95%) diff --git a/lib/galaxies/accounts.ex b/lib/galaxies/accounts.ex index 97a2bb8..b7305c4 100644 --- a/lib/galaxies/accounts.ex +++ b/lib/galaxies/accounts.ex @@ -72,8 +72,12 @@ defmodule Galaxies.Accounts do building = planet_building.building - # check if planet has resources to build the planet - dbg(Galaxies.calc_upgrade_cost(building.upgrade_cost_formula, level)) + # TODO: check if planet has resources to build the planet + + {metal, crystal, deuterium, energy} = + Galaxies.calc_upgrade_cost(building.upgrade_cost_formula, level) + + dbg({metal, crystal, deuterium, energy}) cond do building.name == "Terraformer" -> @@ -83,7 +87,11 @@ defmodule Galaxies.Accounts do Repo.update( Planet.upgrade_planet_building_changeset(planet, %{ used_fields: planet.used_fields + 1, - total_fields: planet.total_fields + extra_fields + total_fields: planet.total_fields + extra_fields, + metal_units: planet.metal_units - metal, + crystal_units: planet.crystal_units - crystal, + deuterium_units: planet.deuterium_units - deuterium, + available_energy: planet.available_energy - energy }) ) @@ -93,7 +101,11 @@ defmodule Galaxies.Accounts do {:ok, _} = Repo.update( Planet.upgrade_planet_building_changeset(planet, %{ - used_fields: planet.used_fields + 1 + used_fields: planet.used_fields + 1, + metal_units: planet.metal_units - metal, + crystal_units: planet.crystal_units - crystal, + deuterium_units: planet.deuterium_units - deuterium, + available_energy: planet.available_energy - energy }) ) diff --git a/lib/galaxies/planet.ex b/lib/galaxies/planet.ex index c888eb5..d3cc78b 100644 --- a/lib/galaxies/planet.ex +++ b/lib/galaxies/planet.ex @@ -41,6 +41,14 @@ defmodule Galaxies.Planet do def upgrade_planet_building_changeset(planet, attrs) do planet - |> cast(attrs, [:used_fields, :total_fields]) + |> cast(attrs, [ + :used_fields, + :total_fields, + :metal_units, + :crystal_units, + :deuterium_units, + :available_energy, + :total_energy + ]) end end diff --git a/lib/galaxies_web/live/facilities_live.ex b/lib/galaxies_web/live/facilities_live.ex index 5915032..6df0c16 100644 --- a/lib/galaxies_web/live/facilities_live.ex +++ b/lib/galaxies_web/live/facilities_live.ex @@ -78,9 +78,22 @@ defmodule GalaxiesWeb.FacilitiesLive do {:ok, _} -> updated_building = Map.put(building, :current_level, level) + {metal, crystal, deuterium, energy} = + Galaxies.calc_upgrade_cost(building.upgrade_cost_formula, level) + + updated_planet = + socket.assigns.current_planet + |> Map.put(:metal_units, socket.assigns.current_planet.metal_units - metal) + |> Map.put(:crystal_units, socket.assigns.current_planet.crystal_units - crystal) + |> Map.put(:deuterium_units, socket.assigns.current_planet.deuterium_units - deuterium) + |> Map.put(:available_energy, socket.assigns.current_planet.available_energy - energy) + planet_buildings = list_replace(socket.assigns.planet_buildings, updated_building) - {:noreply, assign(socket, :planet_buildings, planet_buildings)} + {:noreply, + socket + |> assign(:current_planet, updated_planet) + |> assign(:planet_buildings, planet_buildings)} {:error, error} -> {:noreply, put_flash(socket, :error, error)} diff --git a/lib/galaxies_web/live/resources_live.ex b/lib/galaxies_web/live/resources_live.ex index c97180b..b4d7a50 100644 --- a/lib/galaxies_web/live/resources_live.ex +++ b/lib/galaxies_web/live/resources_live.ex @@ -78,9 +78,22 @@ defmodule GalaxiesWeb.ResourcesLive do {:ok, _} -> updated_building = Map.put(building, :current_level, level) + {metal, crystal, deuterium, energy} = + Galaxies.calc_upgrade_cost(building.upgrade_cost_formula, level) + + updated_planet = + socket.assigns.current_planet + |> Map.put(:metal_units, socket.assigns.current_planet.metal_units - metal) + |> Map.put(:crystal_units, socket.assigns.current_planet.crystal_units - crystal) + |> Map.put(:deuterium_units, socket.assigns.current_planet.deuterium_units - deuterium) + |> Map.put(:available_energy, socket.assigns.current_planet.available_energy - energy) + planet_buildings = list_replace(socket.assigns.planet_buildings, updated_building) - {:noreply, assign(socket, :planet_buildings, planet_buildings)} + {:noreply, + socket + |> assign(:current_planet, updated_planet) + |> assign(:planet_buildings, planet_buildings)} {:error, error} -> {:noreply, put_flash(socket, :error, error)} diff --git a/priv/repo/migrations/20240124123636_add_player_planets.exs b/priv/repo/migrations/20240124123636_create_planets_table.exs similarity index 95% rename from priv/repo/migrations/20240124123636_add_player_planets.exs rename to priv/repo/migrations/20240124123636_create_planets_table.exs index d258e74..246169c 100644 --- a/priv/repo/migrations/20240124123636_add_player_planets.exs +++ b/priv/repo/migrations/20240124123636_create_planets_table.exs @@ -1,4 +1,4 @@ -defmodule Galaxies.Repo.Migrations.AddPlayerPlanets do +defmodule Galaxies.Repo.Migrations.CreatePlanetsTable do use Ecto.Migration def change do