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.
This commit is contained in:
2026-11-12 21:13:54 +01:00
committed by GitHub
parent 9e76045862
commit 20b53dae6f
11 changed files with 135 additions and 49 deletions
@@ -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}
/>
<div :if={@user_rating} class="mb-4">
@@ -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
+17 -30
View File
@@ -65,17 +65,14 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
<span class="text-muted">Avg/puzzle</span>
<span class="font-mono">{format_duration(avg_ms(@session))}</span>
</span>
<span>
<span class="flex items-center gap-2">
<span class="text-muted">Rating</span>
<span class={["font-mono", gain_class(net_gain(@session, @user_rating))]}>
{format_delta(net_gain(@session, @user_rating))}
</span>
</span>
<span :if={@last_rating_delta}>
<span class="text-muted">Last</span>
<span class={["font-mono", gain_class(@last_rating_delta)]}>
{format_delta(@last_rating_delta)}
</span>
<span id="session-rating" class="font-mono font-semibold">{round(@user_rating)}</span>
<.rating_change
:if={@session.count > 0}
id="session-rating-change"
delta={net_gain(@session, @user_rating)}
/>
</span>
</div>
@@ -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 %>
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
@@ -126,11 +125,13 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
<span class="text-muted">Avg/puzzle</span>
<span class="font-mono">{format_duration(@last_session.avg_ms)}</span>
</span>
<span>
<span class="flex items-center gap-2">
<span class="text-muted">Rating</span>
<span class={["font-mono", gain_class(@last_session.gain)]}>
{format_delta(@last_session.gain)}
</span>
<span class="font-mono font-semibold">{round(@last_session.rating)}</span>
<.rating_change
:if={@last_session.count > 0}
delta={@last_session.gain}
/>
</span>
</div>
</div>
@@ -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