diff --git a/assets/js/hooks/chessBoard.js b/assets/js/hooks/chessBoard.js index e6f1f45..83ad857 100644 --- a/assets/js/hooks/chessBoard.js +++ b/assets/js/hooks/chessBoard.js @@ -161,6 +161,8 @@ export const ChessBoard = { fen: this.chess.fen(), san: move.san, promotion: promotion || null, + draw: this.chess.isDraw(), + checkmate: this.chess.isCheckmate(), }; if (isTrainer) { @@ -204,6 +206,8 @@ export const ChessBoard = { this.pushEvent("opponent_reply_applied", { fen: this.chess.fen(), san: move.san, + draw: this.chess.isDraw(), + checkmate: this.chess.isCheckmate(), }); }, diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index 9139f50..a55cb01 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -40,10 +40,16 @@ defmodule ChesstrainerWeb.EndgameLive.Play do

Tablebase unavailable

{@blocked_reason}

<% @status == :solved -> %> -

Solved!

+

+ {if @endgame.result == :draw, do: "Draw held!", else: "Solved!"} +

<% @status == :failed -> %>

Failed

-

That move throws away the win.

+

+ {if @endgame.result == :draw, + do: "That move loses the draw.", + else: "That move throws away the win."} +

<%= if @best_move do %>

Best was: {@best_move}

<% end %> @@ -119,6 +125,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do def handle_event("endgame_move_played", params, socket) do %{"from" => from, "to" => to, "san" => san} = params promotion = params["promotion"] + draw = params["draw"] || false prev_fen = params["prev_fen"] || socket.assigns.fen fen_after = params["fen"] || socket.assigns.fen @@ -135,32 +142,40 @@ defmodule ChesstrainerWeb.EndgameLive.Play do put_flash(socket, :error, "That move is out of sequence. Press Reset to try again.")} true -> - grade_and_record(socket, from, to, promotion, san, fen_after) + grade_and_record(socket, from, to, promotion, san, fen_after, draw) end end - def handle_event("opponent_reply_applied", %{"fen" => fen}, socket) do - if socket.assigns.awaiting_reply do - case Tablebase.new(fen) do - {:ok, tablebase} -> - {:noreply, - socket - |> assign(:fen, fen) - |> assign(:tablebase, tablebase) - |> assign(:awaiting_reply, false) - |> assign(:status, :playing) - |> assign(:blocked_reason, nil) - |> push_event("resume_board", %{})} + def handle_event("opponent_reply_applied", params, socket) do + %{"fen" => fen} = params + draw = params["draw"] || false - other -> - {:noreply, - socket - |> assign(:awaiting_reply, false) - |> assign(:status, :blocked) - |> assign(:blocked_reason, blocked_reason(other))} - end - else - {:noreply, socket} + cond do + not socket.assigns.awaiting_reply -> + {:noreply, socket} + + draw and socket.assigns.endgame.result == :draw -> + solve_attempt(socket) + + true -> + case Tablebase.new(fen) do + {:ok, tablebase} -> + {:noreply, + socket + |> assign(:fen, fen) + |> assign(:tablebase, tablebase) + |> assign(:awaiting_reply, false) + |> assign(:status, :playing) + |> assign(:blocked_reason, nil) + |> push_event("resume_board", %{})} + + other -> + {:noreply, + socket + |> assign(:awaiting_reply, false) + |> assign(:status, :blocked) + |> assign(:blocked_reason, blocked_reason(other))} + end end end @@ -251,7 +266,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do "The tablebase is unavailable (#{inspect(reason)})." end - defp grade_and_record(socket, from, to, promotion, san, fen_after) do + defp grade_and_record(socket, from, to, promotion, san, fen_after, draw) do case find_move(socket.assigns.tablebase, from, to, promotion) do nil -> {:noreply, put_flash(socket, :error, "Could not identify that move in the tablebase.")} @@ -270,12 +285,18 @@ defmodule ChesstrainerWeb.EndgameLive.Play do grade == :losing -> fail_attempt(socket) - grade in [:optimal, :winning] and not is_nil(promotion) -> - # Conversion cutoff: a promotion that keeps the win ends the puzzle. + grade not in [:optimal, :winning] -> + {:noreply, socket} + + draw and socket.assigns.endgame.result == :draw -> + # A drawn-by-rule position is the goal of a draw puzzle. solve_attempt(socket) - grade in [:optimal, :winning] and - not same_position?(fen_after, socket.assigns.fen) -> + socket.assigns.endgame.result == :win and not is_nil(promotion) -> + # Conversion cutoff: a promotion that keeps the win ends a win puzzle. + solve_attempt(socket) + + not same_position?(fen_after, socket.assigns.fen) -> start_opponent_reply(socket, fen_after) true -> diff --git a/test/chesstrainer_web/live/endgame_live/play_test.exs b/test/chesstrainer_web/live/endgame_live/play_test.exs index cfb89d4..168b39a 100644 --- a/test/chesstrainer_web/live/endgame_live/play_test.exs +++ b/test/chesstrainer_web/live/endgame_live/play_test.exs @@ -39,6 +39,13 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do ] end + defp draw_moves do + [ + %Tablebase.Move{uci: "d1d7", san: "Qd7", category: :draw, dtz: nil}, + %Tablebase.Move{uci: "d1d4", san: "Qd4", category: :win, dtz: nil} + ] + end + defp play_move(view, endgame, from, to, san) do render_hook(view, "endgame_move_played", %{ "from" => from, @@ -294,6 +301,112 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do end end + describe "draw endgames" do + test "loads and is playable", %{conn: conn} do + endgame = endgame_fixture(result: :draw) + seed_tablebase(endgame.fen, category: :draw, moves: []) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + assert has_element?(view, "#endgame-board") + assert render(view) =~ "Make a move" + end + + test "passes a move that keeps the draw and replies", %{conn: conn} do + endgame = endgame_fixture(result: :draw) + fen_after = "8/8/8/8/8/8/8/8 b - - 0 1" + reply = %Tablebase.Move{uci: "e8e7", san: "Ke7", category: :draw, dtz: nil} + + seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) + seed_tablebase(fen_after, category: :draw, moves: [reply]) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + render_hook(view, "endgame_move_played", %{ + "from" => "d1", + "to" => "d7", + "san" => "Qd7", + "draw" => false, + "prev_fen" => endgame.fen, + "fen" => fen_after, + "endgame_id" => endgame.id + }) + + %{proxy: {ref, _topic, _}} = view + + assert_receive {^ref, {:push_event, "opponent_move", payload}}, 100 + assert payload == %{from: "e8", to: "e7", promotion: nil, san: "Ke7"} + end + + test "fails a move that loses the draw", %{conn: conn} do + endgame = endgame_fixture(result: :draw) + seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + render_hook(view, "endgame_move_played", %{ + "from" => "d1", + "to" => "d4", + "san" => "Qd4", + "draw" => false, + "prev_fen" => endgame.fen, + "fen" => endgame.fen, + "endgame_id" => endgame.id + }) + + html = render(view) + assert html =~ "Failed" + assert html =~ "loses the draw" + assert html =~ "Qd7" + end + + test "solves when the user's move draws by rule", %{conn: conn} do + endgame = endgame_fixture(result: :draw) + seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + render_hook(view, "endgame_move_played", %{ + "from" => "d1", + "to" => "d7", + "san" => "Qd7", + "draw" => true, + "prev_fen" => endgame.fen, + "fen" => endgame.fen, + "endgame_id" => endgame.id + }) + + assert render(view) =~ "Draw held!" + assert Endgames.get_endgame!(endgame.id).times_solved == 1 + end + + test "solves when the opponent's reply draws by rule", %{conn: conn} do + endgame = endgame_fixture(result: :draw) + fen_after = "8/8/8/8/8/8/8/8 b - - 0 1" + reply = %Tablebase.Move{uci: "e8e7", san: "Ke7", category: :draw, dtz: nil} + + seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) + seed_tablebase(fen_after, category: :draw, moves: [reply]) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + render_hook(view, "endgame_move_played", %{ + "from" => "d1", + "to" => "d7", + "san" => "Qd7", + "draw" => false, + "prev_fen" => endgame.fen, + "fen" => fen_after, + "endgame_id" => endgame.id + }) + + render_hook(view, "opponent_reply_applied", %{"fen" => fen_after, "draw" => true}) + + assert render(view) =~ "Draw held!" + assert Endgames.get_endgame!(endgame.id).times_solved == 1 + end + end + describe "reset" do test "clears the move list and grade, and re-pushes init_board", %{conn: conn} do endgame = endgame_fixture()