From 18e2a3f30bcae7b216c1924ed32f22613bc39c58 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Wed, 11 Jan 2023 11:44:52 +0100 Subject: [PATCH] finished lobby --- truthseeker/routes/routes_api.py | 13 ++++++++ truthseeker/static/js/game_lobby.js | 49 +++++++++++++++-------------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/truthseeker/routes/routes_api.py b/truthseeker/routes/routes_api.py index a4af166..fa60b43 100644 --- a/truthseeker/routes/routes_api.py +++ b/truthseeker/routes/routes_api.py @@ -63,6 +63,19 @@ def join_game(): return {"error": 0} +@routes_api.route("/isOwner", methods=["GET", "POST"]) +def is_owner(): + if not flask.session: + return {"error": 0, "owner": False} + game = game_logic.get_game(flask.session["game_id"]) + if game == None: + return {"error": 0, "owner": False} + + if not flask.session["is_owner"]: + return {"error": 0, "owner": False} + + return {"error": 0, "owner": True} + @routes_api.route("/hasJoined", methods=["GET", "POST"]) def has_joined(): if not flask.session: diff --git a/truthseeker/static/js/game_lobby.js b/truthseeker/static/js/game_lobby.js index 5aca9bc..0ffd0bb 100644 --- a/truthseeker/static/js/game_lobby.js +++ b/truthseeker/static/js/game_lobby.js @@ -58,7 +58,7 @@ function displayJoinRoomView() { } -function addJoinRoomView() { +function hideJoinRoomView() { document.getElementsByClassName("join_room_view")[0].classList.add("hidden"); } /** @@ -115,10 +115,8 @@ function startChallengeGame() { // Join room functions function joinRoom() { - unsetListenerToJoinRoomButton(); if (isNickNameInvalid()) { displayInvalidNickNameErrorMessage("Le nom saisi n'est pas valide."); - setListenerToJoinRoomButton(); return; } @@ -130,7 +128,8 @@ function joinRoom() { response.then((value)=>{ displayRoomView(); displayPlayerList(); - displayJoinRoomView(); + initSock(); + hideJoinRoomView(); }) } // Room code functions @@ -230,9 +229,9 @@ function unsetListenerToCopyCodeButton() { // Utility functions -function isRoomOwner() { - //FIXME: check if player is room owner - return true; +async function isRoomOwner() { + response = await makeAPIRequest("isOwner"); + return response["owner"]; } async function hasJoinedRoom() { @@ -319,6 +318,24 @@ function getRoomCode() { return gameid; } +function initSock(){ + socket = io({ + auth:{ + game_id: gameid + } + }); + + socket.on("connect", () => { + console.log("Connected !") + }) + + socket.on("playersjoin", (username) => { + console.log(`${username} joined`); + player_list = document.getElementsByClassName("player_names")[0]; + player_list.appendChild(document.createTextNode(username)+"\n"); + }); +} + // Lobby initialization /** @@ -338,25 +355,11 @@ function getRoomCode() { async function initLobby() { gameid = getRoomCode(); - socket = io({ - auth:{ - game_id: gameid - } - }); - - socket.on("connect", () => { - console.log("Connected !") - }) - - socket.on("playersjoin", (username) => { - console.log(`${username} joined`); - player_list = document.getElementsByClassName("player_names")[0]; - player_list.appendChild(document.createTextNode(username)); - }); if (await hasJoinedRoom()) { + initSock(); displayRoomView(); - if (isRoomOwner()) { + if (await isRoomOwner()) { displayRoomCode(); displayMultiPlayerModeChoices(); setListenersToGameButtons();