mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Record per-user Attempt history (#39)
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user