mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f27e16af1d | ||
|
|
fddb94e2bd | ||
|
|
20b53dae6f |
@@ -41,6 +41,7 @@ h1 em { color: var(--color-coral); font-weight: 400; }
|
|||||||
.settings-subtitle {
|
.settings-subtitle {
|
||||||
font-size: 1rem;;
|
font-size: 1rem;;
|
||||||
color: var(--color-muted);
|
color: var(--color-muted);
|
||||||
|
margin-top: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 650px) {
|
@media (max-width: 650px) {
|
||||||
|
|||||||
@@ -0,0 +1,218 @@
|
|||||||
|
defmodule Chesstrainer.Endgames.BulkImport do
|
||||||
|
@moduledoc """
|
||||||
|
Classifies pasted FENs for the admin bulk importer and inserts the positions
|
||||||
|
the local Syzygy tablebase can answer.
|
||||||
|
|
||||||
|
Results always come from `Chesstrainer.Endgames.Tablebase.probe_local_result/1`,
|
||||||
|
so a bulk import never consults the remote (Lichess) backend.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import Ecto.Query, warn: false
|
||||||
|
|
||||||
|
alias Chesstrainer.Endgames.Endgame
|
||||||
|
alias Chesstrainer.Endgames.Tablebase
|
||||||
|
alias Chesstrainer.{FEN, Key, Repo}
|
||||||
|
|
||||||
|
@max_fens 1000
|
||||||
|
@max_pieces 5
|
||||||
|
@default_rating 1500.0
|
||||||
|
|
||||||
|
@type status ::
|
||||||
|
:ready
|
||||||
|
| :duplicate
|
||||||
|
| :invalid_fen
|
||||||
|
| :too_many_pieces
|
||||||
|
| :illegal
|
||||||
|
| :terminal
|
||||||
|
| :unsupported
|
||||||
|
|
||||||
|
@type entry :: %{
|
||||||
|
line: pos_integer(),
|
||||||
|
fen: String.t(),
|
||||||
|
key: String.t() | nil,
|
||||||
|
color: :white | :black | nil,
|
||||||
|
result: Tablebase.result() | nil,
|
||||||
|
status: status(),
|
||||||
|
reason: String.t() | nil
|
||||||
|
}
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
The most FENs a single import will consider.
|
||||||
|
"""
|
||||||
|
@spec max_fens() :: pos_integer()
|
||||||
|
def max_fens, do: @max_fens
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Parses one FEN per line, skipping blank lines and lines beginning with `#`.
|
||||||
|
|
||||||
|
Returns entries with a status explaining whether the line can be imported,
|
||||||
|
and `too_many?` when the input exceeded `max_fens/0` (only the first
|
||||||
|
`max_fens/0` lines are considered).
|
||||||
|
|
||||||
|
FENs already in the database and duplicates within the batch are reported as
|
||||||
|
duplicates without probing the tablebase.
|
||||||
|
"""
|
||||||
|
@spec preview(String.t()) :: %{entries: [entry()], too_many?: boolean()}
|
||||||
|
def preview(text) when is_binary(text) do
|
||||||
|
lines = parse_lines(text)
|
||||||
|
{lines, rest} = Enum.split(lines, @max_fens)
|
||||||
|
|
||||||
|
fens = Enum.map(lines, fn {_line, fen} -> FEN.normalize(fen) end)
|
||||||
|
existing = existing_fens(fens)
|
||||||
|
|
||||||
|
{entries, _seen} =
|
||||||
|
Enum.map_reduce(lines, %{}, fn {line, raw_fen}, seen ->
|
||||||
|
classify(line, FEN.normalize(raw_fen), seen, existing)
|
||||||
|
end)
|
||||||
|
|
||||||
|
%{entries: entries, too_many?: rest != []}
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Inserts every `:ready` entry, attaching `themes` and `rating` to each.
|
||||||
|
|
||||||
|
Rows that collide with an existing FEN are skipped. Returns the number
|
||||||
|
imported and the number of entries that were not ready or were skipped.
|
||||||
|
"""
|
||||||
|
@spec commit([entry()], [Chesstrainer.Themes.Theme.t()], number()) ::
|
||||||
|
%{imported: non_neg_integer(), skipped: non_neg_integer()}
|
||||||
|
def commit(entries, themes \\ [], rating \\ @default_rating) do
|
||||||
|
ready = Enum.filter(entries, &(&1.status == :ready))
|
||||||
|
now = DateTime.utc_now() |> DateTime.truncate(:second)
|
||||||
|
|
||||||
|
rows =
|
||||||
|
Enum.map(ready, fn entry ->
|
||||||
|
%{
|
||||||
|
id: Ecto.UUID.generate(),
|
||||||
|
public_code: Endgame.generate_public_code(),
|
||||||
|
fen: entry.fen,
|
||||||
|
color: entry.color,
|
||||||
|
key: entry.key,
|
||||||
|
result: entry.result,
|
||||||
|
rating: (rating || @default_rating) * 1.0,
|
||||||
|
rating_deviation: 350.0,
|
||||||
|
times_attempted: 0,
|
||||||
|
times_solved: 0,
|
||||||
|
inserted_at: now,
|
||||||
|
updated_at: now
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
|
||||||
|
{imported, inserted_rows} =
|
||||||
|
Repo.insert_all(Endgame, rows, on_conflict: :nothing, returning: [:id])
|
||||||
|
|
||||||
|
join_rows =
|
||||||
|
for %Endgame{id: id} <- inserted_rows, theme <- themes do
|
||||||
|
%{
|
||||||
|
endgame_id: Ecto.UUID.dump!(id),
|
||||||
|
theme_id: theme.id
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
if join_rows != [] do
|
||||||
|
Repo.insert_all("endgames_themes", join_rows, on_conflict: :nothing)
|
||||||
|
end
|
||||||
|
|
||||||
|
%{imported: imported, skipped: length(entries) - imported}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_lines(text) do
|
||||||
|
text
|
||||||
|
|> String.split(~r/\r?\n/)
|
||||||
|
|> Enum.with_index(1)
|
||||||
|
|> Enum.reduce([], fn {raw, index}, acc ->
|
||||||
|
trimmed = String.trim(raw)
|
||||||
|
|
||||||
|
if trimmed == "" or String.starts_with?(trimmed, "#") do
|
||||||
|
acc
|
||||||
|
else
|
||||||
|
[{index, trimmed} | acc]
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|> Enum.reverse()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp existing_fens(fens) do
|
||||||
|
fens
|
||||||
|
|> Enum.uniq()
|
||||||
|
|> then(fn fens -> Repo.all(from e in Endgame, where: e.fen in ^fens, select: e.fen) end)
|
||||||
|
|> Map.new(&{&1, true})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp classify(line, fen, seen, existing) do
|
||||||
|
cond do
|
||||||
|
Map.has_key?(seen, fen) ->
|
||||||
|
{entry(line, fen, :duplicate, "Duplicate FEN in this batch"), seen}
|
||||||
|
|
||||||
|
Map.has_key?(existing, fen) ->
|
||||||
|
{entry(line, fen, :duplicate, "FEN already exists"), seen}
|
||||||
|
|
||||||
|
true ->
|
||||||
|
{classify_new(line, fen), Map.put(seen, fen, true)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp classify_new(line, fen) do
|
||||||
|
issues = Endgame.fen_issues(fen)
|
||||||
|
pieces = FEN.piece_count(fen)
|
||||||
|
|
||||||
|
cond do
|
||||||
|
issues != [] ->
|
||||||
|
entry(line, fen, :invalid_fen, Enum.join(issues, "; "))
|
||||||
|
|
||||||
|
is_integer(pieces) and pieces > @max_pieces ->
|
||||||
|
entry(
|
||||||
|
line,
|
||||||
|
fen,
|
||||||
|
:too_many_pieces,
|
||||||
|
"Only positions with #{@max_pieces} or fewer pieces are supported"
|
||||||
|
)
|
||||||
|
|
||||||
|
true ->
|
||||||
|
classify_probe(line, fen)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp classify_probe(line, fen) do
|
||||||
|
case Tablebase.probe_local_result(fen) do
|
||||||
|
{:ok, result} ->
|
||||||
|
entry(line, fen, :ready, nil) |> Map.put(:result, result)
|
||||||
|
|
||||||
|
{:error, :terminal} ->
|
||||||
|
entry(line, fen, :terminal, "Checkmate, stalemate, or no legal moves")
|
||||||
|
|
||||||
|
{:error, :illegal} ->
|
||||||
|
entry(line, fen, :illegal, "The tablebase rejected this position")
|
||||||
|
|
||||||
|
{:error, :not_configured} ->
|
||||||
|
entry(
|
||||||
|
line,
|
||||||
|
fen,
|
||||||
|
:unsupported,
|
||||||
|
"Local tablebase is not configured (set TABLEBASE_PATH)"
|
||||||
|
)
|
||||||
|
|
||||||
|
{:error, :unsupported} ->
|
||||||
|
entry(line, fen, :unsupported, "The local tablebase cannot answer this position")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp entry(line, fen, status, reason) do
|
||||||
|
%{
|
||||||
|
line: line,
|
||||||
|
fen: fen,
|
||||||
|
key: key_for(fen),
|
||||||
|
color: FEN.color_from_fen(fen),
|
||||||
|
result: nil,
|
||||||
|
status: status,
|
||||||
|
reason: reason
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp key_for(fen) do
|
||||||
|
case Key.from_fen(fen) do
|
||||||
|
{:ok, key} -> key
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -90,10 +90,15 @@ defmodule Chesstrainer.Endgames.Endgame do
|
|||||||
|
|
||||||
defp normalize_attrs(attrs), do: attrs
|
defp normalize_attrs(attrs), do: attrs
|
||||||
|
|
||||||
# Strict structural validation. Mirrors chess.js rules so the FEN survives
|
@doc """
|
||||||
# both the server-side parser (Chex, permissive) and the client-side parser
|
Returns the structural problems with a FEN, or `[]` when it is well formed.
|
||||||
# (chess.js, strict).
|
|
||||||
defp fen_issues(fen) do
|
Strict structural validation. Mirrors chess.js rules so the FEN survives
|
||||||
|
both the server-side parser (Chex, permissive) and the client-side parser
|
||||||
|
(chess.js, strict). Used by the admin bulk importer to explain why a line
|
||||||
|
was rejected.
|
||||||
|
"""
|
||||||
|
def fen_issues(fen) do
|
||||||
case String.split(fen, " ") do
|
case String.split(fen, " ") do
|
||||||
[board, active, _castling, _en_passant, halfmove, fullmove] ->
|
[board, active, _castling, _en_passant, halfmove, fullmove] ->
|
||||||
board_valid? = valid_board?(board)
|
board_valid? = valid_board?(board)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ defmodule Chesstrainer.Endgames.Tablebase do
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
alias Chesstrainer.Endgames.Tablebase
|
alias Chesstrainer.Endgames.Tablebase
|
||||||
|
alias Chesstrainer.Endgames.Tablebase.Backend.Fathom, as: FathomBackend
|
||||||
alias Chesstrainer.FEN
|
alias Chesstrainer.FEN
|
||||||
|
|
||||||
@type result :: :win | :draw | :loss | :unknown
|
@type result :: :win | :draw | :loss | :unknown
|
||||||
@@ -67,6 +68,19 @@ defmodule Chesstrainer.Endgames.Tablebase do
|
|||||||
def flip(:draw), do: :draw
|
def flip(:draw), do: :draw
|
||||||
def flip(_outcome), do: :unknown
|
def flip(_outcome), do: :unknown
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Probes a FEN using only the local Syzygy tablebase and returns the result
|
||||||
|
from the side-to-move's perspective.
|
||||||
|
|
||||||
|
Unlike `new/1` this never falls back to the remote backend and never reads or
|
||||||
|
writes the cache. It is the primitive behind the admin bulk importer, so an
|
||||||
|
imported result always comes from our own tablebases.
|
||||||
|
"""
|
||||||
|
@spec probe_local_result(String.t()) ::
|
||||||
|
{:ok, result()}
|
||||||
|
| {:error, :not_configured | :illegal | :terminal | :unsupported}
|
||||||
|
def probe_local_result(fen), do: FathomBackend.probe_result_local(fen)
|
||||||
|
|
||||||
@spec new(String.t()) ::
|
@spec new(String.t()) ::
|
||||||
{:ok, t()}
|
{:ok, t()}
|
||||||
| {:cooldown, non_neg_integer()}
|
| {:cooldown, non_neg_integer()}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.Fathom do
|
|||||||
alias Chesstrainer.Endgames.Tablebase.Local
|
alias Chesstrainer.Endgames.Tablebase.Local
|
||||||
alias Chesstrainer.Endgames.Tablebase.Move
|
alias Chesstrainer.Endgames.Tablebase.Move
|
||||||
alias Chesstrainer.Endgames.Tablebase.San
|
alias Chesstrainer.Endgames.Tablebase.San
|
||||||
|
alias Chesstrainer.FEN
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def probe(fen) do
|
def probe(fen) do
|
||||||
@@ -32,6 +33,53 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.Fathom do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Probes the local tablebase without the remote fallback and returns only the
|
||||||
|
position's result.
|
||||||
|
|
||||||
|
Used by the admin bulk importer, which must never import a result from
|
||||||
|
Lichess. The result is from the side-to-move's perspective, matching the
|
||||||
|
`category` on `Chesstrainer.Endgames.Tablebase`.
|
||||||
|
|
||||||
|
Returns `{:error, :terminal}` for checkmate, stalemate, and positions with no
|
||||||
|
legal moves, because they cannot be played as puzzles.
|
||||||
|
"""
|
||||||
|
@spec probe_result_local(String.t()) ::
|
||||||
|
{:ok, Tablebase.result()}
|
||||||
|
| {:error, :not_configured | :illegal | :terminal | :unsupported}
|
||||||
|
def probe_result_local(fen) do
|
||||||
|
with {:ok, largest} <- Local.check(),
|
||||||
|
pieces when is_integer(pieces) <- FEN.piece_count(fen),
|
||||||
|
true <- pieces <= largest || {:error, :unsupported},
|
||||||
|
{:ok, {root, moves}} <- Nif.probe(fen) do
|
||||||
|
case root do
|
||||||
|
:failed ->
|
||||||
|
{:error, :illegal}
|
||||||
|
|
||||||
|
:checkmate ->
|
||||||
|
{:error, :terminal}
|
||||||
|
|
||||||
|
:stalemate ->
|
||||||
|
{:error, :terminal}
|
||||||
|
|
||||||
|
{wdl, _from, _to, _promotes, _ep, _dtz} ->
|
||||||
|
if moves == [] do
|
||||||
|
{:error, :terminal}
|
||||||
|
else
|
||||||
|
case Tablebase.normalize_wdl(wdl) do
|
||||||
|
:unknown -> {:error, :unsupported}
|
||||||
|
category -> {:ok, category}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
{:error, :not_configured} = error -> error
|
||||||
|
{:error, :unsupported} = error -> error
|
||||||
|
{:error, _reason} -> {:error, :unsupported}
|
||||||
|
nil -> {:error, :unsupported}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp fallback(fen) do
|
defp fallback(fen) do
|
||||||
(Application.get_env(:chesstrainer, :tablebase_fallback) || Lichess).probe(fen)
|
(Application.get_env(:chesstrainer, :tablebase_fallback) || Lichess).probe(fen)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ defmodule Chesstrainer.Themes.Theme do
|
|||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
@type t :: %__MODULE__{}
|
||||||
|
|
||||||
schema "themes" do
|
schema "themes" do
|
||||||
field :name, :string
|
field :name, :string
|
||||||
field :category, Ecto.Enum, values: [endgame: "endgame", opening: "opening", tactic: "tactic"]
|
field :category, Ecto.Enum, values: [endgame: "endgame", opening: "opening", tactic: "tactic"]
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ defmodule ChesstrainerWeb.CoreComponents do
|
|||||||
attr :id, :string, doc: "the optional id of flash container"
|
attr :id, :string, doc: "the optional id of flash container"
|
||||||
attr :flash, :map, default: %{}, doc: "the map of flash messages to display"
|
attr :flash, :map, default: %{}, doc: "the map of flash messages to display"
|
||||||
attr :title, :string, default: nil
|
attr :title, :string, default: nil
|
||||||
attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup"
|
attr :kind, :atom, values: [:info, :success, :error], doc: "used for styling and flash lookup"
|
||||||
attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container"
|
attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container"
|
||||||
|
|
||||||
slot :inner_block, doc: "the optional inner block that renders the flash message"
|
slot :inner_block, doc: "the optional inner block that renders the flash message"
|
||||||
@@ -86,9 +86,11 @@ defmodule ChesstrainerWeb.CoreComponents do
|
|||||||
<div class={[
|
<div class={[
|
||||||
"flex w-full items-start gap-3 rounded-lg border px-4 py-3 text-sm text-wrap shadow-lg",
|
"flex w-full items-start gap-3 rounded-lg border px-4 py-3 text-sm text-wrap shadow-lg",
|
||||||
@kind == :info && "border-info bg-info text-info-content",
|
@kind == :info && "border-info bg-info text-info-content",
|
||||||
|
@kind == :success && "border-accent bg-accent text-accent-content",
|
||||||
@kind == :error && "border-error bg-error text-error-content"
|
@kind == :error && "border-error bg-error text-error-content"
|
||||||
]}>
|
]}>
|
||||||
<.icon :if={@kind == :info} name="hero-information-circle" class="size-5 shrink-0" />
|
<.icon :if={@kind == :info} name="hero-information-circle" class="size-5 shrink-0" />
|
||||||
|
<.icon :if={@kind == :success} name="hero-check-circle" class="size-5 shrink-0" />
|
||||||
<.icon :if={@kind == :error} name="hero-exclamation-circle" class="size-5 shrink-0" />
|
<.icon :if={@kind == :error} name="hero-exclamation-circle" class="size-5 shrink-0" />
|
||||||
<div>
|
<div>
|
||||||
<p :if={@title} class="font-semibold">{@title}</p>
|
<p :if={@title} class="font-semibold">{@title}</p>
|
||||||
@@ -356,10 +358,10 @@ defmodule ChesstrainerWeb.CoreComponents do
|
|||||||
~H"""
|
~H"""
|
||||||
<header class={[@actions != [] && "flex items-center justify-between gap-6", "pb-4"]}>
|
<header class={[@actions != [] && "flex items-center justify-between gap-6", "pb-4"]}>
|
||||||
<div>
|
<div>
|
||||||
<h1 class="text-lg font-semibold leading-8">
|
<h1 class="settings-header">
|
||||||
{render_slot(@inner_block)}
|
{render_slot(@inner_block)}
|
||||||
</h1>
|
</h1>
|
||||||
<p :if={@subtitle != []} class="text-sm text-base-content/70 mt-4">
|
<p :if={@subtitle != []} class="settings-subtitle">
|
||||||
{render_slot(@subtitle)}
|
{render_slot(@subtitle)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -247,9 +247,10 @@ defmodule ChesstrainerWeb.Layouts do
|
|||||||
<div
|
<div
|
||||||
id={@id}
|
id={@id}
|
||||||
aria-live="polite"
|
aria-live="polite"
|
||||||
class="pointer-events-none fixed right-4 top-4 z-50 flex w-80 max-w-80 flex-col gap-2 sm:w-96 sm:max-w-96"
|
class="pointer-events-none fixed left-4 top-4 z-50 flex w-80 max-w-80 flex-col gap-2 sm:w-96 sm:max-w-96"
|
||||||
>
|
>
|
||||||
<.flash kind={:info} flash={@flash} />
|
<.flash kind={:info} flash={@flash} />
|
||||||
|
<.flash kind={:success} flash={@flash} />
|
||||||
<.flash kind={:error} flash={@flash} />
|
<.flash kind={:error} flash={@flash} />
|
||||||
|
|
||||||
<.flash
|
<.flash
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ defmodule ChesstrainerWeb.PlayComponents do
|
|||||||
attr :move_input_error, :string, default: nil
|
attr :move_input_error, :string, default: nil
|
||||||
attr :last_check, :boolean, default: false
|
attr :last_check, :boolean, default: false
|
||||||
attr :last_checkmate, :boolean, default: false
|
attr :last_checkmate, :boolean, default: false
|
||||||
|
attr :rating, :any, default: nil
|
||||||
|
attr :rating_delta, :any, default: nil
|
||||||
|
|
||||||
def play_surface(assigns) do
|
def play_surface(assigns) do
|
||||||
assigns =
|
assigns =
|
||||||
@@ -250,9 +252,17 @@ defmodule ChesstrainerWeb.PlayComponents do
|
|||||||
<p class="text-muted font-bold">Make a move.</p>
|
<p class="text-muted font-bold">Make a move.</p>
|
||||||
<p class="text-muted">You play {colour_name(@endgame.color)}</p>
|
<p class="text-muted">You play {colour_name(@endgame.color)}</p>
|
||||||
<% end %>
|
<% end %>
|
||||||
<p :if={@status in [:solved, :failed]} class="mt-1 text-xs text-muted">
|
<div
|
||||||
Puzzle rating: {round(@endgame.rating)}
|
:if={@status in [:solved, :failed]}
|
||||||
</p>
|
class="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs"
|
||||||
|
>
|
||||||
|
<span class="text-muted">Puzzle rating: {round(@endgame.rating)}</span>
|
||||||
|
<span :if={@rating} id="your-rating" class="flex items-center gap-1.5 text-muted">
|
||||||
|
Your rating
|
||||||
|
<span class="font-mono font-semibold text-base-content">{round(@rating)}</span>
|
||||||
|
<.rating_change :if={@rating_delta} id="rating-change" delta={@rating_delta} />
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ defmodule ChesstrainerWeb.RatingComponents do
|
|||||||
|
|
||||||
use Phoenix.Component
|
use Phoenix.Component
|
||||||
|
|
||||||
|
import ChesstrainerWeb.CoreComponents, only: [icon: 1]
|
||||||
|
|
||||||
# Glicko ratings are conventionally shown on a 0–3000 scale. The default
|
# Glicko ratings are conventionally shown on a 0–3000 scale. The default
|
||||||
# rating is 1500 and the deviation is capped at 350, so 500–3000 keeps the
|
# rating is 1500 and the deviation is capped at 350, so 500–3000 keeps the
|
||||||
# useful range readable without letting a stray value push the marker off.
|
# useful range readable without letting a stray value push the marker off.
|
||||||
@@ -78,6 +80,56 @@ defmodule ChesstrainerWeb.RatingComponents do
|
|||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Renders a signed rating change as a compact, colour-coded pill.
|
||||||
|
|
||||||
|
A gain is green with an upward trend, a loss is red with a downward trend,
|
||||||
|
and no change is neutral.
|
||||||
|
"""
|
||||||
|
attr :delta, :any, required: true
|
||||||
|
attr :id, :string, default: nil
|
||||||
|
attr :class, :any, default: nil
|
||||||
|
|
||||||
|
def rating_change(assigns) do
|
||||||
|
~H"""
|
||||||
|
<span
|
||||||
|
id={@id}
|
||||||
|
class={[
|
||||||
|
"inline-flex items-center gap-0.5 rounded-full px-1.5 py-0.5 font-mono text-xs font-semibold",
|
||||||
|
change_class(@delta),
|
||||||
|
@class
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<.icon name={change_icon(@delta)} class="size-3.5" />
|
||||||
|
{format_delta(@delta)}
|
||||||
|
</span>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Formats a rating change with an explicit sign, e.g. `+12`, `-8` or `0`."
|
||||||
|
def format_delta(nil), do: "—"
|
||||||
|
|
||||||
|
def format_delta(delta) when is_number(delta) do
|
||||||
|
rounded = round(delta)
|
||||||
|
|
||||||
|
cond do
|
||||||
|
rounded > 0 -> "+#{rounded}"
|
||||||
|
true -> Integer.to_string(rounded)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp change_class(delta) when is_number(delta) and delta > 0,
|
||||||
|
do: "bg-success/15 text-success-text"
|
||||||
|
|
||||||
|
defp change_class(delta) when is_number(delta) and delta < 0,
|
||||||
|
do: "bg-error/15 text-error-text"
|
||||||
|
|
||||||
|
defp change_class(_delta), do: "bg-base-200 text-muted"
|
||||||
|
|
||||||
|
defp change_icon(delta) when is_number(delta) and delta > 0, do: "hero-arrow-trending-up"
|
||||||
|
defp change_icon(delta) when is_number(delta) and delta < 0, do: "hero-arrow-trending-down"
|
||||||
|
defp change_icon(_delta), do: "hero-minus"
|
||||||
|
|
||||||
# Position on the gauge as a percentage, clamped to the drawn scale.
|
# Position on the gauge as a percentage, clamped to the drawn scale.
|
||||||
defp pct(value) do
|
defp pct(value) do
|
||||||
clamped = value |> max(@scale_min) |> min(@scale_max)
|
clamped = value |> max(@scale_min) |> min(@scale_max)
|
||||||
|
|||||||
@@ -0,0 +1,131 @@
|
|||||||
|
defmodule ChesstrainerWeb.ThemePicker do
|
||||||
|
@moduledoc """
|
||||||
|
Search-and-select control shared by the endgame form and bulk importer.
|
||||||
|
|
||||||
|
The parent LiveView owns the assignments and handles the events:
|
||||||
|
`search_themes`, `add_theme`, `create_theme`, `create_theme_from_search` and
|
||||||
|
`remove_theme`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
use ChesstrainerWeb, :html
|
||||||
|
|
||||||
|
alias Chesstrainer.Themes
|
||||||
|
|
||||||
|
attr :selected_themes, :list, required: true
|
||||||
|
attr :theme_query, :string, required: true
|
||||||
|
attr :theme_results, :list, required: true
|
||||||
|
attr :can_create, :boolean, required: true
|
||||||
|
|
||||||
|
def theme_picker(assigns) do
|
||||||
|
~H"""
|
||||||
|
<div class="mb-6">
|
||||||
|
<span class="mb-1 block text-sm font-medium text-base-content">Themes</span>
|
||||||
|
|
||||||
|
<div class="flex flex-wrap gap-2 mb-2">
|
||||||
|
<.badge :for={theme <- @selected_themes} color="primary">
|
||||||
|
{theme.name}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
phx-click="remove_theme"
|
||||||
|
phx-value-id={theme.id}
|
||||||
|
aria-label={"Remove #{theme.name}"}
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</.badge>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form id="theme-form" phx-change="search_themes" phx-submit="create_theme_from_search">
|
||||||
|
<.input
|
||||||
|
name="theme_search"
|
||||||
|
value={@theme_query}
|
||||||
|
phx-debounce="200"
|
||||||
|
autocomplete="off"
|
||||||
|
placeholder="Search or create a theme…"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li :for={theme <- @theme_results}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
phx-click="add_theme"
|
||||||
|
phx-value-id={theme.id}
|
||||||
|
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
|
||||||
|
>
|
||||||
|
{theme.name}
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<li :if={@can_create}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
phx-click="create_theme"
|
||||||
|
phx-value-name={@theme_query}
|
||||||
|
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
|
||||||
|
>
|
||||||
|
Create theme "{String.trim(@theme_query)}"
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Updates the search query, matching endgame themes and whether the query can be
|
||||||
|
created as a new theme.
|
||||||
|
"""
|
||||||
|
def assign_search(socket, query) do
|
||||||
|
trimmed = String.trim(query)
|
||||||
|
results = Themes.search(query, "endgame")
|
||||||
|
|
||||||
|
can_create =
|
||||||
|
trimmed != "" and
|
||||||
|
not Enum.any?(results, &(String.downcase(&1.name) == String.downcase(trimmed)))
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(:theme_query, query)
|
||||||
|
|> assign(:theme_results, results)
|
||||||
|
|> assign(:can_create, can_create)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Creates a theme from the search box (or selects the existing match) and adds
|
||||||
|
it to the selected themes.
|
||||||
|
"""
|
||||||
|
def create_theme(socket, name) do
|
||||||
|
if String.trim(name) == "" do
|
||||||
|
socket
|
||||||
|
else
|
||||||
|
case Themes.get_or_create(name, "endgame") do
|
||||||
|
{:ok, theme} ->
|
||||||
|
socket |> add_selected_theme(theme) |> clear_search()
|
||||||
|
|
||||||
|
{:error, _reason} ->
|
||||||
|
Phoenix.LiveView.put_flash(socket, :error, "Could not create that theme.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Clears the theme search results.
|
||||||
|
"""
|
||||||
|
def clear_search(socket) do
|
||||||
|
socket
|
||||||
|
|> assign(:theme_query, "")
|
||||||
|
|> assign(:theme_results, [])
|
||||||
|
|> assign(:can_create, false)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Adds a theme to the selected themes, ignoring duplicates.
|
||||||
|
"""
|
||||||
|
def add_selected_theme(socket, theme) do
|
||||||
|
if Enum.any?(socket.assigns.selected_themes, &(&1.id == theme.id)) do
|
||||||
|
socket
|
||||||
|
else
|
||||||
|
assign(socket, :selected_themes, socket.assigns.selected_themes ++ [theme])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -8,6 +8,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
|||||||
alias Chesstrainer.Endgames.Endgame
|
alias Chesstrainer.Endgames.Endgame
|
||||||
|
|
||||||
import ChesstrainerWeb.Board.BoardComponent
|
import ChesstrainerWeb.Board.BoardComponent
|
||||||
|
import ChesstrainerWeb.ThemePicker
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
@@ -20,61 +21,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
|||||||
<:subtitle>Use this form to manage endgame records in your database.</:subtitle>
|
<:subtitle>Use this form to manage endgame records in your database.</:subtitle>
|
||||||
</.header>
|
</.header>
|
||||||
|
|
||||||
<div class="mb-6">
|
<.theme_picker
|
||||||
<span class="mb-1 block text-sm font-medium text-base-content">Themes</span>
|
selected_themes={@selected_themes}
|
||||||
|
theme_query={@theme_query}
|
||||||
<div class="flex flex-wrap gap-2 mb-2">
|
theme_results={@theme_results}
|
||||||
<.badge :for={theme <- @selected_themes} color="primary">
|
can_create={@can_create}
|
||||||
{theme.name}
|
/>
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
phx-click="remove_theme"
|
|
||||||
phx-value-id={theme.id}
|
|
||||||
aria-label={"Remove #{theme.name}"}
|
|
||||||
>
|
|
||||||
×
|
|
||||||
</button>
|
|
||||||
</.badge>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<form
|
|
||||||
id="theme-form"
|
|
||||||
phx-change="search_themes"
|
|
||||||
phx-submit="create_theme_from_search"
|
|
||||||
>
|
|
||||||
<.input
|
|
||||||
name="theme_search"
|
|
||||||
value={@theme_query}
|
|
||||||
phx-debounce="200"
|
|
||||||
autocomplete="off"
|
|
||||||
placeholder="Search or create a theme…"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li :for={theme <- @theme_results}>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
phx-click="add_theme"
|
|
||||||
phx-value-id={theme.id}
|
|
||||||
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
|
|
||||||
>
|
|
||||||
{theme.name}
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
|
|
||||||
<li :if={@can_create}>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
phx-click="create_theme"
|
|
||||||
phx-value-name={@theme_query}
|
|
||||||
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
|
|
||||||
>
|
|
||||||
Create theme "{String.trim(@theme_query)}"
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save">
|
<.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save">
|
||||||
<div class="flex flex-col gap-6 sm:flex-row sm:items-start">
|
<div class="flex flex-col gap-6 sm:flex-row sm:items-start">
|
||||||
@@ -87,6 +39,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
|||||||
value={rating_display(@form[:rating])}
|
value={rating_display(@form[:rating])}
|
||||||
label="Rating"
|
label="Rating"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<.input field={@form[:message]} type="textarea" label="Message" />
|
<.input field={@form[:message]} type="textarea" label="Message" />
|
||||||
<.input field={@form[:notes]} type="textarea" label="Notes" />
|
<.input field={@form[:notes]} type="textarea" label="Notes" />
|
||||||
<.input
|
<.input
|
||||||
@@ -231,46 +184,6 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp assign_search(socket, query) do
|
|
||||||
trimmed = String.trim(query)
|
|
||||||
results = Themes.search(query, "endgame")
|
|
||||||
|
|
||||||
can_create =
|
|
||||||
trimmed != "" and
|
|
||||||
not Enum.any?(results, &(String.downcase(&1.name) == String.downcase(trimmed)))
|
|
||||||
|
|
||||||
socket
|
|
||||||
|> assign(:theme_query, query)
|
|
||||||
|> assign(:theme_results, results)
|
|
||||||
|> assign(:can_create, can_create)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp create_theme(socket, name) do
|
|
||||||
if String.trim(name) == "" do
|
|
||||||
socket
|
|
||||||
else
|
|
||||||
case Themes.get_or_create(name, "endgame") do
|
|
||||||
{:ok, theme} -> socket |> add_selected_theme(theme) |> clear_search()
|
|
||||||
{:error, _reason} -> put_flash(socket, :error, "Could not create that theme.")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp clear_search(socket) do
|
|
||||||
socket
|
|
||||||
|> assign(:theme_query, "")
|
|
||||||
|> assign(:theme_results, [])
|
|
||||||
|> assign(:can_create, false)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp add_selected_theme(socket, theme) do
|
|
||||||
if Enum.any?(socket.assigns.selected_themes, &(&1.id == theme.id)) do
|
|
||||||
socket
|
|
||||||
else
|
|
||||||
assign(socket, :selected_themes, socket.assigns.selected_themes ++ [theme])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Ratings are stored as floats (Glicko); the admin form presents them as whole numbers.
|
# Ratings are stored as floats (Glicko); the admin form presents them as whole numbers.
|
||||||
defp rating_display(%Phoenix.HTML.FormField{value: value}) when is_float(value),
|
defp rating_display(%Phoenix.HTML.FormField{value: value}) when is_float(value),
|
||||||
do: round(value)
|
do: round(value)
|
||||||
@@ -280,13 +193,8 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
|
|||||||
# Shows the position as the admin types a FEN; hidden while the FEN is invalid or blank.
|
# Shows the position as the admin types a FEN; hidden while the FEN is invalid or blank.
|
||||||
# Parses directly rather than through Game.game_from_fen/2 so keystrokes never probe a tablebase.
|
# Parses directly rather than through Game.game_from_fen/2 so keystrokes never probe a tablebase.
|
||||||
defp preview_game(fen) when is_binary(fen) do
|
defp preview_game(fen) when is_binary(fen) do
|
||||||
case Chex.Parser.FEN.parse(fen) do
|
{:ok, game} = Chex.Parser.FEN.parse(fen)
|
||||||
{:ok, game} ->
|
%{board: game.board, orientation: game.active_color, move_from_square: nil}
|
||||||
%{board: game.board, orientation: game.active_color, move_from_square: nil}
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
rescue
|
rescue
|
||||||
MatchError -> nil
|
MatchError -> nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,333 @@
|
|||||||
|
defmodule ChesstrainerWeb.EndgameLive.Import do
|
||||||
|
@moduledoc """
|
||||||
|
Bulk FEN importer for the admin endgame section.
|
||||||
|
|
||||||
|
Pasted FENs are validated and probed against the local Syzygy tablebase only;
|
||||||
|
the remote (Lichess) fallback is deliberately not used here.
|
||||||
|
"""
|
||||||
|
|
||||||
|
use ChesstrainerWeb, :live_view
|
||||||
|
|
||||||
|
alias Chesstrainer.Endgames.BulkImport
|
||||||
|
alias Chesstrainer.Endgames.Tablebase.Local
|
||||||
|
|
||||||
|
import ChesstrainerWeb.ThemePicker
|
||||||
|
|
||||||
|
@default_rating 1500
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<Layouts.app flash={@flash} wide>
|
||||||
|
<Layouts.admin_nav current_scope={@current_scope} />
|
||||||
|
|
||||||
|
<.header>
|
||||||
|
Import endgames
|
||||||
|
<:subtitle>
|
||||||
|
Paste one FEN per line. Results are read from the local Syzygy tablebase,
|
||||||
|
so positions must have five pieces or fewer.
|
||||||
|
</:subtitle>
|
||||||
|
<:actions>
|
||||||
|
<.button navigate={~p"/admin/endgames"}>
|
||||||
|
<.icon name="hero-arrow-left" /> All endgames
|
||||||
|
</.button>
|
||||||
|
</:actions>
|
||||||
|
</.header>
|
||||||
|
|
||||||
|
<p
|
||||||
|
id="tablebase-status"
|
||||||
|
class={[
|
||||||
|
"mb-6 rounded-md border px-4 py-3 text-sm",
|
||||||
|
match?({:ok, _}, @tablebase_status) && "border-success/30 bg-success/10 text-success-text",
|
||||||
|
match?({:error, _}, @tablebase_status) &&
|
||||||
|
"border-warning/30 bg-warning/10 text-warning-text"
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{elem(@tablebase_status, 1)}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<form id="endgame-import-form" phx-submit="lookup" class="space-y-4">
|
||||||
|
<.input
|
||||||
|
id="import-fens"
|
||||||
|
name="fens"
|
||||||
|
type="textarea"
|
||||||
|
label="FENs"
|
||||||
|
value={@fen_text}
|
||||||
|
rows="10"
|
||||||
|
class="font-mono"
|
||||||
|
placeholder="8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
|
||||||
|
autocomplete="off"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div class="sm:w-56">
|
||||||
|
<.input
|
||||||
|
id="import-rating"
|
||||||
|
name="rating"
|
||||||
|
type="number"
|
||||||
|
label="Rating for all imported endgames"
|
||||||
|
value={@rating}
|
||||||
|
min="1"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<.button type="submit" variant="primary" phx-disable-with="Looking up…">
|
||||||
|
Look up results
|
||||||
|
</.button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<.theme_picker
|
||||||
|
selected_themes={@selected_themes}
|
||||||
|
theme_query={@theme_query}
|
||||||
|
theme_results={@theme_results}
|
||||||
|
can_create={@can_create}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<section :if={@entries != nil} id="import-preview" class="mt-10">
|
||||||
|
<div class="mb-4 flex flex-wrap items-center justify-between gap-3">
|
||||||
|
<div class="text-sm text-base-content/70">
|
||||||
|
{summary(@entries)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<.button type="button" phx-click="toggle_all" phx-value-selected="true">
|
||||||
|
Select ready
|
||||||
|
</.button>
|
||||||
|
<.button type="button" phx-click="toggle_all" phx-value-selected="false">
|
||||||
|
Clear
|
||||||
|
</.button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p
|
||||||
|
:if={@too_many?}
|
||||||
|
class="mb-4 rounded-md border border-warning/30 bg-warning/10 px-4 py-3 text-sm text-warning-text"
|
||||||
|
>
|
||||||
|
Only the first {BulkImport.max_fens()} FENs were considered.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<table class={table_class()}>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th class="w-10"><span class="sr-only">Include</span></th>
|
||||||
|
<th class="w-12">Line</th>
|
||||||
|
<th>FEN</th>
|
||||||
|
<th>Key</th>
|
||||||
|
<th>Color</th>
|
||||||
|
<th>Result</th>
|
||||||
|
<th>Status</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr :for={entry <- @entries} id={"import-row-#{entry.line}"}>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
id={"import-include-#{entry.line}"}
|
||||||
|
checked={entry.selected}
|
||||||
|
disabled={entry.status != :ready}
|
||||||
|
phx-click="toggle_entry"
|
||||||
|
phx-value-line={entry.line}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td>{entry.line}</td>
|
||||||
|
<td class="break-all font-mono text-xs">{entry.fen}</td>
|
||||||
|
<td>{entry.key || "—"}</td>
|
||||||
|
<td>{entry.color || "—"}</td>
|
||||||
|
<td>{entry.result || "—"}</td>
|
||||||
|
<td>
|
||||||
|
<.badge color={status_color(entry.status)}>{status_label(entry.status)}</.badge>
|
||||||
|
<span :if={entry.reason} class="mt-1 block text-xs text-base-content/60">
|
||||||
|
{entry.reason}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<footer class="mt-6 flex items-center gap-3">
|
||||||
|
<.button
|
||||||
|
type="button"
|
||||||
|
variant="primary"
|
||||||
|
phx-click="import"
|
||||||
|
disabled={selected_count(@entries) == 0}
|
||||||
|
phx-disable-with="Importing…"
|
||||||
|
>
|
||||||
|
Import {selected_count(@entries)} endgame(s)
|
||||||
|
</.button>
|
||||||
|
|
||||||
|
<span class="text-sm text-base-content/70">
|
||||||
|
{selected_count(@entries)} of {count(@entries, :ready)} ready line(s) selected.
|
||||||
|
</span>
|
||||||
|
</footer>
|
||||||
|
</section>
|
||||||
|
</Layouts.app>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def mount(_params, _session, socket) do
|
||||||
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> assign(:page_title, "Import endgames")
|
||||||
|
|> assign(:tablebase_status, tablebase_status())
|
||||||
|
|> assign(:fen_text, "")
|
||||||
|
|> assign(:rating, @default_rating)
|
||||||
|
|> assign(:entries, nil)
|
||||||
|
|> assign(:too_many?, false)
|
||||||
|
|> assign(:selected_themes, [])
|
||||||
|
|> assign(:theme_query, "")
|
||||||
|
|> assign(:theme_results, [])
|
||||||
|
|> assign(:can_create, false)}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_event("search_themes", %{"theme_search" => query}, socket) do
|
||||||
|
{:noreply, assign_search(socket, query)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("add_theme", %{"id" => id}, socket) do
|
||||||
|
theme = Chesstrainer.Themes.get_theme!(id)
|
||||||
|
{:noreply, socket |> add_selected_theme(theme) |> clear_search()}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("create_theme", %{"name" => name}, socket) do
|
||||||
|
{:noreply, create_theme(socket, name)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("create_theme_from_search", %{"theme_search" => name}, socket) do
|
||||||
|
{:noreply, create_theme(socket, name)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("remove_theme", %{"id" => id}, socket) do
|
||||||
|
selected = Enum.reject(socket.assigns.selected_themes, &(Integer.to_string(&1.id) == id))
|
||||||
|
{:noreply, assign(socket, :selected_themes, selected)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("lookup", %{"fens" => fens} = params, socket) do
|
||||||
|
rating = parse_rating(params["rating"])
|
||||||
|
|
||||||
|
socket =
|
||||||
|
assign(socket,
|
||||||
|
fen_text: fens,
|
||||||
|
rating: rating,
|
||||||
|
entries: nil,
|
||||||
|
too_many?: false
|
||||||
|
)
|
||||||
|
|
||||||
|
if String.trim(fens) == "" do
|
||||||
|
{:noreply, put_flash(socket, :error, "Paste at least one FEN.")}
|
||||||
|
else
|
||||||
|
{:noreply, start_async(socket, :lookup, fn -> BulkImport.preview(fens) end)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("toggle_entry", %{"line" => line}, socket) do
|
||||||
|
line = String.to_integer(line)
|
||||||
|
|
||||||
|
entries =
|
||||||
|
Enum.map(socket.assigns.entries, fn entry ->
|
||||||
|
if entry.line == line, do: %{entry | selected: not entry.selected}, else: entry
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:noreply, assign(socket, :entries, entries)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("toggle_all", %{"selected" => selected}, socket) do
|
||||||
|
selected? = selected == "true"
|
||||||
|
|
||||||
|
entries =
|
||||||
|
Enum.map(socket.assigns.entries, fn entry ->
|
||||||
|
if entry.status == :ready, do: %{entry | selected: selected?}, else: entry
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:noreply, assign(socket, :entries, entries)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("import", _params, socket) do
|
||||||
|
entries =
|
||||||
|
Enum.filter(socket.assigns.entries || [], &(&1.status == :ready and &1.selected))
|
||||||
|
|
||||||
|
if entries == [] do
|
||||||
|
{:noreply, put_flash(socket, :error, "Select at least one ready endgame to import.")}
|
||||||
|
else
|
||||||
|
%{imported: imported, skipped: skipped} =
|
||||||
|
BulkImport.commit(entries, socket.assigns.selected_themes, socket.assigns.rating)
|
||||||
|
|
||||||
|
message =
|
||||||
|
"Imported #{imported} endgame(s)." <>
|
||||||
|
if(skipped > 0, do: " #{skipped} skipped.", else: "")
|
||||||
|
|
||||||
|
{:noreply,
|
||||||
|
socket
|
||||||
|
|> put_flash(:info, message)
|
||||||
|
|> push_navigate(to: ~p"/admin/endgames")}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_async(:lookup, {:ok, %{entries: entries, too_many?: too_many?}}, socket) do
|
||||||
|
entries = Enum.map(entries, &Map.put(&1, :selected, &1.status == :ready))
|
||||||
|
|
||||||
|
{:noreply, assign(socket, entries: entries, too_many?: too_many?)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_async(:lookup, {:exit, reason}, socket) do
|
||||||
|
{:noreply, put_flash(socket, :error, "Could not look up the FENs: #{inspect(reason)}")}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp tablebase_status do
|
||||||
|
case Local.check() do
|
||||||
|
{:ok, 0} ->
|
||||||
|
{:error,
|
||||||
|
"The local Syzygy tablebase directory contains no files, so imports are disabled."}
|
||||||
|
|
||||||
|
{:ok, largest} ->
|
||||||
|
{:ok, "Local Syzygy tablebase ready (up to #{largest} pieces)."}
|
||||||
|
|
||||||
|
{:error, :not_configured} ->
|
||||||
|
{:error, "Local tablebase is not configured. Set TABLEBASE_PATH to enable imports."}
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
{:error, "Local tablebase failed to load: #{inspect(reason)}"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_rating(rating) when is_integer(rating) and rating > 0, do: rating
|
||||||
|
|
||||||
|
defp parse_rating(rating) when is_binary(rating) do
|
||||||
|
case Integer.parse(String.trim(rating)) do
|
||||||
|
{value, ""} when value > 0 -> value
|
||||||
|
_ -> @default_rating
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_rating(_rating), do: @default_rating
|
||||||
|
|
||||||
|
defp count(entries, status), do: Enum.count(entries, &(&1.status == status))
|
||||||
|
|
||||||
|
defp summary(entries) do
|
||||||
|
"#{length(entries)} line(s): " <>
|
||||||
|
"#{count(entries, :ready)} ready, " <>
|
||||||
|
"#{count(entries, :duplicate)} duplicate(s), " <>
|
||||||
|
"#{count(entries, :invalid_fen)} invalid, " <>
|
||||||
|
"#{count(entries, :too_many_pieces)} too many pieces, " <>
|
||||||
|
"#{count(entries, :terminal)} terminal, " <>
|
||||||
|
"#{count(entries, :illegal)} illegal, " <>
|
||||||
|
"#{count(entries, :unsupported)} unsupported."
|
||||||
|
end
|
||||||
|
|
||||||
|
defp selected_count(entries) do
|
||||||
|
Enum.count(entries, &(&1.status == :ready and &1.selected))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp status_label(:ready), do: "Ready"
|
||||||
|
defp status_label(:duplicate), do: "Duplicate"
|
||||||
|
defp status_label(:invalid_fen), do: "Invalid FEN"
|
||||||
|
defp status_label(:too_many_pieces), do: "Too many pieces"
|
||||||
|
defp status_label(:illegal), do: "Illegal"
|
||||||
|
defp status_label(:terminal), do: "Terminal"
|
||||||
|
defp status_label(:unsupported), do: "Unsupported"
|
||||||
|
|
||||||
|
defp status_color(:ready), do: "primary"
|
||||||
|
defp status_color(_status), do: "warning"
|
||||||
|
end
|
||||||
@@ -14,6 +14,9 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
|
|||||||
<.header>
|
<.header>
|
||||||
Listing Endgames
|
Listing Endgames
|
||||||
<:actions>
|
<:actions>
|
||||||
|
<.button navigate={~p"/admin/endgames/import"}>
|
||||||
|
<.icon name="hero-arrow-up-tray" /> Bulk import
|
||||||
|
</.button>
|
||||||
<.button variant="primary" navigate={~p"/admin/endgames/new"}>
|
<.button variant="primary" navigate={~p"/admin/endgames/new"}>
|
||||||
<.icon name="hero-plus" /> New Endgame
|
<.icon name="hero-plus" /> New Endgame
|
||||||
</.button>
|
</.button>
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
move_input_error={@move_input_error}
|
move_input_error={@move_input_error}
|
||||||
last_check={@last_check}
|
last_check={@last_check}
|
||||||
last_checkmate={@last_checkmate}
|
last_checkmate={@last_checkmate}
|
||||||
|
rating={@user_rating}
|
||||||
|
rating_delta={@last_rating_delta}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div :if={@user_rating} class="mb-4">
|
<div :if={@user_rating} class="mb-4">
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do
|
|||||||
alias Chesstrainer.Endgames
|
alias Chesstrainer.Endgames
|
||||||
alias Chesstrainer.Endgames.Tablebase
|
alias Chesstrainer.Endgames.Tablebase
|
||||||
alias Chesstrainer.Ratings
|
alias Chesstrainer.Ratings
|
||||||
|
alias ChesstrainerWeb.RatingComponents
|
||||||
|
|
||||||
@uci_pattern ~r/^[a-h][1-8][a-h][1-8][qrbn]?$/
|
@uci_pattern ~r/^[a-h][1-8][a-h][1-8][qrbn]?$/
|
||||||
|
|
||||||
@@ -517,6 +518,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do
|
|||||||
|> assign(:user_rating, user.rating)
|
|> assign(:user_rating, user.rating)
|
||||||
|> assign(:user_deviation, user.rating_deviation)
|
|> assign(:user_deviation, user.rating_deviation)
|
||||||
|> assign(:last_rating_delta, delta)
|
|> assign(:last_rating_delta, delta)
|
||||||
|
|> put_rating_flash(outcome, user.rating, delta)
|
||||||
|
|
||||||
{:skipped, _attempt} ->
|
{:skipped, _attempt} ->
|
||||||
socket
|
socket
|
||||||
@@ -532,6 +534,24 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A finished rating change is shown as a flash so the player sees it
|
||||||
|
# immediately, whether they are in a session or playing a single endgame.
|
||||||
|
defp put_rating_flash(socket, outcome, rating, delta) do
|
||||||
|
kind = if outcome == :solved, do: :success, else: :error
|
||||||
|
put_flash(socket, kind, rating_change_message(socket, outcome, rating, delta))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp rating_change_message(socket, :solved, rating, delta) do
|
||||||
|
result = if socket.assigns.endgame.result == :draw, do: "Draw held", else: "Solved"
|
||||||
|
|
||||||
|
"#{result}! Your rating is now #{round(rating)} " <>
|
||||||
|
"(#{RatingComponents.format_delta(delta)})."
|
||||||
|
end
|
||||||
|
|
||||||
|
defp rating_change_message(_socket, :failed, rating, delta) do
|
||||||
|
"Failed. Your rating is now #{round(rating)} (#{RatingComponents.format_delta(delta)})."
|
||||||
|
end
|
||||||
|
|
||||||
defp user_rating(%{user: %{rating: rating}}), do: rating
|
defp user_rating(%{user: %{rating: rating}}), do: rating
|
||||||
defp user_rating(_), do: nil
|
defp user_rating(_), do: nil
|
||||||
|
|
||||||
|
|||||||
@@ -65,17 +65,14 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
<span class="text-muted">Avg/puzzle</span>
|
<span class="text-muted">Avg/puzzle</span>
|
||||||
<span class="font-mono">{format_duration(avg_ms(@session))}</span>
|
<span class="font-mono">{format_duration(avg_ms(@session))}</span>
|
||||||
</span>
|
</span>
|
||||||
<span>
|
<span class="flex items-center gap-2">
|
||||||
<span class="text-muted">Rating</span>
|
<span class="text-muted">Rating</span>
|
||||||
<span class={["font-mono", gain_class(net_gain(@session, @user_rating))]}>
|
<span id="session-rating" class="font-mono font-semibold">{round(@user_rating)}</span>
|
||||||
{format_delta(net_gain(@session, @user_rating))}
|
<.rating_change
|
||||||
</span>
|
:if={@session.count > 0}
|
||||||
</span>
|
id="session-rating-change"
|
||||||
<span :if={@last_rating_delta}>
|
delta={net_gain(@session, @user_rating)}
|
||||||
<span class="text-muted">Last</span>
|
/>
|
||||||
<span class={["font-mono", gain_class(@last_rating_delta)]}>
|
|
||||||
{format_delta(@last_rating_delta)}
|
|
||||||
</span>
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -99,6 +96,8 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
move_input_error={@move_input_error}
|
move_input_error={@move_input_error}
|
||||||
last_check={@last_check}
|
last_check={@last_check}
|
||||||
last_checkmate={@last_checkmate}
|
last_checkmate={@last_checkmate}
|
||||||
|
rating={@user_rating}
|
||||||
|
rating_delta={@last_rating_delta}
|
||||||
/>
|
/>
|
||||||
<% else %>
|
<% else %>
|
||||||
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
|
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
|
||||||
@@ -126,11 +125,13 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
<span class="text-muted">Avg/puzzle</span>
|
<span class="text-muted">Avg/puzzle</span>
|
||||||
<span class="font-mono">{format_duration(@last_session.avg_ms)}</span>
|
<span class="font-mono">{format_duration(@last_session.avg_ms)}</span>
|
||||||
</span>
|
</span>
|
||||||
<span>
|
<span class="flex items-center gap-2">
|
||||||
<span class="text-muted">Rating</span>
|
<span class="text-muted">Rating</span>
|
||||||
<span class={["font-mono", gain_class(@last_session.gain)]}>
|
<span class="font-mono font-semibold">{round(@last_session.rating)}</span>
|
||||||
{format_delta(@last_session.gain)}
|
<.rating_change
|
||||||
</span>
|
:if={@last_session.count > 0}
|
||||||
|
delta={@last_session.gain}
|
||||||
|
/>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -333,7 +334,8 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
duration_ms: elapsed_ms(session),
|
duration_ms: elapsed_ms(session),
|
||||||
count: session.count,
|
count: session.count,
|
||||||
avg_ms: avg_ms(session),
|
avg_ms: avg_ms(session),
|
||||||
gain: net_gain(session, rating)
|
gain: net_gain(session, rating),
|
||||||
|
rating: rating
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -373,19 +375,4 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
seconds = String.pad_leading(Integer.to_string(rem(total, 60)), 2, "0")
|
seconds = String.pad_leading(Integer.to_string(rem(total, 60)), 2, "0")
|
||||||
"#{minutes}:#{seconds}"
|
"#{minutes}:#{seconds}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_delta(nil), do: "—"
|
|
||||||
|
|
||||||
defp format_delta(delta) when is_number(delta) do
|
|
||||||
rounded = round(delta)
|
|
||||||
|
|
||||||
cond do
|
|
||||||
rounded > 0 -> "+#{rounded}"
|
|
||||||
true -> Integer.to_string(rounded)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp gain_class(delta) when is_number(delta) and delta > 0, do: "text-success-text"
|
|
||||||
defp gain_class(delta) when is_number(delta) and delta < 0, do: "text-error-text"
|
|
||||||
defp gain_class(_delta), do: "text-muted"
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,18 +7,20 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
|||||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||||
<div class="mx-auto max-w-sm space-y-4">
|
<div class="mx-auto max-w-sm space-y-4">
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<h1 class="settings-header">Log in</h1>
|
<.header>
|
||||||
<%= if @current_scope do %>
|
Log in
|
||||||
You need to reauthenticate to perform sensitive actions on your account.
|
<:subtitle :if={@current_scope || !@prelaunch}>
|
||||||
<% else %>
|
<%= if @current_scope do %>
|
||||||
<p :if={!@prelaunch} class="settings-subtitle my-4">
|
You need to reauthenticate to perform sensitive actions on your account.
|
||||||
Don't have an account? <.link
|
<% else %>
|
||||||
navigate={~p"/users/register"}
|
Don't have an account? <.link
|
||||||
class="font-semibold text-primary-text hover:underline"
|
navigate={~p"/users/register"}
|
||||||
phx-no-format
|
class="font-semibold text-primary-text hover:underline"
|
||||||
>Sign up</.link> for an account now.
|
phx-no-format
|
||||||
</p>
|
>Sign up</.link> for an account now.
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</:subtitle>
|
||||||
|
</.header>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<.form
|
<.form
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ defmodule ChesstrainerWeb.Router do
|
|||||||
] do
|
] do
|
||||||
live "/endgames", EndgameLive.Index, :index
|
live "/endgames", EndgameLive.Index, :index
|
||||||
live "/endgames/new", EndgameLive.Form, :new
|
live "/endgames/new", EndgameLive.Form, :new
|
||||||
|
live "/endgames/import", EndgameLive.Import, :new
|
||||||
live "/endgames/:id", EndgameLive.Show, :show
|
live "/endgames/:id", EndgameLive.Show, :show
|
||||||
live "/endgames/:id/edit", EndgameLive.Form, :edit
|
live "/endgames/:id/edit", EndgameLive.Form, :edit
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ defmodule ChesstrainerWeb.UserAuth do
|
|||||||
ChesstrainerWeb.EndgameLive.Index,
|
ChesstrainerWeb.EndgameLive.Index,
|
||||||
ChesstrainerWeb.EndgameLive.Form,
|
ChesstrainerWeb.EndgameLive.Form,
|
||||||
ChesstrainerWeb.EndgameLive.Show,
|
ChesstrainerWeb.EndgameLive.Show,
|
||||||
|
ChesstrainerWeb.EndgameLive.Import,
|
||||||
ChesstrainerWeb.Admin.ThemeLive.Index,
|
ChesstrainerWeb.Admin.ThemeLive.Index,
|
||||||
ChesstrainerWeb.Admin.ThemeLive.Form,
|
ChesstrainerWeb.Admin.ThemeLive.Form,
|
||||||
ChesstrainerWeb.Admin.UserLive.Index,
|
ChesstrainerWeb.Admin.UserLive.Index,
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
defmodule Chesstrainer.Endgames.BulkImportTest do
|
||||||
|
# Not async: the tablebase path is application config and Fathom keeps global
|
||||||
|
# state, so other tests must not flip the local backend while this runs.
|
||||||
|
use Chesstrainer.DataCase, async: false
|
||||||
|
|
||||||
|
import Chesstrainer.ThemesFixtures
|
||||||
|
|
||||||
|
alias Chesstrainer.Endgames
|
||||||
|
alias Chesstrainer.Endgames.BulkImport
|
||||||
|
alias Chesstrainer.Endgames.Endgame
|
||||||
|
alias Chesstrainer.Repo
|
||||||
|
|
||||||
|
@krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1"
|
||||||
|
@mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1"
|
||||||
|
@six_pieces "4k2r/8/8/8/8/8/8/R2QK2R w - - 0 1"
|
||||||
|
|
||||||
|
describe "preview/1" do
|
||||||
|
test "classifies a position the local tablebase can answer" do
|
||||||
|
assert %{entries: [entry], too_many?: false} = BulkImport.preview(@krk)
|
||||||
|
|
||||||
|
assert entry.status == :ready
|
||||||
|
assert entry.result == :win
|
||||||
|
assert entry.color == :white
|
||||||
|
assert entry.key == "KR v K"
|
||||||
|
assert entry.fen == @krk
|
||||||
|
assert entry.reason == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test "marks checkmate positions as terminal" do
|
||||||
|
assert %{entries: [entry]} = BulkImport.preview(@mate)
|
||||||
|
|
||||||
|
assert entry.status == :terminal
|
||||||
|
assert entry.result == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test "rejects positions with more than five pieces" do
|
||||||
|
assert %{entries: [entry]} = BulkImport.preview(@six_pieces)
|
||||||
|
|
||||||
|
assert entry.status == :too_many_pieces
|
||||||
|
assert entry.reason =~ "5 or fewer pieces"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "rejects malformed FENs" do
|
||||||
|
assert %{entries: [entry]} = BulkImport.preview("not a fen")
|
||||||
|
|
||||||
|
assert entry.status == :invalid_fen
|
||||||
|
assert entry.reason =~ "6 space-separated fields"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "reports duplicates within the batch without probing twice" do
|
||||||
|
assert %{entries: [first, second]} = BulkImport.preview("#{@krk}\n#{@krk}")
|
||||||
|
|
||||||
|
assert first.status == :ready
|
||||||
|
assert second.status == :duplicate
|
||||||
|
assert second.reason == "Duplicate FEN in this batch"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "reports FENs already stored as duplicates" do
|
||||||
|
{:ok, _endgame} =
|
||||||
|
Endgames.create_endgame(%{fen: @krk, color: :white, result: :win})
|
||||||
|
|
||||||
|
assert %{entries: [entry]} = BulkImport.preview(@krk)
|
||||||
|
|
||||||
|
assert entry.status == :duplicate
|
||||||
|
assert entry.reason == "FEN already exists"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ignores blank lines and comments" do
|
||||||
|
text = "\n# a comment\n#{@krk}\n\n"
|
||||||
|
|
||||||
|
assert %{entries: [entry]} = BulkImport.preview(text)
|
||||||
|
assert entry.status == :ready
|
||||||
|
end
|
||||||
|
|
||||||
|
test "caps the input at max_fens/0" do
|
||||||
|
text = Enum.join(List.duplicate("not a fen", BulkImport.max_fens() + 1), "\n")
|
||||||
|
|
||||||
|
assert %{entries: entries, too_many?: true} = BulkImport.preview(text)
|
||||||
|
assert length(entries) == BulkImport.max_fens()
|
||||||
|
end
|
||||||
|
|
||||||
|
test "reports unsupported when the local tablebase is not configured" do
|
||||||
|
previous = Application.get_env(:chesstrainer, :tablebase_path)
|
||||||
|
|
||||||
|
on_exit(fn -> Application.put_env(:chesstrainer, :tablebase_path, previous) end)
|
||||||
|
|
||||||
|
Application.put_env(:chesstrainer, :tablebase_path, nil)
|
||||||
|
|
||||||
|
assert %{entries: [entry]} = BulkImport.preview(@krk)
|
||||||
|
assert entry.status == :unsupported
|
||||||
|
assert entry.reason =~ "not configured"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "commit/3" do
|
||||||
|
test "inserts ready entries with the batch rating and themes" do
|
||||||
|
theme = theme_fixture(%{name: "Lucena"})
|
||||||
|
|
||||||
|
assert %{entries: entries} = BulkImport.preview(@krk)
|
||||||
|
assert %{imported: 1, skipped: 0} = BulkImport.commit(entries, [theme], 1700)
|
||||||
|
|
||||||
|
endgame =
|
||||||
|
Repo.get_by!(Endgame, fen: @krk)
|
||||||
|
|> Repo.preload(:themes)
|
||||||
|
|
||||||
|
assert endgame.color == :white
|
||||||
|
assert endgame.result == :win
|
||||||
|
assert endgame.key == "KR v K"
|
||||||
|
assert endgame.rating == 1700.0
|
||||||
|
assert [%{id: theme_id}] = endgame.themes
|
||||||
|
assert theme_id == theme.id
|
||||||
|
end
|
||||||
|
|
||||||
|
test "ignores entries that are not ready" do
|
||||||
|
assert %{entries: entries} = BulkImport.preview("not a fen")
|
||||||
|
|
||||||
|
assert %{imported: 0, skipped: 1} = BulkImport.commit(entries)
|
||||||
|
assert Repo.aggregate(Endgame, :count) == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
test "skips rows that collide with an existing FEN" do
|
||||||
|
{:ok, _existing} =
|
||||||
|
Endgames.create_endgame(%{fen: @krk, color: :white, result: :win})
|
||||||
|
|
||||||
|
entries = [
|
||||||
|
%{
|
||||||
|
line: 1,
|
||||||
|
fen: @krk,
|
||||||
|
key: "KR v K",
|
||||||
|
color: :white,
|
||||||
|
result: :win,
|
||||||
|
status: :ready,
|
||||||
|
reason: nil
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
assert %{imported: 0, skipped: 1} = BulkImport.commit(entries)
|
||||||
|
assert Repo.aggregate(Endgame, :count) == 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -46,7 +46,8 @@ defmodule ChesstrainerWeb.AdminGatingTest do
|
|||||||
~p"/admin/themes",
|
~p"/admin/themes",
|
||||||
~p"/admin/themes/new",
|
~p"/admin/themes/new",
|
||||||
~p"/admin/users",
|
~p"/admin/users",
|
||||||
~p"/admin/users/new"
|
~p"/admin/users/new",
|
||||||
|
~p"/admin/endgames/import"
|
||||||
] do
|
] do
|
||||||
assert redirected_to(get(conn, path)) == ~p"/"
|
assert redirected_to(get(conn, path)) == ~p"/"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
defmodule ChesstrainerWeb.EndgameLive.ImportTest do
|
||||||
|
use ChesstrainerWeb.ConnCase
|
||||||
|
|
||||||
|
import Phoenix.LiveViewTest
|
||||||
|
import Chesstrainer.ThemesFixtures
|
||||||
|
|
||||||
|
alias Chesstrainer.Endgames
|
||||||
|
alias Chesstrainer.Endgames.Endgame
|
||||||
|
alias Chesstrainer.Repo
|
||||||
|
|
||||||
|
@krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1"
|
||||||
|
@mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1"
|
||||||
|
@six_pieces "4k2r/8/8/8/8/8/8/R2QK2R w - - 0 1"
|
||||||
|
|
||||||
|
setup :register_and_log_in_admin
|
||||||
|
|
||||||
|
test "shows the local tablebase status", %{conn: conn} do
|
||||||
|
{:ok, _view, html} = live(conn, ~p"/admin/endgames/import")
|
||||||
|
|
||||||
|
assert html =~ "Local Syzygy tablebase ready"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "looks up FENs and imports the selected ones", %{conn: conn} do
|
||||||
|
theme = theme_fixture(%{name: "Lucena"})
|
||||||
|
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/admin/endgames/import")
|
||||||
|
|
||||||
|
view
|
||||||
|
|> form("#endgame-import-form", %{fens: @krk, rating: "1700"})
|
||||||
|
|> render_submit()
|
||||||
|
|
||||||
|
html = render_async(view, 5_000)
|
||||||
|
|
||||||
|
assert html =~ "Ready"
|
||||||
|
assert html =~ "KR v K"
|
||||||
|
assert html =~ "win"
|
||||||
|
|
||||||
|
view |> form("#theme-form", %{theme_search: "Luc"}) |> render_change()
|
||||||
|
view |> element("button", "Lucena") |> render_click()
|
||||||
|
|
||||||
|
view |> element("button", "Import 1 endgame(s)") |> render_click()
|
||||||
|
|
||||||
|
assert_redirect(view, ~p"/admin/endgames")
|
||||||
|
|
||||||
|
endgame = Repo.get_by!(Endgame, fen: @krk) |> Repo.preload(:themes)
|
||||||
|
|
||||||
|
assert endgame.rating == 1700.0
|
||||||
|
assert endgame.result == :win
|
||||||
|
assert endgame.color == :white
|
||||||
|
assert [%{id: theme_id}] = endgame.themes
|
||||||
|
assert theme_id == theme.id
|
||||||
|
end
|
||||||
|
|
||||||
|
test "explains why lines cannot be imported", %{conn: conn} do
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/admin/endgames/import")
|
||||||
|
|
||||||
|
view
|
||||||
|
|> form("#endgame-import-form", %{
|
||||||
|
fens: "#{@six_pieces}\nnot a fen\n#{@mate}",
|
||||||
|
rating: "1500"
|
||||||
|
})
|
||||||
|
|> render_submit()
|
||||||
|
|
||||||
|
html = render_async(view, 5_000)
|
||||||
|
|
||||||
|
assert html =~ "Too many pieces"
|
||||||
|
assert html =~ "Invalid FEN"
|
||||||
|
assert html =~ "Terminal"
|
||||||
|
refute html =~ "Import 1 endgame(s)"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "does not import when every line is a duplicate", %{conn: conn} do
|
||||||
|
{:ok, _endgame} = Endgames.create_endgame(%{fen: @krk, color: :white, result: :win})
|
||||||
|
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/admin/endgames/import")
|
||||||
|
|
||||||
|
view
|
||||||
|
|> form("#endgame-import-form", %{fens: @krk, rating: "1500"})
|
||||||
|
|> render_submit()
|
||||||
|
|
||||||
|
html = render_async(view, 5_000)
|
||||||
|
|
||||||
|
assert html =~ "Duplicate"
|
||||||
|
assert html =~ "FEN already exists"
|
||||||
|
assert html =~ "Import 0 endgame(s)"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "warns when the local tablebase is not configured", %{conn: conn} do
|
||||||
|
previous = Application.get_env(:chesstrainer, :tablebase_path)
|
||||||
|
on_exit(fn -> Application.put_env(:chesstrainer, :tablebase_path, previous) end)
|
||||||
|
|
||||||
|
Application.put_env(:chesstrainer, :tablebase_path, nil)
|
||||||
|
|
||||||
|
{:ok, _view, html} = live(conn, ~p"/admin/endgames/import")
|
||||||
|
|
||||||
|
assert html =~ "Local tablebase is not configured"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -495,6 +495,9 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
})
|
})
|
||||||
|
|
||||||
assert render(view) =~ "Solved!"
|
assert render(view) =~ "Solved!"
|
||||||
|
assert render(view) =~ "Your rating is now"
|
||||||
|
assert has_element?(view, "#your-rating")
|
||||||
|
assert has_element?(view, "#rating-change")
|
||||||
assert [attempt] = Repo.all(Attempt)
|
assert [attempt] = Repo.all(Attempt)
|
||||||
assert attempt.user_id == user.id
|
assert attempt.user_id == user.id
|
||||||
assert attempt.endgame_id == endgame.id
|
assert attempt.endgame_id == endgame.id
|
||||||
|
|||||||
@@ -149,6 +149,11 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
assert assigns.last_rating_delta > 0
|
assert assigns.last_rating_delta > 0
|
||||||
assert assigns.user_rating > 1500.0
|
assert assigns.user_rating > 1500.0
|
||||||
assert assigns.user_deviation < 350.0
|
assert assigns.user_deviation < 350.0
|
||||||
|
|
||||||
|
assert has_element?(view, "#session-rating")
|
||||||
|
assert has_element?(view, "#session-rating-change")
|
||||||
|
assert has_element?(view, "#rating-change")
|
||||||
|
assert render(view) =~ "Your rating is now"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "loads a different puzzle on next", %{conn: conn} do
|
test "loads a different puzzle on next", %{conn: conn} do
|
||||||
@@ -205,6 +210,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
assert last.count == 1
|
assert last.count == 1
|
||||||
assert is_float(last.gain)
|
assert is_float(last.gain)
|
||||||
assert last.gain > 0
|
assert last.gain > 0
|
||||||
|
assert last.rating > 1500.0
|
||||||
assert last.duration_ms >= 0
|
assert last.duration_ms >= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,18 @@ defmodule ChesstrainerWeb.Plugs.PrelaunchTest do
|
|||||||
assert get_resp_header(conn, "x-robots-tag") == ["noindex, nofollow"]
|
assert get_resp_header(conn, "x-robots-tag") == ["noindex, nofollow"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "allows the admin bulk import page", %{conn: conn} do
|
||||||
|
admin =
|
||||||
|
user_fixture()
|
||||||
|
|> Ecto.Changeset.change(role: :admin)
|
||||||
|
|> Repo.update!()
|
||||||
|
|
||||||
|
conn = log_in_user(conn, admin)
|
||||||
|
|
||||||
|
assert {:ok, _view, html} = live(conn, ~p"/admin/endgames/import")
|
||||||
|
assert html =~ "Import endgames"
|
||||||
|
end
|
||||||
|
|
||||||
test "redirects registration and unknown pages", %{conn: conn} do
|
test "redirects registration and unknown pages", %{conn: conn} do
|
||||||
assert conn |> get(~p"/users/register") |> redirected_to() == "/"
|
assert conn |> get(~p"/users/register") |> redirected_to() == "/"
|
||||||
assert conn |> get("/definitely-not-a-page") |> redirected_to() == "/"
|
assert conn |> get("/definitely-not-a-page") |> redirected_to() == "/"
|
||||||
|
|||||||
Reference in New Issue
Block a user