Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions bldg_server/lib/bldg_server/buildings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,21 @@ Given an entity:
end


def calculate_nesting_depth(entity) do
num_slashes = Map.get(entity, "address")
def calculate_nesting_depth(address) do
num_slashes = address
|> String.split(address_delimiter)
|> Enum.drop(1) |> length()
depth = case num_slashes do
|> Enum.drop(1)
|> length()
case num_slashes do
0 -> 0
_ -> trunc((num_slashes + 1) / 2)
end
Map.put(entity, "nesting_depth", depth)
end

def set_nesting_depth(entity) do
depth = calculate_nesting_depth(Map.get(entity, "address"))
Map.put(entity, "nesting_depth", depth)
end

def remove_build_params(entity) do
Map.delete(entity, "container_web_url")
Expand Down Expand Up @@ -364,7 +368,7 @@ Given an entity:
|> figure_out_flr()
|> figure_out_bldg_url()
|> decide_on_location()
|> calculate_nesting_depth()
|> set_nesting_depth()
|> remove_build_params()
end

Expand Down
35 changes: 32 additions & 3 deletions bldg_server/lib/bldg_server/residents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,52 @@ defmodule BldgServer.Residents do
update_resident(resident, changes)
end

def enter_bldg(%Resident{} = resident, address, bldg_url) do
def enter_bldg(%Resident{} = resident, address, bldg_url, flr) do
IO.puts("~~~~~ 1")
container_bldg = Buildings.get_by_bldg_url(bldg_url)
IO.puts("~~~~~ 2")
IO.inspect(container_bldg)

{initial_x, initial_y} = {8, 40} # TODO read from config, per bldg type
changes = %{flr: "#{address}/l0", flr_url: "#{bldg_url}/l0", location: "#{address}/l0/b(#{initial_x},#{initial_y})", x: initial_x, y: initial_y}
changes = %{
flr: "#{address}/#{flr}",
flr_url: "#{bldg_url}/#{flr}",
location: "#{address}/#{flr}/b(#{initial_x},#{initial_y})",
x: initial_x,
y: initial_y,
nesting_depth: Buildings.calculate_nesting_depth(address),
container_entity_type: container_bldg.entity_type
}
IO.inspect(changes)
update_resident(resident, changes)
end

def exit_bldg(%Resident{} = resident, address, bldg_url) do
# get the container flr
container_flr = Buildings.get_container_flr(address)
container_flr_url = Buildings.get_container_flr_url(bldg_url)
container_entity_type = case container_flr_url do
"g" -> "g"
_ ->
container_bldg_url = Buildings.get_container(container_flr_url)
container_bldg = Buildings.get_by_bldg_url(container_bldg_url)
container_bldg.entity_type
end

# determine the location next to the door of the bldg exited
{x, y} = Buildings.extract_coords(address)
new_x = x
new_y = y + 6

changes = %{flr: container_flr, flr_url: container_flr_url, location: "#{container_flr}/b(#{new_x},#{new_y})", x: new_x, y: new_y}
changes = %{
flr: container_flr,
flr_url: container_flr_url,
location: "#{container_flr}/b(#{new_x},#{new_y})",
x: new_x,
y: new_y,
nesting_depth: Buildings.calculate_nesting_depth(container_flr),
container_entity_type: container_entity_type
}
update_resident(resident, changes)
end

Expand Down
4 changes: 3 additions & 1 deletion bldg_server/lib/bldg_server/residents/resident.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ defmodule BldgServer.Residents.Resident do
field :x, :integer
field :y, :integer
field :flr_url, :string
field :nesting_depth, :integer
field :container_entity_type, :string

timestamps()
end

@doc false
def changeset(resident, attrs) do
resident
|> cast(attrs, [:email, :alias, :name, :home_bldg, :is_online, :location, :direction, :previous_messages, :other_attributes, :session_id, :last_login_at, :flr, :flr_url, :x, :y])
|> cast(attrs, [:email, :alias, :name, :home_bldg, :is_online, :location, :direction, :previous_messages, :other_attributes, :session_id, :last_login_at, :flr, :flr_url, :x, :y, :nesting_depth, :container_entity_type])
|> validate_required([:email, :alias, :name, :home_bldg])
|> unique_constraint(:email)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ defmodule BldgServerWeb.ResidentController do
end

# ENTER_BLDG action
def act(conn, %{"resident_email" => email, "action_type" => "ENTER_BLDG", "bldg_address" => address, "bldg_url" => bldg_url}) do
def act(conn, %{"resident_email" => email, "action_type" => "ENTER_BLDG", "bldg_address" => address, "bldg_url" => bldg_url, "flr" => flr}) do
resident = Residents.get_resident_by_email!(email)
# TODO validate that the resident is authorized to enter the given bldg

with {:ok, %Resident{} = upd_rsdt} <- Residents.enter_bldg(resident, address, bldg_url) do
with {:ok, %Resident{} = upd_rsdt} <- Residents.enter_bldg(resident, address, bldg_url, flr) do
conn
|> put_status(:ok)
|> put_resp_header("location", Routes.resident_path(conn, :show, upd_rsdt))
Expand All @@ -175,6 +175,8 @@ defmodule BldgServerWeb.ResidentController do
# TODO validate that the resident is authorized to enter the container bldg (although if not, are they essentially locked?)

with {:ok, %Resident{} = upd_rsdt} <- Residents.exit_bldg(resident, address, bldg_url) do
IO.puts("~~~~~~~~~~~ exit bldg done, resident returned:")
IO.inspect(upd_rsdt)
conn
|> put_status(:ok)
|> put_resp_header("location", Routes.resident_path(conn, :show, upd_rsdt))
Expand Down
2 changes: 2 additions & 0 deletions bldg_server/lib/bldg_server_web/views/resident_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ defmodule BldgServerWeb.ResidentView do
location: resident.location,
flr: resident.flr,
flr_url: resident.flr_url,
container_entity_type: resident.container_entity_type,
nesting_depth: resident.nesting_depth,
x: resident.x,
y: resident.y,
direction: resident.direction,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule BldgServer.Repo.Migrations.AddNestingDepthToResident do
use Ecto.Migration

def change do
alter table("residents") do
add :nesting_depth, :integer
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule BldgServer.Repo.Migrations.AddContainerEntityTypeToResident do
use Ecto.Migration

def change do
alter table("residents") do
add :container_entity_type, :string
end
end
end