-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
89 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
93 changes: 40 additions & 53 deletions
93
apps/game_service/lib/game_service/systems/entity_message.ex
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 |
---|---|---|
@@ -1,53 +1,40 @@ | ||
defmodule GameService.EntityMessageSystem do | ||
@moduledoc """ | ||
TODO: Documentation for GameService.EntityMessageSystem | ||
""" | ||
|
||
use GameService.System, | ||
lock_components: [], | ||
event_subscriptions: [ | ||
GameService.Events.EntityMessage | ||
] | ||
|
||
require Logger | ||
|
||
alias GameService.Events.EntityMessage | ||
|
||
# System behaviour | ||
|
||
@impl true | ||
def run(%EntityMessage{scope: :map} = event, _context) do | ||
# Creating our event message, to send map message | ||
%EntityMessage{ | ||
entity_type: entity_type, | ||
entity_id: entity_id, | ||
message: message | ||
} = event | ||
|
||
# In the GameService, Entity's id is a combination of it's type and it's id | ||
ecs_id = GameService.real_entity_id(entity_type, entity_id) | ||
|
||
# Check if the Entity exists | ||
with {:ok, entity} <- Query.fetch_entity(ecs_id), | ||
{:ok, position} <- Query.fetch_component(entity, E.PositionComponent) do | ||
# Finally, notify all players on map | ||
event = {:chat_message, entity_type, entity_id, message} | ||
|
||
# Here, the 3rd component means that we don't want to send the event to ourself | ||
GameService.System.map_event(event, position, [entity]) | ||
end | ||
|> maybe_print_error(event) | ||
end | ||
|
||
def run(event, _context) do | ||
Logger.warn("#{inspect(__MODULE__)} unhandled event #{inspect(event)}") | ||
end | ||
|
||
## Helpers | ||
|
||
defp maybe_print_error({:error, _} = error, event) do | ||
System.error(__MODULE__, error, event) | ||
end | ||
|
||
defp maybe_print_error(_, _), do: :ok | ||
end | ||
defmodule GameService.EntityMessageSystem do | ||
@moduledoc """ | ||
TODO: Documentation for GameService.EntityMessageSystem | ||
""" | ||
|
||
use GameService.System, | ||
lock_components: [], | ||
event_subscriptions: [ | ||
GameService.Events.EntityMessage | ||
] | ||
|
||
require Logger | ||
|
||
alias GameService.Events.EntityMessage | ||
|
||
# System behaviour | ||
|
||
@impl true | ||
def run(%EntityMessage{scope: :map} = event, _context) do | ||
# Creating our event message, to send map message | ||
%EntityMessage{ | ||
entity_type: entity_type, | ||
entity_id: entity_id, | ||
message: message | ||
} = event | ||
|
||
# In the GameService, Entity's id is a combination of it's type and it's id | ||
ecs_id = GameService.real_entity_id(entity_type, entity_id) | ||
|
||
# Check if the Entity exists | ||
with {:ok, entity} <- Query.fetch_entity(ecs_id), | ||
{:ok, position} <- Query.fetch_component(entity, E.PositionComponent) do | ||
# Finally, notify all players on map | ||
event = {:chat_message, entity_type, entity_id, message} | ||
|
||
# Here, the 3rd component means that we don't want to send the event to ourself | ||
GameService.System.map_event(event, position, [entity]) | ||
end | ||
end | ||
end |
85 changes: 53 additions & 32 deletions
85
apps/game_service/test/game_service/systems/entity_message_system_test.exs
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 |
---|---|---|
@@ -1,32 +1,53 @@ | ||
defmodule GameService.EntityMessageSystemTest do | ||
use GameService.SystemCase | ||
|
||
import ExUnit.CaptureLog | ||
|
||
alias GameService.EntityMessageSystem | ||
|
||
## Tests | ||
|
||
describe "EntityMessage" do | ||
test "system event on entity writting in general chat" do | ||
# Register our process to receive message | ||
ref = make_ref() | ||
position = %E.PositionComponent{map_ref: ref} | ||
endpoint = %P.EndpointComponent{pid: self()} | ||
entity = spawn_player(components: [endpoint, position]) | ||
|
||
# Call our System with a EntityMessage event | ||
event = %Evt.EntityMessage{ | ||
entity_type: :player, | ||
entity_id: GameService.entity_id(entity), | ||
scope: :map, | ||
message: "Best message" | ||
} | ||
|
||
_ = EntityMessageSystem.run(event, 0) | ||
|
||
# # We shouldn't receive an event | ||
refute_received {:chat_message, _, _, _} | ||
end | ||
end | ||
end | ||
defmodule GameService.EntityMessageSystemTest do | ||
use GameService.SystemCase | ||
|
||
import ExUnit.CaptureLog | ||
|
||
alias GameService.EntityMessageSystem | ||
|
||
## Tests | ||
|
||
describe "EntityMessage" do | ||
test "system event on entity writting in general chat" do | ||
# Register our process to receive message | ||
ref = make_ref() | ||
position = %E.PositionComponent{map_ref: ref} | ||
endpoint = %P.EndpointComponent{pid: self()} | ||
entity = spawn_player(components: [endpoint, position]) | ||
|
||
# Call our System with a EntityMessage event | ||
event = %Evt.EntityMessage{ | ||
entity_type: GameService.entity_type(entity), | ||
entity_id: GameService.entity_id(entity), | ||
scope: :map, | ||
message: "Best message" | ||
} | ||
|
||
_ = EntityMessageSystem.run(event, 0) | ||
|
||
# # We shouldn't receive an event | ||
refute_received {:chat_message, _, _, _} | ||
end | ||
|
||
test "system event on another entity writting in general chat" do | ||
# Register our process to receive message | ||
ref = make_ref() | ||
position = %E.PositionComponent{map_ref: ref} | ||
endpoint = %P.EndpointComponent{pid: self()} | ||
entity = spawn_monster(components: [endpoint, position]) | ||
|
||
# Call our System with a EntityMessage event | ||
event = %Evt.EntityMessage{ | ||
entity_type: GameService.entity_type(entity), | ||
entity_id: GameService.entity_id(entity), | ||
scope: :map, | ||
message: "Best message" | ||
} | ||
|
||
_ = EntityMessageSystem.run(event, 0) | ||
|
||
# # We shouldn't receive an event | ||
refute_received {:chat_message, _, _, _} | ||
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