Bonus za sérii a hájení plných bodů 3,5 s (#26)
CI / test (push) Successful in 19s
CI / docker (push) Successful in 5s

Série nově ovlivňuje body, aby běžná hra a připravovaný živý režim pro třídu (`zivy-rezim-trida`) bodovaly srovnatelně. OpenSpec změna `bonus-za-serii`.

## Změny
- Za každou odpověď `spravne` se k bodům z časové křivky přičte bonus `min(série − 1, 5) × 10 %` základního maxima režimu (*Psaní* +100 až +500 b., *A–D* +50 až +250 b.). Bonus nezávisí na čase.
- `max_body` počítá s nepřerušenou sérií (4 otázky *Psaní* = 4600, 8 otázek *A–D* = 5250). `odpovedi.body` obsahují bonus, součet dál sedí s `kola.body`.
- API po odpovědi vrací `streak_bonus`. Zpětná vazba ukazuje body za odpověď a štítek „+200 za sérii ×3“ zvlášť, pod prstencem je štítek s bonusem za příští správnou odpověď.
- **BREAKING:** výchozí `POZNAVACKY_SCORE_GRACE_SECONDS` je `3.5` (dosud `2.0`). Starší kola se nepřepočítávají.
- `docs/navrh.md` a hlavní specifikace `answer-scoring` a `quiz-gameplay` aktualizovány, změna archivována.

## Ověření
- `pytest` 357 passed, `ruff check` bez chyb.
- Ručně v Chrome nad syntetickou sadou o 8 položkách: *Psaní* bonus 0 / +100 / … / +500 / +500, *A–D* 0 / +50 / … / +250, po chybě bez bonusu, shrnutí „z 5 250 možných“.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_013nPfHU2PPEvpqm9w9WixvF
Reviewed-on: #26
Co-authored-by: David Spáčil <spacildavi@gmail.com>
This commit was merged in pull request #26.
This commit is contained in:
david-spacil authored and David Spáčil committed 2026-11-29 18:00:45 +01:00
1 parent 745fb341c0
commit 4e52d46e7f
18 files changed
+617 -63

No files matched your search

+64 -11
View File
@@ -501,6 +501,7 @@ def test_anonymous_writing_round_combo_summary_and_no_db_write(
assert r1["feedback"]["verdict"] == "spravne"
assert r1["feedback"]["points"] == 1000
assert r1["feedback"]["combo_after"] == 1
assert r1["feedback"]["streak_bonus"] == 0
client.post(f"/hra/{token}/dalsi", json={}, headers={"X-CSRF-Token": csrf})
@@ -515,6 +516,7 @@ def test_anonymous_writing_round_combo_summary_and_no_db_write(
assert r2["feedback"]["verdict"] == "castecne"
assert r2["feedback"]["points"] == 500
assert r2["feedback"]["combo_after"] == 0
assert r2["feedback"]["streak_bonus"] == 0
client.post(f"/hra/{token}/dalsi", json={}, headers={"X-CSRF-Token": csrf})
@@ -527,6 +529,7 @@ def test_anonymous_writing_round_combo_summary_and_no_db_write(
).get_json()
assert r3["feedback"]["verdict"] == "spatne"
assert r3["feedback"]["points"] == 0
assert r3["feedback"]["streak_bonus"] == 0
assert r3["feedback"]["is_last_question"] is True
# Přechod na shrnutí.
@@ -536,7 +539,8 @@ def test_anonymous_writing_round_combo_summary_and_no_db_write(
assert summary_state["phase"] == "summary"
summary = summary_state["summary"]
assert summary["total_points"] == 1500
assert summary["max_points"] == 3000
# Maximum počítá s nepřerušenou sérií: 1000 + 1100 + 1200.
assert summary["max_points"] == 3300
assert summary["accuracy"] == 0.5
assert summary["accuracy_percent"] == 50.0
assert summary["best_combo"] == 1
@@ -548,6 +552,50 @@ def test_anonymous_writing_round_combo_summary_and_no_db_write(
assert db.execute("SELECT COUNT(*) FROM odpovedi").fetchone()[0] == 0
def test_feedback_reports_streak_bonus_separately(
app: Flask,
client: FlaskClient,
db: sqlite3.Connection,
) -> None:
clock = FakeClock(50.0)
app.extensions["monotonic_clock"] = clock
app.extensions["game_rng"] = lambda: random.Random(3)
_create_set_with_items(
db,
slug="psani-serie",
nazev="Série psaní",
items_spec=[
{"nazev": "Sýkora koňadra", "photos": 1},
{"nazev": "Kos černý", "photos": 1},
{"nazev": "Vrabec domácí", "photos": 1},
],
)
token, csrf = _start_round(client, "psani-serie", rezim="psani")
rnd = app.extensions["round_store"]._rounds[token]
names = [q.canonical_name for q in rnd.questions]
feedback: dict[str, Any] = {}
for idx, name in enumerate(names):
# První dvě v grace period, třetí za 9250 ms (koeficient 0.65).
clock.advance(1.0 if idx < 2 else 9.25)
feedback = client.post(
f"/hra/{token}/odpoved",
json={"answer": name},
headers={"X-CSRF-Token": csrf},
).get_json()["feedback"]
if idx < 2:
client.post(
f"/hra/{token}/dalsi", json={}, headers={"X-CSRF-Token": csrf}
)
assert feedback["verdict"] == "spravne"
assert feedback["combo_after"] == 3
assert feedback["points"] == 850
assert feedback["streak_bonus"] == 200
def test_abcd_round_and_authenticated_persistence_and_records(
app: Flask,
db: sqlite3.Connection,
@@ -593,7 +641,8 @@ def test_abcd_round_and_authenticated_persistence_and_records(
assert bad_idx.status_code == 400
assert db.execute("SELECT COUNT(*) FROM kola").fetchone()[0] == 0
# 2. kolo: kompletní průchod A–D s 2 správnými a 1 chybnou odpovědí.
# 2. kolo: kompletní průchod A–D s 2 správnými a 1 chybnou odpovědí
# (2. správná dostane bonus za sérii 2: +50 b.).
token, csrf = _start_round(student, "ptaci-abcd", rezim="abcd")
student.get(f"/hra/{token}/stav")
rnd = app.extensions["round_store"]._rounds[token]
@@ -612,7 +661,8 @@ def test_abcd_round_and_authenticated_persistence_and_records(
).get_json()
expected_verdict = "spravne" if idx < 2 else "spatne"
assert res["feedback"]["verdict"] == expected_verdict
assert res["feedback"]["points"] == (500 if idx < 2 else 0)
assert res["feedback"]["points"] == [500, 550, 0][idx]
assert res["feedback"]["streak_bonus"] == [0, 50, 0][idx]
student.post(
f"/hra/{token}/dalsi",
@@ -622,8 +672,8 @@ def test_abcd_round_and_authenticated_persistence_and_records(
final_state = student.get(f"/hra/{token}/stav").get_json()
assert final_state["phase"] == "summary"
assert final_state["summary"]["total_points"] == 1000
assert final_state["summary"]["max_points"] == 1500
assert final_state["summary"]["total_points"] == 1050
assert final_state["summary"]["max_points"] == 1650
assert final_state["summary"]["persisted"] is True
assert final_state["summary"]["is_new_record"] is True
@@ -631,18 +681,20 @@ def test_abcd_round_and_authenticated_persistence_and_records(
kola_rows = db.execute("SELECT * FROM kola").fetchall()
assert len(kola_rows) == 1
assert kola_rows[0]["rezim"] == "abcd"
assert kola_rows[0]["body"] == 1000
assert kola_rows[0]["max_body"] == 1500
assert kola_rows[0]["body"] == 1050
assert kola_rows[0]["max_body"] == 1650
assert kola_rows[0]["dokonceno_at"] is not None
odp_rows = db.execute("SELECT * FROM odpovedi").fetchall()
assert len(odp_rows) == 3
# `odpovedi.body` obsahují bonus, takže jejich součet sedí s `kola.body`.
assert sum(row["body"] for row in odp_rows) == kola_rows[0]["body"]
# Rekord se zobrazí v katalogu i detailu sady.
cat_html = student.get("/").get_data(as_text=True)
assert "A–D 1000 b." in cat_html
assert "A–D 1050 b." in cat_html
det_html = student.get("/sada/ptaci-abcd").get_data(as_text=True)
assert "1000 /" in det_html
assert "1050 /" in det_html
def test_static_game_js_assets_served(client: FlaskClient) -> None:
@@ -1024,7 +1076,8 @@ def test_practice_mistakes_from_set_detail_and_record_exclusion(
rnd = app.extensions["round_store"]._rounds[tok]
assert {q.item_id for q in rnd.questions} == {a, b}
# Dohrajeme kolo chyby se 100 % (2000 b.): uloží se do DB, ale NENÍ rekord.
# Dohrajeme kolo chyby se 100 % (1000 + 1100 b. se sérií): uloží se do DB,
# ale NENÍ rekord.
last_data: dict[str, Any] = {}
for q in rnd.questions:
clock.advance(1.0)
@@ -1043,7 +1096,7 @@ def test_practice_mistakes_from_set_detail_and_record_exclusion(
kolo_row = db.execute("SELECT typ_kola, body FROM kola").fetchone()
assert kolo_row is not None
assert kolo_row["typ_kola"] == "chyby"
assert kolo_row["body"] == 2000
assert kolo_row["body"] == 2100
# V detailu ani katalogu se kolo chyby nesmí zobrazit jako osobní rekord.
det_after = student.get("/sada/chyby-sada").get_data(as_text=True)
+40 -11
View File
@@ -9,6 +9,7 @@ from poznavacky.scoring import (
AnswerItemSpec,
ScoringConfig,
calculate_question_points,
calculate_question_score,
calculate_round_max_points,
calculate_weighted_accuracy,
damerau_levenshtein,
@@ -34,7 +35,7 @@ def _item(
def test_scoring_config_defaults_and_custom_env() -> None:
cfg = load_config(environ={}, overrides={"TESTING": True})
assert cfg.score_grace_seconds == 2.0
assert cfg.score_grace_seconds == 3.5
assert cfg.score_max_seconds == 15.0
assert cfg.score_floor_ratio == 0.30
assert cfg.quick_round_size == 10
@@ -82,19 +83,19 @@ def test_scoring_config_rejects_invalid_values(
def test_time_scoring_curve_default_boundaries() -> None:
cfg = ScoringConfig()
# Grace period 0-2 s -> 100 % maxima.
# Grace period 0-3.5 s -> 100 % maxima.
assert calculate_question_points("psani", "spravne", 0, cfg) == 1000
assert calculate_question_points("psani", "spravne", 1800, cfg) == 1000
assert calculate_question_points("psani", "spravne", 2000, cfg) == 1000
assert calculate_question_points("psani", "spravne", 3400, cfg) == 1000
assert calculate_question_points("psani", "spravne", 3500, cfg) == 1000
assert calculate_question_points("psani", "castecne", 1500, cfg) == 500
assert calculate_question_points("abcd", "spravne", 2000, cfg) == 500
assert calculate_question_points("abcd", "spravne", 3500, cfg) == 500
assert calculate_question_points("psani", "spatne", 500, cfg) == 0
assert calculate_question_points("abcd", "spatne", 500, cfg) == 0
# Střed intervalu (8.5 s mezi 2.0 a 15.0 s -> koeficient 0.65).
assert calculate_question_points("psani", "spravne", 8500, cfg) == 650
assert calculate_question_points("psani", "castecne", 8500, cfg) == 325
assert calculate_question_points("abcd", "spravne", 8500, cfg) == 325
# Střed intervalu (9.25 s mezi 3.5 a 15.0 s -> koeficient 0.65).
assert calculate_question_points("psani", "spravne", 9250, cfg) == 650
assert calculate_question_points("psani", "castecne", 9250, cfg) == 325
assert calculate_question_points("abcd", "spravne", 9250, cfg) == 325
# Strop a nad stropem -> podlaha 30 %.
assert calculate_question_points("psani", "spravne", 15000, cfg) == 300
@@ -104,6 +105,33 @@ def test_time_scoring_curve_default_boundaries() -> None:
assert calculate_question_points("psani", "spatne", 20000, cfg) == 0
def test_streak_bonus_grows_and_caps() -> None:
totals = [
calculate_question_score("psani", "spravne", 1000, streak).total
for streak in range(1, 8)
]
assert totals == [1000, 1100, 1200, 1300, 1400, 1500, 1500]
def test_streak_bonus_is_time_independent_and_halved_in_abcd() -> None:
score = calculate_question_score("abcd", "spravne", 20000, 3)
assert score.time_points == 150
assert score.streak_bonus == 100
assert score.total == 250
def test_partial_answer_gets_no_bonus_and_breaks_streak() -> None:
# Po dvou `spravne` přijde `castecne` (série -> 0) a pak `spravne`
# (série 1).
partial = calculate_question_score("psani", "castecne", 1000, 0)
assert (partial.time_points, partial.streak_bonus) == (500, 0)
# Ani případná nenulová série bonus k `castecne` nepřidá.
assert calculate_question_score("psani", "castecne", 1000, 3).total == 500
after = calculate_question_score("psani", "spravne", 1000, 1)
assert (after.time_points, after.streak_bonus) == (1000, 0)
assert calculate_question_score("psani", "spatne", 1000, 0).total == 0
def test_time_scoring_curve_custom_parameters() -> None:
cfg = ScoringConfig(grace_seconds=3.0, max_seconds=13.0, floor_ratio=0.5)
assert calculate_question_points("psani", "spravne", 3000, cfg) == 1000
@@ -116,8 +144,9 @@ def test_weighted_accuracy_and_round_max_points() -> None:
assert calculate_weighted_accuracy(2, 1, 4) == pytest.approx(0.625)
assert calculate_weighted_accuracy(0, 0, 0) == 0.0
assert calculate_weighted_accuracy(3, 0, 3) == 1.0
assert calculate_round_max_points("psani", 4) == 4000
assert calculate_round_max_points("abcd", 4) == 2000
assert calculate_round_max_points("psani", 4) == 4600
assert calculate_round_max_points("abcd", 8) == 5250
assert calculate_round_max_points("abcd", 0) == 0
def test_normalize_and_damerau_levenshtein() -> None: