From cc3728b62f5e93ad2c04ed3f5b86559a12049ef1 Mon Sep 17 00:00:00 2001
From: Djalim Simaila
Date: Mon, 20 Mar 2023 16:39:37 +0100
Subject: [PATCH] can see player before joining game
---
truthinquiry/routes/routes_api.py | 9 +++++----
truthinquiry/static/js/api.js | 2 +-
truthinquiry/static/js/game_lobby.js | 24 +++++++++++++++---------
truthinquiry/templates/lobby.html | 4 ++++
4 files changed, 25 insertions(+), 14 deletions(-)
diff --git a/truthinquiry/routes/routes_api.py b/truthinquiry/routes/routes_api.py
index 879a764..25ed9ee 100644
--- a/truthinquiry/routes/routes_api.py
+++ b/truthinquiry/routes/routes_api.py
@@ -34,12 +34,13 @@ def create_game():
@routes_api.route("/getGameMembers", methods=["GET", "POST"])
def get_members():
- if not flask.session:
- return {"error": 1, "msg": "No session"}
- game = game_logic.get_game(flask.session["game_id"])
+ game_id = flask.request.values.get("game_id")
+ print(50 * "#")
+ print(game_id)
+ print(50*"_")
+ game = game_logic.get_game(game_id)
if game is None:
return {"error": 1, "msg": "this game doesn't exist"}
-
response = {"error" : 0}
player_list = [member.username for member in game.members]
response["members"] = player_list
diff --git a/truthinquiry/static/js/api.js b/truthinquiry/static/js/api.js
index e242ab9..b49f660 100644
--- a/truthinquiry/static/js/api.js
+++ b/truthinquiry/static/js/api.js
@@ -34,7 +34,7 @@ async function makeAPIRequest(endpoint, body, options={}) {
response.json().then(jsonResponse => {
if (typeof(jsonResponse["error"]) === 'number' && jsonResponse["error"] !== 0) {
const message = jsonResponse["msg"];
- alert("Erreur du serveur : " + message);
+ alert("Erreur du serveur " + endpoint+ " : " + message);
reject(endpoint + ": " + message);
} else {
resolve(jsonResponse);
diff --git a/truthinquiry/static/js/game_lobby.js b/truthinquiry/static/js/game_lobby.js
index a0c9820..1581878 100644
--- a/truthinquiry/static/js/game_lobby.js
+++ b/truthinquiry/static/js/game_lobby.js
@@ -26,15 +26,6 @@ function displayRoomCode() {
* Display the players list element.
*/
function displayPlayerList() {
- const response = makeAPIRequest("getGameMembers");
-
- response.then(value => {
- const playerList = document.querySelector(".player_names");
- value["members"].forEach(username => {
- playerList.appendChild(document.createTextNode(username + "\n"));
- });
- });
-
showFirstClassElement("players_list");
}
@@ -98,6 +89,20 @@ function startChallengeGame() {
alert("Ce mode de jeu n'est malheureusement pas disponible.");
}
+function getMembers(){
+ let data = {};
+ data['game_id'] = getRoomCode();
+ const response = makeAPIRequest("getGameMembers",data);
+ response.then(value => {
+ let divs = document.getElementsByClassName("player_names");
+ for (let playerList of divs) {
+ value["members"].forEach(username => {
+ console.log(username);
+ playerList.appendChild(document.createTextNode(username + "\n"));
+ });
+ }
+ });
+}
// Join room functions
function joinRoom() {
@@ -350,6 +355,7 @@ function initSock() {
*
*/
async function initLobby() {
+ getMembers()
if (await hasJoinedRoom()) {
initSock();
displayRoomView();
diff --git a/truthinquiry/templates/lobby.html b/truthinquiry/templates/lobby.html
index baad5de..bcf3a9c 100644
--- a/truthinquiry/templates/lobby.html
+++ b/truthinquiry/templates/lobby.html
@@ -19,6 +19,10 @@