forked from yaso_meth/mih-project
NEW: MIH MineSweeper Package pt2
This commit is contained in:
BIN
backend/.DS_Store
vendored
BIN
backend/.DS_Store
vendored
Binary file not shown.
@@ -20,6 +20,7 @@ import routers.mzansi_wallet as mzansi_wallet
|
||||
import routers.mzansi_directory as mzansi_directory
|
||||
import routers.user_consent as user_consent
|
||||
import routers.icd10_codes as icd10_codes
|
||||
import routers.mine_sweeper_leaderboard as mine_sweeper_leaderboard
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.middleware import Middleware
|
||||
from supertokens_python import get_all_cors_headers
|
||||
@@ -98,6 +99,7 @@ app.include_router(mzansi_directory.router)
|
||||
app.include_router(user_consent.router)
|
||||
app.include_router(icd10_codes.router)
|
||||
app.include_router(appointments.router)
|
||||
app.include_router(mine_sweeper_leaderboard.router)
|
||||
|
||||
# Check if server is up
|
||||
@app.get("/", tags=["Server Check"])
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import DateTime, Column, Integer, String, text
|
||||
from sqlalchemy import DateTime, Column, Integer, String, DECIMAL, text
|
||||
from sqlalchemy.orm import declarative_base
|
||||
Base = declarative_base()
|
||||
|
||||
@@ -83,6 +83,7 @@ class BookmarkedBusiness(Base):
|
||||
f"business_id='{self.business_id}', created_date='{self.created_date}')>"
|
||||
)
|
||||
|
||||
|
||||
class UserConsent(Base):
|
||||
__tablename__ = 'user_consent'
|
||||
__table_args__ = {'schema': 'app_data'}
|
||||
@@ -97,4 +98,24 @@ class UserConsent(Base):
|
||||
f"app_id='{self.app_id}', "
|
||||
f"privacy_policy_accepted='{self.privacy_policy_accepted}', "
|
||||
f"terms_of_services_accepted='{self.terms_of_services_accepted}')>"
|
||||
)
|
||||
|
||||
class MineSweeperLeaderboard(Base):
|
||||
__tablename__ = 'player_score'
|
||||
__table_args__ = {'schema': 'minesweeper_leaderboard'}
|
||||
idplayer_score = Column(Integer, primary_key=True)
|
||||
app_id = Column(String(128), nullable=False,server_default=text("''"))
|
||||
difficulty = Column(String(45), nullable=False,server_default=text("''"))
|
||||
game_time = Column(String(45), nullable=False,server_default=text("''"))
|
||||
game_score = Column(DECIMAL(45), nullable=False)
|
||||
played_date = Column(DateTime, nullable=True)
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"<MineSweeperLeaderboard(idplayer_score={self.idplayer_score}, "
|
||||
f"app_id='{self.app_id}', "
|
||||
f"difficulty='{self.difficulty}', "
|
||||
f"game_time='{self.game_time}', "
|
||||
f"game_score='{self.game_score}' "
|
||||
f"played_date='{self.played_date}')>"
|
||||
)
|
||||
171
backend/routers/mine_sweeper_leaderboard.py
Normal file
171
backend/routers/mine_sweeper_leaderboard.py
Normal file
@@ -0,0 +1,171 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from supertokens_python.recipe.session.framework.fastapi import verify_session
|
||||
from supertokens_python.recipe.session import SessionContainer
|
||||
from fastapi import Depends
|
||||
import mih_database
|
||||
import mih_database.mihDbConnections
|
||||
from mih_database.mihDbObjects import MineSweeperLeaderboard, User
|
||||
from sqlalchemy import and_, func, literal_column
|
||||
from sqlalchemy.orm import Session, aliased
|
||||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
from datetime import datetime
|
||||
from sqlalchemy.sql.expression import select
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class playerScoreInsertRequest(BaseModel):
|
||||
app_id: str
|
||||
difficulty: str
|
||||
game_time: str
|
||||
game_score: float
|
||||
played_date: datetime
|
||||
|
||||
# get top 20 scores
|
||||
@router.get("/minesweeper/leaderboard/top20/{difficulty}", tags=["Minesweeper"])
|
||||
async def get_user_consent(difficulty: str, session: SessionContainer = Depends(verify_session())):#session: SessionContainer = Depends(verify_session())
|
||||
dbEngine = mih_database.mihDbConnections.dbAllConnect()
|
||||
dbSession = Session(dbEngine)
|
||||
try:
|
||||
max_score_subquery = (
|
||||
dbSession.query(
|
||||
MineSweeperLeaderboard.app_id,
|
||||
func.max(MineSweeperLeaderboard.game_score).label('max_score')
|
||||
)
|
||||
.filter(MineSweeperLeaderboard.difficulty == difficulty)
|
||||
.group_by(MineSweeperLeaderboard.app_id)
|
||||
.subquery('max_scores')
|
||||
)
|
||||
queryResults = (
|
||||
dbSession.query(MineSweeperLeaderboard, User)
|
||||
.join(User, User.app_id == MineSweeperLeaderboard.app_id)
|
||||
.join(
|
||||
max_score_subquery,
|
||||
and_(
|
||||
MineSweeperLeaderboard.app_id == max_score_subquery.c.app_id,
|
||||
MineSweeperLeaderboard.game_score == max_score_subquery.c.max_score
|
||||
)
|
||||
)
|
||||
.filter(MineSweeperLeaderboard.difficulty == difficulty)
|
||||
.order_by(MineSweeperLeaderboard.game_score.desc())
|
||||
.limit(20)
|
||||
.all()
|
||||
)
|
||||
leaderboardData = []
|
||||
if queryResults:
|
||||
for playerScore, user in queryResults:
|
||||
leaderboardData.append({
|
||||
"app_id": playerScore.app_id,
|
||||
"username": user.username,
|
||||
"proPicUrl":user.pro_pic_path,
|
||||
"difficulty":playerScore.difficulty,
|
||||
"game_time":playerScore.game_time,
|
||||
"game_score":playerScore.game_score,
|
||||
"played_date":playerScore.played_date,
|
||||
})
|
||||
return leaderboardData
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No Score available for user."
|
||||
)
|
||||
except HTTPException as http_exc:
|
||||
raise http_exc
|
||||
except Exception as e:
|
||||
print(f"An error occurred during the ORM query: {e}")
|
||||
if dbSession.is_active:
|
||||
dbSession.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to retrieve records due to an internal server error."
|
||||
)
|
||||
finally:
|
||||
dbSession.close()
|
||||
|
||||
@router.get("/minesweeper/leaderboard/top_score/{difficulty}/{app_id}", tags=["Minesweeper"])
|
||||
async def get_user_consent(app_id: str,
|
||||
difficulty: str,
|
||||
session: SessionContainer = Depends(verify_session())):#session: SessionContainer = Depends(verify_session())
|
||||
dbEngine = mih_database.mihDbConnections.dbAllConnect()
|
||||
dbSession = Session(dbEngine)
|
||||
try:
|
||||
queryResults =(dbSession.query(MineSweeperLeaderboard, User)
|
||||
.join(User, User.app_id == MineSweeperLeaderboard.app_id)
|
||||
.filter(
|
||||
and_(
|
||||
MineSweeperLeaderboard.app_id == app_id,
|
||||
MineSweeperLeaderboard.difficulty == difficulty
|
||||
)
|
||||
)
|
||||
.order_by(MineSweeperLeaderboard.game_score.desc())
|
||||
.all())
|
||||
if not queryResults:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="No scores found for this user and difficulty level."
|
||||
)
|
||||
leaderboard_data = []
|
||||
for player_score, user in queryResults:
|
||||
score_data = {
|
||||
"app_id": player_score.app_id,
|
||||
"username": user.username,
|
||||
"proPicUrl": user.pro_pic_path,
|
||||
"difficulty": player_score.difficulty,
|
||||
"game_time": player_score.game_time,
|
||||
"game_score": player_score.game_score,
|
||||
"played_date": player_score.played_date,
|
||||
}
|
||||
leaderboard_data.append(score_data)
|
||||
return leaderboard_data
|
||||
except HTTPException as http_exc:
|
||||
raise http_exc
|
||||
except Exception as e:
|
||||
print(f"An error occurred during the ORM query: {e}")
|
||||
if dbSession.is_active:
|
||||
dbSession.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to retrieve records due to an internal server error."
|
||||
)
|
||||
finally:
|
||||
dbSession.close()
|
||||
|
||||
@router.post("/minesweeper/leaderboard/player_score/insert/",
|
||||
tags=["Minesweeper"],
|
||||
status_code=status.HTTP_201_CREATED)
|
||||
async def insert_user_consent(itemRequest: playerScoreInsertRequest,
|
||||
session: SessionContainer = Depends(verify_session())):#session: SessionContainer = Depends(verify_session())
|
||||
dbEngine = mih_database.mihDbConnections.dbAllConnect()
|
||||
dbSession = Session(dbEngine)
|
||||
try:
|
||||
newPlayerScore = MineSweeperLeaderboard(
|
||||
app_id = itemRequest.app_id,
|
||||
difficulty = itemRequest.difficulty,
|
||||
game_time = itemRequest.game_time,
|
||||
game_score = itemRequest.game_score,
|
||||
played_date = itemRequest.played_date,
|
||||
)
|
||||
dbSession.add(newPlayerScore)
|
||||
dbSession.commit()
|
||||
dbSession.refresh(newPlayerScore)
|
||||
return {"message": "Successfully Created Player Score Record"}
|
||||
except IntegrityError as e:
|
||||
dbSession.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, # 409 Conflict is often suitable for constraint errors
|
||||
detail=f"Data integrity error: The provided data violates a database constraint. Details: {e.orig}"
|
||||
) from e
|
||||
except SQLAlchemyError as e:
|
||||
dbSession.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"A database error occurred during insertion. Details: {e.orig}"
|
||||
) from e
|
||||
except Exception as e:
|
||||
dbSession.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"An unexpected error occurred: {e}"
|
||||
) from e
|
||||
finally:
|
||||
dbSession.close()
|
||||
Reference in New Issue
Block a user