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

Commit 12e6b62

Browse files
author
Marcelo Amancio de Lima Santos
committed
WIP
1 parent 22b18a4 commit 12e6b62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+950
-123
lines changed

lib/account/websocket/channel/account.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ channel Helix.Account.Websocket.Channel.Account do
1414
alias Helix.Network.Websocket.Requests.Bounce.Remove, as: BounceRemoveRequest
1515
alias Helix.Software.Websocket.Requests.Virus.Collect, as: VirusCollectRequest
1616
alias Helix.Story.Websocket.Requests.Email.Reply, as: EmailReplyRequest
17-
alias Helix.Universe.Websocket.Channel.Requests.CreateAccount,
17+
alias Helix.Universe.Bank.Websocket.Requests.CreateAccount,
1818
as: BankAccountCreateRequest
1919

2020
@doc """

lib/core/validator/validator.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Helix.Core.Validator do
55
| :hostname
66
| :bounce_name
77
| :reply_id
8+
| :token
89

910
@regex_hostname ~r/^[a-zA-Z0-9-_.@#]{1,20}$/
1011

lib/event/dispatcher.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,28 @@ defmodule Helix.Event.Dispatcher do
272272
# All
273273
event BankEvent.Bank.Account.Login
274274
event BankEvent.Bank.Account.Updated
275+
event BankEvent.Bank.Account.Removed
275276
event BankEvent.Bank.Account.Password.Revealed
277+
event BankEvent.Bank.Account.Password.Changed
276278
event BankEvent.Bank.Account.Token.Acquired
277279
event BankEvent.Bank.Transfer.Processed
278280
event BankEvent.Bank.Transfer.Aborted
279281
event BankEvent.RevealPassword.Processed
282+
event BankEvent.ChangePassword.Processed
280283

281284
# Custom handlers
282285
event BankEvent.Bank.Transfer.Processed,
283286
BankHandler.Bank.Transfer,
284287
:transfer_processed
288+
285289
event BankEvent.Bank.Transfer.Processed,
286290
NetworkHandler.Connection,
287291
:bank_transfer_processed
288292

289293
event BankEvent.Bank.Transfer.Aborted,
290294
BankHandler.Bank.Transfer,
291295
:transfer_aborted
296+
292297
event BankEvent.Bank.Transfer.Aborted,
293298
SoftwareHandler.Cracker,
294299
:bank_transfer_aborted
@@ -311,9 +316,10 @@ defmodule Helix.Event.Dispatcher do
311316

312317
event BankEvent.Bank.Account.Password.Changed,
313318
BankHandler.Bank.Account,
314-
:bank_password_changed
319+
:password_changed
315320

316321
event BankEvent.Bank.Account.Login,
317322
EntityHandler.Database,
318323
:bank_account_login
324+
319325
end

lib/event/notificable/notificable.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defprotocol Helix.Event.Notificable do
6363
action, also receive special treatment. Hence:
6464
6565
- third AT attack_target - Third party on victim's server
66-
- third AT attack_source - Third party on attacker's server.
66+
- third AT attack_source - Third party on attacker's server.
6767
- third AT random_server - Third party on unrelated (random) server.
6868
6969
Last but not least, in some cases we do not have an attacker/victim
@@ -98,6 +98,8 @@ defprotocol Helix.Event.Notificable do
9898

9999
alias Helix.Account.Model.Account
100100
alias Helix.Server.Model.Server
101+
alias Helix.Universe.Bank.Model.ATM
102+
alias Helix.Universe.Bank.Model.BankAccount
101103

102104
@type whom_to_notify ::
103105
%{

lib/event/notification_handler.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule Helix.Event.NotificationHandler do
88
alias Helix.Entity.Model.Entity
99
alias Helix.Server.Model.Server
1010
alias Helix.Server.State.Websocket.Channel, as: ServerWebsocketChannelState
11+
alias Helix.Universe.Bank.Model.ATM
12+
alias Helix.Universe.Bank.Model.BankAccount
1113

1214
@type channel_account_id :: Account.id | Entity.id
1315

lib/universe/bank/action/bank.ex

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ defmodule Helix.Universe.Bank.Action.Bank do
2727
as: BankAccountPasswordChangedEvent
2828
alias Helix.Universe.Bank.Event.Bank.Account.Token.Acquired,
2929
as: BankAccountTokenAcquiredEvent
30+
alias Helix.Universe.Bank.Event.Bank.Account.Removed,
31+
as: BankAccountRemovedEvent
32+
alias Helix.Universe.Bank.Event.Bank.Transfer.Successful,
33+
as: BankTransferSuccessfulEvent
34+
alias Helix.Universe.Bank.Event.Bank.Transfer.Failed,
35+
as: BankTransferFailedEvent
3036

3137
@spec start_transfer(BankAccount.t, BankAccount.t, pos_integer, Account.idt) ::
3238
{:ok, BankTransfer.t}
3339
| {:error, {:funds, :insufficient}}
34-
| {:error, {:account, :notfound}}
40+
| {:error, {:bank_account, :not_found}}
3541
| {:error, Ecto.Changeset.t}
3642
@doc """
3743
Starts a bank transfer.
@@ -51,9 +57,8 @@ defmodule Helix.Universe.Bank.Action.Bank do
5157
as: :start
5258

5359
@spec complete_transfer(BankTransfer.t) ::
54-
:ok
55-
| {:error, {:transfer, :notfound}}
56-
| {:error, :internal}
60+
{:ok, BankTransfer.t, [BankTransferSuccessfulEvent.t]}
61+
| {:error, term, [BankTransferFailedEvent.t]}
5762
@doc """
5863
Completes the transfer.
5964
@@ -66,9 +71,19 @@ defmodule Helix.Universe.Bank.Action.Bank do
6671
This function should not be called directly by Public. Instead, it must be
6772
triggered by the BankTransferCompletedEvent.
6873
"""
69-
defdelegate complete_transfer(transfer),
70-
to: BankTransferInternal,
71-
as: :complete
74+
def complete_transfer(transfer) do
75+
case BankTransferInternal.complete(transfer) do
76+
:ok ->
77+
{:ok, transfer, [BankTransferSuccessfulEvent.new(transfer)]}
78+
79+
{:error, reason} ->
80+
{
81+
:error,
82+
reason,
83+
[BankTransferFailedEvent.new(transfer, reason)]
84+
}
85+
end
86+
end
7287

7388
@spec abort_transfer(BankTransfer.t) ::
7489
:ok
@@ -91,7 +106,7 @@ defmodule Helix.Universe.Bank.Action.Bank do
91106
as: :abort
92107

93108
@spec open_account(Account.idt, ATM.id) ::
94-
{:ok, BankAccount.t}
109+
{:ok, BankAccount.t, [BankAccountUpdatedEvent.t]}
95110
| {:error, Ecto.Changeset.t}
96111
@doc """
97112
Opens a bank account.
@@ -103,23 +118,34 @@ defmodule Helix.Universe.Bank.Action.Bank do
103118
|> Map.get(:entity_id)
104119
|> NPCQuery.fetch()
105120

106-
%{owner_id: owner, atm_id: atm, bank_id: bank}
107-
|> BankAccountInternal.create()
121+
case BankAccountInternal.create(owner, atm, bank) do
122+
{:ok, bank_acc} ->
123+
{:ok, bank_acc, [BankAccountUpdatedEvent.new(bank_acc, :created)]}
124+
125+
error ->
126+
error
127+
end
108128
end
109129

110130
@spec close_account(BankAccount.t) ::
111131
:ok
112-
| {:error, {:account, :notfound}}
113-
| {:error, {:account, :notempty}}
132+
| {:error, {:bank_account, :not_found}}
133+
| {:error, {:bank_account, :not_empty}}
114134
@doc """
115135
Closes a bank account.
116136
117137
May fail if the account is invalid or not empty. In order to close an account,
118138
its balance must be empty.
119139
"""
120-
defdelegate close_account(account),
121-
to: BankAccountInternal,
122-
as: :close
140+
def close_account(account) do
141+
case BankAccountInternal.close(account) do
142+
:ok ->
143+
{:ok, [BankAccountRemovedEvent.new(account)]}
144+
145+
{:error, reason} ->
146+
{:error, reason}
147+
end
148+
end
123149

124150
@spec direct_deposit(BankAccount.t, BankAccount.amount) ::
125151
{:ok, BankAccount.t, [BankAccountUpdatedEvent.t]}
@@ -203,8 +229,14 @@ defmodule Helix.Universe.Bank.Action.Bank do
203229
{:ok, BankAccount.t, [BankAccountPasswordChangedEvent.t]}
204230
| {:error, :internal}
205231
def change_password(account, changed_by) do
206-
event = BankAccountPasswordChangedEvent.new(account, changed_by)
207-
{:ok, account, [event]}
232+
with {:ok, account} <- update_password(account) do
233+
event = BankAccountPasswordChangedEvent.new(account, changed_by)
234+
235+
{:ok, account, [event]}
236+
else
237+
_ ->
238+
{:error, :internal}
239+
end
208240
end
209241

210242
@spec update_password(BankAccount.t) ::

lib/universe/bank/action/flow/bank_account.ex

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
55
alias Helix.Event
66
alias Helix.Entity.Query.Entity, as: EntityQuery
77
alias Helix.Network.Action.Flow.Tunnel, as: TunnelFlow
8-
alias Helix.Network.Model.Connection
98
alias Helix.Network.Query.Network, as: NetworkQuery
109
alias Helix.Process.Model.Process
1110
alias Helix.Server.Model.Server
@@ -61,21 +60,46 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
6160
Opens a new BankAccount to given Account.id
6261
"""
6362
def open(account_id, atm_id) do
64-
bank_account =
65-
BankAction.open_account(account_id, atm_id)
66-
case bank_account do
67-
{:ok, bank_account} ->
68-
{:ok, bank_account}
69-
{:error, _} ->
70-
{:error, :internal}
63+
flowing do
64+
with \
65+
{:ok, bank_acc, events} <- BankAction.open_account(account_id, atm_id),
66+
on_success(fn -> Event.emit(events) end)
67+
do
68+
{:ok, bank_acc}
69+
else
70+
_ ->
71+
{:error, :internal}
72+
end
73+
end
74+
end
75+
76+
@spec close(BankAccount.t) ::
77+
:ok
78+
| {:error, {:bank_account, :not_found}}
79+
| {:error, {:bank_account, :not_empty}}
80+
def close(bank_account) do
81+
flowing do
82+
with \
83+
true <- not is_nil(bank_account),
84+
{:ok, events} <- BankAction.close_account(bank_account),
85+
on_success(fn -> Event.emit(events) end)
86+
do
87+
:ok
88+
end
7189
end
7290
end
7391

7492
@spec change_password(BankAccount.t, Server.t, Server.t, relay) ::
7593
{:ok, Process.t}
7694
| BankAccountChangePasswordProcess.executable_error
7795
@doc """
78-
Starts a ChangePasswordProcess
96+
Starts the `bank_change_password` process.
97+
98+
This game mechanic happens on the BankAccount control panel where the
99+
BankAccount's owner can change the BankAccount's Password
100+
101+
It is a process managed by TOP. in the sense that the password change does not
102+
happen instantly. Completion is handled by `BankAccountEvent`.
79103
80104
Emits: ProcessCreatedEvent
81105
"""
@@ -152,9 +176,9 @@ defmodule Helix.Universe.Bank.Action.Flow.BankAccount do
152176
{:ok, _, events} <- BankAction.login_token(acc, token, entity),
153177
on_success(fn -> Event.emit(events) end),
154178

155-
{:ok, _, connection} <- start_connection.()
179+
{:ok, tunnel, connection} <- start_connection.()
156180
do
157-
{:ok, connection}
181+
{:ok, tunnel, connection}
158182
end
159183
end
160184
end

0 commit comments

Comments
 (0)