mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user