mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
* Show rating changes in flashes and session UI (#146) - Flash the new rating and delta on every rated completion, with a dedicated success flash kind for solved puzzles - Move flash notifications to the top-left - Add a colour-coded rating-change pill and show the current rating with the session's net change in the train session bar - Show the user's rating and change beside the puzzle rating after an attempt, and show the finishing rating in the last-session summary * fix header component and start reusing it * Hide the sign-up link on the prelaunch admin login The header refactor dropped the @prelaunch guard, so the gated admin login linked to the public registration page again.
148 lines
4.8 KiB
Elixir
148 lines
4.8 KiB
Elixir
defmodule ChesstrainerWeb.RatingComponents do
|
||
@moduledoc """
|
||
Visual guide to a user's rating.
|
||
|
||
Places the rating on a fixed scale and shades the range the true rating is
|
||
likely to sit in, derived from the rating deviation (rating ± 2×RD). The
|
||
band is intentionally wide for a new or returning player: it narrows as
|
||
rated attempts accumulate, which is exactly what the deviation measures.
|
||
"""
|
||
|
||
use Phoenix.Component
|
||
|
||
import ChesstrainerWeb.CoreComponents, only: [icon: 1]
|
||
|
||
# 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
|
||
# useful range readable without letting a stray value push the marker off.
|
||
@scale_min 500
|
||
@scale_max 3000
|
||
|
||
@doc """
|
||
Renders the rating gauge with its confidence band and explainer.
|
||
"""
|
||
attr :rating, :float, required: true
|
||
attr :deviation, :float, default: nil
|
||
attr :id, :string, default: "rating-guide"
|
||
|
||
def rating_guide(assigns) do
|
||
low = assigns.deviation && assigns.rating - 2 * assigns.deviation
|
||
high = assigns.deviation && assigns.rating + 2 * assigns.deviation
|
||
|
||
assigns =
|
||
assign(assigns,
|
||
low: low,
|
||
high: high,
|
||
scale_min: @scale_min,
|
||
scale_max: @scale_max,
|
||
marker_pct: pct(assigns.rating),
|
||
band_left: low && pct(low),
|
||
band_width: low && pct(high) - pct(low),
|
||
label: aria_label(assigns.rating, low, high)
|
||
)
|
||
|
||
~H"""
|
||
<section id={@id} class="rounded-lg bg-canvas-raised p-3 text-sm">
|
||
<div class="flex flex-wrap items-baseline justify-between gap-x-3 gap-y-1">
|
||
<p class="font-semibold">Your rating: {round(@rating)}</p>
|
||
<p :if={@low} id={"#{@id}-range"} class="text-xs text-muted">
|
||
likely {round(@low)}–{round(@high)}
|
||
</p>
|
||
</div>
|
||
|
||
<div class="relative mt-3 h-2 rounded-full bg-base-300" role="img" aria-label={@label}>
|
||
<div
|
||
:if={@low}
|
||
class="absolute inset-y-0 rounded-full bg-primary/30"
|
||
style={"left: #{@band_left}%; width: #{@band_width}%"}
|
||
>
|
||
</div>
|
||
<div
|
||
class="absolute -top-1 h-4 w-0.5 -translate-x-1/2 rounded-full bg-primary"
|
||
style={"left: #{@marker_pct}%"}
|
||
>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="mt-1 flex justify-between text-[10px] text-muted">
|
||
<span>{@scale_min}</span>
|
||
<span>{@scale_max}</span>
|
||
</div>
|
||
|
||
<p class="mt-2 text-xs text-muted">
|
||
Your rating estimates how well you solve endgames. It moves after each
|
||
rated attempt — solving a harder puzzle gains more, and replaying the
|
||
same puzzle within a week does not count. The shaded band is the range
|
||
your true rating is likely to sit in (±2 rating deviations); it narrows
|
||
as you play.
|
||
</p>
|
||
</section>
|
||
"""
|
||
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.
|
||
defp pct(value) do
|
||
clamped = value |> max(@scale_min) |> min(@scale_max)
|
||
Float.round((clamped - @scale_min) / (@scale_max - @scale_min) * 100, 1)
|
||
end
|
||
|
||
defp aria_label(rating, nil, nil) do
|
||
"Rating #{round(rating)} on a #{@scale_min} to #{@scale_max} scale"
|
||
end
|
||
|
||
defp aria_label(rating, low, high) do
|
||
"Rating #{round(rating)}, likely between #{round(low)} and #{round(high)}, " <>
|
||
"on a #{@scale_min} to #{@scale_max} scale"
|
||
end
|
||
end
|