From 2f3f39a21d072366175ef6adf72e01305f5e7df3 Mon Sep 17 00:00:00 2001 From: Owen Date: Thu, 5 Nov 2026 16:40:58 +0100 Subject: [PATCH] show live player count on landing page --- lib/chesstrainer/attempts.ex | 11 ++++++++++ .../components/landing_components.ex | 9 +++++---- .../controllers/page_controller.ex | 2 ++ .../controllers/page_html/home.html.heex | 4 ++-- test/chesstrainer/attempts_test.exs | 20 +++++++++++++++++++ .../components/landing_components_test.exs | 20 +++++++++---------- .../controllers/page_controller_test.exs | 14 +++++++++++++ 7 files changed, 64 insertions(+), 16 deletions(-) diff --git a/lib/chesstrainer/attempts.ex b/lib/chesstrainer/attempts.ex index b07de97..492224a 100644 --- a/lib/chesstrainer/attempts.ex +++ b/lib/chesstrainer/attempts.ex @@ -59,6 +59,17 @@ defmodule Chesstrainer.Attempts do |> Repo.one() end + @doc """ + Returns the number of users who have recorded at least one attempt. + + A retry does not count twice: the count is over distinct users. + """ + def count_users_with_attempts do + Attempt + |> select([a], count(a.user_id, :distinct)) + |> Repo.one() + end + @doc """ The distinct endgame ids `user_id` has attempted in the last `days` days. diff --git a/lib/chesstrainer_web/components/landing_components.ex b/lib/chesstrainer_web/components/landing_components.ex index d30c745..96eda76 100644 --- a/lib/chesstrainer_web/components/landing_components.ex +++ b/lib/chesstrainer_web/components/landing_components.ex @@ -78,6 +78,7 @@ defmodule ChesstrainerWeb.LandingComponents do LiveView, use `board_grid/1` directly. """ attr :endgame, :map, default: nil + attr :date, :any, default: nil def board_preview(assigns) do endgame = assigns.endgame @@ -188,20 +189,20 @@ defmodule ChesstrainerWeb.LandingComponents do end @doc """ - Formats the puzzle count for the landing-page stat. + Formats a landing-page stat count. Counts below 100 are shown exactly. Larger counts are floored to the nearest hundred or thousand and suffixed with `+`, so the claim is never overstated. """ - def puzzle_count_label(count) when is_integer(count) and count < 100 do + def count_label(count) when is_integer(count) and count < 100 do Integer.to_string(count) end - def puzzle_count_label(count) when is_integer(count) and count < 1000 do + def count_label(count) when is_integer(count) and count < 1000 do "#{div(count, 100) * 100}+" end - def puzzle_count_label(count) when is_integer(count) do + def count_label(count) when is_integer(count) do "#{div(count, 1000) * 1000}+" end diff --git a/lib/chesstrainer_web/controllers/page_controller.ex b/lib/chesstrainer_web/controllers/page_controller.ex index 5e385a7..9015d8c 100644 --- a/lib/chesstrainer_web/controllers/page_controller.ex +++ b/lib/chesstrainer_web/controllers/page_controller.ex @@ -1,6 +1,7 @@ defmodule ChesstrainerWeb.PageController do use ChesstrainerWeb, :controller + alias Chesstrainer.Attempts alias Chesstrainer.DailyPuzzles alias Chesstrainer.Endgames @@ -20,6 +21,7 @@ defmodule ChesstrainerWeb.PageController do endgames: endgames, active_endgame: DailyPuzzles.today(), puzzle_count: Endgames.count_endgames(), + user_count: Attempts.count_users_with_attempts(), today: Date.utc_today() ) end diff --git a/lib/chesstrainer_web/controllers/page_html/home.html.heex b/lib/chesstrainer_web/controllers/page_html/home.html.heex index fcb4ac6..9c24bea 100644 --- a/lib/chesstrainer_web/controllers/page_html/home.html.heex +++ b/lib/chesstrainer_web/controllers/page_html/home.html.heex @@ -23,7 +23,7 @@ ♜ - 12,400+ players training + {count_label(@user_count)} players training @@ -42,7 +42,7 @@ minutes a day
- {puzzle_count_label(@puzzle_count)} + {count_label(@puzzle_count)} puzzles
diff --git a/test/chesstrainer/attempts_test.exs b/test/chesstrainer/attempts_test.exs index d33680e..15a6f1f 100644 --- a/test/chesstrainer/attempts_test.exs +++ b/test/chesstrainer/attempts_test.exs @@ -39,6 +39,26 @@ defmodule Chesstrainer.AttemptsTest do end end + describe "count_users_with_attempts/0" do + test "counts distinct users who have recorded an attempt" do + first = user_fixture() + second = user_fixture() + endgame = endgame_fixture() + + {:ok, _} = Attempts.record_attempt(Scope.for_user(first), endgame, :solved) + {:ok, _} = Attempts.record_attempt(Scope.for_user(first), endgame, :failed) + {:ok, _} = Attempts.record_attempt(Scope.for_user(second), endgame, :solved) + + assert Attempts.count_users_with_attempts() == 2 + end + + test "ignores users without attempts" do + user_fixture() + + assert Attempts.count_users_with_attempts() == 0 + end + end + describe "cascade" do test "deleting a user removes their attempts" do user = user_fixture() diff --git a/test/chesstrainer_web/components/landing_components_test.exs b/test/chesstrainer_web/components/landing_components_test.exs index 170783c..7735475 100644 --- a/test/chesstrainer_web/components/landing_components_test.exs +++ b/test/chesstrainer_web/components/landing_components_test.exs @@ -54,23 +54,23 @@ defmodule ChesstrainerWeb.LandingComponentsTest do assert html =~ "currentColor" end - describe "puzzle_count_label/1" do + describe "count_label/1" do test "shows counts below 100 exactly" do - assert LandingComponents.puzzle_count_label(0) == "0" - assert LandingComponents.puzzle_count_label(47) == "47" - assert LandingComponents.puzzle_count_label(99) == "99" + assert LandingComponents.count_label(0) == "0" + assert LandingComponents.count_label(47) == "47" + assert LandingComponents.count_label(99) == "99" end test "floors counts of 100 or more to hundreds" do - assert LandingComponents.puzzle_count_label(100) == "100+" - assert LandingComponents.puzzle_count_label(199) == "100+" - assert LandingComponents.puzzle_count_label(999) == "900+" + assert LandingComponents.count_label(100) == "100+" + assert LandingComponents.count_label(199) == "100+" + assert LandingComponents.count_label(999) == "900+" end test "floors counts of 1000 or more to thousands" do - assert LandingComponents.puzzle_count_label(1000) == "1000+" - assert LandingComponents.puzzle_count_label(1999) == "1000+" - assert LandingComponents.puzzle_count_label(2400) == "2000+" + assert LandingComponents.count_label(1000) == "1000+" + assert LandingComponents.count_label(1999) == "1000+" + assert LandingComponents.count_label(2400) == "2000+" end end end diff --git a/test/chesstrainer_web/controllers/page_controller_test.exs b/test/chesstrainer_web/controllers/page_controller_test.exs index b3f5c8e..1a7f808 100644 --- a/test/chesstrainer_web/controllers/page_controller_test.exs +++ b/test/chesstrainer_web/controllers/page_controller_test.exs @@ -2,7 +2,10 @@ defmodule ChesstrainerWeb.PageControllerTest do use ChesstrainerWeb.ConnCase import Chesstrainer.AccountsFixtures + import Chesstrainer.EndgamesFixtures + alias Chesstrainer.Accounts.Scope + alias Chesstrainer.Attempts alias Chesstrainer.DailyPuzzles alias Chesstrainer.Endgames alias Chesstrainer.Themes @@ -89,6 +92,17 @@ defmodule ChesstrainerWeb.PageControllerTest do end end + test "hero shows the number of players with at least one attempt", %{conn: conn} do + user = user_fixture() + endgame = endgame_fixture() + {:ok, _} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) + + html = conn |> get(~p"/") |> html_response(200) + document = LazyHTML.from_document(html) + + assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "1" + end + test "guest navbar links to log in and register", %{conn: conn} do html = conn |> get(~p"/") |> html_response(200)