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();

0
express/db/.gitkeep Normal file
View file

View file

@ -10,6 +10,10 @@
<button id="isCritterButton">Is Critter</button>
<button id="isNotCritterButton">Is <b>not</b> Critter</button>
<p>
<button id="startOverButton">Start over</button>
</p>
<script src="/asset/frontend.js"></script>
</body>
</html>

View file

@ -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];