From 20b53dae6f9ff8581d05c098bf3ae96ab8235a1a Mon Sep 17 00:00:00 2001 From: Owen Rees Date: Thu, 12 Nov 2026 21:13:54 +0100 Subject: [PATCH] Show rating changes on puzzle completion (#146) (#154) * 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. --- assets/css/base/_typography.css | 1 + .../components/core_components.ex | 8 +-- lib/chesstrainer_web/components/layouts.ex | 3 +- .../components/play_components.ex | 16 ++++-- .../components/rating_components.ex | 52 +++++++++++++++++++ .../live/endgame_live/play.ex | 2 + .../live/endgame_live/play_core.ex | 20 +++++++ .../live/endgame_live/train.ex | 47 ++++++----------- lib/chesstrainer_web/live/user_live/login.ex | 26 +++++----- .../live/endgame_live/play_test.exs | 3 ++ .../live/endgame_live/train_test.exs | 6 +++ 11 files changed, 135 insertions(+), 49 deletions(-) diff --git a/assets/css/base/_typography.css b/assets/css/base/_typography.css index ac454bb..42fd776 100644 --- a/assets/css/base/_typography.css +++ b/assets/css/base/_typography.css @@ -41,6 +41,7 @@ h1 em { color: var(--color-coral); font-weight: 400; } .settings-subtitle { font-size: 1rem;; color: var(--color-muted); + margin-top: 0.5rem; } @media (max-width: 650px) { diff --git a/lib/chesstrainer_web/components/core_components.ex b/lib/chesstrainer_web/components/core_components.ex index aa8d180..cce7ab7 100644 --- a/lib/chesstrainer_web/components/core_components.ex +++ b/lib/chesstrainer_web/components/core_components.ex @@ -65,7 +65,7 @@ defmodule ChesstrainerWeb.CoreComponents do attr :id, :string, doc: "the optional id of flash container" attr :flash, :map, default: %{}, doc: "the map of flash messages to display" attr :title, :string, default: nil - attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup" + attr :kind, :atom, values: [:info, :success, :error], doc: "used for styling and flash lookup" attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container" slot :inner_block, doc: "the optional inner block that renders the flash message" @@ -86,9 +86,11 @@ defmodule ChesstrainerWeb.CoreComponents do
<.icon :if={@kind == :info} name="hero-information-circle" class="size-5 shrink-0" /> + <.icon :if={@kind == :success} name="hero-check-circle" class="size-5 shrink-0" /> <.icon :if={@kind == :error} name="hero-exclamation-circle" class="size-5 shrink-0" />

{@title}

@@ -356,10 +358,10 @@ defmodule ChesstrainerWeb.CoreComponents do ~H"""
-

+

{render_slot(@inner_block)}

-

+

{render_slot(@subtitle)}

diff --git a/lib/chesstrainer_web/components/layouts.ex b/lib/chesstrainer_web/components/layouts.ex index 5b43e59..90699ac 100644 --- a/lib/chesstrainer_web/components/layouts.ex +++ b/lib/chesstrainer_web/components/layouts.ex @@ -247,9 +247,10 @@ defmodule ChesstrainerWeb.Layouts do
<.flash kind={:info} flash={@flash} /> + <.flash kind={:success} flash={@flash} /> <.flash kind={:error} flash={@flash} /> <.flash diff --git a/lib/chesstrainer_web/components/play_components.ex b/lib/chesstrainer_web/components/play_components.ex index 8747f24..a29553a 100644 --- a/lib/chesstrainer_web/components/play_components.ex +++ b/lib/chesstrainer_web/components/play_components.ex @@ -30,6 +30,8 @@ defmodule ChesstrainerWeb.PlayComponents do attr :move_input_error, :string, default: nil attr :last_check, :boolean, default: false attr :last_checkmate, :boolean, default: false + attr :rating, :any, default: nil + attr :rating_delta, :any, default: nil def play_surface(assigns) do assigns = @@ -250,9 +252,17 @@ defmodule ChesstrainerWeb.PlayComponents do

Make a move.

You play {colour_name(@endgame.color)}

<% end %> -

- Puzzle rating: {round(@endgame.rating)} -

+
+ Puzzle rating: {round(@endgame.rating)} + + Your rating + {round(@rating)} + <.rating_change :if={@rating_delta} id="rating-change" delta={@rating_delta} /> + +
+ <.icon name={change_icon(@delta)} class="size-3.5" /> + {format_delta(@delta)} + + """ + 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) diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index b6750ae..f584bb4 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -63,6 +63,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do move_input_error={@move_input_error} last_check={@last_check} last_checkmate={@last_checkmate} + rating={@user_rating} + rating_delta={@last_rating_delta} />
diff --git a/lib/chesstrainer_web/live/endgame_live/play_core.ex b/lib/chesstrainer_web/live/endgame_live/play_core.ex index 3fe3a01..3bb3fda 100644 --- a/lib/chesstrainer_web/live/endgame_live/play_core.ex +++ b/lib/chesstrainer_web/live/endgame_live/play_core.ex @@ -19,6 +19,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do alias Chesstrainer.Endgames alias Chesstrainer.Endgames.Tablebase alias Chesstrainer.Ratings + alias ChesstrainerWeb.RatingComponents @uci_pattern ~r/^[a-h][1-8][a-h][1-8][qrbn]?$/ @@ -517,6 +518,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do |> assign(:user_rating, user.rating) |> assign(:user_deviation, user.rating_deviation) |> assign(:last_rating_delta, delta) + |> put_rating_flash(outcome, user.rating, delta) {:skipped, _attempt} -> socket @@ -532,6 +534,24 @@ defmodule ChesstrainerWeb.EndgameLive.PlayCore do end end + # A finished rating change is shown as a flash so the player sees it + # immediately, whether they are in a session or playing a single endgame. + defp put_rating_flash(socket, outcome, rating, delta) do + kind = if outcome == :solved, do: :success, else: :error + put_flash(socket, kind, rating_change_message(socket, outcome, rating, delta)) + end + + defp rating_change_message(socket, :solved, rating, delta) do + result = if socket.assigns.endgame.result == :draw, do: "Draw held", else: "Solved" + + "#{result}! Your rating is now #{round(rating)} " <> + "(#{RatingComponents.format_delta(delta)})." + end + + defp rating_change_message(_socket, :failed, rating, delta) do + "Failed. Your rating is now #{round(rating)} (#{RatingComponents.format_delta(delta)})." + end + defp user_rating(%{user: %{rating: rating}}), do: rating defp user_rating(_), do: nil diff --git a/lib/chesstrainer_web/live/endgame_live/train.ex b/lib/chesstrainer_web/live/endgame_live/train.ex index 218836c..72bd281 100644 --- a/lib/chesstrainer_web/live/endgame_live/train.ex +++ b/lib/chesstrainer_web/live/endgame_live/train.ex @@ -65,17 +65,14 @@ defmodule ChesstrainerWeb.EndgameLive.Train do Avg/puzzle {format_duration(avg_ms(@session))} - + Rating - - {format_delta(net_gain(@session, @user_rating))} - - - - Last - - {format_delta(@last_rating_delta)} - + {round(@user_rating)} + <.rating_change + :if={@session.count > 0} + id="session-rating-change" + delta={net_gain(@session, @user_rating)} + />
@@ -99,6 +96,8 @@ defmodule ChesstrainerWeb.EndgameLive.Train do move_input_error={@move_input_error} last_check={@last_check} last_checkmate={@last_checkmate} + rating={@user_rating} + rating_delta={@last_rating_delta} /> <% else %>
@@ -126,11 +125,13 @@ defmodule ChesstrainerWeb.EndgameLive.Train do Avg/puzzle {format_duration(@last_session.avg_ms)} - + Rating - - {format_delta(@last_session.gain)} - + {round(@last_session.rating)} + <.rating_change + :if={@last_session.count > 0} + delta={@last_session.gain} + />
@@ -333,7 +334,8 @@ defmodule ChesstrainerWeb.EndgameLive.Train do duration_ms: elapsed_ms(session), count: session.count, avg_ms: avg_ms(session), - gain: net_gain(session, rating) + gain: net_gain(session, rating), + rating: rating } end @@ -373,19 +375,4 @@ defmodule ChesstrainerWeb.EndgameLive.Train do seconds = String.pad_leading(Integer.to_string(rem(total, 60)), 2, "0") "#{minutes}:#{seconds}" end - - defp format_delta(nil), do: "—" - - defp format_delta(delta) when is_number(delta) do - rounded = round(delta) - - cond do - rounded > 0 -> "+#{rounded}" - true -> Integer.to_string(rounded) - end - end - - defp gain_class(delta) when is_number(delta) and delta > 0, do: "text-success-text" - defp gain_class(delta) when is_number(delta) and delta < 0, do: "text-error-text" - defp gain_class(_delta), do: "text-muted" end diff --git a/lib/chesstrainer_web/live/user_live/login.ex b/lib/chesstrainer_web/live/user_live/login.ex index 6429d92..1cbaf57 100644 --- a/lib/chesstrainer_web/live/user_live/login.ex +++ b/lib/chesstrainer_web/live/user_live/login.ex @@ -7,18 +7,20 @@ defmodule ChesstrainerWeb.UserLive.Login do
-

Log in

- <%= if @current_scope do %> - You need to reauthenticate to perform sensitive actions on your account. - <% else %> -

- Don't have an account? <.link - navigate={~p"/users/register"} - class="font-semibold text-primary-text hover:underline" - phx-no-format - >Sign up for an account now. -

- <% end %> + <.header> + Log in + <:subtitle :if={@current_scope || !@prelaunch}> + <%= if @current_scope do %> + You need to reauthenticate to perform sensitive actions on your account. + <% else %> + Don't have an account? <.link + navigate={~p"/users/register"} + class="font-semibold text-primary-text hover:underline" + phx-no-format + >Sign up for an account now. + <% end %> + +
<.form diff --git a/test/chesstrainer_web/live/endgame_live/play_test.exs b/test/chesstrainer_web/live/endgame_live/play_test.exs index c72b290..0c21ade 100644 --- a/test/chesstrainer_web/live/endgame_live/play_test.exs +++ b/test/chesstrainer_web/live/endgame_live/play_test.exs @@ -495,6 +495,9 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do }) assert render(view) =~ "Solved!" + assert render(view) =~ "Your rating is now" + assert has_element?(view, "#your-rating") + assert has_element?(view, "#rating-change") assert [attempt] = Repo.all(Attempt) assert attempt.user_id == user.id assert attempt.endgame_id == endgame.id diff --git a/test/chesstrainer_web/live/endgame_live/train_test.exs b/test/chesstrainer_web/live/endgame_live/train_test.exs index 25f50bc..e2342fe 100644 --- a/test/chesstrainer_web/live/endgame_live/train_test.exs +++ b/test/chesstrainer_web/live/endgame_live/train_test.exs @@ -149,6 +149,11 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do assert assigns.last_rating_delta > 0 assert assigns.user_rating > 1500.0 assert assigns.user_deviation < 350.0 + + assert has_element?(view, "#session-rating") + assert has_element?(view, "#session-rating-change") + assert has_element?(view, "#rating-change") + assert render(view) =~ "Your rating is now" end test "loads a different puzzle on next", %{conn: conn} do @@ -205,6 +210,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do assert last.count == 1 assert is_float(last.gain) assert last.gain > 0 + assert last.rating > 1500.0 assert last.duration_ms >= 0 end