Spaces:
Sleeping
Sleeping
File size: 1,399 Bytes
5645638 ae79671 c22a760 5645638 54d8d37 5645638 c22a760 54d8d37 ae79671 c22a760 5645638 c22a760 5645638 c22a760 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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)
|