2024-11-19 21:23:34 +01:00
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
from pydantic import BaseModel
|
2024-12-19 23:26:42 +01:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-12-21 19:34:39 +01:00
|
|
|
from calls import *
|
|
|
|
from db import get_db_connection
|
2024-11-19 21:23:34 +01:00
|
|
|
|
|
|
|
app = FastAPI()
|
2024-12-19 23:26:42 +01:00
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=["*"], # Replace "*" with your allowed origins
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"], # Allow all HTTP methods
|
|
|
|
allow_headers=["*"], # Allow all HTTP headers
|
|
|
|
)
|
2024-11-19 21:23:34 +01:00
|
|
|
class RegisterRequest(BaseModel):
|
|
|
|
email: str
|
|
|
|
display_name: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
class LoginRequest(BaseModel):
|
|
|
|
email: str
|
|
|
|
password: str
|
2024-12-21 19:34:39 +01:00
|
|
|
|
|
|
|
class ReloginRequest(BaseModel):
|
|
|
|
token: str
|
2024-11-19 21:23:34 +01:00
|
|
|
|
|
|
|
class FriendRequest(BaseModel):
|
2024-12-21 19:34:39 +01:00
|
|
|
token: str
|
2024-11-19 21:23:34 +01:00
|
|
|
friend_uid: int
|
|
|
|
|
|
|
|
class MatchInviteRequest(BaseModel):
|
|
|
|
sender_uid: int
|
|
|
|
receiver_uid: int
|
|
|
|
|
|
|
|
class AcceptInviteRequest(BaseModel):
|
|
|
|
match_id: int
|
|
|
|
player2_uid: int
|
|
|
|
|
2024-12-21 19:34:39 +01:00
|
|
|
class getFriendList(BaseModel):
|
|
|
|
token: str
|
|
|
|
|
2024-11-19 21:23:34 +01:00
|
|
|
@app.post("/register")
|
|
|
|
def register(request: RegisterRequest):
|
|
|
|
try:
|
|
|
|
uid = register_user(request.email, request.display_name, request.password)
|
|
|
|
return {"message": "User registered successfully", "uid": uid}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
@app.post("/login")
|
|
|
|
def login(request: LoginRequest):
|
2024-12-19 23:26:42 +01:00
|
|
|
sessiontoken = authenticate_user(request.email, request.password)
|
|
|
|
if sessiontoken:
|
|
|
|
return {"message": "Login successful", "uid": sessiontoken}
|
2024-11-19 21:23:34 +01:00
|
|
|
else:
|
|
|
|
raise HTTPException(status_code=401, detail="Invalid credentials")
|
2024-12-21 19:34:39 +01:00
|
|
|
|
|
|
|
@app.post("/auth")
|
|
|
|
def login(request: ReloginRequest):
|
|
|
|
sessiontoken = reauth_user(request.token)
|
|
|
|
if sessiontoken:
|
|
|
|
return {"message": "Login successful", "uid": sessiontoken}
|
|
|
|
else:
|
|
|
|
raise HTTPException(status_code=503, detail="Bad Token")
|
|
|
|
|
2024-11-19 21:23:34 +01:00
|
|
|
@app.post("/add_friend")
|
|
|
|
def add_friend_endpoint(request: FriendRequest):
|
|
|
|
try:
|
2024-12-21 19:34:39 +01:00
|
|
|
success = add_friend(request.token, request.friend_uid)
|
2024-11-19 21:23:34 +01:00
|
|
|
return {"message": "Friend added successfully"} if success else HTTPException(400, "Failed to add friend")
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
@app.post("/send_match_invite")
|
|
|
|
def send_invite(request: MatchInviteRequest):
|
|
|
|
try:
|
|
|
|
match_id = send_match_invite(request.sender_uid, request.receiver_uid)
|
|
|
|
return {"message": "Match invite sent successfully", "match_id": match_id}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
@app.post("/accept_match_invite")
|
|
|
|
def accept_invite(request: AcceptInviteRequest):
|
|
|
|
try:
|
|
|
|
success = accept_match_invite(request.match_id, request.player2_uid)
|
|
|
|
return {"message": "Match invite accepted"} if success else HTTPException(400, "Failed to accept invite")
|
|
|
|
except Exception as e:
|
2024-12-19 23:26:42 +01:00
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
@app.get("/matches")
|
|
|
|
def get_matches():
|
|
|
|
try:
|
|
|
|
matches = get_all_matches()
|
|
|
|
return {"matches": matches}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
2024-12-21 19:34:39 +01:00
|
|
|
@app.get("/leaderboards")
|
|
|
|
def leaderboard():
|
|
|
|
try:
|
|
|
|
leaderboard = get_leaderboard()
|
|
|
|
return leaderboard
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
2024-12-19 23:26:42 +01:00
|
|
|
@app.post("/elo")
|
|
|
|
def get_elo_endpoint(authorization: str):
|
|
|
|
print(f"Received Authorization header: {authorization}")
|
|
|
|
try:
|
|
|
|
result = get_elo(authorization)
|
|
|
|
return result
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
2024-12-21 19:34:39 +01:00
|
|
|
|
|
|
|
def get_friend_details(friend_uid):
|
|
|
|
conn = get_db_connection()
|
|
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
|
|
cursor.execute("SELECT uid, display_name FROM users WHERE uid = %s;", (friend_uid,))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result:
|
|
|
|
return {"uid": result["uid"], "name": result["display_name"]}
|
|
|
|
return None
|
|
|
|
finally:
|
|
|
|
cursor.close()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
@app.post("/get_friends")
|
|
|
|
def get_friends_list(request: getFriendList):
|
|
|
|
token = request.token
|
|
|
|
conn = get_db_connection()
|
|
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
|
|
|
|
|
|
cursor.execute("SELECT friend_list FROM users WHERE session_token = %s;", (token,))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
|
|
|
|
if not result or not result["friend_list"]:
|
|
|
|
raise HTTPException(status_code=404, detail="No friends found.")
|
|
|
|
friends = result["friend_list"]
|
|
|
|
friends_details = []
|
|
|
|
for key, friend_uid in friends.items():
|
|
|
|
friend_details = get_friend_details(friend_uid)
|
|
|
|
if friend_details:
|
|
|
|
friends_details.append(friend_details)
|
|
|
|
|
|
|
|
return {"friends": friends_details}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
finally:
|
|
|
|
cursor.close()
|
|
|
|
conn.close()
|