Rate users and endgames with Glicko (#40)

This commit is contained in:
2026-11-03 22:04:51 +01:00
parent a3fb878383
commit 4a36ffd2e6
12 changed files with 228 additions and 16 deletions
+12 -1
View File
@@ -73,7 +73,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
<.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save">
<.input field={@form[:fen]} type="text" label="Fen" disabled={@live_action == :edit} />
<.input field={@form[:color]} type="text" label="Color" disabled />
<.input field={@form[:rating]} type="number" label="Rating" />
<.input
field={@form[:rating]}
type="number"
value={rating_display(@form[:rating])}
label="Rating"
/>
<.input field={@form[:message]} type="textarea" label="Message" />
<.input field={@form[:notes]} type="textarea" label="Notes" />
<.input
@@ -244,6 +249,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
end
end
# Ratings are stored as floats (Glicko); the admin form presents them as whole numbers.
defp rating_display(%Phoenix.HTML.FormField{value: value}) when is_float(value),
do: round(value)
defp rating_display(%Phoenix.HTML.FormField{value: value}), do: value
defp return_path("index", _endgame), do: ~p"/admin/endgames"
defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame}"
end
@@ -58,7 +58,7 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
<span :if={endgame.themes == []} class="text-base-content/50">None</span>
</td>
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer">
{endgame.rating}
{round(endgame.rating)}
</td>
<td class="w-0 font-semibold">
<div class="flex gap-4">
+28 -6
View File
@@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
alias Chesstrainer.Attempts
alias Chesstrainer.Endgames
alias Chesstrainer.Endgames.Tablebase
alias Chesstrainer.Ratings
@impl true
def render(assigns) do
@@ -12,8 +13,12 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
<.header>
Play endgame {@endgame.key}
<:subtitle>
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {@endgame.rating} · Attempts: {@endgame.times_attempted ||
0} · Solved: {@endgame.times_solved || 0}
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {round(
@endgame.rating
)} · Attempts: {@endgame.times_attempted ||
0} · Solved: {@endgame.times_solved || 0}<span :if={@user_rating}> · Your rating: {round(
@user_rating
)}</span>
<p class="text-xs text-gray-500 flex items-center mt-1">
<span>FEN: {@endgame.fen}</span>
<button
@@ -180,6 +185,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
socket
|> assign(:page_title, "Play endgame")
|> assign(:endgame, endgame)
|> assign(:user_rating, user_rating(socket.assigns.current_scope))
|> assign(:fen, endgame.fen)
|> assign(:tablebase, nil)
|> assign(:status, :loading)
@@ -658,18 +664,21 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
defp solve_attempt(socket) do
{:ok, endgame} = Endgames.record_solve(socket.assigns.endgame)
record_attempt(socket, :solved)
socket =
socket
|> assign(:endgame, endgame)
|> record_attempt(:solved)
{:noreply,
socket
|> assign(:status, :solved)
|> assign(:endgame, endgame)
|> assign(:awaiting_reply, false)
|> assign(:best_move, nil)}
end
defp fail_attempt(socket) do
record_attempt(socket, :failed)
socket = record_attempt(socket, :failed)
{:noreply,
socket
@@ -679,9 +688,22 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
end
defp record_attempt(socket, outcome) do
Attempts.record_attempt(socket.assigns.current_scope, socket.assigns.endgame, outcome)
case Attempts.record_attempt(socket.assigns.current_scope, socket.assigns.endgame, outcome) do
{:ok, attempt} ->
{:ok, %{user: user, endgame: endgame}} = Ratings.rate_attempt(attempt)
socket
|> assign(:endgame, endgame)
|> assign(:user_rating, user.rating)
_ ->
socket
end
end
defp user_rating(%{user: %{rating: rating}}), do: rating
defp user_rating(_), do: nil
defp best_move_text(tablebase) do
san =
tablebase
@@ -40,7 +40,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndex do
{endgame.result}
</td>
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer">
{endgame.rating}
{round(endgame.rating)}
</td>
<td>
<span :for={theme <- endgame.themes} class="badge badge-sm">
@@ -48,7 +48,7 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
<:item title="Fen">{@endgame.fen}</:item>
<:item title="Key">{@endgame.key}</:item>
<:item title="Color">{@endgame.color}</:item>
<:item title="Rating">{@endgame.rating}</:item>
<:item title="Rating">{round(@endgame.rating)}</:item>
<:item title="Message">{@endgame.message}</:item>
<:item title="Notes">{@endgame.notes}</:item>
<:item title="Result">{@endgame.result}</:item>