From e8d6306714b1317633cb8d96e89d095ac5548513 Mon Sep 17 00:00:00 2001 From: tototomate123 Date: Tue, 24 Feb 2026 19:23:34 +0100 Subject: [PATCH] add friend table, start work on ws --- app/__init__.py | 5 ++++- app/db.py | 11 +++++++++++ app/routes/main.py | 13 ++++++++++--- app/sockets/socket.py | 5 +++++ app/templates/home.html | 2 +- app/templates/{main.html => index.html} | 0 run.py | 1 + 7 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 app/sockets/socket.py rename app/templates/{main.html => index.html} (100%) diff --git a/app/__init__.py b/app/__init__.py index ea2ebe3..32bd689 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,10 +1,11 @@ from typing import Optional -from flask import Flask +from flask import Flask, app from flask_socketio import SocketIO from flask_login import LoginManager from .db import close_db, init_db from app.models.user import User + sIO = SocketIO() login_manager = LoginManager() @@ -16,6 +17,8 @@ def create_app(): app = Flask(__name__) app.config['SECRET_KEY'] = 'dev' #! ofc not intended for prod use + from .sockets import socket as _socket + app.teardown_appcontext(close_db) sIO.init_app(app) diff --git a/app/db.py b/app/db.py index 7d7f448..d0f9ae3 100644 --- a/app/db.py +++ b/app/db.py @@ -53,6 +53,17 @@ def init_db(app): CREATE INDEX IF NOT EXISTS idx_game_players_player_id ON game_players(player_id); + + CREATE TABLE IF NOT EXISTS friendships ( + requester_id INTEGER NOT NULL, + addressee_id INTEGER NOT NULL, + status TEXT NOT NULL CHECK(status IN ('pending','accepted','blocked')), + + PRIMARY KEY (requester_id, addressee_id), + + FOREIGN KEY (requester_id) REFERENCES users(id) ON DELETE CASCADE, + FOREIGN KEY (addressee_id) REFERENCES users(id) ON DELETE CASCADE + ); """ ) db.commit() \ No newline at end of file diff --git a/app/routes/main.py b/app/routes/main.py index 61d1e67..53f503d 100644 --- a/app/routes/main.py +++ b/app/routes/main.py @@ -13,10 +13,17 @@ main_bp = Blueprint("main", __name__) """ @main_bp.route("/", methods=["GET", "POST"]) -def main(): - return render_template("main.html") +def index(): + return render_template("index.html") @main_bp.route("/home", methods=["GET", "POST"]) @login_required def home(): - return render_template("home.html", username=current_user.username) \ No newline at end of file + print(f"Current user: {current_user.username}") + return render_template("home.html") + + +@main_bp.route("/play", methods=["GET"]) +@login_required +def play(): + return render_template("play.html") \ No newline at end of file diff --git a/app/sockets/socket.py b/app/sockets/socket.py new file mode 100644 index 0000000..0df3bfa --- /dev/null +++ b/app/sockets/socket.py @@ -0,0 +1,5 @@ +from app import sIO + +@sIO.on('auth') +def handle_auth(msg): + print(msg) \ No newline at end of file diff --git a/app/templates/home.html b/app/templates/home.html index 7efd7dd..5159827 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -11,5 +11,5 @@

logged in

-

hi, {{ username }}!

+

hi, {{ current_user.username }}!

diff --git a/app/templates/main.html b/app/templates/index.html similarity index 100% rename from app/templates/main.html rename to app/templates/index.html diff --git a/run.py b/run.py index 56719cd..4f3b0b6 100644 --- a/run.py +++ b/run.py @@ -3,4 +3,5 @@ from app import create_app, sIO app = create_app() if __name__ == "__main__": + print("Starting server...") sIO.run(app, debug=True) \ No newline at end of file