Skip to content

Commit

Permalink
implement terraformer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Jan 27, 2024
1 parent a7a41a2 commit c45aab4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
54 changes: 38 additions & 16 deletions lib/galaxies/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ defmodule Galaxies.Accounts do
"""
def upgrade_planet_building(planet, building_id, level) do
Ecto.Multi.new()
|> Ecto.Multi.run(:update_planet_building, fn repo, _changes ->
|> Ecto.Multi.run(:planet_building, fn repo, _changes ->
planet_building =
repo.one!(
from pb in PlanetBuilding,
where: pb.building_id == ^building_id and pb.planet_id == ^planet.id,
select: pb
preload: :building
)

dbg(planet_building)

if planet_building.current_level == level - 1 do
{:ok, _} =
Repo.update(
Expand All @@ -55,31 +57,51 @@ defmodule Galaxies.Accounts do
})
)

{:ok, level}
{:ok, planet_building}
else
{:error, "You shouldn't be playing around with my forms..."}
{:error, "Cannot upgrade from level #{planet_building.current_level} to #{level}"}
end
end)
|> Ecto.Multi.run(:update_planet, fn repo, _changes ->
|> Ecto.Multi.run(:update_planet, fn repo, %{planet_building: planet_building} ->
planet =
repo.one!(
from p in Planet,
where: p.id == ^planet.id,
select: p
)

if planet.used_fields < planet.total_fields do
{:ok, _} =
Repo.update(
Planet.upgrade_planet_building_changeset(planet, %{
used_fields: planet.used_fields + 1
})
)
building = planet_building.building

{:ok, planet.used_fields + 1}
else
{:error,
"The planet has no more construction space. Build or upgrade the Terraformer to increase planet fields."}
# check if planet has resources to build the planet
dbg(Galaxies.calc_upgrade_cost(building.upgrade_cost_formula, level))

cond do
building.name == "Terraformer" ->
extra_fields = Building.terraformer_extra_fields(level)

{:ok, _} =
Repo.update(
Planet.upgrade_planet_building_changeset(planet, %{
used_fields: planet.used_fields + 1,
total_fields: planet.total_fields + extra_fields
})
)

{:ok, planet.used_fields + 1}

planet.used_fields < planet.total_fields ->
{:ok, _} =
Repo.update(
Planet.upgrade_planet_building_changeset(planet, %{
used_fields: planet.used_fields + 1
})
)

{:ok, planet.used_fields + 1}

true ->
{:error,
"The planet has no more construction space. Build or upgrade the Terraformer to increase planet fields."}
end
end)
|> Repo.transaction()
Expand Down
3 changes: 3 additions & 0 deletions lib/galaxies/building.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ defmodule Galaxies.Building do

timestamps(type: :utc_datetime_usec)
end

def terraformer_extra_fields(level) when rem(level, 2) == 0, do: 6
def terraformer_extra_fields(_level), do: 5
end
2 changes: 1 addition & 1 deletion lib/galaxies/planet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ defmodule Galaxies.Planet do

def upgrade_planet_building_changeset(planet, attrs) do
planet
|> cast(attrs, [:used_fields])
|> cast(attrs, [:used_fields, :total_fields])
end
end

0 comments on commit c45aab4

Please sign in to comment.