From 2df62ff7143731cf1796e66ffa276826f05dd8f6 Mon Sep 17 00:00:00 2001 From: Leonardo Segala Date: Fri, 1 Aug 2025 22:07:39 +0200 Subject: [PATCH] Add queue next endpoint --- backend/requirements.txt | 1 + backend/src/app.py | 26 ++++++++++++++++++++++---- backend/src/room.py | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/requirements.txt b/backend/requirements.txt index 7623d9f..57b0b9d 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,4 +1,5 @@ Flask==3.1.0 flask-cors +flask-socketio dotenv requests \ No newline at end of file diff --git a/backend/src/app.py b/backend/src/app.py index 1abebfb..5f2b1f6 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -1,6 +1,8 @@ import dotenv from flask import Flask, Response, jsonify, request + from flask_cors import CORS +from flask_socketio import SocketIO, emit from .room import Room from .song import Song, init_db @@ -11,6 +13,8 @@ dotenv.load_dotenv() app = Flask(__name__) +app.config["SECRET_KEY"] = "your_secret_key" +socketio = SocketIO(app) CORS(app) @@ -24,6 +28,7 @@ ROOMS: dict[int, Room] = { {}, [], [Song(mbid="test", title="", artist="<artist placeholder>", tags=["Metal"], image_id="img-id", youtube_id="yt-id")], + playing_idx=1, ) } # { room_id: room, ... } @@ -63,13 +68,26 @@ def queue(): @app.post("/api/queue/next") -def queue_next(): ... +def queue_next(): + if (room_id := request.args.get("room")) is None: + return error("Missing room id") + if (room := ROOMS.get(int(room_id))) is None: + return error("Invalid room") -@app.post("/api/queue/renew") -def queue_renew(): ... + room.playing_idx += 1 + + if room.playing_idx >= len(room.playing): + ## queue ended + + # room.renew_queue() + emit("update_songs", {"songs": [1, 2, 3]}, broadcast=True, namespace="/") + + return {"success": True, "ended": True, "index": room.playing_idx, "queue": []} + + return {"success": True, "ended": False, "index": room.playing_idx} init_db() if __name__ == "__main__": - app.run(debug=True) + socketio.run(app, debug=True) diff --git a/backend/src/room.py b/backend/src/room.py index e6ebd4b..a5e38a8 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -24,6 +24,7 @@ class Room: songs: dict[str, UserScoredSong] # canzoni + voto history: list[Song] # canzoni riprodotte (in ordine) playing: list[Song] # canzoni che sono i riproduzione + playing_idx: int def rank_song(self, song: Song, user_score: int) -> float: rank = 0.0