backend
This commit is contained in:
60
backend/lib/backend_web/channels/game_channel.ex
Normal file
60
backend/lib/backend_web/channels/game_channel.ex
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule BackendWeb.GameChannel do
|
||||
@moduledoc """
|
||||
Channel for handling game events and player movements.
|
||||
"""
|
||||
use BackendWeb, :channel
|
||||
require Logger
|
||||
|
||||
@impl true
|
||||
def join("game:lobby", _payload, socket) do
|
||||
Logger.info("WebSocket connected to #{node()}, waiting for name")
|
||||
Phoenix.PubSub.subscribe(Backend.PubSub, "game_state")
|
||||
|
||||
current_state = Backend.GameState.get_state()
|
||||
{:ok, %{players: current_state}, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:game_state_updated, state}, socket) do
|
||||
push(socket, "game_state", %{players: state})
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_in("join_game", %{"name" => name}, socket) do
|
||||
Logger.info("Player '#{name}' joining game on #{node()}")
|
||||
|
||||
socket = assign(socket, :player_name, name)
|
||||
Backend.GameState.add_player(name)
|
||||
|
||||
{:reply, :ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_in("move", %{"directions" => directions}, socket) do
|
||||
case socket.assigns[:player_name] do
|
||||
nil ->
|
||||
Logger.warning("Move attempted without joining game")
|
||||
{:noreply, socket}
|
||||
|
||||
player_name ->
|
||||
Logger.debug("Player '#{player_name}' moved #{inspect(directions)} on #{node()}")
|
||||
Backend.GameState.move_player(player_name, directions)
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate(_reason, socket) do
|
||||
case socket.assigns[:player_name] do
|
||||
nil ->
|
||||
Logger.info("WebSocket disconnected from #{node()}")
|
||||
|
||||
player_name ->
|
||||
Logger.info("Player '#{player_name}' disconnected from #{node()}")
|
||||
Backend.GameState.remove_player(player_name)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
36
backend/lib/backend_web/channels/user_socket.ex
Normal file
36
backend/lib/backend_web/channels/user_socket.ex
Normal file
@@ -0,0 +1,36 @@
|
||||
defmodule BackendWeb.UserSocket do
|
||||
use Phoenix.Socket
|
||||
|
||||
# A Socket handler
|
||||
#
|
||||
# It's possible to control the websocket connection and
|
||||
# assign values that can be accessed by your channel topics.
|
||||
|
||||
## Channels
|
||||
channel("game:*", BackendWeb.GameChannel)
|
||||
|
||||
# Socket params are passed from the client and can
|
||||
# be used to verify and authenticate a user. After
|
||||
# verification, you can put default assigns into
|
||||
# the socket that will be set for all channels, ie
|
||||
#
|
||||
# {:ok, assign(socket, :user_id, verified_user_id)}
|
||||
#
|
||||
# To deny connection, return `:error` or `{:error, term}`.
|
||||
@impl true
|
||||
def connect(_params, socket, _connect_info) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
# Socket id's are topics that allow you to identify all sockets for a given user:
|
||||
#
|
||||
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
|
||||
#
|
||||
# Would allow you to broadcast a "disconnect" event and terminate
|
||||
# all active sockets and channels for a given user:
|
||||
#
|
||||
#
|
||||
# Returning `nil` makes this socket anonymous.
|
||||
@impl true
|
||||
def id(_socket), do: nil
|
||||
end
|
||||
Reference in New Issue
Block a user