+
Last session
+
+
+ Difficulty
+ {difficulty_label(@last_session.difficulty)}
+
+
+ Time
+ {format_duration(@last_session.duration_ms)}
+
+
+ Puzzles
+ {@last_session.count}
+
+
+ Avg/puzzle
+ {format_duration(@last_session.avg_ms)}
+
+
+ Rating
+
+ {format_delta(@last_session.gain)}
+
+
+
+
Choose a difficulty
@@ -139,6 +175,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
socket
|> assign(:page_title, "Train")
|> assign(:session, nil)
+ |> assign(:last_session, nil)
|> assign(:puzzle_started_mono, nil)
|> assign(:puzzle_counted, false)
|> assign(:user_rating, socket.assigns.current_scope.user.rating)}
@@ -199,6 +236,12 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
end
def handle_event("stop_session", _params, socket) do
+ socket =
+ case socket.assigns.session do
+ nil -> socket
+ session -> assign(socket, :last_session, summarise(session, socket.assigns.user_rating))
+ end
+
{:noreply, assign(socket, :session, nil)}
end
@@ -261,6 +304,19 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
%{user | rating: socket.assigns.user_rating || user.rating}
end
+ defp premium?(%{user: %{plan: :premium}}), do: true
+ defp premium?(_scope), do: false
+
+ defp summarise(session, rating) do
+ %{
+ difficulty: session.difficulty,
+ duration_ms: elapsed_ms(session),
+ count: session.count,
+ avg_ms: avg_ms(session),
+ gain: net_gain(session, rating)
+ }
+ end
+
defp parse_difficulty(value) when value in ~w(easy medium hard),
do: String.to_existing_atom(value)
diff --git a/test/chesstrainer_web/live/endgame_live/train_test.exs b/test/chesstrainer_web/live/endgame_live/train_test.exs
index d0ba561..b918814 100644
--- a/test/chesstrainer_web/live/endgame_live/train_test.exs
+++ b/test/chesstrainer_web/live/endgame_live/train_test.exs
@@ -2,6 +2,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
use ChesstrainerWeb.ConnCase
import Phoenix.LiveViewTest
+ import Chesstrainer.AccountsFixtures
import Chesstrainer.EndgamesFixtures
alias Chesstrainer.Attempts.Attempt
@@ -87,6 +88,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
assert has_element?(view, "#difficulty-hard")
assert has_element?(view, "#close-difficulty[href='/']")
assert has_element?(view, "a", "Cancel")
+ refute has_element?(view, "#puzzle-search")
assert render(view) =~ "Choose a difficulty"
end
@@ -112,6 +114,21 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
end
end
+ describe "premium access" do
+ test "shows the puzzle search link for premium users", %{conn: conn} do
+ {:ok, user} =
+ user_fixture()
+ |> Ecto.Changeset.change(plan: :premium)
+ |> Repo.update()
+
+ conn = log_in_user(conn, user)
+ {:ok, view, _html} = live(conn, ~p"/train")
+
+ assert has_element?(view, "#puzzle-search[href='/endgames']")
+ assert render(view) =~ "Puzzle Search"
+ end
+ end
+
describe "playing a session" do
setup :register_and_log_in_user
@@ -156,6 +173,23 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
assert :sys.get_state(view.pid).socket.assigns.session.count == 1
end
+ test "stopping shows the finished session in the picker", %{conn: conn} do
+ {view, [_endgame]} = start_medium_session(conn, 1)
+ solve_current(view)
+
+ view |> element("button", "Stop session") |> render_click()
+
+ assert has_element?(view, "#last-session")
+ assert render(view) =~ "Last session"
+
+ last = :sys.get_state(view.pid).socket.assigns.last_session
+ assert last.difficulty == :medium
+ assert last.count == 1
+ assert is_float(last.gain)
+ assert last.gain > 0
+ assert last.duration_ms >= 0
+ end
+
test "stopping a session returns to the picker so a new one can start", %{conn: conn} do
{view, [_endgame]} = start_medium_session(conn, 1)