Add public codes for player URLs and admin endgame search

Player-facing endgame links now use a 10-character Crockford base32 public code instead of the database UUID; admin routes keep the UUID. Existing rows are backfilled by migration, and the admin Show page displays and copies the code.

The admin endgame index gains its own search box: exact database id, exact public code, or a FEN / FEN fragment. A single match opens the endgame; multiple matches list below; nothing found says so.

Closes #44
Closes #45
This commit is contained in:
2026-11-03 22:46:38 +01:00
parent 160e8629b7
commit 49736d186c
18 changed files with 544 additions and 64 deletions
+75 -5
View File
@@ -92,6 +92,63 @@ defmodule Chesstrainer.Endgames do
"""
def get_endgame!(id), do: Repo.get!(Endgame, id)
@doc """
Gets a single endgame by its public code, raising if it does not exist.
"""
def get_endgame_by_public_code!(public_code) do
Repo.get_by!(Endgame, public_code: public_code)
end
@doc """
Finds endgames by database id, public code, or FEN.
A full FEN matches exactly (after normalisation); a fragment matches any FEN
containing it. Themes are preloaded. A blank query returns `[]`.
"""
def search(query) do
query = String.trim(query || "")
if query == "" do
[]
else
[search_by_id(query), search_by_public_code(query), search_by_fen(query)]
|> Enum.reject(&is_nil/1)
|> List.flatten()
|> Enum.uniq_by(& &1.id)
end
end
defp search_by_id(query) do
case Ecto.UUID.cast(query) do
{:ok, id} -> Repo.get(Endgame, id) |> preload_themes()
:error -> nil
end
end
defp search_by_public_code(query) do
Repo.get_by(Endgame, public_code: query) |> preload_themes()
end
defp search_by_fen(query) do
pattern = "%" <> escape_like(Chesstrainer.FEN.normalize(query)) <> "%"
Endgame
|> where([e], like(e.fen, ^pattern))
|> preload(:themes)
|> order_by([e], asc: e.key, asc: e.fen)
|> Repo.all()
end
defp preload_themes(nil), do: nil
defp preload_themes(endgame), do: Repo.preload(endgame, :themes)
defp escape_like(value) do
value
|> String.replace("\\", "\\\\")
|> String.replace("%", "\\%")
|> String.replace("_", "\\_")
end
@doc """
Creates a endgame.
@@ -104,11 +161,24 @@ defmodule Chesstrainer.Endgames do
{:error, %Ecto.Changeset{}}
"""
def create_endgame(attrs, themes \\ []) do
%Endgame{}
|> Endgame.changeset(attrs)
|> Ecto.Changeset.put_assoc(:themes, themes)
|> Repo.insert()
def create_endgame(attrs, themes \\ []), do: insert_endgame(attrs, themes, 3)
defp insert_endgame(attrs, themes, attempts) do
result =
%Endgame{}
|> Endgame.changeset(attrs)
|> Ecto.Changeset.put_assoc(:themes, themes)
|> Repo.insert()
case result do
{:error, changeset} when attempts > 1 ->
if Keyword.has_key?(changeset.errors, :public_code),
do: insert_endgame(attrs, themes, attempts - 1),
else: result
other ->
other
end
end
@doc """
+30
View File
@@ -8,9 +8,13 @@ defmodule Chesstrainer.Endgames.Endgame do
alias Chesstrainer.Key
@public_code_alphabet ~c"0123456789abcdefghjkmnpqrstvwxyz"
@public_code_length 10
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "endgames" do
field :public_code, :string
field :fen, :string
field :color, Ecto.Enum, values: [:white, :black]
field :key, :string
@@ -42,7 +46,33 @@ defmodule Chesstrainer.Endgames.Endgame do
end
end)
|> unique_constraint(:fen, message: "FEN already exists")
|> unique_constraint(:public_code)
|> put_key()
|> put_public_code()
end
@doc """
Generates a public code: a short, random identifier for player-facing URLs.
10 characters of Crockford base32 (no `i`, `l`, `o` or `u`) give roughly 50
bits, so collisions are vanishingly rare; the unique index is the backstop.
"""
@spec generate_public_code() :: String.t()
def generate_public_code do
@public_code_length
|> :crypto.strong_rand_bytes()
|> :binary.bin_to_list()
|> Enum.map(fn byte ->
Enum.at(@public_code_alphabet, rem(byte, length(@public_code_alphabet)))
end)
|> List.to_string()
end
defp put_public_code(changeset) do
case get_field(changeset, :public_code) do
nil -> put_change(changeset, :public_code, generate_public_code())
_ -> changeset
end
end
defp put_key(changeset) do