mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
landing page (#63)
* remove daisyui and rebuild styles * very basic setup of new design - missing components etc * basic navbar with reusable landing components * basic puzzle of the day * update todos * update todos * add curated landing puzzle cards with selectable board (#55) * update with footer * add button to solve today's puzzle * Make landing page a dead view Render the home page from PageController with a static board preview, link puzzle cards and CTAs to public-code play routes, count puzzles from the DB, and add a global footer. * Serve a marketing-only JS bundle on the landing page Build a small marketing entry point with phoenix_html and the plain-DOM navbar, select it on controller-rendered pages, and keep the full LiveView bundle for LiveView pages. * Add landing page SEO metadata Replace the Phoenix default title with a descriptive Lucid Chess title, and add description, canonical, Open Graph, and Twitter card tags to the landing page. * Complete the hero copy block Add the spark icon primitive and the Endgames, made clear eyebrow. The primary CTA keeps linking to the active puzzle rather than scrolling to the board. * Add a persisted global daily puzzle Assign one endgame per UTC day in a daily_puzzles table with a unique date, preferring endgames unused in the last 30 days and falling back to any. Concurrent first requests insert with on-conflict-ignore and read the winning row. The landing board now shows the daily puzzle. * remove unused CSS, nav links, time pill * allow devices to connect to dev server by changing IP address to 0.0.0.0 * Restore the colour-mode switch and add a dark palette * Use brand colour tokens on the play page * self host fonts - closes issue #62 * begin splitting CSS into modules * remove hover on hero chessboard, split more css modules * remove interactivity from hero chessboard * update todo
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
defmodule Chesstrainer.DailyPuzzles do
|
||||
@moduledoc """
|
||||
Assigns a single endgame to each UTC day.
|
||||
|
||||
The assignment is persisted so every request and every user sees the same
|
||||
featured puzzle for the day. Assignment is race-safe: concurrent first
|
||||
requests rely on the unique date constraint and read back the winning row.
|
||||
|
||||
Selection prefers endgames not used in the last 30 days and falls back to
|
||||
any endgame when that pool is exhausted.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
|
||||
alias Chesstrainer.DailyPuzzles.DailyPuzzle
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
alias Chesstrainer.Repo
|
||||
|
||||
@recent_days 30
|
||||
|
||||
@doc """
|
||||
Returns today's endgame (UTC), assigning one if needed.
|
||||
|
||||
Returns `nil` when no endgames exist.
|
||||
"""
|
||||
def today, do: for_date(Date.utc_today())
|
||||
|
||||
@doc """
|
||||
Returns the endgame assigned to `date`, assigning one if needed.
|
||||
|
||||
Returns `nil` when no endgames exist.
|
||||
"""
|
||||
def for_date(%Date{} = date) do
|
||||
case get_for_date(date) do
|
||||
nil -> assign_for_date(date)
|
||||
endgame -> endgame
|
||||
end
|
||||
end
|
||||
|
||||
defp get_for_date(date) do
|
||||
case Repo.get_by(DailyPuzzle, date: date) do
|
||||
nil -> nil
|
||||
daily_puzzle -> Repo.preload(daily_puzzle, endgame: :themes).endgame
|
||||
end
|
||||
end
|
||||
|
||||
defp assign_for_date(date) do
|
||||
case pick_endgame_id(date) do
|
||||
nil ->
|
||||
nil
|
||||
|
||||
endgame_id ->
|
||||
%DailyPuzzle{}
|
||||
|> DailyPuzzle.changeset(%{date: date, endgame_id: endgame_id})
|
||||
|> Repo.insert(on_conflict: :nothing, conflict_target: :date)
|
||||
|
||||
get_for_date(date)
|
||||
end
|
||||
end
|
||||
|
||||
defp pick_endgame_id(date) do
|
||||
cutoff = Date.add(date, -@recent_days)
|
||||
|
||||
recent_ids =
|
||||
Repo.all(
|
||||
from daily_puzzle in DailyPuzzle,
|
||||
where: daily_puzzle.date >= ^cutoff,
|
||||
select: daily_puzzle.endgame_id
|
||||
)
|
||||
|
||||
eligible_ids =
|
||||
Repo.all(
|
||||
from endgame in Endgame,
|
||||
where: endgame.id not in ^recent_ids,
|
||||
select: endgame.id
|
||||
)
|
||||
|
||||
ids =
|
||||
case eligible_ids do
|
||||
[] -> Repo.all(from endgame in Endgame, select: endgame.id)
|
||||
ids -> ids
|
||||
end
|
||||
|
||||
case ids do
|
||||
[] -> nil
|
||||
ids -> Enum.random(ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
defmodule Chesstrainer.DailyPuzzles.DailyPuzzle do
|
||||
@moduledoc """
|
||||
A persisted endgame assignment for one UTC day.
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
|
||||
@foreign_key_type :binary_id
|
||||
|
||||
schema "daily_puzzles" do
|
||||
field :date, :date
|
||||
|
||||
belongs_to :endgame, Endgame
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(daily_puzzle, attrs) do
|
||||
daily_puzzle
|
||||
|> cast(attrs, [:date, :endgame_id])
|
||||
|> validate_required([:date, :endgame_id])
|
||||
|> unique_constraint(:date)
|
||||
|> foreign_key_constraint(:endgame_id)
|
||||
end
|
||||
end
|
||||
@@ -21,6 +21,74 @@ defmodule Chesstrainer.Endgames do
|
||||
Repo.all(Endgame)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a random endgame, or `nil` when none exist.
|
||||
"""
|
||||
def random_endgame do
|
||||
Repo.one(from e in Endgame, order_by: fragment("RANDOM()"), limit: 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of stored endgames, each of which is played as a puzzle.
|
||||
"""
|
||||
def count_endgames do
|
||||
Repo.aggregate(Endgame, :count)
|
||||
end
|
||||
|
||||
@curated_count 3
|
||||
|
||||
@doc """
|
||||
Returns curated endgames for the landing page, with themes preloaded.
|
||||
|
||||
Curation is the configured list of `:curated_endgame_codes`; codes that do not
|
||||
resolve are skipped. The list is topped up with a date-seeded selection from
|
||||
the database so it is stable for a given day, and is empty when no endgames
|
||||
exist.
|
||||
"""
|
||||
def curated_endgames(count \\ @curated_count) do
|
||||
codes = Application.get_env(:chesstrainer, :curated_endgame_codes, [])
|
||||
|
||||
curated =
|
||||
codes
|
||||
|> Enum.flat_map(&endgame_for_code/1)
|
||||
|> Enum.uniq_by(& &1.id)
|
||||
|
||||
fill_curated(curated, count)
|
||||
end
|
||||
|
||||
defp endgame_for_code(code) do
|
||||
case Repo.get_by(Endgame, public_code: code) do
|
||||
nil -> []
|
||||
endgame -> [Repo.preload(endgame, :themes)]
|
||||
end
|
||||
end
|
||||
|
||||
defp fill_curated(endgames, count) when length(endgames) >= count,
|
||||
do: Enum.take(endgames, count)
|
||||
|
||||
defp fill_curated(endgames, count) do
|
||||
exclude_ids = Enum.map(endgames, & &1.id)
|
||||
endgames ++ date_seeded_endgames(exclude_ids, count - length(endgames))
|
||||
end
|
||||
|
||||
defp date_seeded_endgames([], limit), do: date_seeded_endgames_query(Endgame, limit)
|
||||
|
||||
defp date_seeded_endgames(exclude_ids, limit) do
|
||||
Endgame
|
||||
|> where([e], e.id not in ^exclude_ids)
|
||||
|> date_seeded_endgames_query(limit)
|
||||
end
|
||||
|
||||
defp date_seeded_endgames_query(query, limit) do
|
||||
today = Date.utc_today()
|
||||
|
||||
query
|
||||
|> preload(:themes)
|
||||
|> Repo.all()
|
||||
|> Enum.sort_by(fn endgame -> :erlang.phash2({today, endgame.id}) end)
|
||||
|> Enum.take(limit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the distinct Keys across all endgames, sorted.
|
||||
"""
|
||||
@@ -99,6 +167,15 @@ defmodule Chesstrainer.Endgames do
|
||||
Repo.get_by!(Endgame, public_code: public_code)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single endgame by its public code with its themes preloaded.
|
||||
"""
|
||||
def get_endgame_by_public_code_with_themes!(public_code) do
|
||||
Endgame
|
||||
|> Repo.get_by!(public_code: public_code)
|
||||
|> Repo.preload(:themes)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Finds endgames by database id, public code, or FEN.
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
defmodule Chesstrainer.FEN do
|
||||
@board_pieces ~w(p n b r q k P N B R Q K)
|
||||
|
||||
@spec color_from_fen(String.t()) :: :white | :black | nil
|
||||
def color_from_fen(fen) do
|
||||
fen
|
||||
@@ -92,6 +94,56 @@ defmodule Chesstrainer.FEN do
|
||||
|
||||
def piece_count(_fen), do: nil
|
||||
|
||||
@doc """
|
||||
Returns the board field of a FEN as eight ranks, each a list of eight squares,
|
||||
where each square is a piece letter or `nil`.
|
||||
|
||||
Returns `nil` when the board field is not a well-formed FEN board. Only the
|
||||
board field is read; the active colour and clocks are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Chesstrainer.FEN.board("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1") |> Enum.at(2)
|
||||
[nil, nil, nil, "k", nil, nil, nil, nil]
|
||||
|
||||
iex> Chesstrainer.FEN.board("not a fen")
|
||||
nil
|
||||
"""
|
||||
@spec board(String.t() | nil) :: [[String.t() | nil]] | nil
|
||||
def board(fen) when is_binary(fen) do
|
||||
case String.split(fen, " ") do
|
||||
[board | _] -> parse_board(board)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
def board(_fen), do: nil
|
||||
|
||||
defp parse_board(board) do
|
||||
case String.split(board, "/") do
|
||||
ranks when length(ranks) == 8 ->
|
||||
squares = Enum.map(ranks, &parse_rank/1)
|
||||
if Enum.all?(squares, &is_list/1), do: squares, else: nil
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_rank(rank) do
|
||||
rank
|
||||
|> String.graphemes()
|
||||
|> Enum.reduce_while([], fn
|
||||
<<c>>, acc when c in ?1..?8 -> {:cont, List.duplicate(nil, c - ?0) ++ acc}
|
||||
piece, acc when piece in @board_pieces -> {:cont, [piece | acc]}
|
||||
_other, _acc -> {:halt, nil}
|
||||
end)
|
||||
|> case do
|
||||
squares when is_list(squares) and length(squares) == 8 -> Enum.reverse(squares)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp board_piece_count(board) do
|
||||
board
|
||||
|> String.graphemes()
|
||||
|
||||
Reference in New Issue
Block a user