mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
add visual rating guide (#48)
This commit is contained in:
@@ -88,6 +88,8 @@ defmodule ChesstrainerWeb do
|
|||||||
import ChesstrainerWeb.CoreComponents
|
import ChesstrainerWeb.CoreComponents
|
||||||
# Landing-page primitives (pill buttons, marketing icons)
|
# Landing-page primitives (pill buttons, marketing icons)
|
||||||
import ChesstrainerWeb.LandingComponents
|
import ChesstrainerWeb.LandingComponents
|
||||||
|
# Rating gauge and explainer
|
||||||
|
import ChesstrainerWeb.RatingComponents
|
||||||
|
|
||||||
# Common modules used in templates
|
# Common modules used in templates
|
||||||
alias Phoenix.LiveView.JS
|
alias Phoenix.LiveView.JS
|
||||||
|
|||||||
@@ -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"""
|
||||||
|
<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
|
||||||
|
|
||||||
|
# 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
|
||||||
@@ -16,9 +16,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {round(
|
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {round(
|
||||||
@endgame.rating
|
@endgame.rating
|
||||||
)} · Attempts: {@endgame.times_attempted ||
|
)} · Attempts: {@endgame.times_attempted ||
|
||||||
0} · Solved: {@endgame.times_solved || 0}<span :if={@user_rating}> · Your rating: {round(
|
0} · Solved: {@endgame.times_solved || 0}
|
||||||
@user_rating
|
|
||||||
)}</span>
|
|
||||||
<p class="text-xs text-muted flex items-center mt-1">
|
<p class="text-xs text-muted flex items-center mt-1">
|
||||||
<span>FEN: {@endgame.fen}</span>
|
<span>FEN: {@endgame.fen}</span>
|
||||||
<button
|
<button
|
||||||
@@ -42,6 +40,10 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
|
|||||||
</:actions>
|
</:actions>
|
||||||
</.header>
|
</.header>
|
||||||
|
|
||||||
|
<div :if={@user_rating} class="mb-4">
|
||||||
|
<.rating_guide rating={@user_rating} deviation={@user_deviation} />
|
||||||
|
</div>
|
||||||
|
|
||||||
<PlayComponents.play_surface
|
<PlayComponents.play_surface
|
||||||
endgame={@endgame}
|
endgame={@endgame}
|
||||||
status={@status}
|
status={@status}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do
|
|||||||
|> assign(:page_title, Keyword.get(opts, :page_title, "Play endgame"))
|
|> assign(:page_title, Keyword.get(opts, :page_title, "Play endgame"))
|
||||||
|> assign(:endgame, endgame)
|
|> assign(:endgame, endgame)
|
||||||
|> assign_new(:user_rating, fn -> user_rating(socket.assigns.current_scope) end)
|
|> assign_new(:user_rating, fn -> user_rating(socket.assigns.current_scope) end)
|
||||||
|
|> assign_new(:user_deviation, fn -> user_deviation(socket.assigns.current_scope) end)
|
||||||
|> assign(:fen, endgame.fen)
|
|> assign(:fen, endgame.fen)
|
||||||
|> assign(:tablebase, nil)
|
|> assign(:tablebase, nil)
|
||||||
|> assign(:status, :loading)
|
|> assign(:status, :loading)
|
||||||
@@ -441,6 +442,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do
|
|||||||
socket
|
socket
|
||||||
|> assign(:endgame, endgame)
|
|> assign(:endgame, endgame)
|
||||||
|> assign(:user_rating, user.rating)
|
|> assign(:user_rating, user.rating)
|
||||||
|
|> assign(:user_deviation, user.rating_deviation)
|
||||||
|> assign(:last_rating_delta, delta)
|
|> assign(:last_rating_delta, delta)
|
||||||
|
|
||||||
{:skipped, _attempt} ->
|
{:skipped, _attempt} ->
|
||||||
@@ -460,6 +462,9 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do
|
|||||||
defp user_rating(%{user: %{rating: rating}}), do: rating
|
defp user_rating(%{user: %{rating: rating}}), do: rating
|
||||||
defp user_rating(_), do: nil
|
defp user_rating(_), do: nil
|
||||||
|
|
||||||
|
defp user_deviation(%{user: %{rating_deviation: deviation}}), do: deviation
|
||||||
|
defp user_deviation(_), do: nil
|
||||||
|
|
||||||
defp pick_best_move(tablebase), do: tablebase |> best_moves() |> List.first()
|
defp pick_best_move(tablebase), do: tablebase |> best_moves() |> List.first()
|
||||||
|
|
||||||
defp best_moves(%Tablebase{moves: moves} = tablebase) when is_list(moves) do
|
defp best_moves(%Tablebase{moves: moves} = tablebase) when is_list(moves) do
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
<div>
|
<div>
|
||||||
<h2 class="text-lg font-semibold">Choose a difficulty</h2>
|
<h2 class="text-lg font-semibold">Choose a difficulty</h2>
|
||||||
<p class="mt-1 text-sm text-muted">
|
<p class="mt-1 text-sm text-muted">
|
||||||
Your rating: {round(@user_rating)} · bands of ±{Puzzles.band_width()}
|
Difficulty bands are ±{Puzzles.band_width()} around your rating.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<.link
|
<.link
|
||||||
@@ -146,6 +146,9 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
<.icon name="hero-x-mark" class="size-5" />
|
<.icon name="hero-x-mark" class="size-5" />
|
||||||
</.link>
|
</.link>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mt-4">
|
||||||
|
<.rating_guide rating={@user_rating} deviation={@user_deviation} />
|
||||||
|
</div>
|
||||||
<div class="mt-4 grid gap-2">
|
<div class="mt-4 grid gap-2">
|
||||||
<%= for {difficulty, label, description} <- difficulty_options(@user_rating) do %>
|
<%= for {difficulty, label, description} <- difficulty_options(@user_rating) do %>
|
||||||
<.button
|
<.button
|
||||||
@@ -179,7 +182,8 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
|> assign(:last_session, nil)
|
|> assign(:last_session, nil)
|
||||||
|> assign(:puzzle_started_mono, nil)
|
|> assign(:puzzle_started_mono, nil)
|
||||||
|> assign(:puzzle_counted, false)
|
|> assign(:puzzle_counted, false)
|
||||||
|> assign(:user_rating, socket.assigns.current_scope.user.rating)}
|
|> assign(:user_rating, socket.assigns.current_scope.user.rating)
|
||||||
|
|> assign(:user_deviation, socket.assigns.current_scope.user.rating_deviation)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
defmodule ChesstrainerWeb.RatingComponentsTest do
|
||||||
|
use ChesstrainerWeb.ConnCase, async: true
|
||||||
|
|
||||||
|
import Phoenix.LiveViewTest
|
||||||
|
|
||||||
|
alias ChesstrainerWeb.RatingComponents
|
||||||
|
|
||||||
|
test "shows the rating and the ±2 deviation confidence band" do
|
||||||
|
html = render_component(&RatingComponents.rating_guide/1, %{rating: 1500.0, deviation: 100.0})
|
||||||
|
|
||||||
|
assert html =~ "Your rating: 1500"
|
||||||
|
assert html =~ "likely 1300–1700"
|
||||||
|
assert html =~ "left: 40.0%"
|
||||||
|
assert html =~ "left: 32.0%"
|
||||||
|
assert html =~ "width: 16.0%"
|
||||||
|
assert html =~ "Rating 1500, likely between 1300 and 1700"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "omits the band when the deviation is unknown" do
|
||||||
|
html = render_component(&RatingComponents.rating_guide/1, %{rating: 1500.0, deviation: nil})
|
||||||
|
|
||||||
|
assert html =~ "Your rating: 1500"
|
||||||
|
refute html =~ ~s(id="rating-guide-range")
|
||||||
|
refute html =~ "bg-primary/30"
|
||||||
|
assert html =~ "Rating 1500 on a 500 to 3000 scale"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "clamps the marker to the drawn scale" do
|
||||||
|
low = render_component(&RatingComponents.rating_guide/1, %{rating: 100.0, deviation: nil})
|
||||||
|
high = render_component(&RatingComponents.rating_guide/1, %{rating: 4000.0, deviation: nil})
|
||||||
|
|
||||||
|
assert low =~ "left: 0.0%"
|
||||||
|
assert high =~ "left: 100.0%"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "accepts a custom id for the gauge" do
|
||||||
|
html =
|
||||||
|
render_component(&RatingComponents.rating_guide/1, %{
|
||||||
|
rating: 1500.0,
|
||||||
|
deviation: 100.0,
|
||||||
|
id: "session-rating"
|
||||||
|
})
|
||||||
|
|
||||||
|
assert html =~ ~s(id="session-rating")
|
||||||
|
assert html =~ ~s(id="session-rating-range")
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -501,6 +501,22 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
|||||||
assert attempt.outcome == :solved
|
assert attempt.outcome == :solved
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "shows a rating guide with the deviation band", %{conn: conn, user: user} do
|
||||||
|
endgame = endgame_fixture()
|
||||||
|
seed_tablebase(endgame.fen)
|
||||||
|
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
|
||||||
|
|
||||||
|
assert has_element?(view, "#rating-guide")
|
||||||
|
|
||||||
|
html = render(view)
|
||||||
|
low = round(user.rating - 2 * user.rating_deviation)
|
||||||
|
high = round(user.rating + 2 * user.rating_deviation)
|
||||||
|
|
||||||
|
assert html =~ "Your rating: #{round(user.rating)}"
|
||||||
|
assert html =~ "likely #{low}–#{high}"
|
||||||
|
end
|
||||||
|
|
||||||
test "records a failed attempt for the signed-in user", %{conn: conn, user: user} do
|
test "records a failed attempt for the signed-in user", %{conn: conn, user: user} do
|
||||||
endgame = endgame_fixture()
|
endgame = endgame_fixture()
|
||||||
seed_tablebase(endgame.fen, moves: grading_moves())
|
seed_tablebase(endgame.fen, moves: grading_moves())
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
assert has_element?(view, "#close-difficulty[href='/']")
|
assert has_element?(view, "#close-difficulty[href='/']")
|
||||||
assert has_element?(view, "a", "Cancel")
|
assert has_element?(view, "a", "Cancel")
|
||||||
refute has_element?(view, "#puzzle-search")
|
refute has_element?(view, "#puzzle-search")
|
||||||
|
assert has_element?(view, "#rating-guide")
|
||||||
assert render(view) =~ "Choose a difficulty"
|
assert render(view) =~ "Choose a difficulty"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -147,6 +148,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
assert is_float(assigns.last_rating_delta)
|
assert is_float(assigns.last_rating_delta)
|
||||||
assert assigns.last_rating_delta > 0
|
assert assigns.last_rating_delta > 0
|
||||||
assert assigns.user_rating > 1500.0
|
assert assigns.user_rating > 1500.0
|
||||||
|
assert assigns.user_deviation < 350.0
|
||||||
end
|
end
|
||||||
|
|
||||||
test "loads a different puzzle on next", %{conn: conn} do
|
test "loads a different puzzle on next", %{conn: conn} do
|
||||||
|
|||||||
Reference in New Issue
Block a user