From 1addd381a3cf06a0c5d3ca42fc804994f534a4d8 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Tue, 10 Jan 2023 13:34:47 +0100 Subject: [PATCH 1/6] startgame in js --- truthseeker/static/js/api.js | 17 +++++++++++++++++ truthseeker/static/js/game_start_page.js | 12 +++++++++++- truthseeker/templates/index.html | 2 ++ truthseeker/templates/lobby.html | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 truthseeker/static/js/api.js diff --git a/truthseeker/static/js/api.js b/truthseeker/static/js/api.js new file mode 100644 index 0000000..803dd3f --- /dev/null +++ b/truthseeker/static/js/api.js @@ -0,0 +1,17 @@ +async function makeAPIRequest(endpoint, body){ + return new Promise((resolve, reject)=>{ + const fetchOptions = { + method: "POST", + body: new URLSearchParams(body) + } + fetch("/api/v1/"+endpoint, fetchOptions).then(resp => { + resp.json().then(jsonResp=>{ + if(jsonResp["error"] == 0){ + resolve(jsonResp) + }else{ + reject(endpoint+": "+jsonResp["msg"]) + } + }); + }) + }) +} diff --git a/truthseeker/static/js/game_start_page.js b/truthseeker/static/js/game_start_page.js index 52b68c0..8cc83b1 100644 --- a/truthseeker/static/js/game_start_page.js +++ b/truthseeker/static/js/game_start_page.js @@ -125,7 +125,7 @@ function createMultiPlayerRoom() { hideInvalidInputErrorMessage(); - //TODO: code to create multi player game + startGame() } function joinMultiPlayerRoom() { @@ -208,6 +208,16 @@ function changeTheme() { } } +async function startGame(){ + username = document.getElementById("game_username").value; + let data = {} + data["username"] = username + await makeAPIRequest("createGame",data); +} +async function joinGame(){ + username = document.getElementById("game_username").value(); + gameid = document.getElementById("game_room_code").value(); +} // Set event listeners document.getElementById("play_button").addEventListener("click", showGameModeSelection); diff --git a/truthseeker/templates/index.html b/truthseeker/templates/index.html index c5cb68b..cf6dc02 100644 --- a/truthseeker/templates/index.html +++ b/truthseeker/templates/index.html @@ -72,6 +72,8 @@ Mentions légales + + diff --git a/truthseeker/templates/lobby.html b/truthseeker/templates/lobby.html index e3bdfdc..5e52138 100644 --- a/truthseeker/templates/lobby.html +++ b/truthseeker/templates/lobby.html @@ -61,7 +61,10 @@ + + + From 6059172c951ff754c21a3729ac933ddc4933dcb7 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Wed, 11 Jan 2023 08:59:53 +0100 Subject: [PATCH 2/6] join game in js --- truthseeker/routes/routes_ui.py | 2 +- truthseeker/static/js/game_lobby.js | 20 +++++++++++-- truthseeker/static/js/game_start_page.js | 36 ++++++++++++++++++++---- truthseeker/templates/index.html | 2 +- truthseeker/templates/lobby.html | 4 +-- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/truthseeker/routes/routes_ui.py b/truthseeker/routes/routes_ui.py index 9342c17..a439b70 100644 --- a/truthseeker/routes/routes_ui.py +++ b/truthseeker/routes/routes_ui.py @@ -24,7 +24,7 @@ def legal(): @routes_ui.route("/lobby/") def lobby(game_id): # rendered by the javascript client-side - return flask.render_template("lobby.html") + return flask.render_template("lobby.html",gameid=game_id) @routes_ui.route("/solo") def solo(): diff --git a/truthseeker/static/js/game_lobby.js b/truthseeker/static/js/game_lobby.js index c47c880..3cd4f50 100644 --- a/truthseeker/static/js/game_lobby.js +++ b/truthseeker/static/js/game_lobby.js @@ -1,5 +1,4 @@ // Display functions - /** * Display the invalid rounds count message element, by removing the hidden CSS class. * @@ -296,8 +295,8 @@ function getChallengeModeRoundsCount() { * @returns the code of the room */ function getRoomCode() { - //FIXME get the real room code - return "ABCDEF"; + gameid = document.getElementById("gameid") + return gameid; } // Lobby initialization @@ -317,6 +316,21 @@ function getRoomCode() { *

*/ function initLobby() { + + gameid = getRoomCode(); + socket = io({ + auth:{ + game_id: gameid + } + }); + + socket.on("connect", () => { + console.log("Connected !") + }) + + socket.on("playersjoin", (err) => { + console.log(`Failed to connect to socket: ${err.message}`); + }); if (hasJoinedRoom()) { displayRoomView(); if (isRoomOwner()) { diff --git a/truthseeker/static/js/game_start_page.js b/truthseeker/static/js/game_start_page.js index 8cc83b1..53f2b5a 100644 --- a/truthseeker/static/js/game_start_page.js +++ b/truthseeker/static/js/game_start_page.js @@ -125,7 +125,7 @@ function createMultiPlayerRoom() { hideInvalidInputErrorMessage(); - startGame() + startGame(); } function joinMultiPlayerRoom() { @@ -135,7 +135,7 @@ function joinMultiPlayerRoom() { hideInvalidInputErrorMessage(); - //TODO: code to join multi player game + joinGame(); } /** @@ -211,12 +211,36 @@ function changeTheme() { async function startGame(){ username = document.getElementById("game_username").value; let data = {} - data["username"] = username - await makeAPIRequest("createGame",data); + data["username"] = username; + response = makeAPIRequest("createGame",data); + response.then((value) => { + if (value["error"] != 0){ + alert(value["msg"]); + } + else{ + gameid = value["game_id"] + window.location.href = "/lobby/" + gameid; + } + }); + } async function joinGame(){ - username = document.getElementById("game_username").value(); - gameid = document.getElementById("game_room_code").value(); + username = document.getElementById("game_username").value; + gameid = document.getElementById("game_room_code").value; + console.log(username); + data = {} + data["username"] = username; + data["game_id"] = gameid; + response = makeAPIRequest("joinGame",data); + response.then((value)=>{ + console.log(value); + if (value["error"] != 0){ + //alert(value["msg"]); + } + else{ + //window.location.href = "/lobby/" + gameid; + } + }) } // Set event listeners diff --git a/truthseeker/templates/index.html b/truthseeker/templates/index.html index cf6dc02..2eff2bd 100644 --- a/truthseeker/templates/index.html +++ b/truthseeker/templates/index.html @@ -72,8 +72,8 @@ Mentions légales - + diff --git a/truthseeker/templates/lobby.html b/truthseeker/templates/lobby.html index 5e52138..e7602f2 100644 --- a/truthseeker/templates/lobby.html +++ b/truthseeker/templates/lobby.html @@ -61,10 +61,10 @@ - + - + From 3728b2986f81cf0849bb83b0f85ef3f64f8fbdcd Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Wed, 11 Jan 2023 09:20:47 +0100 Subject: [PATCH 3/6] fixed copy clipboard --- truthseeker/static/js/game_lobby.js | 8 ++++++-- truthseeker/static/js/game_start_page.js | 2 +- truthseeker/templates/lobby.html | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/truthseeker/static/js/game_lobby.js b/truthseeker/static/js/game_lobby.js index 3cd4f50..401a2aa 100644 --- a/truthseeker/static/js/game_lobby.js +++ b/truthseeker/static/js/game_lobby.js @@ -127,7 +127,8 @@ function joinRoom() { */ function copyCode() { // Get the room code from the displayed text to avoid an extra API call - let roomCode = document.getElementsByClassName("room_code")[0].textContent; + let roomCode = getRoomCode(); + console.log(roomCode); if (roomCode == "") { alert("Veuillez patientez, le code d'équipe est en cours de génération."); } @@ -295,7 +296,8 @@ function getChallengeModeRoundsCount() { * @returns the code of the room */ function getRoomCode() { - gameid = document.getElementById("gameid") + gameid = document.getElementById("game_id").value; + console.log(gameid); return gameid; } @@ -331,6 +333,8 @@ function initLobby() { socket.on("playersjoin", (err) => { console.log(`Failed to connect to socket: ${err.message}`); }); + + if (hasJoinedRoom()) { displayRoomView(); if (isRoomOwner()) { diff --git a/truthseeker/static/js/game_start_page.js b/truthseeker/static/js/game_start_page.js index 53f2b5a..0c6d66d 100644 --- a/truthseeker/static/js/game_start_page.js +++ b/truthseeker/static/js/game_start_page.js @@ -238,7 +238,7 @@ async function joinGame(){ //alert(value["msg"]); } else{ - //window.location.href = "/lobby/" + gameid; + window.location.href = "/lobby/" + gameid; } }) } diff --git a/truthseeker/templates/lobby.html b/truthseeker/templates/lobby.html index e7602f2..220099e 100644 --- a/truthseeker/templates/lobby.html +++ b/truthseeker/templates/lobby.html @@ -8,6 +8,7 @@ +