mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Feature/guest trainer (#89)
* add anonymous guest training with a daily puzzle limit - /guest-train serves random endgames with no difficulty picker; signed-in visitors are redirected to /train - guests get 5 puzzles per UTC day, keyed by an in-memory HMAC of the client address; the raw address is never stored or logged - a sign-up wall replaces the next puzzle once the budget is spent - move the endgame library behind auth so guests cannot browse it - privacy disclosure tracked in #87 * remove chess-test endpoint and liveview
This commit is contained in:
@@ -23,11 +23,23 @@ defmodule Chesstrainer.Endgames do
|
||||
|
||||
@doc """
|
||||
Returns a random endgame, or `nil` when none exist.
|
||||
|
||||
`:exclude_ids` (default `[]`) skips endgames by id — used to avoid repeating
|
||||
the puzzle just played.
|
||||
"""
|
||||
def random_endgame do
|
||||
Repo.one(from e in Endgame, order_by: fragment("RANDOM()"), limit: 1)
|
||||
def random_endgame(opts \\ []) do
|
||||
exclude_ids = Keyword.get(opts, :exclude_ids, [])
|
||||
|
||||
Endgame
|
||||
|> skip_ids(exclude_ids)
|
||||
|> order_by([e], fragment("RANDOM()"))
|
||||
|> limit(1)
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
defp skip_ids(query, []), do: query
|
||||
defp skip_ids(query, ids), do: where(query, [e], e.id not in ^ids)
|
||||
|
||||
@doc """
|
||||
Returns the number of stored endgames, each of which is played as a puzzle.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user