diff --git a/lib/chesstrainer/attempts.ex b/lib/chesstrainer/attempts.ex new file mode 100644 index 0000000..2237838 --- /dev/null +++ b/lib/chesstrainer/attempts.ex @@ -0,0 +1,26 @@ +defmodule Chesstrainer.Attempts do + @moduledoc """ + The Attempts context: a per-user history of finished playthroughs. + """ + + alias Chesstrainer.Repo + alias Chesstrainer.Accounts.{Scope, User} + alias Chesstrainer.Endgames.Endgame + alias Chesstrainer.Attempts.Attempt + + @doc """ + Records a finished attempt for the signed-in user in `scope`. + + Anonymous scopes record nothing and return `:ignored`. + """ + def record_attempt(nil, _endgame, _outcome), do: :ignored + + def record_attempt(%Scope{user: nil}, _endgame, _outcome), do: :ignored + + def record_attempt(%Scope{user: %User{} = user}, %Endgame{} = endgame, outcome) + when outcome in [:solved, :failed] do + %Attempt{user_id: user.id, endgame_id: endgame.id} + |> Attempt.changeset(%{outcome: outcome}) + |> Repo.insert() + end +end diff --git a/lib/chesstrainer/attempts/attempt.ex b/lib/chesstrainer/attempts/attempt.ex new file mode 100644 index 0000000..d3e5d2d --- /dev/null +++ b/lib/chesstrainer/attempts/attempt.ex @@ -0,0 +1,27 @@ +defmodule Chesstrainer.Attempts.Attempt do + @moduledoc """ + A per-user playthrough of an endgame, ending solved or failed. + """ + + use Ecto.Schema + import Ecto.Changeset + + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id + schema "attempts" do + field :outcome, Ecto.Enum, values: [:solved, :failed] + field :rated_at, :utc_datetime + + belongs_to :user, Chesstrainer.Accounts.User + belongs_to :endgame, Chesstrainer.Endgames.Endgame + + timestamps(type: :utc_datetime) + end + + @doc false + def changeset(attempt, attrs) do + attempt + |> cast(attrs, [:outcome, :rated_at]) + |> validate_required([:outcome]) + end +end diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index a5e094b..7c319a5 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -1,6 +1,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do use ChesstrainerWeb, :live_view + alias Chesstrainer.Attempts alias Chesstrainer.Endgames alias Chesstrainer.Endgames.Tablebase @@ -657,6 +658,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do defp solve_attempt(socket) do {:ok, endgame} = Endgames.record_solve(socket.assigns.endgame) + record_attempt(socket, :solved) {:noreply, socket @@ -667,6 +669,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do end defp fail_attempt(socket) do + record_attempt(socket, :failed) + {:noreply, socket |> assign(:status, :failed) @@ -674,6 +678,10 @@ defmodule ChesstrainerWeb.EndgameLive.Play do |> assign(:best_move, best_move_text(socket.assigns.tablebase))} end + defp record_attempt(socket, outcome) do + Attempts.record_attempt(socket.assigns.current_scope, socket.assigns.endgame, outcome) + end + defp best_move_text(tablebase) do san = tablebase diff --git a/priv/repo/migrations/20261103204832_create_attempts.exs b/priv/repo/migrations/20261103204832_create_attempts.exs new file mode 100644 index 0000000..e041281 --- /dev/null +++ b/priv/repo/migrations/20261103204832_create_attempts.exs @@ -0,0 +1,25 @@ +defmodule Chesstrainer.Repo.Migrations.CreateAttempts do + use Ecto.Migration + + def change do + create table(:attempts, primary_key: false) do + add :id, :binary_id, primary_key: true + add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false + + add :endgame_id, references(:endgames, type: :binary_id, on_delete: :delete_all), + null: false + + add :outcome, :string, null: false + add :rated_at, :utc_datetime + + timestamps(type: :utc_datetime) + end + + # Independent foreign-key indexes so cascade deletes stay cheap. + create index(:attempts, [:user_id]) + create index(:attempts, [:endgame_id]) + + # Rating-window lookup: the latest rated attempt for a user/endgame pair. + create index(:attempts, [:user_id, :endgame_id, :rated_at], where: "rated_at IS NOT NULL") + end +end diff --git a/test/chesstrainer/attempts_test.exs b/test/chesstrainer/attempts_test.exs new file mode 100644 index 0000000..d33680e --- /dev/null +++ b/test/chesstrainer/attempts_test.exs @@ -0,0 +1,75 @@ +defmodule Chesstrainer.AttemptsTest do + use Chesstrainer.DataCase, async: true + + import Chesstrainer.AccountsFixtures + import Chesstrainer.EndgamesFixtures + + alias Chesstrainer.Accounts.Scope + alias Chesstrainer.Attempts + alias Chesstrainer.Attempts.Attempt + alias Chesstrainer.Endgames + + describe "record_attempt/3" do + test "records a solved attempt for a signed-in user" do + user = user_fixture() + endgame = endgame_fixture() + + assert {:ok, %Attempt{} = attempt} = + Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + + assert attempt.user_id == user.id + assert attempt.endgame_id == endgame.id + assert attempt.outcome == :solved + assert is_nil(attempt.rated_at) + end + + test "records a failed attempt for a signed-in user" do + user = user_fixture() + endgame = endgame_fixture() + + assert {:ok, %Attempt{outcome: :failed}} = + Attempts.record_attempt(Scope.for_user(user), endgame, :failed) + end + + test "records nothing for an anonymous scope" do + endgame = endgame_fixture() + + assert :ignored = Attempts.record_attempt(nil, endgame, :solved) + assert Repo.aggregate(Attempt, :count) == 0 + end + end + + describe "cascade" do + test "deleting a user removes their attempts" do + user = user_fixture() + endgame = endgame_fixture() + {:ok, _attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + + Repo.delete!(user) + + assert Repo.aggregate(Attempt, :count) == 0 + end + + test "deleting an endgame removes attempts against it" do + user = user_fixture() + endgame = endgame_fixture() + {:ok, _attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + + assert {:ok, _} = Endgames.delete_endgame(endgame) + + assert Repo.aggregate(Attempt, :count) == 0 + end + end + + describe "rating-window index" do + test "rated rows are indexed by user, endgame and rated_at" do + %{rows: [[indexdef]]} = + Repo.query!( + "SELECT indexdef FROM pg_indexes " <> + "WHERE tablename = 'attempts' AND indexname = 'attempts_user_id_endgame_id_rated_at_index'" + ) + + assert indexdef =~ "WHERE (rated_at IS NOT NULL)" + end + 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 023db04..90f456d 100644 --- a/test/chesstrainer_web/live/endgame_live/play_test.exs +++ b/test/chesstrainer_web/live/endgame_live/play_test.exs @@ -4,11 +4,13 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do import Phoenix.LiveViewTest import Chesstrainer.EndgamesFixtures + alias Chesstrainer.Attempts.Attempt alias Chesstrainer.Endgames alias Chesstrainer.Endgames.Tablebase alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache alias Chesstrainer.FEN alias Chesstrainer.Ratelimiters + alias Chesstrainer.Repo defp assert_init_board(view, expected_fen, expected_orientation, expected_player_color) do %{proxy: {ref, _topic, _}} = view @@ -417,6 +419,63 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do end end + describe "per-user attempt records" do + setup :register_and_log_in_user + + test "records a solved attempt for the signed-in user", %{conn: conn, user: user} do + endgame = endgame_fixture() + 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") + + render_hook(view, "endgame_move_played", %{ + "from" => "d1", + "to" => "d7", + "san" => "Qd7+", + "prev_fen" => endgame.fen, + "fen" => fen_after, + "endgame_id" => endgame.id + }) + + assert render(view) =~ "Solved!" + assert [attempt] = Repo.all(Attempt) + assert attempt.user_id == user.id + assert attempt.endgame_id == endgame.id + assert attempt.outcome == :solved + end + + test "records a failed attempt for the signed-in user", %{conn: conn, user: user} do + endgame = endgame_fixture() + seed_tablebase(endgame.fen, moves: grading_moves()) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + play_move(view, endgame, "d1", "d4", "Qd4") + + assert render(view) =~ "Failed" + assert [attempt] = Repo.all(Attempt) + assert attempt.user_id == user.id + assert attempt.outcome == :failed + end + end + + describe "per-user attempt records without a signed-in user" do + test "records nothing for an anonymous visitor", %{conn: conn} do + endgame = endgame_fixture() + seed_tablebase(endgame.fen, moves: grading_moves()) + + {:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play") + + play_move(view, endgame, "d1", "d4", "Qd4") + + assert render(view) =~ "Failed" + assert Repo.aggregate(Attempt, :count) == 0 + end + end + describe "draw endgames" do test "loads and is playable", %{conn: conn} do endgame = endgame_fixture(result: :draw)