Spaces:
Sleeping
Sleeping
Download main.py from MarkoeZApollo/BrothersWhack: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/MarkoeZApollo/BrothersWhack/resolve/main/main.py
- Command line
-
hf download hf://spaces/MarkoeZApollo/BrothersWhack/main.py
-
curl -L -o main.py https://huggingface.co/spaces/MarkoeZApollo/BrothersWhack/resolve/main/main.py
2.01 kB
| from fastapi import FastAPI, HTTPException, Depends | |
| from fastapi.responses import FileResponse # Added this | |
| from pydantic import BaseModel | |
| from sqlalchemy import create_engine, Column, Integer, String | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker, Session | |
| from typing import List | |
| import os | |
| DATABASE_URL = "/data/crypto_whack.db" | |
| engine = create_engine(f"sqlite:///{DATABASE_URL}", connect_args={"check_same_thread": False}) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| class UserScore(Base): | |
| __tablename__ = "scores" | |
| user_id = Column(Integer, primary_key=True, index=True) | |
| username = Column(String) | |
| high_score = Column(Integer, default=0) | |
| os.makedirs("/data", exist_ok=True) | |
| Base.metadata.create_all(bind=engine) | |
| app = FastAPI() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| class ScoreUpdate(BaseModel): | |
| user_id: int | |
| username: str | |
| score: int | |
| # --- THIS IS THE FIX --- | |
| def read_root(): | |
| return FileResponse("index.html") # This serves the actual game file | |
| # ----------------------- | |
| def get_leaderboard(db: Session = Depends(get_db)): | |
| scores = db.query(UserScore).order_by(UserScore.high_score.desc()).limit(10).all() | |
| return [{"username": s.username, "score": s.high_score} for s in scores] | |
| def update_score(update: ScoreUpdate, db: Session = Depends(get_db)): | |
| user = db.query(UserScore).filter(UserScore.user_id == update.user_id).first() | |
| if user: | |
| if update.score > user.high_score: | |
| user.high_score = update.score | |
| db.commit() | |
| db.refresh(user) | |
| else: | |
| user = UserScore(user_id=update.user_id, username=update.username, high_score=update.score) | |
| db.add(user) | |
| db.commit() | |
| db.refresh(user) | |
| return {"status": "success", "high_score": user.high_score} |