Current section
Files
Jump to
Current section
Files
lib/soulless/protocol/packet.ex
defmodule Soulless.Protocol.Packet do
alias Soulless.Protocol.GamePacket
alias Soulless.Protocol.SpectatorRPC
alias Soulless.Protocol.SpectatorPacket
alias Soulless.Protocol.TourneyChat
@type cache :: %{non_neg_integer() => atom() | String.t()}
@spec parse(binary(), boolean(), boolean(), boolean(), cache()) ::
{:ok, {GamePacket.t(), cache()}}
| {:ok, {SpectatorPacket.t(), cache()}}
| {:ok, {SpectatorRPC.t(), cache()}}
| {:error, String.t()}
def parse(binary, is_binary, is_spectator, is_tourney, module_cache) do
case {is_binary, is_spectator, is_tourney} do
{true, false, false} -> GamePacket.parse(binary, module_cache)
{false, true, false} -> SpectatorRPC.parse(binary, module_cache)
{true, false, true} -> tourney_chat_packet_with_cache(binary, module_cache)
{true, true, false} -> spectator_packet_with_cache(binary, module_cache)
_ -> {:error, "unknown protocol"}
end
end
defp spectator_packet_with_cache(binary, module_cache) do
case SpectatorPacket.parse(binary) do
{:ok, packet} -> {:ok, {packet, module_cache}}
error -> error
end
end
defp tourney_chat_packet_with_cache(binary, module_cache) do
case TourneyChat.parse(binary) do
{:ok, packet} -> {:ok, {packet, module_cache}}
error -> error
end
end
end