End the attempt on solve or fail and count attempts

#9: solve the puzzle on checkmate or a winning promotion, fail
immediately on a losing move (revealing the missed best move), and
increment times_attempted when an attempt starts and times_solved when
it is solved. Terminal states lock play; reset starts a new attempt.
This commit is contained in:
2026-11-01 22:01:19 +01:00
parent 248504cc58
commit c5989628bc
3 changed files with 176 additions and 14 deletions
+16
View File
@@ -105,6 +105,22 @@ defmodule Chesstrainer.Endgames do
Endgame.changeset(endgame, attrs)
end
@doc """
Records the start of an attempt, incrementing `times_attempted`.
"""
def record_attempt(%Endgame{id: id}), do: increment(id, times_attempted: 1)
@doc """
Records a solved attempt, incrementing `times_solved`.
"""
def record_solve(%Endgame{id: id}), do: increment(id, times_solved: 1)
defp increment(id, increments) do
Repo.update_all(from(e in Endgame, where: e.id == ^id), inc: increments)
{:ok, Repo.get!(Endgame, id)}
end
def add_tag(endgame, tag) do
endgame
|> Repo.preload(:tags)