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.
Add MotherboardUpdated and MotherboardUpdateFailed events
- Loading branch information
1 parent
3fecf8d
commit 7859e41
Showing
10 changed files
with
208 additions
and
19 deletions.
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
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,90 @@ | ||
defmodule Helix.Server.Event.Motherboard do | ||
|
||
import Helix.Event | ||
|
||
event Updated do | ||
|
||
alias Helix.Server.Model.Server | ||
alias Helix.Server.Public.Index.Hardware, as: HardwareIndex | ||
|
||
event_struct [:server, :index_cache] | ||
|
||
def new(server = %Server{}) do | ||
%__MODULE__{ | ||
server: server, | ||
|
||
# `index_cache` is a cache of the new server hardware index (bootstrap) | ||
# We save it on the event struct so it is only generated once; | ||
# otherwise it would have to be recalculated to every player joined | ||
# on the server channel. | ||
# We save the full cache (`:local`) and, if the Notificable receiver is | ||
# a remote server, we nilify the `:motherboard` entry | ||
index_cache: HardwareIndex.index(server, :local) | ||
} | ||
end | ||
|
||
notify do | ||
|
||
@event :motherboard_updated | ||
|
||
def generate_payload(event, %{assigns: %{meta: %{access_type: :local}}}) do | ||
data = HardwareIndex.render_index(event.index_cache) | ||
|
||
{:ok, data} | ||
end | ||
|
||
def generate_payload(event, %{assigns: %{meta: %{access_type: :remote}}}) do | ||
data = | ||
event.index_cache | ||
|> HardwareIndex.render_index() | ||
|> Map.replace(:motherboard, nil) | ||
|
||
{:ok, data} | ||
end | ||
|
||
def whom_to_notify(event), | ||
do: %{server: event.server.server_id} | ||
end | ||
end | ||
|
||
event UpdateFailed do | ||
|
||
alias Helix.Server.Model.Server | ||
|
||
event_struct [:server_id, :reason] | ||
|
||
@type t :: %__MODULE__{ | ||
server_id: Server.id, | ||
reason: reason | ||
} | ||
|
||
@type reason :: :internal | ||
|
||
@spec new(Server.idt, reason) :: | ||
t | ||
def new(server = %Server{}, reason), | ||
do: new(server.server_id, reason) | ||
def new(server_id = %Server.ID{}, reason) do | ||
%__MODULE__{ | ||
server_id: server_id, | ||
reason: reason | ||
} | ||
end | ||
|
||
notify do | ||
|
||
@event :motherboard_update_failed | ||
|
||
def generate_payload(event, %{assigns: %{meta: %{access_type: :local}}}) do | ||
data = %{reason: event.reason} | ||
|
||
{:ok, data} | ||
end | ||
def generate_payload(_, _), | ||
do: :noreply | ||
|
||
def whom_to_notify(event), | ||
do: %{server: event.server_id} | ||
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
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,34 @@ | ||
defmodule Helix.Server.Event.MotherboardTest do | ||
|
||
use Helix.Test.Case.Integration | ||
|
||
alias Helix.Event.Notificable | ||
|
||
alias Helix.Test.Channel.Setup, as: ChannelSetup | ||
alias Helix.Test.Event.Setup, as: EventSetup | ||
|
||
@socket_local ChannelSetup.mock_server_socket(access_type: :local) | ||
@socket_remote ChannelSetup.mock_server_socket(access_type: :remote) | ||
|
||
describe "MotherboardUpdatedEvent.generate_payload/2" do | ||
test "generates full hardware index on gateway (local)" do | ||
event = EventSetup.Server.motherboard_updated() | ||
|
||
assert {:ok, data} = Notificable.generate_payload(event, @socket_local) | ||
|
||
# Returns full data about the motherboard | ||
assert data.motherboard.motherboard_id | ||
assert data.motherboard.slots | ||
assert data.motherboard.network_connections | ||
end | ||
|
||
test "generates partial hardware index on endpoint (remote)" do | ||
event = EventSetup.Server.motherboard_updated() | ||
|
||
assert {:ok, data} = Notificable.generate_payload(event, @socket_remote) | ||
|
||
# Does not return information about the motherboard | ||
refute data.motherboard | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Helix.Test.Event.Setup.Server do | ||
|
||
alias Helix.Server.Model.Server | ||
|
||
alias Helix.Server.Event.Motherboard.Updated, as: MotherboardUpdatedEvent | ||
|
||
alias Helix.Test.Server.Setup, as: ServerSetup | ||
|
||
def motherboard_updated(server = %Server{}), | ||
do: MotherboardUpdatedEvent.new(server) | ||
def motherboard_updated do | ||
{server, _} = ServerSetup.server() | ||
motherboard_updated(server) | ||
end | ||
end |