Weight puzzle selection and stop repeats inside a session (#183)

Selection took a hard rating band (medium = user rating ± 100), removed
everything attempted in the last 30 days, and drew uniformly from what was
left. Because the bulk importer gives every endgame in a batch the same
rating, the band held either the whole library or — once a user's rating
drifted past it — only the endgames that user had already played. With
`fresh` empty, the fallback re-drew from the same band ignoring recency, so
recently played puzzles came straight back. An empty band used a
deterministic nearest-match, alternating between the same two puzzles.

Difficulty is now a bias rather than a filter: candidates are weighted by
`exp(-|rating - target| / spread)` and drawn with an exponential race, so
the whole library stays reachable and a thin region around the user's rating
can never empty the pool. `easy` and `hard` keep one-sided guards so the
labels stay honest. When every candidate is excluded, selection serves the
least recently attempted endgame instead of a random recent one.

Sessions now carry the ids they have served, which fixes the observed
in-session repeat (only the current puzzle used to be excluded), and guest
training gets the same treatment. The 30-day window is configurable as the
repeat window and is now named in GLOSSARY.md, distinct from the rating
window and from the tablebase cooldown.

Difficulty copy is qualitative, since the band numbers no longer describe
how selection works.
This commit is contained in:
2026-11-17 14:28:24 +01:00
committed by GitHub
parent fa1fef7b62
commit e0b446bbfb
8 changed files with 212 additions and 115 deletions
@@ -135,7 +135,10 @@ defmodule ChesstrainerWeb.EndgameLive.GuestTrain do
@impl true
def mount(_params, _session, socket) do
socket = assign(socket, :limit, GuestLimits.limit())
socket =
socket
|> assign(:limit, GuestLimits.limit())
|> assign(:served_ids, [])
cond do
signed_in?(socket) ->
@@ -217,11 +220,7 @@ defmodule ChesstrainerWeb.EndgameLive.GuestTrain do
|> assign(:limit_reached, retry_ms)
{:ok, _remaining} ->
endgame =
Endgames.random_endgame(exclude_ids: [socket.assigns.endgame.id]) ||
Endgames.random_endgame()
case endgame do
case random_unserved(socket) do
nil -> assign(socket, :empty, :exhausted)
endgame -> start_guest_puzzle(socket, socket.assigns.guest_address, endgame)
end
@@ -234,13 +233,21 @@ defmodule ChesstrainerWeb.EndgameLive.GuestTrain do
{:ok, limit_wall(socket, address, retry_ms)}
{:ok, _remaining} ->
case Endgames.random_endgame() do
case random_unserved(socket) do
nil -> {:ok, empty_pool(socket, address)}
endgame -> {:ok, start_guest_puzzle(socket, address, endgame)}
end
end
end
# Prefer an endgame this visitor has not seen in this session. Once the whole
# pool is served — only possible on a very long guest session — start over
# rather than dead-end.
defp random_unserved(socket) do
Endgames.random_endgame(exclude_ids: socket.assigns.served_ids) ||
Endgames.random_endgame()
end
defp start_guest_puzzle(socket, address, endgame) do
case GuestLimits.take(address) do
{:ok, remaining} ->
@@ -250,6 +257,7 @@ defmodule ChesstrainerWeb.EndgameLive.GuestTrain do
|> assign(:guest_remaining, remaining)
|> assign(:limit_reached, nil)
|> assign(:empty, nil)
|> assign(:served_ids, [endgame.id | socket.assigns.served_ids])
|> PlayCore.init(endgame, page_title: "Train")
{:limit_reached, retry_ms} ->
+13 -11
View File
@@ -106,7 +106,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
id="pool-exhausted"
class="rounded-xl border border-hairline bg-canvas-raised p-6"
>
<h2 class="text-lg font-semibold">You've finished every puzzle at this difficulty</h2>
<h2 class="text-lg font-semibold">You've played every puzzle available right now</h2>
<p class="mt-1 text-sm text-muted">
There are no more {difficulty_label(@session.difficulty)} puzzles right now.
Try again, or pick another difficulty.
@@ -161,7 +161,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
<div>
<h2 class="text-lg font-semibold">Choose a difficulty</h2>
<p class="mt-1 text-sm text-muted">
Difficulty bands are ±{Puzzles.band_width()} around your rating.
Easy draws easier endgames, hard draws harder ones — your rating sets the centre.
</p>
</div>
<.link
@@ -190,7 +190,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
</.button>
</div>
<div class="mt-4 grid gap-2">
<%= for {difficulty, label, description} <- difficulty_options(@user_rating) do %>
<%= for {difficulty, label, description} <- difficulty_options() do %>
<.button
id={"difficulty-#{difficulty}"}
phx-click="start_session"
@@ -315,7 +315,8 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
started_at_ms: System.system_time(:millisecond),
baseline: socket.assigns.user_rating,
count: 0,
sum_ms: 0
sum_ms: 0,
served_ids: [endgame.id]
}
socket
@@ -333,13 +334,16 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
if session do
case Puzzles.next_endgame(selection_user(socket), session.difficulty,
exclude_ids: [socket.assigns.endgame.id]
exclude_ids: session.served_ids
) do
nil ->
assign(socket, :pool, %{difficulty: session.difficulty})
endgame ->
session = %{session | served_ids: [endgame.id | session.served_ids]}
socket
|> assign(:session, session)
|> assign(:pool, nil)
|> assign(:puzzle_started_mono, System.monotonic_time(:millisecond))
|> assign(:puzzle_counted, false)
@@ -395,13 +399,11 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
defp parse_difficulty(_value), do: :medium
defp difficulty_options(rating) do
width = Puzzles.band_width()
defp difficulty_options do
[
{:easy, "Easy", "below #{round(rating - width)}"},
{:medium, "Medium", "#{round(rating - width)}–#{round(rating + width)}"},
{:hard, "Hard", "above #{round(rating + width)}"}
{:easy, "Easy", "easier than your rating"},
{:medium, "Medium", "around your rating"},
{:hard, "Hard", "harder than your rating"}
]
end