Files
2025-04-17 20:32:52 +02:00

46 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI, Form
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
import random
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
user_state = {}
@app.get("/")
def index():
return FileResponse("static/index.html")
@app.get("/api/challenge")
def challenge(username: str):
a1, b1 = random.randint(1,10), random.randint(1,10)
a2, b2 = random.randint(1,10), random.randint(1,10)
result = a1 * b1 + a2 * b2
user_state[username] = {"sum": result}
return {"task1": f"{a1} × {b1}", "task2": f"{a2} × {b2}"}
@app.post("/api/submit")
def submit(username: str = Form(...), answer: str = Form(...)):
print("[DEBUG] submit() called")
print("[DEBUG] username:", username)
print("[DEBUG] answer:", answer)
try:
parsed = int(answer)
except ValueError:
return JSONResponse(content={"result": "error", "message": "Answer must be a number"}, status_code=400)
user = user_state.get(username)
if not user or "sum" not in user:
return JSONResponse(content={"result": "error", "message": "No challenge found"}, status_code=400)
expected = user["sum"]
correct = parsed == expected
return JSONResponse(content={
"result": "correct" if correct else "wrong",
"correct": expected
})