Spaces:
Sleeping
Sleeping
Download main.py from MarksAssistantApollo/BrothersWhack: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/MarksAssistantApollo/BrothersWhack/resolve/main/main.py
- Command line
-
hf download hf://spaces/MarksAssistantApollo/BrothersWhack/main.py
-
curl -L -o main.py https://huggingface.co/spaces/MarksAssistantApollo/BrothersWhack/resolve/main/main.py
1.4 kB
| 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") | |
| async def read_root(): | |
| return FileResponse("index.html") | |
| async def get_leaderboard(): | |
| scores = load_scores() | |
| return {"leaderboard": scores} | |
| 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} | |
| async def status(): | |
| return {"status": "Crypto-Whack Backend Online with Persistence"} | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |