diff --git a/lib/chesstrainer_web/components/landing_components.ex b/lib/chesstrainer_web/components/landing_components.ex index 862a3ab..d30c745 100644 --- a/lib/chesstrainer_web/components/landing_components.ex +++ b/lib/chesstrainer_web/components/landing_components.ex @@ -88,7 +88,8 @@ defmodule ChesstrainerWeb.LandingComponents do turn: turn_label(endgame), title: title(endgame), themes: theme_names(endgame), - orientation: orientation(endgame) + orientation: orientation(endgame), + date: Calendar.strftime(assigns.date || Date.utc_today(), "%d %b %Y") ) ~H""" @@ -101,6 +102,7 @@ defmodule ChesstrainerWeb.LandingComponents do {@turn} +
{@date}
<.board_grid fen={@fen} orientation={@orientation} /> diff --git a/lib/chesstrainer_web/components/layouts.ex b/lib/chesstrainer_web/components/layouts.ex index 0c7f425..a34b350 100644 --- a/lib/chesstrainer_web/components/layouts.ex +++ b/lib/chesstrainer_web/components/layouts.ex @@ -109,9 +109,14 @@ defmodule ChesstrainerWeb.Layouts do end defp nav_links(scope) do - marketing = [ - %{label: "Puzzle Search", href: "/endgames"} - ] + marketing = + if premium?(scope) do + [ + %{label: "Puzzle Search", href: "/endgames"} + ] + else + [] + end admin = if admin?(scope) do @@ -142,6 +147,8 @@ defmodule ChesstrainerWeb.Layouts do defp admin?(%{user: %{role: role}}), do: Role.admin?(role) defp admin?(_scope), do: false + defp premium?(scope), do: match?(%{user: %{plan: :premium}}, scope) + # Signed-in users start a puzzle session; visitors browse the public list. defp start_href(%{user: %{}}), do: ~p"/train" defp start_href(_scope), do: ~p"/endgames" diff --git a/lib/chesstrainer_web/controllers/page_controller.ex b/lib/chesstrainer_web/controllers/page_controller.ex index 70cb445..5e385a7 100644 --- a/lib/chesstrainer_web/controllers/page_controller.ex +++ b/lib/chesstrainer_web/controllers/page_controller.ex @@ -19,7 +19,8 @@ defmodule ChesstrainerWeb.PageController do |> render(:home, endgames: endgames, active_endgame: DailyPuzzles.today(), - puzzle_count: Endgames.count_endgames() + puzzle_count: Endgames.count_endgames(), + today: Date.utc_today() ) end end diff --git a/lib/chesstrainer_web/controllers/page_html/home.html.heex b/lib/chesstrainer_web/controllers/page_html/home.html.heex index d36a2f9..fcb4ac6 100644 --- a/lib/chesstrainer_web/controllers/page_html/home.html.heex +++ b/lib/chesstrainer_web/controllers/page_html/home.html.heex @@ -31,7 +31,7 @@
- <.board_preview endgame={@active_endgame} /> + <.board_preview endgame={@active_endgame} date={@today} />
diff --git a/lib/chesstrainer_web/live/endgame_live/train.ex b/lib/chesstrainer_web/live/endgame_live/train.ex index 2daf9bf..5edd14e 100644 --- a/lib/chesstrainer_web/live/endgame_live/train.ex +++ b/lib/chesstrainer_web/live/endgame_live/train.ex @@ -30,8 +30,12 @@ defmodule ChesstrainerWeb.EndgameLive.Train do <.button :if={@session} phx-click="stop_session"> <.icon name="hero-x-mark" /> Stop session - <.button navigate={~p"/endgames"}> - <.icon name="hero-arrow-left" /> Endgames + <.button + :if={premium?(@current_scope)} + id="puzzle-search" + navigate={~p"/endgames"} + > + <.icon name="hero-magnifying-glass" /> Puzzle Search @@ -93,6 +97,38 @@ defmodule ChesstrainerWeb.EndgameLive.Train do <% else %>
+
+

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)