feat: add scoreboard with player names, per-category leaderboards, and themed tables
Add player_name column to games (migration included), with regex-based validation and anti-abuse sanitization. New /scoreboard page shows recent games from localStorage, popular categories with play counts, and top-10 per-category leaderboards sorted by completion time. Also includes two-click reverse word matching, base URL prefix support for reverse-proxy hosting, BasePageHandler refactoring, themed table CSS for all 5 themes, and comprehensive test coverage for player names and scoreboard API. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
9319f08875
commit
36c3c712fe
@@ -258,3 +258,101 @@ async def test_guess_cells_with_null_element(http_server_client, seeded_db):
|
||||
assert resp.code == 200
|
||||
data = json.loads(resp.body)
|
||||
assert data["correct"] is False
|
||||
|
||||
|
||||
async def test_new_game_with_player_name(http_server_client, seeded_db):
|
||||
body = json.dumps(
|
||||
{"category_id": seeded_db.id, "board_size": 10, "player_name": "Alice"}
|
||||
)
|
||||
resp = await http_server_client.fetch(
|
||||
"/api/game/new",
|
||||
method="POST",
|
||||
body=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
assert resp.code == 200
|
||||
data = json.loads(resp.body)
|
||||
assert data["player_name"] == "Alice"
|
||||
|
||||
|
||||
async def test_new_game_default_player_name(http_server_client, seeded_db):
|
||||
body = json.dumps({"category_id": seeded_db.id, "board_size": 10})
|
||||
resp = await http_server_client.fetch(
|
||||
"/api/game/new",
|
||||
method="POST",
|
||||
body=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
assert resp.code == 200
|
||||
data = json.loads(resp.body)
|
||||
assert data["player_name"] == "Anonymous"
|
||||
|
||||
|
||||
async def test_new_game_invalid_player_name_chars(http_server_client, seeded_db):
|
||||
body = json.dumps(
|
||||
{"category_id": seeded_db.id, "player_name": "<script>alert(1)</script>"}
|
||||
)
|
||||
resp = await http_server_client.fetch(
|
||||
"/api/game/new",
|
||||
method="POST",
|
||||
body=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
raise_error=False,
|
||||
)
|
||||
assert resp.code == 400
|
||||
data = json.loads(resp.body)
|
||||
assert "error" in data
|
||||
|
||||
|
||||
async def test_scoreboard_empty(http_server_client, seeded_db):
|
||||
resp = await http_server_client.fetch("/api/scoreboard")
|
||||
assert resp.code == 200
|
||||
data = json.loads(resp.body)
|
||||
assert data["leaderboards"] == {}
|
||||
assert isinstance(data["popular_categories"], list)
|
||||
|
||||
|
||||
async def test_scoreboard_with_completed_game(http_server_client, seeded_db):
|
||||
# Create a game with few words so we can complete it
|
||||
body = json.dumps(
|
||||
{
|
||||
"category_id": seeded_db.id,
|
||||
"board_size": 10,
|
||||
"word_count": 3,
|
||||
"player_name": "TestPlayer",
|
||||
}
|
||||
)
|
||||
create_resp = await http_server_client.fetch(
|
||||
"/api/game/new",
|
||||
method="POST",
|
||||
body=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
game_data = json.loads(create_resp.body)
|
||||
game_id = game_data["id"]
|
||||
|
||||
# Find all words by text guess
|
||||
for w in game_data["words"]:
|
||||
guess_body = json.dumps({"word": w["text"]})
|
||||
resp = await http_server_client.fetch(
|
||||
f"/api/game/{game_id}/guess",
|
||||
method="POST",
|
||||
body=guess_body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
assert resp.code == 200
|
||||
|
||||
# Verify game is completed
|
||||
state_resp = await http_server_client.fetch(f"/api/game/{game_id}")
|
||||
state_data = json.loads(state_resp.body)
|
||||
assert state_data["status"] == "completed"
|
||||
|
||||
# Now check scoreboard
|
||||
sb_resp = await http_server_client.fetch("/api/scoreboard")
|
||||
assert sb_resp.code == 200
|
||||
sb_data = json.loads(sb_resp.body)
|
||||
assert "animals" in sb_data["leaderboards"]
|
||||
entry = sb_data["leaderboards"]["animals"][0]
|
||||
assert entry["player_name"] == "TestPlayer"
|
||||
assert entry["time_seconds"] >= 0
|
||||
assert entry["board_size"] == 10
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import pytest
|
||||
|
||||
from app import make_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
return make_app()
|
||||
|
||||
|
||||
async def test_homepage_returns_200(http_server_client):
|
||||
response = await http_server_client.fetch("/")
|
||||
assert response.code == 200
|
||||
|
||||
|
||||
async def test_game_returns_200(http_server_client):
|
||||
response = await http_server_client.fetch("/game")
|
||||
assert response.code == 200
|
||||
|
||||
|
||||
async def test_howtoplay_returns_200(http_server_client):
|
||||
response = await http_server_client.fetch("/howtoplay")
|
||||
assert response.code == 200
|
||||
|
||||
|
||||
async def test_about_returns_200(http_server_client):
|
||||
response = await http_server_client.fetch("/about")
|
||||
assert response.code == 200
|
||||
|
||||
|
||||
async def test_scoreboard_returns_200(http_server_client):
|
||||
response = await http_server_client.fetch("/scoreboard")
|
||||
assert response.code == 200
|
||||
Reference in New Issue
Block a user