mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Route tablebase lookups through a piece-count backend seam (#27)
Add a Backend behaviour, a Lichess implementation, and a Router that picks a backend by piece count: five or fewer to the local slot, six or seven to the remote slot, and eight or more short-circuited as not covered with no network call. Both slots point at Lichess for now; the local Syzygy backend replaces the local slot when it lands. Map :not_covered to the existing 'not covered by the tablebase' message.
This commit is contained in:
@@ -2,6 +2,9 @@ defmodule Chesstrainer.Endgames.Tablebase do
|
||||
@moduledoc """
|
||||
All endgame tablebase related functions.
|
||||
|
||||
Lookups are routed by piece count through `Tablebase.Router`, which picks the
|
||||
backend that can answer the position.
|
||||
|
||||
The cached value carries only what the trainer reads: `category`,
|
||||
`checkmate`, and `moves`. Cursed-win/blessed-loss are normalised to `:draw` at
|
||||
parse time, and distance fields (`dtm`/`dtc`/`dtw`) are not kept because the
|
||||
@@ -51,7 +54,7 @@ defmodule Chesstrainer.Endgames.Tablebase do
|
||||
{:ok, tablebase}
|
||||
|
||||
_ ->
|
||||
case Tablebase.Parser.from_fen(fen) do
|
||||
case Tablebase.Router.probe(fen) do
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
defmodule Chesstrainer.Endgames.Tablebase.Backend do
|
||||
@moduledoc """
|
||||
A backend answers a tablebase probe for one position.
|
||||
|
||||
The router chooses a backend by piece count, so the play flow never depends on
|
||||
which one answered. Every backend returns the same shape.
|
||||
"""
|
||||
|
||||
alias Chesstrainer.Endgames.Tablebase
|
||||
|
||||
@type result ::
|
||||
{:ok, Tablebase.t()}
|
||||
| {:cooldown, non_neg_integer()}
|
||||
| {:error, term()}
|
||||
|
||||
@callback probe(String.t()) :: result()
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
defmodule Chesstrainer.Endgames.Tablebase.Backend.Lichess do
|
||||
@moduledoc """
|
||||
The Lichess tablebase API backend (covers up to seven pieces).
|
||||
"""
|
||||
|
||||
@behaviour Chesstrainer.Endgames.Tablebase.Backend
|
||||
|
||||
alias Chesstrainer.Endgames.Tablebase.Parser
|
||||
|
||||
@impl true
|
||||
def probe(fen), do: Parser.from_fen(fen)
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
defmodule Chesstrainer.Endgames.Tablebase.Router do
|
||||
@moduledoc """
|
||||
Chooses a tablebase backend by piece count.
|
||||
|
||||
Positions with five or fewer pieces go to the local slot; six or seven go to
|
||||
the remote slot. The local slot currently points at Lichess until the local
|
||||
Syzygy backend lands, so this ships without changing today's behaviour.
|
||||
|
||||
Positions with eight or more pieces are beyond every tablebase and are
|
||||
rejected without a network call.
|
||||
"""
|
||||
|
||||
alias Chesstrainer.Endgames.Tablebase
|
||||
alias Chesstrainer.Endgames.Tablebase.Backend.Lichess
|
||||
alias Chesstrainer.FEN
|
||||
|
||||
@max_local_pieces 5
|
||||
@max_pieces 7
|
||||
|
||||
# The local Syzygy backend will replace Lichess here once it exists.
|
||||
@local_backend Lichess
|
||||
@remote_backend Lichess
|
||||
|
||||
@spec backend(String.t()) :: {:ok, module()} | {:error, :not_covered | :invalid_endgame}
|
||||
def backend(fen) do
|
||||
case FEN.piece_count(fen) do
|
||||
count when is_integer(count) and count <= @max_local_pieces -> {:ok, @local_backend}
|
||||
count when is_integer(count) and count <= @max_pieces -> {:ok, @remote_backend}
|
||||
count when is_integer(count) -> {:error, :not_covered}
|
||||
_ -> {:error, :invalid_endgame}
|
||||
end
|
||||
end
|
||||
|
||||
@spec probe(String.t()) ::
|
||||
{:ok, Tablebase.t()} | {:cooldown, non_neg_integer()} | {:error, term()}
|
||||
def probe(fen) do
|
||||
case backend(fen) do
|
||||
{:ok, backend} -> backend.probe(fen)
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -572,6 +572,10 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
||||
"This position is not covered by the tablebase."
|
||||
end
|
||||
|
||||
defp blocked_reason({:error, :not_covered}) do
|
||||
"This position is not covered by the tablebase."
|
||||
end
|
||||
|
||||
defp blocked_reason({:error, reason}) do
|
||||
"The tablebase is unavailable (#{inspect(reason)})."
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user