Skip to content

Commit

Permalink
✨ Send dir packet to client
Browse files Browse the repository at this point in the history
  • Loading branch information
ImNotAVirus committed Sep 22, 2023
1 parent 8bf4b1a commit 922ad84
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 40 deletions.
13 changes: 0 additions & 13 deletions apps/channel_service/lib/channel_service/actions/map_actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ defmodule ChannelService.MapActions do

## Packet handlers

@spec dir(String.t(), map, Socket.t()) :: {:cont, Socket.t()}
def dir("dir", params, %Socket{} = socket) do
%{dir: dir, entity_type: entity_type, entity_id: entity_id} = params
maybe_entity = ElvenCaching.get_entity_by_id(entity_type, entity_id)

case maybe_entity do
{:ok, entity} -> EntityInteractions.set_dir(entity, dir)
_ -> :ok
end

{:cont, socket}
end

@spec ncif(String.t(), map, Socket.t()) :: {:cont, Socket.t()}
def ncif("ncif", params, %Socket{} = socket) do
%{entity_type: entity_type, entity_id: entity_id} = params
Expand Down
10 changes: 10 additions & 0 deletions apps/channel_service/lib/channel_service/endpoint/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ defmodule ChannelService.Endpoint.Protocol do
{:noreply, socket}
end

def handle_info({:direction_changed, entity_type, entity_id, value}, socket) do
# Ignore the event if the target is ourself
if entity_id != socket.assigns.character_id do
attrs = %{entity_type: entity_type, entity_id: entity_id, direction: value}
Socket.send(socket, EntityViews.render(:dir, attrs))
end

{:noreply, socket}
end

def handle_info(msg, socket) do
Logger.warn("unhandled message: #{inspect(msg)}")
{:noreply, socket}
Expand Down
16 changes: 0 additions & 16 deletions apps/channel_service/lib/channel_service/entity_interactions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,6 @@ defmodule ChannelService.EntityInteractions do
Enum.each(players, &send_entity_leave_packets(&1, character))
end

@spec set_dir(Character.t(), EntityEnums.direction_type_keys()) ::
{:ok, new_char :: Character.t()} | {:error, atom}
def set_dir(%Character{} = character, new_dir) do
new_char = %Character{character | direction: new_dir}

case CharacterRegistry.write(new_char) do
{:ok, new_char} ->
render = EntityViews.render(:dir, %{entity: new_char})
broadcast_on_map(new_char, render, false)
{:ok, new_char}

{:error, _} = x ->
x
end
end

@spec say_to_map(Character.t(), String.t()) :: :ok
def say_to_map(%Character{} = character, message) do
broadcast_on_map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule ElvenPackets.Client.AreaPackets do
#######
@deserializable true
defpacket "dir", as: Dir do
field :dir, NsEnum, values: direction_type(:__enumerators__)
field :direction, NsEnum, values: direction_type(:__enumerators__)
field :entity_type, NsEnum, values: entity_type(:__enumerators__)
field :entity_id, NsInteger
end
Expand Down
8 changes: 3 additions & 5 deletions apps/elven_packets/lib/elven_packets/views/entity_views.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ defmodule ElvenPackets.Views.EntityViews do
end

def render(:dir, args) do
entity = required_param(args, :entity)

%Dir{
entity_type: GameService.entity_type(entity),
entity_id: GameService.entity_id(entity),
direction: entity.__struct__.direction(entity)
entity_type: required_param(args, :entity_type),
entity_id: required_param(args, :entity_id),
direction: required_param(args, :direction)
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule ElvenPackets.Client.AreaPacketsTest do
test "can be deserialized" do
params = "1 2 3"
assert %Dir{} = packet = Dir.deserialize("dir", params, %Socket{})
assert packet.dir == :east
assert packet.direction == :east
assert packet.entity_type == :npc
assert packet.entity_id == 3
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ defmodule ElvenPackets.Views.EntityViewsTest do

describe "dir" do
test "default serialization for players" do
args = %{entity: new_player()}
args = %{entity_type: :player, entity_id: 123, direction: :south}
packet = EntityViews.render(:dir, args)

assert %Dir{} = packet
assert packet.entity_type == :player
assert packet.entity_id == args.entity.id
assert packet.direction == :south
assert packet.entity_type == args.entity_type
assert packet.entity_id == args.entity_id
assert packet.direction == args.direction
end
end

Expand Down

0 comments on commit 922ad84

Please sign in to comment.