From 1addd381a3cf06a0c5d3ca42fc804994f534a4d8 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Tue, 10 Jan 2023 13:34:47 +0100 Subject: [PATCH] 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 @@ + + +