Skip to content

Commit 9ec4863

Browse files
committed
Use PlanetAction to encode game logic for upgrading building
1 parent f512aca commit 9ec4863

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

lib/galaxies/accounts.ex

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule Galaxies.Accounts do
44
"""
55

66
import Ecto.Query, warn: false
7+
alias Galaxies.Planets.Actions.EnqueueBuilding
8+
alias Galaxies.Planets.PlanetAction
79
alias Galaxies.Planets
810
alias Galaxies.Planet
911
alias Galaxies.Repo
@@ -19,14 +21,17 @@ defmodule Galaxies.Accounts do
1921
@doc """
2022
Tries to upgrade a planet building.
2123
"""
22-
def upgrade_planet_building(planet, building_id, level) do
24+
def upgrade_planet_building(player, planet, building_id, demolish) do
2325
# TODO maybe wrap in transaction
2426
now = DateTime.utc_now()
2527

26-
with {:prerequisites_ok, true} <-
27-
{:prerequisites_ok, Planets.can_build_building?(planet, building_id)},
28-
:ok <- Planets.enqueue_building(planet.id, building_id, level),
29-
:ok <- Planets.process_planet_events(planet.id, now) do
28+
action = %PlanetAction{
29+
type: :enqueue_building,
30+
data: %{planet_id: planet.id, data: %{building_id: building_id, demolish: demolish}}
31+
}
32+
33+
with {:ok, result} <- EnqueueBuilding.perform(player, action) do
34+
dbg(result)
3035
:ok
3136
else
3237
{:prerequisites_ok, false} ->

lib/galaxies/planet.ex

+11
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@ defmodule Galaxies.Planet do
3131

3232
has_many :buildings, Galaxies.PlanetBuilding
3333
has_many :units, Galaxies.PlanetUnit
34+
has_many :events, Galaxies.Planets.PlanetEvent
3435

3536
timestamps(type: :utc_datetime_usec)
3637
end
3738

39+
def update_resources_changeset(planet, attrs) do
40+
planet
41+
|> cast(attrs, [
42+
:metal_units,
43+
:crystal_units,
44+
:deuterium_units,
45+
:updated_at
46+
])
47+
end
48+
3849
def building_construction_complete_changeset(planet, attrs) do
3950
planet
4051
|> cast(attrs, [

lib/galaxies_web/live/facilities_live.ex

+4-7
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,14 @@ defmodule GalaxiesWeb.FacilitiesLive do
8181
end
8282

8383
def handle_event("upgrade_building:" <> building_id, _value, socket) do
84-
building =
85-
Enum.find(socket.assigns.planet_buildings, fn building ->
86-
"#{building.id}" == building_id
87-
end)
88-
89-
level = building.level + 1
84+
# TODO implement demolish feature
85+
demolish = false
9086

9187
case Accounts.upgrade_planet_building(
88+
socket.assigns.current_player,
9289
socket.assigns.current_planet,
9390
String.to_integer(building_id),
94-
level
91+
demolish
9592
) do
9693
:ok ->
9794
{:noreply, load_build_queue(socket)}

lib/galaxies_web/live/resources_live.ex

+4-7
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,14 @@ defmodule GalaxiesWeb.ResourcesLive do
8181
end
8282

8383
def handle_event("upgrade_building:" <> building_id, _value, socket) do
84-
building =
85-
Enum.find(socket.assigns.planet_buildings, fn building ->
86-
"#{building.id}" == building_id
87-
end)
88-
89-
level = building.level + 1
84+
# TODO implement demolish feature
85+
demolish = false
9086

9187
case Accounts.upgrade_planet_building(
88+
socket.assigns.current_player,
9289
socket.assigns.current_planet,
9390
String.to_integer(building_id),
94-
level
91+
demolish
9592
) do
9693
:ok ->
9794
{:noreply, load_build_queue(socket)}

0 commit comments

Comments
 (0)