from fastapi import FastAPI, Request from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles import uvicorn import json import os app = FastAPI() # File to store high scores SCORE_FILE = "highscores.json" def load_scores(): if os.path.exists(SCORE_FILE): with open(SCORE_FILE, "r") as f: return json.load(f) return [] def save_scores(scores): with open(SCORE_FILE, "w") as f: json.dump(scores, f) app.mount("/static", StaticFiles(directory="."), name="static") @app.get("/") async def read_root(): return FileResponse("index.html") @app.get("/leaderboard") async def get_leaderboard(): scores = load_scores() return {"leaderboard": scores} @app.post("/update_score") async def update_score(request: Request): data = await request.json() username = data.get("username", "Anonymous") score = data.get("score", 0) scores = load_scores() scores.append({"username": username, "score": score}) # Sort by score descending and take top 3 scores = sorted(scores, key=lambda x: x['score'], reverse=True)[:3] save_scores(scores) return {"status": "success", "leaderboard": scores} @app.get("/status") async def status(): return {"status": "Crypto-Whack Backend Online with Persistence"} if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)