mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
WIP: Show captured pieces
This commit is contained in:
parent
35f26d87df
commit
316ee3aa8c
@ -36,10 +36,11 @@ defmodule Chess.Repo.Queries do
|
|||||||
|> Repo.get!(game_id)
|
|> Repo.get!(game_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def moves_with_captures(game_id) do
|
def captures_for_colour(game, colour) do
|
||||||
Move
|
Move
|
||||||
|> Move.for_game_id(game_id)
|
|> Move.for_game_id(game.id)
|
||||||
|> Move.with_captures
|
|> Move.with_captures
|
||||||
|
|> Move.captures_for_colour(colour)
|
||||||
|> Repo.all
|
|> Repo.all
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -37,6 +37,11 @@ defmodule Chess.Store.Move do
|
|||||||
where: not is_nil(move.piece_captured)
|
where: not is_nil(move.piece_captured)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def captures_for_colour(query, colour) do
|
||||||
|
from move in query,
|
||||||
|
where: fragment("(piece_captured -> 'colour')::jsonb = ?", ^colour)
|
||||||
|
end
|
||||||
|
|
||||||
def transform(move) do
|
def transform(move) do
|
||||||
%{
|
%{
|
||||||
id: move.id,
|
id: move.id,
|
||||||
|
|||||||
@ -6,8 +6,8 @@ defmodule ChessWeb.GameChannelView do
|
|||||||
import ChessWeb.GameView, only: [player: 2, opponent: 2]
|
import ChessWeb.GameView, only: [player: 2, opponent: 2]
|
||||||
|
|
||||||
alias Chess.Board
|
alias Chess.Board
|
||||||
|
|
||||||
alias Chess.MoveList
|
alias Chess.MoveList
|
||||||
|
alias Chess.Repo.Queries
|
||||||
|
|
||||||
def after_join_payload(socket, game) do
|
def after_join_payload(socket, game) do
|
||||||
%{
|
%{
|
||||||
@ -19,6 +19,10 @@ defmodule ChessWeb.GameChannelView do
|
|||||||
turn: game.turn,
|
turn: game.turn,
|
||||||
state: game.state,
|
state: game.state,
|
||||||
moves: MoveList.transform(game.moves),
|
moves: MoveList.transform(game.moves),
|
||||||
|
graveyard: %{
|
||||||
|
white: Queries.captures_for_colour(game, "white"),
|
||||||
|
black: Queries.captures_for_colour(game, "black"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -28,6 +32,10 @@ defmodule ChessWeb.GameChannelView do
|
|||||||
turn: game.turn,
|
turn: game.turn,
|
||||||
state: game.state,
|
state: game.state,
|
||||||
moves: MoveList.transform(game.moves),
|
moves: MoveList.transform(game.moves),
|
||||||
|
graveyard: %{
|
||||||
|
white: Queries.captures_for_colour(game, "white"),
|
||||||
|
black: Queries.captures_for_colour(game, "black"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -145,6 +145,37 @@ defmodule Chess.Features.MovesTest do
|
|||||||
|> assert_has(square_containing("f4-r3", %{type: "pawn", colour: "white"}))
|
|> assert_has(square_containing("f4-r3", %{type: "pawn", colour: "white"}))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "captured pieces are displayed on the page", %{session: session} do
|
||||||
|
user = insert(:user, %{password: "mypassword"})
|
||||||
|
opponent = insert(:opponent)
|
||||||
|
|
||||||
|
insert(:game, %{
|
||||||
|
board: %{
|
||||||
|
"4,0" => %{"type" => "queen", "colour" => "white"},
|
||||||
|
"0,2" => %{"type" => "king", "colour" => "white"},
|
||||||
|
"4,7" => %{"type" => "pawn", "colour" => "black"},
|
||||||
|
"0,7" => %{"type" => "king", "colour" => "black"},
|
||||||
|
},
|
||||||
|
user_id: user.id,
|
||||||
|
opponent_id: opponent.id,
|
||||||
|
turn: "white",
|
||||||
|
})
|
||||||
|
|
||||||
|
session
|
||||||
|
|> login(user.email, "mypassword")
|
||||||
|
|> visit("/games")
|
||||||
|
|> click(link("Game with #{opponent.name}"))
|
||||||
|
|
||||||
|
refute_has(session, css("li", text: "Black Pawn"))
|
||||||
|
|
||||||
|
session
|
||||||
|
|> click(css("#f4-r0"))
|
||||||
|
|> click(css("#f4-r7"))
|
||||||
|
|> assert_has(square_containing("f4-r7", %{type: "queen", colour: "white"}))
|
||||||
|
|> refute_has(square_containing("f4-r0", %{type: "queen", colour: "white"}))
|
||||||
|
|> assert_has(css("li", text: "Black Pawn"))
|
||||||
|
end
|
||||||
|
|
||||||
test "cannot move the king into a position that would result in check",
|
test "cannot move the king into a position that would result in check",
|
||||||
%{session: session} do
|
%{session: session} do
|
||||||
user = insert(:user, %{
|
user = insert(:user, %{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user