Skip to content

Commit

Permalink
subtract resources from player when upgrading building
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Jan 27, 2024
1 parent 14b2f4f commit 1868089
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
20 changes: 16 additions & 4 deletions lib/galaxies/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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" ->
Expand All @@ -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
})
)

Expand All @@ -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
})
)

Expand Down
10 changes: 9 additions & 1 deletion lib/galaxies/planet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion lib/galaxies_web/live/facilities_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
15 changes: 14 additions & 1 deletion lib/galaxies_web/live/resources_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Galaxies.Repo.Migrations.AddPlayerPlanets do
defmodule Galaxies.Repo.Migrations.CreatePlanetsTable do
use Ecto.Migration

def change do
Expand Down

0 comments on commit 1868089

Please sign in to comment.