From 2710d482f6c23e6f93a28bf0d8f7e6dd50cb9aba Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 15 Sep 2023 08:26:01 -0400 Subject: [PATCH] express: clear session and send a map Signed-off-by: Vincent Batts --- express/asset/frontend.js | 19 ++++++++++++++----- express/db/.gitkeep | 0 express/index.html | 4 ++++ express/server.js | 6 ++++-- 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 express/db/.gitkeep diff --git a/express/asset/frontend.js b/express/asset/frontend.js index 2b180be..aab9d3d 100644 --- a/express/asset/frontend.js +++ b/express/asset/frontend.js @@ -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(); diff --git a/express/db/.gitkeep b/express/db/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/express/index.html b/express/index.html index e66fce5..dfa4d8d 100644 --- a/express/index.html +++ b/express/index.html @@ -10,6 +10,10 @@ +

+ +

+ diff --git a/express/server.js b/express/server.js index b5377bb..d3d2057 100644 --- a/express/server.js +++ b/express/server.js @@ -5,7 +5,7 @@ const app = express(); const port = 3000; // Create an SQLite database and initialize tables -const db = new sqlite3.Database('mydb.db', (err) => { +const db = new sqlite3.Database('db/results.db', (err) => { if (err) { console.error('Error opening SQLite database:', err.message); } else { @@ -14,6 +14,7 @@ const db = new sqlite3.Database('mydb.db', (err) => { CREATE TABLE IF NOT EXISTS button_clicks ( id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT, + animal_name TEXT, button_name TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, time_difference INTEGER -- Add this column for time difference @@ -33,8 +34,9 @@ app.get('/asset/frontend.js', (req, res) => { }); // Route to get a random animal name -app.get('/getRandomAnimal', async (req, res) => { +app.get('/getNextAnimal', async (req, res) => { try { + // TODO this is currently random, and should have a bit of reasoning behind the next choice const animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe']; const randomIndex = Math.floor(Math.random() * animals.length); const randomAnimal = animals[randomIndex];