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.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
<div class={[
|
||||
"flex w-full items-start gap-3 rounded-lg border px-4 py-3 text-sm text-wrap shadow-lg",
|
||||
@kind == :info && "border-info bg-info text-info-content",
|
||||
@kind == :success && "border-accent bg-accent text-accent-content",
|
||||
@kind == :error && "border-error bg-error text-error-content"
|
||||
]}>
|
||||
<.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" />
|
||||
<div>
|
||||
<p :if={@title} class="font-semibold">{@title}</p>
|
||||
@@ -356,10 +358,10 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
~H"""
|
||||
<header class={[@actions != [] && "flex items-center justify-between gap-6", "pb-4"]}>
|
||||
<div>
|
||||
<h1 class="text-lg font-semibold leading-8">
|
||||
<h1 class="settings-header">
|
||||
{render_slot(@inner_block)}
|
||||
</h1>
|
||||
<p :if={@subtitle != []} class="text-sm text-base-content/70 mt-4">
|
||||
<p :if={@subtitle != []} class="settings-subtitle">
|
||||
{render_slot(@subtitle)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -247,9 +247,10 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
<div
|
||||
id={@id}
|
||||
aria-live="polite"
|
||||
class="pointer-events-none fixed right-4 top-4 z-50 flex w-80 max-w-80 flex-col gap-2 sm:w-96 sm:max-w-96"
|
||||
class="pointer-events-none fixed left-4 top-4 z-50 flex w-80 max-w-80 flex-col gap-2 sm:w-96 sm:max-w-96"
|
||||
>
|
||||
<.flash kind={:info} flash={@flash} />
|
||||
<.flash kind={:success} flash={@flash} />
|
||||
<.flash kind={:error} flash={@flash} />
|
||||
|
||||
<.flash
|
||||
|
||||
@@ -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
|
||||
<p class="text-muted font-bold">Make a move.</p>
|
||||
<p class="text-muted">You play {colour_name(@endgame.color)}</p>
|
||||
<% end %>
|
||||
<p :if={@status in [:solved, :failed]} class="mt-1 text-xs text-muted">
|
||||
Puzzle rating: {round(@endgame.rating)}
|
||||
</p>
|
||||
<div
|
||||
:if={@status in [:solved, :failed]}
|
||||
class="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs"
|
||||
>
|
||||
<span class="text-muted">Puzzle rating: {round(@endgame.rating)}</span>
|
||||
<span :if={@rating} id="your-rating" class="flex items-center gap-1.5 text-muted">
|
||||
Your rating
|
||||
<span class="font-mono font-semibold text-base-content">{round(@rating)}</span>
|
||||
<.rating_change :if={@rating_delta} id="rating-change" delta={@rating_delta} />
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
|
||||
@@ -10,6 +10,8 @@ defmodule ChesstrainerWeb.RatingComponents do
|
||||
|
||||
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.
|
||||
@@ -78,6 +80,56 @@ defmodule ChesstrainerWeb.RatingComponents do
|
||||
"""
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -7,18 +7,20 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||
<div class="mx-auto max-w-sm space-y-4">
|
||||
<div class="text-center">
|
||||
<h1 class="settings-header">Log in</h1>
|
||||
<.header>
|
||||
Log in
|
||||
<:subtitle :if={@current_scope || !@prelaunch}>
|
||||
<%= if @current_scope do %>
|
||||
You need to reauthenticate to perform sensitive actions on your account.
|
||||
<% else %>
|
||||
<p :if={!@prelaunch} class="settings-subtitle my-4">
|
||||
Don't have an account? <.link
|
||||
navigate={~p"/users/register"}
|
||||
class="font-semibold text-primary-text hover:underline"
|
||||
phx-no-format
|
||||
>Sign up</.link> for an account now.
|
||||
</p>
|
||||
<% end %>
|
||||
</:subtitle>
|
||||
</.header>
|
||||
</div>
|
||||
|
||||
<.form
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user