mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Add rating window and time-based RD inflation (#41)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user