mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Abstract out channel queries
This commit is contained in:
parent
dddebf06ad
commit
a66bc2655b
@ -38,7 +38,7 @@ defmodule Chess.GameState do
|
||||
|> Board.search(%{"type" => "king", "colour" => colour})
|
||||
|> List.first
|
||||
|
||||
if king == nil do
|
||||
if is_nil(king) do
|
||||
raise "There is no #{colour} king!"
|
||||
end
|
||||
|
||||
|
||||
28
lib/chess/repo/queries.ex
Normal file
28
lib/chess/repo/queries.ex
Normal file
@ -0,0 +1,28 @@
|
||||
defmodule Chess.Repo.Queries do
|
||||
@moduledoc false
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias Chess.Repo
|
||||
alias Chess.Store.Game
|
||||
|
||||
def game_for_info(user_id, game_id) do
|
||||
user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload([:moves, :user, :opponent])
|
||||
|> Repo.get!(game_id)
|
||||
end
|
||||
|
||||
def game_with_moves(user_id, game_id) do
|
||||
user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload(:moves)
|
||||
|> Repo.get!(game_id)
|
||||
end
|
||||
|
||||
def game_for_user(user_id, game_id) do
|
||||
user_id
|
||||
|> Game.for_user_id()
|
||||
|> Repo.get!(game_id)
|
||||
end
|
||||
end
|
||||
@ -3,10 +3,11 @@ defmodule ChessWeb.GameChannel do
|
||||
|
||||
use ChessWeb, :channel
|
||||
|
||||
alias Chess.Store.Game
|
||||
alias Chess.Board
|
||||
alias Chess.Moves
|
||||
alias Chess.MoveList
|
||||
alias Chess.Moves
|
||||
alias Chess.Repo.Queries
|
||||
alias Chess.Store.Game
|
||||
|
||||
def join("game:" <> game_id, _params, socket) do
|
||||
send(self(), {:after_join, game_id})
|
||||
@ -17,11 +18,7 @@ defmodule ChessWeb.GameChannel do
|
||||
def handle_info({:after_join, game_id}, socket) do
|
||||
game =
|
||||
socket.assigns.current_user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload(:moves)
|
||||
|> preload(:user)
|
||||
|> preload(:opponent)
|
||||
|> Repo.get!(game_id)
|
||||
|> Queries.game_for_info(game_id)
|
||||
|
||||
payload = %{
|
||||
player: player(socket, game),
|
||||
@ -42,9 +39,7 @@ defmodule ChessWeb.GameChannel do
|
||||
move_params = convert_params(params)
|
||||
|
||||
socket.assigns.current_user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload(:moves)
|
||||
|> Repo.get!(socket.assigns.game_id)
|
||||
|> Queries.game_with_moves(socket.assigns.game_id)
|
||||
|> Moves.make_move(move_params)
|
||||
|> case do
|
||||
{:ok, _} ->
|
||||
@ -65,8 +60,7 @@ defmodule ChessWeb.GameChannel do
|
||||
) do
|
||||
game =
|
||||
socket.assigns.current_user_id
|
||||
|> Game.for_user_id()
|
||||
|> Repo.get!(socket.assigns.game_id)
|
||||
|> Queries.game_for_user(socket.assigns.game_id)
|
||||
|
||||
moves = Moves.available(game.board, {
|
||||
String.to_integer(file),
|
||||
@ -90,9 +84,7 @@ defmodule ChessWeb.GameChannel do
|
||||
def send_update(socket) do
|
||||
game =
|
||||
socket.assigns.current_user_id
|
||||
|> Game.for_user_id()
|
||||
|> preload(:moves)
|
||||
|> Repo.get!(socket.assigns.game_id)
|
||||
|> Queries.game_with_moves(socket.assigns.game_id)
|
||||
|
||||
payload = %{
|
||||
board: Board.transform(game.board),
|
||||
@ -100,6 +92,7 @@ defmodule ChessWeb.GameChannel do
|
||||
state: game.state,
|
||||
moves: MoveList.transform(game.moves),
|
||||
}
|
||||
|
||||
ChessWeb.Endpoint.broadcast("game:#{game.id}", "game:update", payload)
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user