-
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.
♻️ Refacto the player disconnection event
- Loading branch information
1 parent
584a3a1
commit 8bf4b1a
Showing
9 changed files
with
106 additions
and
26 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
2 changes: 1 addition & 1 deletion
2
...b/game_service/events/change_direction.ex → ...service/events/entity_change_direction.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
11 changes: 11 additions & 0 deletions
11
apps/game_service/lib/game_service/events/player_disconnected.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule GameService.Events.PlayerDisconnected do | ||
@moduledoc """ | ||
Event triggered when a player disconnect. | ||
This event is responsible for cleaning the state and send a notification to clients | ||
""" | ||
|
||
use ElvenGard.ECS.Event, fields: [:account_id] | ||
|
||
@type t :: %__MODULE__{account_id: non_neg_integer()} | ||
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
29 changes: 29 additions & 0 deletions
29
apps/game_service/lib/game_service/partitions/system_partition.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule GameService.SystemPartition do | ||
@moduledoc """ | ||
TODO: Documentation for GameService.SystemPartition | ||
""" | ||
|
||
use ElvenGard.ECS.Topology.Partition | ||
|
||
require Logger | ||
|
||
# Partition behaviour | ||
|
||
@impl true | ||
def setup(_opts) do | ||
Logger.debug("SystemPartition is starting...") | ||
|
||
# Run system 60 per seconds (60Hz) | ||
interval = trunc(1 / 60 * 1000) | ||
|
||
{:system, systems: systems(), interval: interval} | ||
end | ||
|
||
# Private functions | ||
|
||
defp systems() do | ||
[ | ||
GameService.SessionDisconnectionSystem | ||
] | ||
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
52 changes: 52 additions & 0 deletions
52
apps/game_service/lib/game_service/systems/session_disconnection_system.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule GameService.SessionDisconnectionSystem do | ||
@moduledoc """ | ||
TODO: Documentation for GameService.SessionDisconnectionSystem | ||
FIXME: Test this system | ||
""" | ||
|
||
use ElvenGard.ECS.System, | ||
lock_components: :sync, | ||
event_subscriptions: [ | ||
GameService.Events.PlayerDisconnected | ||
] | ||
|
||
require Logger | ||
|
||
alias ElvenGard.ECS.{Command, Query} | ||
|
||
alias GameService.EntityComponents, as: E | ||
alias GameService.Events.{EntityDespawned, PlayerDisconnected} | ||
|
||
# System behaviour | ||
|
||
@impl true | ||
def run(%PlayerDisconnected{account_id: account_id}, _delta) do | ||
# Get the disconnected Entity and his PositionComponent | ||
{entity, components} = | ||
Entity | ||
|> Query.select( | ||
with: [{AccountComponent, [{:==, :id, account_id}]}], | ||
preload: [PositionComponent] | ||
) | ||
|> Query.one() | ||
|
||
# Find the PositionComponent | ||
%E.PositionComponent{map_ref: map_ref} = | ||
position = Enum.find(components, &(&1.__struct__ == E.PositionComponent)) | ||
|
||
# Send the EntityDespawned event to notify player | ||
# FIXME: Later rewrite this part: | ||
# - EntityDespawned should be renamed EntityLeaveMap | ||
# - attrs must be only entity_id and map_ref | ||
{:ok, _events} = | ||
ElvenGard.ECS.push( | ||
# Here we only need the position component for the despawn event | ||
%EntityDespawned{entity: entity, components: [position]}, | ||
partition: map_ref | ||
) | ||
|
||
# Remove the Entity from our systems | ||
{:ok, _tuple} = Command.despawn_entity(entity) | ||
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