diff --git a/lib/chesstrainer_web.ex b/lib/chesstrainer_web.ex index 6e4f174..26ef443 100644 --- a/lib/chesstrainer_web.ex +++ b/lib/chesstrainer_web.ex @@ -88,6 +88,8 @@ defmodule ChesstrainerWeb do import ChesstrainerWeb.CoreComponents # Landing-page primitives (pill buttons, marketing icons) import ChesstrainerWeb.LandingComponents + # Rating gauge and explainer + import ChesstrainerWeb.RatingComponents # Common modules used in templates alias Phoenix.LiveView.JS diff --git a/lib/chesstrainer_web/components/rating_components.ex b/lib/chesstrainer_web/components/rating_components.ex new file mode 100644 index 0000000..1d2f223 --- /dev/null +++ b/lib/chesstrainer_web/components/rating_components.ex @@ -0,0 +1,95 @@ +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 + + # 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""" +
+
+

Your rating: {round(@rating)}

+

+ likely {round(@low)}–{round(@high)} +

+
+ + + +
+ {@scale_min} + {@scale_max} +
+ +

+ 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. +

+
+ """ + end + + # 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 diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index 4b3bb79..7da4f49 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -16,9 +16,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do {@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {round( @endgame.rating )} · Attempts: {@endgame.times_attempted || - 0} · Solved: {@endgame.times_solved || 0} · Your rating: {round( - @user_rating - )} + 0} · Solved: {@endgame.times_solved || 0}

FEN: {@endgame.fen}