mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
show date, hide puzzle search for non premium
This commit is contained in:
@@ -88,7 +88,8 @@ defmodule ChesstrainerWeb.LandingComponents do
|
|||||||
turn: turn_label(endgame),
|
turn: turn_label(endgame),
|
||||||
title: title(endgame),
|
title: title(endgame),
|
||||||
themes: theme_names(endgame),
|
themes: theme_names(endgame),
|
||||||
orientation: orientation(endgame)
|
orientation: orientation(endgame),
|
||||||
|
date: Calendar.strftime(assigns.date || Date.utc_today(), "%d %b %Y")
|
||||||
)
|
)
|
||||||
|
|
||||||
~H"""
|
~H"""
|
||||||
@@ -101,6 +102,7 @@ defmodule ChesstrainerWeb.LandingComponents do
|
|||||||
<strong>{@turn}</strong>
|
<strong>{@turn}</strong>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
<div>{@date}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<.board_grid fen={@fen} orientation={@orientation} />
|
<.board_grid fen={@fen} orientation={@orientation} />
|
||||||
|
|||||||
@@ -109,9 +109,14 @@ defmodule ChesstrainerWeb.Layouts do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp nav_links(scope) do
|
defp nav_links(scope) do
|
||||||
marketing = [
|
marketing =
|
||||||
|
if premium?(scope) do
|
||||||
|
[
|
||||||
%{label: "Puzzle Search", href: "/endgames"}
|
%{label: "Puzzle Search", href: "/endgames"}
|
||||||
]
|
]
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
admin =
|
admin =
|
||||||
if admin?(scope) do
|
if admin?(scope) do
|
||||||
@@ -142,6 +147,8 @@ defmodule ChesstrainerWeb.Layouts do
|
|||||||
defp admin?(%{user: %{role: role}}), do: Role.admin?(role)
|
defp admin?(%{user: %{role: role}}), do: Role.admin?(role)
|
||||||
defp admin?(_scope), do: false
|
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.
|
# Signed-in users start a puzzle session; visitors browse the public list.
|
||||||
defp start_href(%{user: %{}}), do: ~p"/train"
|
defp start_href(%{user: %{}}), do: ~p"/train"
|
||||||
defp start_href(_scope), do: ~p"/endgames"
|
defp start_href(_scope), do: ~p"/endgames"
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ defmodule ChesstrainerWeb.PageController do
|
|||||||
|> render(:home,
|
|> render(:home,
|
||||||
endgames: endgames,
|
endgames: endgames,
|
||||||
active_endgame: DailyPuzzles.today(),
|
active_endgame: DailyPuzzles.today(),
|
||||||
puzzle_count: Endgames.count_endgames()
|
puzzle_count: Endgames.count_endgames(),
|
||||||
|
today: Date.utc_today()
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
<div class="board-stage" id="trainer">
|
<div class="board-stage" id="trainer">
|
||||||
<div class="coral-orbit" />
|
<div class="coral-orbit" />
|
||||||
<div class="mint-orbit" />
|
<div class="mint-orbit" />
|
||||||
<.board_preview endgame={@active_endgame} />
|
<.board_preview endgame={@active_endgame} date={@today} />
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
@@ -30,8 +30,12 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
<.button :if={@session} phx-click="stop_session">
|
<.button :if={@session} phx-click="stop_session">
|
||||||
<.icon name="hero-x-mark" /> Stop session
|
<.icon name="hero-x-mark" /> Stop session
|
||||||
</.button>
|
</.button>
|
||||||
<.button navigate={~p"/endgames"}>
|
<.button
|
||||||
<.icon name="hero-arrow-left" /> Endgames
|
:if={premium?(@current_scope)}
|
||||||
|
id="puzzle-search"
|
||||||
|
navigate={~p"/endgames"}
|
||||||
|
>
|
||||||
|
<.icon name="hero-magnifying-glass" /> Puzzle Search
|
||||||
</.button>
|
</.button>
|
||||||
</:actions>
|
</:actions>
|
||||||
</.header>
|
</.header>
|
||||||
@@ -93,6 +97,38 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
<% else %>
|
<% else %>
|
||||||
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
|
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
|
||||||
<div id="difficulty-picker" class="w-full max-w-md rounded-xl bg-base-100 p-6 shadow-xl">
|
<div id="difficulty-picker" class="w-full max-w-md rounded-xl bg-base-100 p-6 shadow-xl">
|
||||||
|
<div
|
||||||
|
:if={@last_session}
|
||||||
|
id="last-session"
|
||||||
|
class="mb-4 rounded-lg bg-canvas-raised p-3 text-sm"
|
||||||
|
>
|
||||||
|
<p class="mb-1 font-semibold">Last session</p>
|
||||||
|
<div class="flex flex-wrap gap-x-4 gap-y-1 text-xs">
|
||||||
|
<span>
|
||||||
|
<span class="text-muted">Difficulty</span>
|
||||||
|
<span class="font-semibold">{difficulty_label(@last_session.difficulty)}</span>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<span class="text-muted">Time</span>
|
||||||
|
<span class="font-mono">{format_duration(@last_session.duration_ms)}</span>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<span class="text-muted">Puzzles</span>
|
||||||
|
<span class="font-mono">{@last_session.count}</span>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<span class="text-muted">Avg/puzzle</span>
|
||||||
|
<span class="font-mono">{format_duration(@last_session.avg_ms)}</span>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<span class="text-muted">Rating</span>
|
||||||
|
<span class={["font-mono", gain_class(@last_session.gain)]}>
|
||||||
|
{format_delta(@last_session.gain)}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="flex items-start justify-between gap-4">
|
<div class="flex items-start justify-between gap-4">
|
||||||
<div>
|
<div>
|
||||||
<h2 class="text-lg font-semibold">Choose a difficulty</h2>
|
<h2 class="text-lg font-semibold">Choose a difficulty</h2>
|
||||||
@@ -139,6 +175,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
socket
|
socket
|
||||||
|> assign(:page_title, "Train")
|
|> assign(:page_title, "Train")
|
||||||
|> assign(:session, nil)
|
|> assign(:session, nil)
|
||||||
|
|> assign(:last_session, nil)
|
||||||
|> assign(:puzzle_started_mono, nil)
|
|> assign(:puzzle_started_mono, nil)
|
||||||
|> assign(:puzzle_counted, false)
|
|> assign(:puzzle_counted, false)
|
||||||
|> assign(:user_rating, socket.assigns.current_scope.user.rating)}
|
|> assign(:user_rating, socket.assigns.current_scope.user.rating)}
|
||||||
@@ -199,6 +236,12 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("stop_session", _params, socket) do
|
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)}
|
{:noreply, assign(socket, :session, nil)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -261,6 +304,19 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|
|||||||
%{user | rating: socket.assigns.user_rating || user.rating}
|
%{user | rating: socket.assigns.user_rating || user.rating}
|
||||||
end
|
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),
|
defp parse_difficulty(value) when value in ~w(easy medium hard),
|
||||||
do: String.to_existing_atom(value)
|
do: String.to_existing_atom(value)
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
use ChesstrainerWeb.ConnCase
|
use ChesstrainerWeb.ConnCase
|
||||||
|
|
||||||
import Phoenix.LiveViewTest
|
import Phoenix.LiveViewTest
|
||||||
|
import Chesstrainer.AccountsFixtures
|
||||||
import Chesstrainer.EndgamesFixtures
|
import Chesstrainer.EndgamesFixtures
|
||||||
|
|
||||||
alias Chesstrainer.Attempts.Attempt
|
alias Chesstrainer.Attempts.Attempt
|
||||||
@@ -87,6 +88,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
assert has_element?(view, "#difficulty-hard")
|
assert has_element?(view, "#difficulty-hard")
|
||||||
assert has_element?(view, "#close-difficulty[href='/']")
|
assert has_element?(view, "#close-difficulty[href='/']")
|
||||||
assert has_element?(view, "a", "Cancel")
|
assert has_element?(view, "a", "Cancel")
|
||||||
|
refute has_element?(view, "#puzzle-search")
|
||||||
assert render(view) =~ "Choose a difficulty"
|
assert render(view) =~ "Choose a difficulty"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -112,6 +114,21 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
|||||||
end
|
end
|
||||||
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
|
describe "playing a session" do
|
||||||
setup :register_and_log_in_user
|
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
|
assert :sys.get_state(view.pid).socket.assigns.session.count == 1
|
||||||
end
|
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
|
test "stopping a session returns to the picker so a new one can start", %{conn: conn} do
|
||||||
{view, [_endgame]} = start_medium_session(conn, 1)
|
{view, [_endgame]} = start_medium_session(conn, 1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user