express: clear session and send a map

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-09-15 08:26:01 -04:00
parent 094465b9a7
commit 2710d482f6
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED
4 changed files with 22 additions and 7 deletions

View file

@ -8,9 +8,9 @@ function generateSessionId() {
}
// Function to fetch a random animal name from the server
async function getRandomAnimal() {
async function getNextAnimal() {
try {
const response = await fetch('/getRandomAnimal');
const response = await fetch('/getNextAnimal');
const data = await response.json();
document.getElementById('animal-name').textContent = data.animalName;
} catch (error) {
@ -29,6 +29,9 @@ function getSessionId() {
return sessionId;
}
function clearSessionId() {
document.cookie = ``;
}
// Function to set session start time
function setSessionStartTime() {
@ -53,6 +56,10 @@ document.getElementById('isNotCritterButton').addEventListener('click', () => {
recordButtonClick('is not critter', getSessionId());
});
document.getElementById('startOverButton').addEventListener('click', () => {
clearSessionId();
});
// Function to record button clicks on the server
async function recordButtonClick(buttonName, sessionId) {
try {
@ -60,23 +67,25 @@ async function recordButtonClick(buttonName, sessionId) {
if (lastButtonClickTime) {
const timeDifference = currentTime - lastButtonClickTime;
// Include the time difference in the POST request data
const animal = document.getElementById('animal-name').textContent;
await fetch('/recordButtonClick', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ buttonName, sessionId, timeDifference }),
body: JSON.stringify({ "animal": animal, "button": buttonName, "session": sessionId, "difference": timeDifference, "time": sessionStartTime }),
});
}
lastButtonClickTime = currentTime; // Record the timestamp of the button click
displayTimeDifference(); // Calculate and display time difference
getRandomAnimal(); // Load another random animal
// TODO - slight delay before loading next animal, to show the user how long that decision took them
getNextAnimal(); // Load another random animal
} catch (error) {
console.error('Error recording button click:', error);
}
}
// Initial random animal load and session start time
getRandomAnimal();
getNextAnimal();
setSessionStartTime();