mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
#5: normalise the full lichess tablebase category vocabulary (cursed-win and blessed-loss classify as draws), return {:ok, tablebase} on a cache hit, and unwrap the tablebase in game_from_fen. #6: replace the LRU cache with an LFU cache storing {key, value, frequency, last_access}, evicting the least-frequently-used entry (LRU tie-break) and halving every frequency on a weekly sweep. Persistence moves to a versioned v2 dets file, dropping the legacy backup; the cache restores synchronously at startup. Also updates stale tags enum and Play LiveView tests, and drops the tracked legacy cache backup.
76 lines
2.1 KiB
Elixir
76 lines
2.1 KiB
Elixir
defmodule Chesstrainer.Endgames.Tablebase do
|
|
@moduledoc """
|
|
All endgame tablebase related functions.
|
|
"""
|
|
|
|
alias Chesstrainer.Endgames.Tablebase
|
|
|
|
@type result :: :win | :draw | :loss | :unknown
|
|
|
|
@type t :: %__MODULE__{
|
|
category: result,
|
|
checkmate: boolean(),
|
|
dtc: integer() | nil,
|
|
dtm: integer() | nil,
|
|
dtw: integer() | nil,
|
|
dtz: integer() | nil,
|
|
insufficient_material: boolean(),
|
|
moves: [Tablebase.Move.t()]
|
|
}
|
|
|
|
defstruct category: nil,
|
|
checkmate: false,
|
|
dtc: nil,
|
|
dtm: nil,
|
|
dtw: nil,
|
|
dtz: nil,
|
|
insufficient_material: false,
|
|
moves: nil
|
|
|
|
@doc """
|
|
Normalises a Lichess tablebase category string into a trainer result.
|
|
|
|
`cursed-win` and `blessed-loss` are wins/losses only when the 50-move rule is
|
|
ignored, so they are draws in play. Unrecognised values become `:unknown`
|
|
rather than minting arbitrary atoms.
|
|
"""
|
|
@spec normalize_category(term()) :: result()
|
|
def normalize_category(category) when category in ["win", "syzygy-win", "maybe-win"], do: :win
|
|
|
|
def normalize_category(category) when category in ["draw", "cursed-win", "blessed-loss"],
|
|
do: :draw
|
|
|
|
def normalize_category(category) when category in ["loss", "syzygy-loss", "maybe-loss"],
|
|
do: :loss
|
|
|
|
def normalize_category(_category), do: :unknown
|
|
|
|
@spec new(String.t()) ::
|
|
{:ok, t()}
|
|
| {:cooldown, non_neg_integer()}
|
|
| {:error, :invalid_endgame}
|
|
| {:error, term()}
|
|
def new(fen) do
|
|
case Tablebase.Cache.get(fen) do
|
|
{:ok, tablebase} ->
|
|
{:ok, tablebase}
|
|
|
|
_ ->
|
|
case Tablebase.Parser.from_fen(fen) do
|
|
{:error, reason} ->
|
|
{:error, reason}
|
|
|
|
{:cooldown, remaining_ms} ->
|
|
{:cooldown, remaining_ms}
|
|
|
|
{:ok, tablebase} when tablebase.category == :unknown ->
|
|
{:error, :invalid_endgame}
|
|
|
|
{:ok, tablebase} ->
|
|
Tablebase.Cache.put(fen, tablebase)
|
|
{:ok, tablebase}
|
|
end
|
|
end
|
|
end
|
|
end
|