added docstring to the game.js file

This commit is contained in:
Djalim Simaila 2023-03-14 19:41:32 +01:00 committed by Djalim Simaila
parent d60cf6c90e
commit 8d878d8eff

View File

@ -73,6 +73,11 @@ function showEmotionAndCulpritChoicesView() {
showFirstClassElement("emotion_and_culprit_choices"); showFirstClassElement("emotion_and_culprit_choices");
} }
/**
* Parse the gamedata object to retreive the room in which the npc passed as parameter is
* located and the second npc located in the same room. When the passed npc is alone in the
* room, a npc is choosen at random as the returned partener
*/
function getNpcLocationAndPartner(npcid) { function getNpcLocationAndPartner(npcid) {
const data = {}; const data = {};
const npcidInt = parseInt(npcid); const npcidInt = parseInt(npcid);
@ -96,6 +101,11 @@ function getNpcLocationAndPartner(npcid) {
return data; return data;
} }
/**
* Parse the gamedata object to retreive the room in which the npc passed as parameter is
* located and the second npc located in the same room. When the passed npc is alone in the
* room, a npc is choosen at random as the returned partener
*/
function disableCulpritButtons(culprit_choices_element, selected_suspect) { function disableCulpritButtons(culprit_choices_element, selected_suspect) {
let childrenCulpritChoicesElement = culprit_choices_element.children; let childrenCulpritChoicesElement = culprit_choices_element.children;
@ -112,12 +122,16 @@ function disableCulpritButtons(culprit_choices_element, selected_suspect) {
} }
} }
/**
* Return the npc designed as the "culprit" of the crime, the culprit
* is determined by being the only npc alone in a room.
*/
function getCulprit() { function getCulprit() {
let culprit = null; let culprit = null;
Object.values(gameData["rooms"]).forEach(element => { Object.values(gameData["rooms"]).forEach(room => {
if (element['npcs'].length === 1) { if (room['npcs'].length === 1) {
culprit = element['npcs'][0]; culprit = room['npcs'][0];
return; return;
} }
}); });
@ -125,16 +139,31 @@ function getCulprit() {
return culprit; return culprit;
} }
async function askTypeOneQuestion() { /**
askQuestion(npcLocationAndPartner => gameData["npcs"][currentNpc]["QA_1"].replace( * handler for the function call "askQuestion" for a type_zero question
"{NPC}", gameData["npcs"][npcLocationAndPartner["partner"]]["name"])); * also known as "Where were you ?"
} */
async function askTypeZeroQuestion() { async function askTypeZeroQuestion() {
askQuestion(npcLocationAndPartner => gameData["npcs"][currentNpc]["QA_0"].replace( askQuestion(npcLocationAndPartner => gameData["npcs"][currentNpc]["QA_0"].replace(
"{SALLE}", npcLocationAndPartner["room"])); "{SALLE}", npcLocationAndPartner["room"]));
} }
/**
* handler for the function call "askQuestion" for a type_one question
* also known as "With who were you with ?"
*/
async function askTypeOneQuestion() {
askQuestion(npcLocationAndPartner => gameData["npcs"][currentNpc]["QA_1"].replace(
"{NPC}", gameData["npcs"][npcLocationAndPartner["partner"]]["name"]));
}
/**
* This function primary goal is to display the answer to the question the player
* asked to a npc.
* It parses the gamedata object to retreive the answer of the npc
* and fill the variables left in the string accordingly to the type of the question.
* Then it fetches the reacion of the npc and diplays it all.
*/
async function askQuestion(buildAnswer) { async function askQuestion(buildAnswer) {
unsetQuestionButtonsListeners(); unsetQuestionButtonsListeners();
@ -157,6 +186,9 @@ async function askQuestion(buildAnswer) {
setQuestionButtonsListeners(); setQuestionButtonsListeners();
} }
/**
* This function sends the player's answers to the server
*/
async function sendAnswers() { async function sendAnswers() {
const selections = document.getElementsByClassName("suspect_emotion_chooser"); const selections = document.getElementsByClassName("suspect_emotion_chooser");
@ -172,6 +204,10 @@ async function sendAnswers() {
return await makeAPIRequest("submitAnswers", data); return await makeAPIRequest("submitAnswers", data);
} }
/**
* Show the screen in which the player fill the emotion of each npc
* then decide on which npc is the culprit.
*/
function renderAnswerSelectionPanel() { function renderAnswerSelectionPanel() {
npcsIds.forEach(element => { npcsIds.forEach(element => {
const suspect = document.createElement("div"); const suspect = document.createElement("div");
@ -211,6 +247,9 @@ function renderAnswerSelectionPanel() {
}); });
} }
/**
* Show the screen in which the player asks auestions to the npcs
*/
function renderInterrogation() { function renderInterrogation() {
document.getElementById("QA_0").textContent = gameData["questions"]["QA_0"]; document.getElementById("QA_0").textContent = gameData["questions"]["QA_0"];
document.getElementById("QA_1").textContent = gameData["questions"]["QA_1"]; document.getElementById("QA_1").textContent = gameData["questions"]["QA_1"];
@ -242,6 +281,13 @@ function renderInterrogation() {
}); });
} }
/**
* Initialize the websocket for this page, its primary use is to
* show the final page once it receive the event that all player have finished
* it parses the payload send by the server containing the other players
* nicknames and scores.
*/
function initSock() { function initSock() {
const socket = io({ const socket = io({
auth : { auth : {
@ -320,6 +366,11 @@ function initSock() {
}); });
}); });
} }
/**
* This function retreive the initial gamedata of the game
* containing all of the needed textual ressources to make
* the game playable
*/
async function setGameData() { async function setGameData() {
const response = await makeAPIRequest("getGameData"); const response = await makeAPIRequest("getGameData");