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

Commit

Permalink
Merge c3609ae into 37c9ede
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Aug 15, 2017
2 parents 37c9ede + c3609ae commit 4f92183
Show file tree
Hide file tree
Showing 44 changed files with 2,384 additions and 110 deletions.
13 changes: 13 additions & 0 deletions lib/event_dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Helix.Event.Dispatcher do
alias Helix.Process
alias Helix.Software
alias Helix.Server
alias Helix.Universe

##############################################################################
# Account events
Expand Down Expand Up @@ -95,4 +96,16 @@ defmodule Helix.Event.Dispatcher do
event Software.Model.SoftwareType.LogForge.Edit.ConclusionEvent,
Log.Event.Log,
:log_forge_conclusion

##############################################################################
# Universe events
##############################################################################

event Universe.Bank.Model.BankTransfer.BankTransferCompletedEvent,
Universe.Bank.Event.BankTransfer,
:transfer_completed

event Universe.Bank.Model.BankTransfer.BankTransferAbortedEvent,
Universe.Bank.Event.BankTransfer,
:transfer_aborted
end
8 changes: 8 additions & 0 deletions lib/hell/hell/password.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule HELL.Password do

def generate(:server),
do: Burette.Internet.password alpha: 8, digit: 4

def generate(:bank_account),
do: Burette.Internet.password alpha: 8
end
4 changes: 3 additions & 1 deletion lib/hell/hell/pk/header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ defmodule HELL.PK.Header do
log_log:
[0x0030, 0x0000, 0x0000],
log_revision:
[0x0030, 0x0001, 0x0000]
[0x0030, 0x0001, 0x0000],
bank_transfer:
[0x0040, 0x0000, 0x0000]
}

@spec pk_for(atom) ::
Expand Down
6 changes: 3 additions & 3 deletions lib/process/action/process.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Helix.Process.Action.Process do

alias Helix.Process.Query.Process, as: ProcessQuery
alias Helix.Process.Model.Process
alias Helix.Process.Model.Process.ProcessCreatedEvent
alias Helix.Process.Query.Process, as: ProcessQuery
alias Helix.Process.State.TOP.Manager, as: ManagerTOP
alias Helix.Process.State.TOP.Server, as: ServerTOP

Expand All @@ -21,11 +21,11 @@ defmodule Helix.Process.Action.Process do
@doc """
Creates a new process
Each process defines it's required arguments. When the process is successfully
Each process defines its required arguments. When the process is successfully
created, it'll cause the server to reallocate resources to properly hold it.
Might return `{:error, :resources}` if the server does not have enough
resources to hold it's current processes along with the input process
resources to hold its current processes along with the input process
### Examples
Expand Down
78 changes: 39 additions & 39 deletions lib/process/model/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Helix.Process.Model.Process do
use Ecto.Schema
use HELL.ID, field: :process_id, meta: [0x0021]

import Ecto.Changeset

alias Ecto.Changeset
alias Helix.Network.Model.Connection
alias Helix.Network.Model.Network
Expand All @@ -15,8 +17,6 @@ defmodule Helix.Process.Model.Process do
alias Helix.Process.Model.Process.ProcessType
alias Helix.Process.Model.Process.State

import Ecto.Changeset

@type t :: %__MODULE__{
process_id: id,
gateway_id: Server.id,
Expand All @@ -40,6 +40,43 @@ defmodule Helix.Process.Model.Process do

@type process :: %__MODULE__{} | %Ecto.Changeset{data: %__MODULE__{}}

@type create_params :: %{
:gateway_id => Server.idtb,
:target_server_id => Server.idtb,
:process_data => ProcessType.t,
:process_type => String.t,
optional(:file_id) => File.idtb,
optional(:network_id) => Network.idtb,
optional(:connection_id) => Connection.idtb,
optional(:objective) => map
}

@type update_params :: %{
optional(:state) => State.state,
optional(:priority) => 0..5,
optional(:creation_time) => DateTime.t,
optional(:updated_time) => DateTime.t,
optional(:estimated_time) => DateTime.t | nil,
optional(:limitations) => map,
optional(:objective) => map,
optional(:processed) => map,
optional(:allocated) => map,
optional(:minimum) => map,
optional(:process_data) => ProcessType.t
}

@creation_fields ~w/
process_data
process_type
gateway_id
target_server_id
file_id
network_id
connection_id/a
@update_fields ~w/state priority updated_time estimated_time minimum/a

@required_fields ~w/gateway_id target_server_id process_data process_type/a

schema "processes" do
field :process_id, ID,
primary_key: true
Expand Down Expand Up @@ -105,29 +142,6 @@ defmodule Helix.Process.Model.Process do
references: :process_id
end

@creation_fields ~w/
process_data
process_type
gateway_id
target_server_id
file_id
network_id
connection_id/a
@update_fields ~w/state priority updated_time estimated_time minimum/a

@required_fields ~w/gateway_id target_server_id process_data process_type/a

@type create_params :: %{
:gateway_id => Server.idtb,
:target_server_id => Server.idtb,
:process_data => ProcessType.t,
:process_type => String.t,
optional(:file_id) => File.idtb,
optional(:network_id) => Network.idtb,
optional(:connection_id) => Connection.idtb,
optional(:objective) => map
}

@spec create_changeset(create_params) ::
Changeset.t
def create_changeset(params) do
Expand Down Expand Up @@ -163,20 +177,6 @@ defmodule Helix.Process.Model.Process do
|> put_embed(:allocated, %{})
end

@type update_params :: %{
optional(:state) => State.state,
optional(:priority) => 0..5,
optional(:creation_time) => DateTime.t,
optional(:updated_time) => DateTime.t,
optional(:estimated_time) => DateTime.t | nil,
optional(:limitations) => map,
optional(:objective) => map,
optional(:processed) => map,
optional(:allocated) => map,
optional(:minimum) => map,
optional(:process_data) => ProcessType.t
}

@spec update_changeset(process, update_params) ::
Changeset.t
def update_changeset(process, params) do
Expand Down
3 changes: 2 additions & 1 deletion lib/process/model/process/process_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ defprotocol Helix.Process.Model.Process.ProcessType do

@type resource :: :cpu | :ram | :dlk | :ulk

@spec dynamic_resources(t) :: [resource]
@spec dynamic_resources(t) ::
[resource]
def dynamic_resources(data)

@spec conclusion(t, Process.t | Ecto.Changeset.t) ::
Expand Down
19 changes: 3 additions & 16 deletions lib/server/model/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Helix.Server.Model.Server do

alias Ecto.Changeset
alias HELL.Constant
alias HELL.Password
alias Helix.Hardware.Model.Component
alias Helix.Server.Model.ServerType

Expand Down Expand Up @@ -82,22 +83,8 @@ defmodule Helix.Server.Model.Server do

@spec generate_password(Changeset.t) ::
Changeset.t
defp generate_password(changeset) do
# HACK: I don't intend to keep this generation method but it'll be good
# enough for now (and is faster than using a proper string generator)
unique =
:seconds
|> System.system_time()
|> :erlang.+(:erlang.unique_integer())
|> to_string()

password =
:md5
|> :crypto.hash(unique)
|> Base.encode16()

put_change(changeset, :password, password)
end
defp generate_password(changeset),
do: put_change(changeset, :password, Password.generate(:server))

defmodule Query do
import Ecto.Query
Expand Down
104 changes: 104 additions & 0 deletions lib/universe/bank/action/bank.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule Helix.Universe.Bank.Action.Bank do

alias Helix.Account.Model.Account
alias Helix.Entity.Query.Entity, as: EntityQuery
alias Helix.Universe.Bank.Internal.BankTransfer, as: BankTransferInternal
alias Helix.Universe.Bank.Internal.BankAccount, as: BankAccountInternal
alias Helix.Universe.Bank.Model.ATM
alias Helix.Universe.Bank.Model.BankAccount
alias Helix.Universe.Bank.Model.BankTransfer
alias Helix.Universe.NPC.Query.NPC, as: NPCQuery

@spec start_transfer(BankAccount.t, BankAccount.t, pos_integer, Account.idt) ::
{:ok, BankTransfer.t}
| {:error, {:funds, :insufficient}}
| {:error, {:account, :notfound}}
| {:error, Ecto.Changeset.t}
@doc """
Starts a bank transfer.
In case of success, the transfer is started and the funds, specified by
`amount`, are withdrawn from the source account.
May fail if the given bank accounts are invalid or if the originating account
does not have enough funds to perform the transaction.
This function should not be called directly by Public. Instead,
`BankTransferFlow.start()` should be use, which will take care of creating
the transfer process as well.
"""
defdelegate start_transfer(from_account, to_account, amount, started_by),
to: BankTransferInternal,
as: :start

@spec complete_transfer(BankTransfer.t) ::
:ok
| {:error, {:transfer, :notfound}}
| {:error, :internal}
@doc """
Completes the transfer.
In case of success, the transfer is removed from the database and the amount
is transferred to the destination account.
May fail if the given transfer is not found, or if an internal error happened
during the transaction.
This function should not be called directly by Public. Instead, it must be
triggered by the BankTransferCompletedEvent.
"""
defdelegate complete_transfer(transfer),
to: BankTransferInternal,
as: :complete

@spec abort_transfer(BankTransfer.t) ::
:ok
| {:error, {:transfer, :notfound}}
| {:error, :internal}
@doc """
Aborts the transfer.
In case of success, the transfer is removed from the database and the amount
is transferred back to the source account.
May fail if the given transfer is not found, or if an internal error happened
during the transaction.
This function should not be called directly by Public. Instead, it must be
triggered by the BankTransferAbortedEvent.
"""
defdelegate abort_transfer(transfer),
to: BankTransferInternal,
as: :abort

@spec open_account(Account.idt, ATM.id) ::
{:ok, BankAccount.t}
| {:error, Ecto.Changeset.t}
@doc """
Opens a bank account.
"""
def open_account(owner, atm) do
bank =
atm
|> EntityQuery.fetch_by_server()
|> Map.get(:entity_id)
|> NPCQuery.fetch()

%{owner_id: owner, atm_id: atm, bank_id: bank}
|> BankAccountInternal.create()
end

@spec close_account(BankAccount.t) ::
:ok
| {:error, {:account, :notfound}}
| {:error, {:account, :notempty}}
@doc """
Closes a bank account.
May fail if the account is invalid or not empty. In order to close an account,
its balance must be empty.
"""
defdelegate close_account(account),
to: BankAccountInternal,
as: :close
end
Loading

0 comments on commit 4f92183

Please sign in to comment.