mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Clients subscribe to the game to get updates
This commit is contained in:
parent
bbc1838d7e
commit
f5c35de8d8
@ -14,6 +14,8 @@ defmodule ChessWeb.BoardLive do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def mount(_params, %{"user_id" => user_id, "game_id" => game_id}, socket) do
|
def mount(_params, %{"user_id" => user_id, "game_id" => game_id}, socket) do
|
||||||
|
ChessWeb.Endpoint.subscribe("game:#{game_id}")
|
||||||
|
|
||||||
user = Repo.get!(User, user_id)
|
user = Repo.get!(User, user_id)
|
||||||
|
|
||||||
game =
|
game =
|
||||||
@ -40,6 +42,10 @@ defmodule ChessWeb.BoardLive do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_info(%{event: "move", payload: state}, socket) do
|
||||||
|
{:noreply, assign(socket, state)}
|
||||||
|
end
|
||||||
|
|
||||||
defp handle_click(socket, file, rank) do
|
defp handle_click(socket, file, rank) do
|
||||||
game = socket.assigns[:game]
|
game = socket.assigns[:game]
|
||||||
board = game.board
|
board = game.board
|
||||||
@ -87,6 +93,8 @@ defmodule ChessWeb.BoardLive do
|
|||||||
{:ok, %{game: game}} ->
|
{:ok, %{game: game}} ->
|
||||||
board = Board.transform(game.board)
|
board = Board.transform(game.board)
|
||||||
|
|
||||||
|
broadcast_move(game, board)
|
||||||
|
|
||||||
[
|
[
|
||||||
{:selected, nil},
|
{:selected, nil},
|
||||||
{:available, []},
|
{:available, []},
|
||||||
@ -98,4 +106,13 @@ defmodule ChessWeb.BoardLive do
|
|||||||
[{:selected, nil}, {:available, []}]
|
[{:selected, nil}, {:available, []}]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp broadcast_move(game, board) do
|
||||||
|
ChessWeb.Endpoint.broadcast_from(
|
||||||
|
self(),
|
||||||
|
"game:#{game.id}",
|
||||||
|
"move",
|
||||||
|
%{game: game, board: board}
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user