This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - Add Henforcers for MotherboardUpdateRequest
- Loading branch information
1 parent
43e5c58
commit aed06db
Showing
6 changed files
with
415 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
defmodule Helix.Server.Henforcer.Component do | ||
|
||
import Helix.Henforcer | ||
|
||
alias Helix.Entity.Henforcer.Entity, as: EntityHenforcer | ||
alias Helix.Server.Model.Component | ||
alias Helix.Server.Model.Motherboard | ||
alias Helix.Server.Query.Component, as: ComponentQuery | ||
|
||
def component_exists?(component_id = %Component.ID{}) do | ||
with component = %Component{} <- ComponentQuery.fetch(component_id) do | ||
reply_ok(%{component: component}) | ||
else | ||
_ -> | ||
reply_error({:component, :not_found}) | ||
end | ||
end | ||
|
||
def is_motherboard?(component = %Component{type: :mobo}), | ||
do: reply_ok(%{component: component}) | ||
def is_motherboard?(%Component{}), | ||
do: reply_error({:component, :not_motherboard}) | ||
def is_motherboard?(component_id = %Component.ID{}) do | ||
henforce(component_exists?(component_id)) do | ||
is_motherboard?(relay.component) | ||
end | ||
end | ||
|
||
def can_link?( | ||
mobo = %Component{type: :mobo}, | ||
component = %Component{}, | ||
slot_id) | ||
do | ||
with :ok <- Motherboard.check_compatibility(mobo, component, slot_id, []) do | ||
reply_ok() | ||
else | ||
{:error, reason} -> | ||
reply_error({:motherboard, reason}) | ||
end | ||
end | ||
|
||
def has_initial_components?(components) do | ||
if Motherboard.has_required_initial_components?(components) do | ||
reply_ok() | ||
else | ||
reply_error({:motherboard, :missing_initial_components}) | ||
end | ||
end | ||
|
||
def can_update_mobo?(entity_id, mobo_id, components, network_connections) do | ||
reduce_components = fn mobo -> | ||
init = {{true, %{}}, nil} | ||
|
||
components | ||
|> Enum.reduce_while(init, fn {slot_id, comp_id}, {{true, acc}, cache} -> | ||
with \ | ||
{true, r1} <- component_exists?(comp_id), | ||
component = r1.component, | ||
|
||
{true, r2} <- | ||
EntityHenforcer.owns_component?(entity_id, component, cache), | ||
|
||
{true, _} <- can_link?(mobo, component, slot_id) | ||
do | ||
acc_components = Map.get(acc, :components, []) | ||
|
||
new_acc = | ||
acc | ||
|> put_in([:components], acc_components ++ [{component, slot_id}]) | ||
|> put_in([:entity], r2.entity) | ||
|> put_in([:owned_components], r2.owned_components) | ||
|
||
{:cont, {{true, new_acc}, r2.owned_components}} | ||
else | ||
error -> | ||
{:halt, {error, cache}} | ||
end | ||
|
||
end) | ||
|> elem(0) | ||
end | ||
|
||
reduce_network_connections = fn components -> | ||
init = {{true, %{}}, nil} | ||
|
||
network_connections | ||
|> Enum.reduce_while(init, fn {nic_id, nip}, {{true, acc}, cache} -> | ||
|
||
# PAREI AQUI \/. COMECAR COM `OWNS_NC?`. Similar a `owns_component?` | ||
{true, r1} <- NetworkHenforcer.nip_exists?(nip) | ||
{true, r2} <- NetworkHenforcer.owns_nc?(entity, nc) | ||
|
||
{:cont, {{true, acc}, cache}} | ||
end) | ||
|> elem(0) | ||
end | ||
|
||
with \ | ||
{true, r0} <- component_exists?(mobo_id), | ||
mobo = r0.component, | ||
|
||
# Make sure user is plugging components into a motherboard | ||
{true, _} <- is_motherboard?(mobo), | ||
|
||
# Iterate over components/slots and make the required henforcements | ||
{true, r1} <- reduce_components.(mobo), | ||
components = r1.components, | ||
entity = r1.entity, | ||
owned = r1.owned_components, | ||
|
||
# Ensure mobo belongs to entity | ||
{true, _} <- EntityHenforcer.owns_component?(entity, mobo, owned), | ||
|
||
# Ensure all required initial components are there | ||
{true, _} <- has_initial_components?(components), | ||
|
||
{true, r2} <- reduce_network_connections.(components) | ||
do | ||
reply_ok(relay(r1)) | ||
else | ||
error -> | ||
error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.