mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
show live player count on landing page
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<i>♜</i>
|
||||
</span>
|
||||
<span>
|
||||
<strong>12,400+</strong> players training
|
||||
<strong>{count_label(@user_count)}</strong> players training
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -42,7 +42,7 @@
|
||||
<span>minutes a day</span>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<strong>{puzzle_count_label(@puzzle_count)}</strong>
|
||||
<strong>{count_label(@puzzle_count)}</strong>
|
||||
<span>puzzles</span>
|
||||
</div>
|
||||
<div class="stat">
|
||||
|
||||
Reference in New Issue
Block a user