Statistiky: dokoncene hry rozdelene na s ludmi / s botmi

Karta "Dokoncene hry" (%) nahradena dvomi poctami dokoncenych hier
podla toho, ci na niektorom sedadle sedel bot (username "bot:...").
Zaroven oprava serializacie avg_game_duration_minutes -- Postgres
vracia z func.avg Decimal, ktory json.dumps nevie serializovat (500
na /api/admin/stats).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
tim
2026-07-10 22:25:32 +02:00
co-authored by Claude Opus 4.8
parent 8e18325660
commit 1636757217
3 changed files with 66 additions and 12 deletions
+36 -2
View File
@@ -22,7 +22,8 @@ import pyotp # noqa: E402
import api as api_module # noqa: E402
from api import auth, history, stats # noqa: E402
from db.db import init_db # noqa: E402
from db.db import async_session, init_db # noqa: E402
from db.models import Player # noqa: E402
CHROME_UA = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
@@ -62,6 +63,20 @@ class StatsCase(unittest.TestCase):
ids.append(ident["player_id"])
return ids
def _make_bot_player(self):
"""Vlozi boti ucet (username "bot:...") priamo do DB a vrati jeho id."""
async def _insert():
async with async_session() as session:
bot = Player(
username="bot:heuristic-" + uuid.uuid4().hex[:8],
totp_secret="x",
totp_last_step=1,
)
session.add(bot)
await session.commit()
return bot.id
return run(_insert())
def test_record_pageview_parses_user_agent(self):
run(stats.record_pageview(
path="/history", referrer="https://example.com", user_agent=CHROME_UA, ip="203.0.113.5",
@@ -307,6 +322,8 @@ class StatsCase(unittest.TestCase):
def test_daily_stats_reflect_games_and_players(self):
before = run(stats.get_daily_stats())
base_total_players = before["total_players"]
base_human_games = before["finished_human_games"]
base_bot_games = before["finished_bot_games"]
ids = self._make_players()
gid = str(uuid.uuid4())
@@ -315,7 +332,24 @@ class StatsCase(unittest.TestCase):
data = run(stats.get_daily_stats())
self.assertEqual(data["total_players"], base_total_players + 4)
self.assertEqual(data["completion_rate"], 1.0)
# Dokoncena hra so 4 ludskymi hracmi -> pripocita sa k "s ludmi", nie "s botmi"
self.assertEqual(data["finished_human_games"], base_human_games + 1)
self.assertEqual(data["finished_bot_games"], base_bot_games)
def test_game_with_a_bot_seat_counts_as_bot_game(self):
before = run(stats.get_daily_stats())
base_human_games = before["finished_human_games"]
base_bot_games = before["finished_bot_games"]
# 3 ludia + 1 bot na poslednom sedadle
ids = self._make_players(n=3) + [self._make_bot_player()]
gid = str(uuid.uuid4())
run(history.record_game_started(gid, "SBotom", ids))
run(history.record_completed_rounds(gid, make_core()))
data = run(stats.get_daily_stats())
self.assertEqual(data["finished_bot_games"], base_bot_games + 1)
self.assertEqual(data["finished_human_games"], base_human_games)
self.assertGreaterEqual(sum(data["games_per_day"].values()), 1)
self.assertGreaterEqual(sum(data["rounds_per_day"].values()), 4)