diff --git a/config/config.exs b/config/config.exs index 5b21238..af2d934 100644 --- a/config/config.exs +++ b/config/config.exs @@ -24,9 +24,17 @@ config :chesstrainer, ecto_repos: [Chesstrainer.Repo], generators: [timestamp_type: :utc_datetime] -# Glicko rating system. `system_constant` is the volatility constraint passed -# through to the Glicko package. -config :chesstrainer, :ratings, system_constant: 0.8 +# Glicko rating system. +# +# `system_constant` is the volatility constraint passed through to the Glicko +# package. `window_days` is the minimum gap before replaying the same +# user/endgame pair may change either rating again. `rd_inflation_c` widens a +# rating deviation by sqrt(c^2 * days) while that side is inactive, capped at +# the Glicko maximum (350). +config :chesstrainer, :ratings, + system_constant: 0.8, + window_days: 7, + rd_inflation_c: 10.0 # Configure the endpoint config :chesstrainer, ChesstrainerWeb.Endpoint, diff --git a/lib/chesstrainer/attempts.ex b/lib/chesstrainer/attempts.ex index 2237838..99175ba 100644 --- a/lib/chesstrainer/attempts.ex +++ b/lib/chesstrainer/attempts.ex @@ -3,6 +3,8 @@ defmodule Chesstrainer.Attempts do The Attempts context: a per-user history of finished playthroughs. """ + import Ecto.Query, warn: false + alias Chesstrainer.Repo alias Chesstrainer.Accounts.{Scope, User} alias Chesstrainer.Endgames.Endgame @@ -23,4 +25,39 @@ defmodule Chesstrainer.Attempts do |> Attempt.changeset(%{outcome: outcome}) |> Repo.insert() end + + @doc """ + The timestamp of the most recent rated attempt for a user/endgame pair, or nil. + """ + def last_rated_at(user_id, endgame_id) do + Attempt + |> where([a], a.user_id == ^user_id and a.endgame_id == ^endgame_id) + |> rated() + |> select([a], max(a.rated_at)) + |> Repo.one() + end + + @doc """ + The timestamp of the most recent rated attempt for a user, or nil. + """ + def last_rated_at_for_user(user_id) do + Attempt + |> where([a], a.user_id == ^user_id) + |> rated() + |> select([a], max(a.rated_at)) + |> Repo.one() + end + + @doc """ + The timestamp of the most recent rated attempt on an endgame, or nil. + """ + def last_rated_at_for_endgame(endgame_id) do + Attempt + |> where([a], a.endgame_id == ^endgame_id) + |> rated() + |> select([a], max(a.rated_at)) + |> Repo.one() + end + + defp rated(query), do: where(query, [a], not is_nil(a.rated_at)) end diff --git a/lib/chesstrainer/ratings.ex b/lib/chesstrainer/ratings.ex index d4a2c31..a9d0b3d 100644 --- a/lib/chesstrainer/ratings.ex +++ b/lib/chesstrainer/ratings.ex @@ -2,37 +2,78 @@ defmodule Chesstrainer.Ratings do @moduledoc """ Applies Glicko rating updates to a finished attempt. - Both sides are rated: the user as the player and the endgame as the - opponent, then the reverse so the endgame's difficulty tracks its results. + Both sides are rated: the user as the player and the endgame as the opponent, + then the reverse so the endgame's difficulty tracks its results. + + An attempt only changes ratings when the same user/endgame pair has not been + rated inside the rating window. When it does rate, each side's deviation is + widened in proportion to how long that side has been inactive, so a return + after a long gap moves ratings further than a quick return. """ alias Chesstrainer.Repo alias Chesstrainer.Accounts.User alias Chesstrainer.Endgames.Endgame + alias Chesstrainer.Attempts alias Chesstrainer.Attempts.Attempt @default_system_constant 0.8 + @default_window_days 7 + @default_rd_inflation_c 10.0 + @max_rd 350.0 + @seconds_per_day 86_400 @doc """ Rates both sides of a finished attempt and stamps it as rated. A solve scores 1 for the user and 0 for the endgame; a failure scores 0 for - the user and 1 for the endgame. Returns the updated user, endgame and attempt. + the user and 1 for the endgame. Returns `{:ok, %{user, endgame, attempt}}`, or + `{:skipped, attempt}` when the pair was already rated inside the window. """ def rate_attempt(%Attempt{outcome: outcome} = attempt) do - user = Repo.get!(User, attempt.user_id) - endgame = Repo.get!(Endgame, attempt.endgame_id) + now = DateTime.utc_now(:second) - score = score_for(outcome) - user_player = player(user) - endgame_player = player(endgame) + if rate?(attempt, now) do + user = Repo.get!(User, attempt.user_id) + endgame = Repo.get!(Endgame, attempt.endgame_id) - new_user = - Glicko.new_rating(user_player, [Glicko.Result.new(endgame_player, score)], opts()) + user_rd = + inflate( + user.rating_deviation, + elapsed_days(now, Attempts.last_rated_at_for_user(user.id)) + ) - new_endgame = - Glicko.new_rating(endgame_player, [Glicko.Result.new(user_player, 1.0 - score)], opts()) + endgame_rd = + inflate( + endgame.rating_deviation, + elapsed_days(now, Attempts.last_rated_at_for_endgame(endgame.id)) + ) + user_player = player(user.rating, user_rd) + endgame_player = player(endgame.rating, endgame_rd) + + score = score_for(outcome) + + new_user = + Glicko.new_rating(user_player, [Glicko.Result.new(endgame_player, score)], opts()) + + new_endgame = + Glicko.new_rating(endgame_player, [Glicko.Result.new(user_player, 1.0 - score)], opts()) + + persist(user, endgame, attempt, new_user, new_endgame, now) + else + {:skipped, attempt} + end + end + + defp rate?(attempt, now) do + case Attempts.last_rated_at(attempt.user_id, attempt.endgame_id) do + nil -> true + last -> DateTime.diff(now, last, :second) >= window_days() * @seconds_per_day + end + end + + defp persist(user, endgame, attempt, new_user, new_endgame, now) do Repo.transaction(fn -> user = user @@ -52,22 +93,39 @@ defmodule Chesstrainer.Ratings do attempt = attempt - |> Ecto.Changeset.change(rated_at: DateTime.utc_now(:second)) + |> Ecto.Changeset.change(rated_at: now) |> Repo.update!() %{user: user, endgame: endgame, attempt: attempt} end) end - defp player(%{rating: rating, rating_deviation: rating_deviation}) do + defp player(rating, rating_deviation) do Glicko.Player.new_v1(rating: rating, rating_deviation: rating_deviation) end + # Widen a deviation by the time since that side last had a rated result, + # capped at the Glicko maximum. + defp inflate(rd, days) when days <= 0.0, do: rd + + defp inflate(rd, days) do + c = rd_inflation_c() + min(:math.sqrt(rd * rd + c * c * days), @max_rd) + end + + defp elapsed_days(_now, nil), do: 0.0 + + defp elapsed_days(now, last) do + max(DateTime.diff(now, last, :second), 0) / @seconds_per_day + end + defp score_for(:solved), do: 1.0 defp score_for(:failed), do: 0.0 - defp opts do - ratings = Application.get_env(:chesstrainer, :ratings, []) - [system_constant: Keyword.get(ratings, :system_constant, @default_system_constant)] - end + defp opts, do: [system_constant: system_constant()] + + defp config, do: Application.get_env(:chesstrainer, :ratings, []) + defp system_constant, do: Keyword.get(config(), :system_constant, @default_system_constant) + defp window_days, do: Keyword.get(config(), :window_days, @default_window_days) + defp rd_inflation_c, do: Keyword.get(config(), :rd_inflation_c, @default_rd_inflation_c) end diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index f1936ca..67ebf00 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -690,11 +690,19 @@ defmodule ChesstrainerWeb.EndgameLive.Play do defp record_attempt(socket, outcome) do case Attempts.record_attempt(socket.assigns.current_scope, socket.assigns.endgame, outcome) do {:ok, attempt} -> - {:ok, %{user: user, endgame: endgame}} = Ratings.rate_attempt(attempt) + case Ratings.rate_attempt(attempt) do + {:ok, %{user: user, endgame: endgame}} -> + socket + |> assign(:endgame, endgame) + |> assign(:user_rating, user.rating) - socket - |> assign(:endgame, endgame) - |> assign(:user_rating, user.rating) + {:skipped, _attempt} -> + put_flash( + socket, + :info, + "Replay recorded. Your rating is unchanged because you played this recently." + ) + end _ -> socket diff --git a/test/chesstrainer/ratings_test.exs b/test/chesstrainer/ratings_test.exs index f4e4ed9..fe7e4f6 100644 --- a/test/chesstrainer/ratings_test.exs +++ b/test/chesstrainer/ratings_test.exs @@ -52,4 +52,85 @@ defmodule Chesstrainer.RatingsTest do assert Repo.get!(Attempt, attempt.id).rated_at end end + + describe "rating window" do + test "skips a replay inside the window and leaves the attempt unrated" do + user = user_fixture() + endgame = endgame_fixture(rating: 1800) + + {:ok, first} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + {:ok, _} = Ratings.rate_attempt(first) + rated_user = Repo.get!(User, user.id) + rated_endgame = Repo.get!(Endgame, endgame.id) + + {:ok, replay} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + + assert {:skipped, %Attempt{rated_at: nil}} = Ratings.rate_attempt(replay) + + assert Repo.get!(User, user.id).rating == rated_user.rating + assert Repo.get!(Endgame, endgame.id).rating == rated_endgame.rating + assert Repo.get!(Attempt, replay.id).rated_at == nil + end + + test "rates a replay after the window" do + user = user_fixture() + endgame = endgame_fixture(rating: 1800) + + {:ok, first} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + {:ok, _} = Ratings.rate_attempt(first) + backdate!(first, 8) + + {:ok, replay} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + + assert {:ok, %{attempt: %Attempt{rated_at: rated_at}}} = Ratings.rate_attempt(replay) + refute is_nil(rated_at) + end + end + + describe "inactivity" do + test "a longer absence produces a larger rating change" do + short = + rating_delta_after_gap( + 8, + "4k3/8/8/8/8/8/1K6/8 w - - 0 1", + "4k3/8/8/8/8/8/2K5/8 w - - 0 1" + ) + + long = + rating_delta_after_gap( + 90, + "4k3/8/8/8/8/8/3K4/8 w - - 0 1", + "4k3/8/8/8/8/8/4K3/8 w - - 0 1" + ) + + assert long > short + end + end + + defp rating_delta_after_gap(days, prior_fen, target_fen) do + {:ok, user} = + user_fixture() + |> Ecto.Changeset.change(rating: 1500.0, rating_deviation: 100.0) + |> Repo.update() + + # A prior rated result on another endgame `days` ago, without changing the + # user's rating, so only the inactivity inflation differs between runs. + prior_endgame = endgame_fixture(fen: prior_fen) + {:ok, prior} = Attempts.record_attempt(Scope.for_user(user), prior_endgame, :solved) + backdate!(prior, days) + + target = endgame_fixture(fen: target_fen, rating: 1500.0) + {:ok, attempt} = Attempts.record_attempt(Scope.for_user(user), target, :solved) + {:ok, %{user: rated}} = Ratings.rate_attempt(attempt) + + rated.rating - 1500.0 + end + + defp backdate!(attempt, days) do + attempt + |> Ecto.Changeset.change( + rated_at: DateTime.add(DateTime.utc_now(:second), -days * 86_400, :second) + ) + |> Repo.update!() + end end diff --git a/test/chesstrainer_web/live/endgame_live/play_test.exs b/test/chesstrainer_web/live/endgame_live/play_test.exs index 4182069..cb84591 100644 --- a/test/chesstrainer_web/live/endgame_live/play_test.exs +++ b/test/chesstrainer_web/live/endgame_live/play_test.exs @@ -486,6 +486,40 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do assert rated_user.rating > 1500.0 assert Endgames.get_endgame!(endgame.id).rating < 1800.0 end + + test "does not re-rate a replay within the window", %{conn: conn, user: user} do + endgame = endgame_fixture(rating: 1800) + fen_after = "8/8/8/8/8/8/8/8 b - - 0 1" + + seed_tablebase(endgame.fen, moves: grading_moves()) + seed_tablebase(fen_after, checkmate: true, moves: []) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + play = fn -> + render_hook(view, "endgame_move_played", %{ + "from" => "d1", + "to" => "d7", + "san" => "Qd7+", + "prev_fen" => endgame.fen, + "fen" => fen_after, + "endgame_id" => endgame.id + }) + end + + play.() + rated_user = Repo.get!(User, user.id) + rated_endgame = Endgames.get_endgame!(endgame.id) + + render_click(view, "reset") + play.() + + html = render(view) + + assert html =~ "rating is unchanged" + assert Repo.get!(User, user.id).rating == rated_user.rating + assert Endgames.get_endgame!(endgame.id).rating == rated_endgame.rating + end end describe "per-user attempt records without a signed-in user" do