Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge 3cf8555 into a4df8a8
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Dec 7, 2017
2 parents a4df8a8 + 3cf8555 commit 7a8e05f
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 32 deletions.
7 changes: 5 additions & 2 deletions lib/account/public/account.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Helix.Account.Public.Account do

alias Helix.Entity.Model.Entity
alias Helix.Entity.Query.Entity, as: EntityQuery
alias Helix.Server.Public.Index, as: ServerIndex
alias Helix.Story.Public.Index, as: StoryIndex
alias Helix.Account.Public.Index, as: AccountIndex
Expand Down Expand Up @@ -28,9 +29,11 @@ defmodule Helix.Account.Public.Account do
@spec bootstrap(Entity.id) ::
bootstrap
def bootstrap(entity_id) do
entity = EntityQuery.fetch(entity_id)

%{
account: AccountIndex.index(entity_id),
servers: ServerIndex.index(entity_id),
account: AccountIndex.index(entity),
servers: ServerIndex.index(entity),
storyline: StoryIndex.index(entity_id)
}
end
Expand Down
34 changes: 19 additions & 15 deletions lib/account/public/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@ defmodule Helix.Account.Public.Index do
alias Helix.Entity.Model.Entity
alias Helix.Entity.Query.Entity, as: EntityQuery
alias Helix.Server.Model.Server
alias Helix.Account.Public.Index.Inventory, as: InventoryIndex

@type index ::
%{
mainframe: Server.id
mainframe: Server.id,
inventory: InventoryIndex.index
}

@type rendered_index ::
%{
mainframe: String.t
mainframe: String.t,
inventory: InventoryIndex.rendered_index
}

@spec index(Entity.id) ::
@spec index(Entity.t) ::
index
def index(entity_id) do
mainframe =
entity_id
|> EntityQuery.fetch()
|> EntityQuery.get_servers()
|> List.first()
def index(entity) do
mainframe =
entity
|> EntityQuery.get_servers()
|> List.first()

%{
mainframe: mainframe
}
%{
mainframe: mainframe,
inventory: InventoryIndex.index(entity)
}
end

@spec render_index(index) ::
rendered_index
def render_index(index) do
%{
mainframe: to_string(index.mainframe)
}
%{
mainframe: to_string(index.mainframe),
inventory: InventoryIndex.render_index(index.inventory)
}
end
end
165 changes: 165 additions & 0 deletions lib/account/public/index/inventory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
defmodule Helix.Account.Public.Index.Inventory do

import HELL.Macros

alias HELL.Utils
alias Helix.Entity.Model.Entity
alias Helix.Entity.Query.Entity, as: EntityQuery
alias Helix.Network.Model.Network
alias Helix.Server.Model.Component
alias Helix.Server.Query.Component, as: ComponentQuery
alias Helix.Server.Query.Motherboard, as: MotherboardQuery

@type index ::
%{
components: component_index,
network_connections: network_connection_index
}

@type rendered_index ::
%{
components: rendered_component_index,
network_connections: rendered_network_connection_index
}

@typep component_index :: [Component.t]
@typep network_connection_index :: [Network.Connection.t]

@typep rendered_component_index ::
%{component_id :: String.t => rendered_component}

@typep rendered_component ::
%{
spec_id: String.t,
durability: float,
used?: boolean,
type: String.t,
custom: %{term => String.t}
}

@typep rendered_network_connection_index ::
[rendered_network_connection]

@typep rendered_network_connection ::
%{
network_id: String.t,
ip: String.t,
name: String.t,
used?: boolean
}

@spec index(Entity.t) ::
index
def index(entity = %Entity{}) do
%{
components: get_components(entity),
network_connections: get_network_connections(entity)
}
end

@spec get_components(Entity.t) ::
component_index
defp get_components(entity) do
entity
|> EntityQuery.get_components()
|> Enum.map(&(ComponentQuery.fetch(&1.component_id)))
end

@spec get_network_connections(Entity.t) ::
network_connection_index
defp get_network_connections(entity) do
entity.entity_id
|> EntityQuery.get_network_connections()
end

@spec render_index(index) ::
rendered_index
def render_index(index) do
%{
components: render_components(index.components),
network_connections: render_network_connections(index.network_connections)
}
end

@spec render_components(component_index) ::
rendered_component_index
defp render_components(components) do
linked_components = get_linked_components(components)

Enum.reduce(components, %{}, fn component, acc ->
component_id = component.component_id |> to_string()

%{}
|> Map.put(component_id, render_component(component, linked_components))
|> Map.merge(acc)
end)
end

docp """
Iterates over all components owned by the user and figures out which ones are
used (i.e. are linked to a motherboard).
Note that, for our indexing purpose, a motherboard with linked components to
it is marked as a linked component itself, so the motherboard entry returned
to the client also tells whether it is being used or not.
"""
defp get_linked_components(components) do
components

# Fetches all motherboards
|> Enum.reduce([], fn component, acc ->
if component.type == :mobo do
motherboard = MotherboardQuery.fetch(component.component_id)

# Fetching a motherboard may return empty if there are no components
# linked to it, hence this check.
if motherboard do
acc ++ [{MotherboardQuery.fetch(component.component_id), component}]
else
acc
end
else
acc
end
end)

# From these mobos, accumulates the linked components (and the mobo itself)
|> Enum.reduce([], fn {motherboard, mobo}, acc ->
acc ++ MotherboardQuery.get_components(motherboard) ++ [mobo]
end)

# Flatten earth
|> List.flatten()
end

@spec render_component(Component.t, [Component.t]) ::
rendered_component
defp render_component(component, linked_components) do
%{
spec_id: to_string(component.spec_id),
type: to_string(component.type),
custom: Utils.stringify_map(component.custom),
used?: component in linked_components,
durability: 1.0
}
end

@spec render_network_connections(network_connection_index) ::
rendered_network_connection_index
defp render_network_connections(network_connections) do
Enum.reduce(network_connections, [], fn nc, acc ->
acc ++ [render_network_connection(nc)]
end)
end

@spec render_network_connection(Network.Connection.t) ::
rendered_network_connection
defp render_network_connection(nc) do
%{
network_id: nc.network_id |> to_string(),
ip: nc.ip,
name: "Internet",
used?: not is_nil(nc.nic_id)
}
end
end
3 changes: 2 additions & 1 deletion lib/entity/action/entity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Helix.Entity.Action.Entity do
alias Helix.Universe.NPC.Model.NPC
alias Helix.Entity.Internal.Entity, as: EntityInternal
alias Helix.Entity.Model.Entity
alias Helix.Entity.Model.EntityComponent

@spec create_from_specialization(struct) ::
{:ok, Entity.t}
Expand Down Expand Up @@ -46,7 +47,7 @@ defmodule Helix.Entity.Action.Entity do
to: EntityInternal

@spec link_component(Entity.t, Component.idt) ::
:ok
{:ok, EntityComponent.t}
| {:error, Ecto.Changeset.t}
@doc """
Links `component` to `entity` effectively making entity the owner of the
Expand Down
15 changes: 13 additions & 2 deletions lib/entity/query/entity.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Helix.Entity.Query.Entity do

alias Helix.Universe.NPC.Model.NPC
alias Helix.Account.Model.Account
alias Helix.Network.Query.Network, as: NetworkQuery
alias Helix.Server.Model.Server
alias Helix.Universe.NPC.Model.NPC
alias Helix.Entity.Internal.Entity, as: EntityInternal
alias Helix.Entity.Model.Entity
alias Helix.Universe.NPC.Model.NPC

@spec fetch(Entity.id) ::
Entity.t
Expand Down Expand Up @@ -58,6 +58,13 @@ defmodule Helix.Entity.Query.Entity do
defdelegate get_components(entity),
to: EntityInternal

@doc """
Returns all network connections owned by the entity.
"""
defdelegate get_network_connections(entity),
to: NetworkQuery.Connection,
as: :get_by_entity

@spec get_entity_id(struct) ::
Entity.id
@doc """
Expand All @@ -67,12 +74,16 @@ defmodule Helix.Entity.Query.Entity do
case entity do
%Account{account_id: %Account.ID{id: id}} ->
%Entity.ID{id: id}

%Account.ID{id: id} ->
%Entity.ID{id: id}

%NPC{npc_id: %NPC.ID{id: id}} ->
%Entity.ID{id: id}

%NPC.ID{id: id} ->
%Entity.ID{id: id}

value ->
Entity.ID.cast!(value)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/network/internal/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ defmodule Helix.Network.Internal.Network.Connection do
|> Repo.one()
end

@spec get_by_entity(Entity.id) ::
[Network.Connection.t]
def get_by_entity(entity_id) do
entity_id
|> Network.Connection.Query.by_entity()
|> Repo.all()
end

@spec create(Network.idt, Network.ip, Entity.idt, Component.idt | nil) ::
repo_result
@doc """
Expand Down
6 changes: 6 additions & 0 deletions lib/network/model/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ defmodule Helix.Network.Model.Network.Connection do

query do

alias Helix.Entity.Model.Entity
alias Helix.Server.Model.Component
alias Helix.Network.Model.Network

Expand All @@ -97,5 +98,10 @@ defmodule Helix.Network.Model.Network.Connection do
Queryable.t
def by_nic(query \\ Network.Connection, nic_id),
do: where(query, [nc], nc.nic_id == ^nic_id)

@spec by_entity(Queryable.t, Entity.id) ::
Queryable.t
def by_entity(query \\ Network.Connection, entity_id),
do: where(query, [nc], nc.entity_id == ^entity_id)
end
end
3 changes: 3 additions & 0 deletions lib/network/query/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ defmodule Helix.Network.Query.Network.Connection do

defdelegate fetch_by_nic(nic),
to: NetworkInternal.Connection

defdelegate get_by_entity(entity_id),
to: NetworkInternal.Connection
end
11 changes: 11 additions & 0 deletions lib/server/internal/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ defmodule Helix.Server.Internal.Motherboard do
Motherboard.get_free_slots(motherboard, mobo.spec_id)
end

@spec get_components(Motherboard.t) ::
[Component.pluggable]
@doc """
Returns all components linked to the motherboard.
"""
def get_components(motherboard = %Motherboard{}) do
Enum.reduce(motherboard.slots, [], fn {_slot_id, component}, acc ->
acc ++ [component]
end)
end

@spec get_cpus(Motherboard.t) ::
[Component.cpu]
@doc """
Expand Down
9 changes: 3 additions & 6 deletions lib/server/public/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule Helix.Server.Public.Index do
tunnels: NetworkIndex.rendered_index
}

@spec index(Entity.id) ::
@spec index(Entity.t) ::
index
@doc """
Returns the server index, which encompasses all other indexes residing under
Expand All @@ -117,11 +117,8 @@ defmodule Helix.Server.Public.Index do
Called on Account bootstrap (as opposed to `remote_server_index`, which is
used after a player logs into a remote server)
"""
def index(entity_id) do
player_servers =
entity_id
|> EntityQuery.fetch()
|> EntityQuery.get_servers()
def index(entity = %Entity{}) do
player_servers = EntityQuery.get_servers(entity)

# Get all endpoints (any remote server the player is SSH-ed to)
endpoints = TunnelQuery.get_remote_endpoints(player_servers)
Expand Down
2 changes: 2 additions & 0 deletions lib/server/query/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ defmodule Helix.Server.Query.Motherboard do
|> MotherboardInternal.get_resources()
end

defdelegate get_components(motherboard),
to: MotherboardInternal
defdelegate get_cpus(motherboard),
to: MotherboardInternal
defdelegate get_hdds(motherboard),
Expand Down
Loading

0 comments on commit 7a8e05f

Please sign in to comment.