From d0dae92c21dae158d521bd39dfc50efbd03eb9e6 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Sat, 14 Jan 2023 20:40:54 +0100 Subject: [PATCH] added questions fontions and reactions display --- truthseeker/logic/game_logic.py | 2 +- truthseeker/routes/routes_api.py | 5 +- truthseeker/static/js/game.js | 103 ++++++++++++++++++++++++------- truthseeker/templates/game.html | 6 +- 4 files changed, 87 insertions(+), 29 deletions(-) diff --git a/truthseeker/logic/game_logic.py b/truthseeker/logic/game_logic.py index e3a7acd..350e557 100644 --- a/truthseeker/logic/game_logic.py +++ b/truthseeker/logic/game_logic.py @@ -116,7 +116,7 @@ class Game: self.members.append(member) return member - def get_npc_reaction(self, npc_id, reaction) -> None: + def get_npc_reaction(self, npc_id) -> None: """ TODO + TODO TYPES """ diff --git a/truthseeker/routes/routes_api.py b/truthseeker/routes/routes_api.py index f054f75..25ff771 100644 --- a/truthseeker/routes/routes_api.py +++ b/truthseeker/routes/routes_api.py @@ -140,9 +140,8 @@ def getNpcReaction(): if game == None: return {"error": 1, "msg": "this game doesn't exist"} npc_id = flask.request.values.get("npcid") - reactionid = flask.request.values.get("reactionid") - image = game.get_npc_reaction(npc_id,reactionid) + image = game.get_npc_reaction(npc_id) errors = ["npc not in game","error reading file"] if image in [0,1]: return {"error" :1, "msg": errors[image]} , 500 @@ -150,7 +149,7 @@ def getNpcReaction(): response = flask.make_response(image) response.headers.set('Content-Type', 'image/png') response.headers.set( - 'Content-Disposition', 'attachment', filename=f'{reactionid}.png') + 'Content-Disposition', 'attachment', filename=f'reaction.png') return response @routes_api.route("/gameProgress", methods=["GET", "POST"]) diff --git a/truthseeker/static/js/game.js b/truthseeker/static/js/game.js index 510785f..0902876 100644 --- a/truthseeker/static/js/game.js +++ b/truthseeker/static/js/game.js @@ -1,44 +1,92 @@ var npcs_ids = [] var gamedata = {} +var currentNpc = null -function showInterogation(){ - document.getElementsByClassName("interrogation")[0].classList.remove("hidden"); -} -function hideInterogation(){ - document.getElementsByClassName("interrogation")[0].classList.add("hidden"); + +function show(className){ + document.getElementsByClassName(className)[0].classList.remove("hidden"); } -function showEmotionAndCulpritChoices(){ - document.getElementsByClassName("emotion_and_culprit_choices")[0].classList.remove("hidden"); -} -function hideEmotionAndCulpritChoices(){ - document.getElementsByClassName("emotion_and_culprit_choices")[0].classList.add("hidden"); +function hide(className){ + document.getElementsByClassName(className)[0].classList.add("hidden"); } -function showIntroduction(){ - document.getElementsByClassName("introduction")[0].classList.remove("hidden"); -} -function hideIntroduction(){ - document.getElementsByClassName("introduction")[0].classList.add("hidden"); -} function setListenerToIntroductionNextBtn(){ - document.getElementById("introduction_next_btn").addEventListener("click", showInterogationView) + document.getElementById("introduction_next_btn").addEventListener("click", showInterogationViewFromIntroduction); +} + +function setListenerToInterrogationSuspectPreviousBtn(){ + document.getElementById("interrogation_suspect_previous_btn").addEventListener("click",goBackToInterogation) } function setListenerToInterrogationNextBtn(){ document.getElementById("interrogation_next_btn").addEventListener("click", showEmotionAndCulpritChoicesView) } -function showInterogationView(){ - hideIntroduction(); - showInterogation(); +function goBackToInterogation(){ + hide("interrogation_suspect"); + show("interrogation"); +} + + +function showInterogationViewFromIntroduction(){ + hide("introduction"); + show("interrogation"); } function showEmotionAndCulpritChoicesView(){ - hideInterogation(); - showEmotionAndCulpritChoices(); + hide("interrogation"); + show("emotion_and_culprit_choices"); } +function getNpcLocationAndPartner(npcid){ + data = {} + npcid = parseInt(npcid) + for(const room in gamedata["rooms"]){ + if(gamedata["rooms"][room]["npcs"].includes(npcid)){ + data["room"] = gamedata["rooms"][room]["name"]; + if(gamedata["rooms"][room]["npcs"].length === 1){ + do{ + const random = Math.floor(Math.random() * npcs_ids.length); + data["partner"] = npcs_ids[random] + }while(data["partner"] === npcid); + } + else{ + data["partner"] = gamedata["rooms"][room]["npcs"][gamedata["rooms"][room]["npcs"][1] === npcid ?0:1]; + } + } + } + return data; +} + +async function askTypeOneQuestion(){ + partnerId = getNpcLocationAndPartner(currentNpc)["partner"]; + anwser = gamedata["npcs"][currentNpc]["QA_1"]; + anwser = anwser.replace("{NPC}",gamedata["npcs"][partnerId]["name"]); + document.getElementsByClassName("suspect_answer")[0].textContent = anwser; + show("question_answer"); + document.getElementById("currentNpcPicure").src = "/api/v1//getNpcReaction?npcid="+currentNpc; + //sleep for 5 sec + await new Promise(r => setTimeout(r, 5000)); + document.getElementById("currentNpcPicure").src = "/api/v1/getNpcImage?npcid="+currentNpc; + hide("question_answer"); + document.getElementsByClassName("suspect_answer")[0].textContent = ""; +} + + +async function askTypeZeroQuestion(){ + room = getNpcLocationAndPartner(currentNpc)["room"]; + anwser = gamedata["npcs"][currentNpc]["QA_0"]; + anwser = anwser.replace("{SALLE}",room); + document.getElementsByClassName("suspect_answer")[0].textContent = anwser; + show("question_answer"); + document.getElementById("currentNpcPicure").src = "/api/v1//getNpcReaction?npcid="+currentNpc; + //sleep for 5 sec + await new Promise(r => setTimeout(r, 5000)); + document.getElementById("currentNpcPicure").src = "/api/v1/getNpcImage?npcid="+currentNpc; + hide("question_answer"); + document.getElementsByClassName("suspect_answer")[0].textContent = ""; +} async function sendAnswers(){ selects = document.getElementsByClassName("suspect_emotion_chooser"); @@ -82,6 +130,8 @@ function renderAnswerSelectionPanel() { } function renderInterogation(){ + document.getElementById("QA_0").textContent = gamedata["questions"]["QA_0"], + document.getElementById("QA_1").textContent = gamedata["questions"]["QA_1"], npcs_ids.forEach(element => { let suspect = document.createElement("div"); suspect.classList.add("suspect"); @@ -93,6 +143,12 @@ function renderInterogation(){ let button = document.getElementById("interogationButton"); let button_clone = button.cloneNode(true); button_clone.classList.remove("hidden"); + button_clone.addEventListener("click",()=>{ + currentNpc = element + document.getElementById("currentNpcPicure").src = "/api/v1/getNpcImage?npcid="+element; + hide("interrogation"); + show("interrogation_suspect"); + }) suspect.appendChild(button_clone) document.getElementById("interrogation_suspects").appendChild(suspect); }); @@ -130,8 +186,9 @@ async function initGame(){ initSock(); renderAnswerSelectionPanel(); renderInterogation(); + setListenerToInterrogationSuspectPreviousBtn() setListenerToIntroductionNextBtn() setListenerToInterrogationNextBtn(); - showIntroduction(); + show("introduction"); } initGame(); \ No newline at end of file diff --git a/truthseeker/templates/game.html b/truthseeker/templates/game.html index 9fa3444..70de15f 100644 --- a/truthseeker/templates/game.html +++ b/truthseeker/templates/game.html @@ -41,14 +41,15 @@ +