From c0744fb6a041e6c84f19496699780417e6c292b7 Mon Sep 17 00:00:00 2001 From: Ankan <93087057+ItsAnkan@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:26:36 +0530 Subject: [PATCH 01/14] jai ga kuso des --- database/Schemas/Team.js | 2 +- database/handler.js | 2 +- routers/live-router.js | 111 +++++++++++++++++------------------ templates/live/interface.njk | 12 +++- 4 files changed, 66 insertions(+), 61 deletions(-) diff --git a/database/Schemas/Team.js b/database/Schemas/Team.js index ee643a3..06ce24b 100644 --- a/database/Schemas/Team.js +++ b/database/Schemas/Team.js @@ -12,7 +12,7 @@ const teamSchema = new mongoose.Schema({ phone: { type: String, required: true } } ], - status: { type: String, required: true }, + status: { type: String, required: true, enum: ['location-code', 'riddle-question', 'riddle-timeout'] }, questionsAttempted: { type: Number, required: true, default: 0 }, order: [ { diff --git a/database/handler.js b/database/handler.js index a19a179..d2633cf 100644 --- a/database/handler.js +++ b/database/handler.js @@ -215,7 +215,7 @@ async function updateTeamStatus (ctx) { team.timeout = null; await team.save(); }, 120 * 1000); - } else if (status === 'location-code') { + } else { team.questionsAttempted = ctx.questionNo; } return await team.save(); diff --git a/routers/live-router.js b/routers/live-router.js index ffdb08d..e7661e7 100644 --- a/routers/live-router.js +++ b/routers/live-router.js @@ -4,10 +4,12 @@ const { body, validationResult } = require('express-validator'); const checker = require('../src/checker.js'); const teams = require('../src/samples/teams.json'); -// const locations = require('../src/samples/locations.json'); +const locations = require('../src/samples/locations.json'); const handlerContext = {}; +const ques = 6; + router.use((req, res, next) => { if (!req.loggedIn) return req.method === 'GET' ? res.redirect('/login') : res.status(403).send('Forbidden. Not logged in.'); return next(); @@ -18,18 +20,6 @@ router.use((req, res, next) => { return req.method === 'GET' ? res.redirect('/') : res.status(400).send('Hunt not started'); }); -// const team = teams[1]; - -// const locationQuestion = { -// id: 1, -// question: 'Where is the best waifu', -// answer: 'Oregairu' -// }; - -// const riddleQuestion = riddleQuestions[0]; - -// const locationCode = 'ABC123'; - router.get('/', async (req, res) => { if (req.isAdmin) { const teamList = await dbh.getTeams(); @@ -40,66 +30,73 @@ router.get('/', async (req, res) => { // if (!handlerContext.quizStarted) return res.redirect('/'); return res.renderFile('live/interface.njk', { team: req.team, - started: handlerContext.huntStarted + started: true }); // return res.renderFile('live/interface.njk'); } }); -router.post('/get-data', async (req, res) => { +// combine the bottom 3 into one route +router.post('/get-data', (req, res) => { const teamID = req.body.teamID; if (teamID !== req.team._id) return res.status(500).send('WHy you hax'); - const locations = await dbh.getLocations(); - return res.status(200).send(JSON.stringify({ - team: req.team, - locations: req.team.order.map((o) => locations.find((l) => l._id === o.location)) - })); + return res.status(200).send(req.team); }); -router.post('/get-attempted', (req, res) => { - const teamID = req.body.teamID; - if (teamID !== req.team._id) return res.status(500).send('WHy you hax'); - return res.status(200).send(req.team.questionsAttempted); -}); -router.post('/get-state', (req, res) => { - const teamID = req.body.teamID; - if (teamID !== req.team._id) return res.status(500).send('WHy you hax'); - return res.status(200).send(req.team.status); -}); -router.post('/get-timeout', async (req, res) => { - if (req.team.status === 'riddle-timeout') { - return res.status(200).send(req.team.timeout); - } - return res.status(400).send(false); +router.post('/get-location-question', async (req, res) => { + const attempted = req.team.questionsAttempted; + if (attempted >= ques) return res.status(418).send('Completed'); + return res.send( + req.team.order.map((o) => + handlerContext.locations.find((l) => l._id === o.location) + )[attempted].pointerQuestion[req.team.order[attempted].pointer] + ); }); -router.post('/update-status', async (req, res) => { - // if (req.body.questionNo !== req.team.questionsAttempted) return; - await dbh.updateTeamStatus({ _id: req.team._id, status: req.body.status, questionNo: req.body.questionNo }); - return res.status(200).send('Updated Successfully'); +router.post('/get-riddle-question', async (req, res) => { + const attempted = req.team.questionsAttempted; + if (attempted >= ques) return res.status(418).send('Completed'); + return res.send( + req.team.order.map((o) => + handlerContext.locations.find((l) => l._id === o.location) + )[attempted].questions[req.team.order[attempted].question].question + ); }); -router.post('/update-attempted', async (req, res) => { - await dbh.updateTeamStatus({ _id: req.team._id, status: req.body.status, questionNo: req.body.questionNo }); - return res.status(200).send('Updated Successfully'); + +router.post('/submit', async (req, res) => { + const attempted = req.team.questionsAttempted; + if (req.body.questionNo !== attempted) return res.status(420).send('Koi bkl hi hoga'); + if (req.body.state !== req.team.status) return res.status(420).send('Koi bkl hi hoga'); + if (attempted >= ques) return res.status(418).send('Completed'); + if (req.body.state === 'riddle-question') { + if ( + req.team.order.map((o) => + handlerContext.locations.find((l) => l._id === o.location) + )[attempted].questions[req.team.order[attempted].question].answer === req.body.answer + ) { + await dbh.updateTeamStatus({ _id: req.team._id, status: 'location-code', questionNo: req.body.questionNo + 1 }); + return res.status(200).send('correct answer'); + } + await dbh.updateTeamStatus({ _id: req.team._id, status: 'riddle-timeout', questionNo: req.body.questionNo }); + return res.status(469).send('wrong answer'); + } else if (req.body.state === 'location-code') { + if ( + req.team.order.map((o) => + handlerContext.locations.find((l) => l._id === o.location) + )[attempted].code === req.body.answer + ) { + await dbh.updateTeamStatus({ _id: req.team._id, status: 'riddle-question', questionNo: req.body.questionNo }); + return res.status(200).send('correct answer'); + } + return res.status(469).send('wrong answer'); + } }); -// router.patch('/location-code', async (req, res) => { -// const teamID = parseInt(req.body.id); -// const location = req.body.locationcode; -// // console.log(teamID); -// // console.log(location); -// // FIND TEAM BY ID IN LOCAL STORAGE -// // MARK TEAM AS COMPLETED FOR LOCATION BY FINDING TEAM BY ID AND ADDING LOCATION TO COMPLETED LOCATIONS LIST -// if (location === locationCode) { -// return res.send('correct'); -// } else { -// return res.status(400).send('incorrect location'); -// } -// }); - -router.post('/start-hunt', (req, res) => { +router.post('/start-hunt', async (req, res) => { if (!req.isAdmin) return res.status(403).send('Forbidden: Admin permissions not detected.'); handlerContext.huntStarted = true; + handlerContext.teams = await dbh.getTeams(); + handlerContext.locations = await dbh.getLocations(); return res.send('Hunt Started'); }); diff --git a/templates/live/interface.njk b/templates/live/interface.njk index 1600026..ade5253 100644 --- a/templates/live/interface.njk +++ b/templates/live/interface.njk @@ -303,8 +303,9 @@ } document.addEventListener("DOMContentLoaded", function () { + axios.post('/live/get-question'); //loadingAnimation(); - document.getElementById("location-submit-button").addEventListener("click", function (event) { + {# document.getElementById("location-submit-button").addEventListener("click", function (event) { event.preventDefault(); submit(); }); @@ -340,7 +341,7 @@ renderForm(isOnload = true); } }); - axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => localStorage.setItem('state', res.data)); + axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => localStorage.setItem('state', res.data)); #} }); async function submit() { @@ -420,4 +421,11 @@ } } + + {% endblock %} \ No newline at end of file From 37cc4333184cbefe4bb777efda72754a5318f9a2 Mon Sep 17 00:00:00 2001 From: destryptor Date: Wed, 13 Mar 2024 19:29:27 +0530 Subject: [PATCH 02/14] Jai kuso yaro --- templates/live/interface.njk | 219 +++++++++++++++-------------------- 1 file changed, 94 insertions(+), 125 deletions(-) diff --git a/templates/live/interface.njk b/templates/live/interface.njk index ade5253..e6c0e7a 100644 --- a/templates/live/interface.njk +++ b/templates/live/interface.njk @@ -209,10 +209,12 @@ axios.defaults.headers.common['X-CSRF-TOKEN'] = '{{ csrfToken }}'; let questions = 6; - let tries = 0 + let tries = 0; + let attempted = 0; + let endOfTimeout; - let state = localStorage.getItem('state') || 'location-code'; - localStorage.setItem('state', state); + let state; + axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => state = res.data); function start() { document.getElementById('instructions').style.display='none' @@ -221,21 +223,15 @@ } function getRiddleQuestion () { - const current = JSON.parse(localStorage.getItem('team')).order[parseInt(localStorage.getItem('attempted'))] - const location = JSON.parse(localStorage.getItem('locations'))[parseInt(localStorage.getItem('attempted'))]; - const q = location.questions[current.question]; - return { question: q.question, answer: q.answer, keywords: location.keywords }; + return axios.post('/live/get-riddle-question').then((res) => res.data); } function getLocationQuestion () { - const current = JSON.parse(localStorage.getItem('team')).order[parseInt(localStorage.getItem('attempted'))] - const location = JSON.parse(localStorage.getItem('locations'))[parseInt(localStorage.getItem('attempted'))]; - return location.pointerQuestion[current.pointer]; - } + return axios.post('/live/get-location-question').then((res) => res.data); + } - function renderForm(isOnload) { - // if(!isOnload) axios.post('/live/update-status', {status: localStorage.getItem('state'), questionNo: parseInt(localStorage.getItem('attempted'))}).then((res) => console.log(res.data)); - state = localStorage.getItem('state'); + function renderForm() { + axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => state = res.data); const locationCode = document.getElementById('location-code'); const riddleForm = document.getElementById('riddle-form'); const messageBox = document.getElementById("message-box"); @@ -249,46 +245,7 @@ messageBox.innerHTML = ''; messageCover.style.display = 'none'; timeoutContainer.style.display = 'none'; - - if (parseInt(localStorage.getItem('attempted')) >= questions) { - state='done'; - localStorage.setItem('state', state); - axios.post('/live/update-status', {status: 'completed', questionNo: parseInt(localStorage.getItem('attempted'))}).then((res) => console.log(res.data)); - document.getElementById('container').style.display = 'none'; - document.getElementById('done-cover').style.display = 'flex'; - } - else { - if (state === 'riddle-timeout') { - const riddleQuestion = getRiddleQuestion(); - if (riddleQuestion) { - document.getElementById('riddle-question').textContent = riddleQuestion.question; - } - document.getElementById('riddleanswer').innerHTML = '' + riddleQuestion.keywords.map((keyword, index) => { - return `` - }).join(' ') - const riddleButton = document.getElementById('riddle-submit-button'); - riddleButton.disabled = true; - if (!localStorage.getItem('endOfTimeout')) { - localStorage.setItem('endOfTimeout', new Date(Date.now() + 120 * 1000)); - } - let timeleft = Math.floor((new Date(localStorage.getItem('endOfTimeout')) - Date.now())/1000); - const countdownInterval = setInterval(() => { - if (timeleft <= 0) { - clearInterval(countdownInterval); - localStorage.removeItem('endOfTimeout'); - state = 'riddle-question'; - localStorage.setItem('state', state); - axios.post('/live/update-status', {status: localStorage.getItem('state'), questionNo: parseInt(localStorage.getItem('attempted'))}).then((res) => console.log(res.data)); - riddleButton.textContent = 'Submit'; - riddleButton.disabled = false; - } else { - const minutes = Math.floor(timeleft / 60); - const seconds = timeleft % 60; - riddleButton.textContent = "Please wait " + (minutes === 0 ? "" : minutes + " min ") + (seconds === 0 ? "" : seconds + " seconds"); - timeleft -= 1; - } - }, 1000); - } else if (state === 'location-code') { + if (state === 'location-code') { document.getElementById('location-question').textContent = getLocationQuestion(); } else if (state === 'riddle-question') { const riddleQuestion = getRiddleQuestion(); @@ -298,14 +255,43 @@ document.getElementById('riddleanswer').innerHTML = '' + riddleQuestion.keywords.map((keyword, index) => { return `` }).join(' ') + } else if (state === 'riddle-timeout') { + return timeout(); } } + + const timeout = () => { + const countdown = document.getElementById("countdown"); + const timeoutContainer = document.getElementById('timeout'); + const interfaceContainer = document.getElementById('interface-container'); + const messageBox = document.getElementById("message-box"); + const messageCover = document.getElementById("message-cover"); + const locationCode = document.getElementById('location-code'); + const riddleForm = document.getElementById('riddle-form'); + const locationButton = document.getElementById('location-submit-button'); + const riddleButton = document.getElementById('riddle-submit-button'); + const countdownHeader = document.getElementById('countdown-header'); + const countdownTime = endOfTimeout - Date.now(); + let timeleft = Math.floor((endOfTimeout - Date.now())/1000); + const countdownInterval = setInterval(() => { + if (timeleft <= 0) { + clearInterval(countdownInterval); + state = 'riddle-question'; + riddleButton.textContent = 'Submit'; + riddleButton.disabled = false; + } else { + const minutes = Math.floor(timeleft / 60); + const seconds = timeleft % 60; + riddleButton.textContent = "Please wait " + (minutes === 0 ? "" : minutes + " min ") + (seconds === 0 ? "" : seconds + " seconds"); + timeleft -= 1; + } + }, 1000); } + document.addEventListener("DOMContentLoaded", function () { - axios.post('/live/get-question'); //loadingAnimation(); - {# document.getElementById("location-submit-button").addEventListener("click", function (event) { + document.getElementById("location-submit-button").addEventListener("click", function (event) { event.preventDefault(); submit(); }); @@ -319,113 +305,96 @@ localStorage.removeItem('encountered-error'); } - if (!localStorage.getItem('team')) { - document.getElementById('continue').textContent = 'Start' - axios.post('/live/get-data', {teamID: {{ team._id }}}).then((res) => { - console.log(res.data); - if(res.status === 200) { - localStorage.setItem('team', JSON.stringify(res.data.team)); - localStorage.setItem('locations', JSON.stringify(res.data.locations)); - } - }) - } - else { - document.getElementById('continue').textContent = 'Continue' - } - axios.post('/live/get-attempted', {teamID: {{ team._id }}}).then((res) => localStorage.setItem('attempted', res.data)); - axios.post('/live/get-timeout').then((res) => { - if (res.status === 200) { - localStorage.setItem('endOfTimeout', res.data); - state = 'riddle-timeout'; - localStorage.setItem('state', state); - renderForm(isOnload = true); + document.getElementById('continue').textContent = 'Continue' + + axios.post('/live/get-data', {teamID: {{ team._id }}}).then((res) => { + if (res.data.status === "riddle-timeout") { + return timeout(res.data.timeout); } - }); - axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => localStorage.setItem('state', res.data)); #} + else{ + state = res.data.status; + attempted = res.data.attempted; + endOfTimeout = res.data.timeout; + renderForm(); + } + }) }); async function submit() { - const attemptedresponse = await axios.post('/live/get-attempted', {teamID: {{ team._id }}}); - const attempted = parseInt(attemptedresponse.data); - localStorage.setItem('attempted', attempted); - - const stateresponse = await axios.post('/live/get-state', {teamID: {{ team._id }}}); - const actualState = stateresponse.data; - if (actualState !== state) { - state = actualState; - localStorage.setItem('encountered-error', true); - location.reload(); - return; - } const data = getData(); - // const answer = document.getElementById('riddleanswer').value; try { - let response; - if (state === 'location-code') { - if (data.locationcode === JSON.parse(localStorage.getItem('locations'))[parseInt(localStorage.getItem('attempted'))].code) { + const response = await axios.post('/live/submit', {state: 'location-code', answer: state === 'location-code'? data.locationcode : data.riddleanswer, questionNo: attempted}); + if(response.status === 420){ + localStorage.setItem('encountered-error', true); + location.reload(); + } + else if(response.status === 418){ + state='done'; + document.getElementById('container').style.display = 'none'; + document.getElementById('done-cover').style.display = 'flex'; + } + else if (state === 'location-code') { + if (response.status === 469) { + const locationButton = document.getElementById('location-submit-button'); + locationButton.disabled = true; const messageBox = document.getElementById("message-box"); - messageBox.innerHTML = '

Correct Location!

'; + messageBox.innerHTML = '

Incorrect Location Code!

'; const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; - state = 'riddle-question'; - localStorage.setItem('state', state); - axios.post('/live/update-status', {status: 'riddle-question', questionNo: parseInt(localStorage.getItem('attempted'))}).then((res) => console.log(res.data)); setTimeout(() => { messageCover.style.display = "none"; - renderForm(); + locationButton.disabled = false; }, 2000); - } else { + } + else { + const locationButton = document.getElementById('location-submit-button'); + locationButton.disabled = true; const messageBox = document.getElementById("message-box"); - messageBox.innerHTML = '

Incorrect Location Code!

'; + messageBox.innerHTML = '

Correct Location!

'; const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; + state = 'riddle-question'; setTimeout(() => { messageCover.style.display = "none"; + locationButton.disabled = false; + renderForm(); }, 2000); } } else { - const riddleQuestion = getRiddleQuestion(); - if(parseInt(data.riddleanswer) === riddleQuestion.answer) { + if (response.status === 469) { + endOfTimeout = new Date(Date.now() + 120 * 1000); + const riddleButton = document.getElementById('riddle-submit-button'); + riddleButton.disabled = true; const messageBox = document.getElementById("message-box"); - messageBox.innerHTML = '

Correct Answer!

'; + messageBox.innerHTML = '

Incorrect Answer!

'; const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; - state = 'location-code'; - localStorage.setItem('state', state); - if (parseInt(localStorage.getItem('attempted')) < questions) { - localStorage.setItem('attempted', parseInt(localStorage.getItem('attempted')) + 1); - axios.post('/live/update-attempted', {status: 'riddle-question', questionNo: parseInt(localStorage.getItem('attempted'))}); - } - axios.post('/live/update-status', {status: 'location-code', questionNo: parseInt(localStorage.getItem('attempted'))}).then((res) => console.log(res.data)); + state = 'riddle-timeout'; setTimeout(() => { messageCover.style.display = "none"; - document.getElementById('locationcode').value= ''; + riddleButton.disabled = false; renderForm(); }, 5000); - } else { + } + else { + const riddleButton = document.getElementById('riddle-submit-button'); + riddleButton.disabled = true; const messageBox = document.getElementById("message-box"); - messageBox.innerHTML = '

Incorrect Answer!

'; + messageBox.innerHTML = '

Correct Answer!

'; const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; - state = 'riddle-timeout'; - localStorage.setItem('state', state); - axios.post('/live/update-status', {status: localStorage.getItem('state'), questionNo: parseInt(localStorage.getItem('attempted'))}).then((res) => console.log(res.data)); + state = 'location-code'; setTimeout(() => { messageCover.style.display = "none"; + document.getElementById('locationcode').value= ''; + riddleButton.disabled = false; renderForm(); }, 5000); } } - } catch (error) { + } catch (error) { console.error(error); } } - - {% endblock %} \ No newline at end of file From 1da9b9f0fdb577ae3fe83092ef9ba7043b49a391 Mon Sep 17 00:00:00 2001 From: destryptor Date: Wed, 13 Mar 2024 20:08:30 +0530 Subject: [PATCH 03/14] Jai in jail --- routers/live-router.js | 4 +-- templates/live/interface.njk | 54 +++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/routers/live-router.js b/routers/live-router.js index e7661e7..03ac62f 100644 --- a/routers/live-router.js +++ b/routers/live-router.js @@ -43,7 +43,7 @@ router.post('/get-data', (req, res) => { return res.status(200).send(req.team); }); -router.post('/get-location-question', async (req, res) => { +router.post('/get-location-question', (req, res) => { const attempted = req.team.questionsAttempted; if (attempted >= ques) return res.status(418).send('Completed'); return res.send( @@ -53,7 +53,7 @@ router.post('/get-location-question', async (req, res) => { ); }); -router.post('/get-riddle-question', async (req, res) => { +router.post('/get-riddle-question', (req, res) => { const attempted = req.team.questionsAttempted; if (attempted >= ques) return res.status(418).send('Completed'); return res.send( diff --git a/templates/live/interface.njk b/templates/live/interface.njk index e6c0e7a..73b9c1b 100644 --- a/templates/live/interface.njk +++ b/templates/live/interface.njk @@ -214,24 +214,23 @@ let endOfTimeout; let state; - axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => state = res.data); - function start() { + async function start() { document.getElementById('instructions').style.display='none' document.getElementById('container').style.display='block' - renderForm(isOnload = true); + await renderForm(); } function getRiddleQuestion () { return axios.post('/live/get-riddle-question').then((res) => res.data); } - function getLocationQuestion () { - return axios.post('/live/get-location-question').then((res) => res.data); + async function getLocationQuestion () { + const response = await axios.post('/live/get-location-question'); + return response.data; } - function renderForm() { - axios.post('/live/get-state', {teamID: {{ team._id }}}).then((res) => state = res.data); + async function renderForm() { const locationCode = document.getElementById('location-code'); const riddleForm = document.getElementById('riddle-form'); const messageBox = document.getElementById("message-box"); @@ -246,7 +245,7 @@ messageCover.style.display = 'none'; timeoutContainer.style.display = 'none'; if (state === 'location-code') { - document.getElementById('location-question').textContent = getLocationQuestion(); + document.getElementById('location-question').textContent = await getLocationQuestion(); } else if (state === 'riddle-question') { const riddleQuestion = getRiddleQuestion(); if (riddleQuestion) { @@ -289,15 +288,15 @@ } - document.addEventListener("DOMContentLoaded", function () { + document.addEventListener("DOMContentLoaded", async function () { //loadingAnimation(); - document.getElementById("location-submit-button").addEventListener("click", function (event) { + document.getElementById("location-submit-button").addEventListener("click", async function (event) { event.preventDefault(); - submit(); + await submit(); }); - document.getElementById("riddle-submit-button").addEventListener("click", function (event) { + document.getElementById("riddle-submit-button").addEventListener("click", async function (event) { event.preventDefault(); - submit(); + await submit(); }); if (localStorage.getItem('encountered-error')) { const p = document.querySelector('#instructions p'); @@ -307,22 +306,26 @@ document.getElementById('continue').textContent = 'Continue' - axios.post('/live/get-data', {teamID: {{ team._id }}}).then((res) => { + axios.post('/live/get-data', {teamID: {{ team._id }}}).then(async (res) => { if (res.data.status === "riddle-timeout") { return timeout(res.data.timeout); } else{ + //console.log each of the following + console.log(res.data); state = res.data.status; - attempted = res.data.attempted; + attempted = res.data.questionsAttempted; endOfTimeout = res.data.timeout; - renderForm(); + await renderForm(); } }) }); async function submit() { const data = getData(); - try { + console.log(data); + console.log(state); + console.log(attempted); const response = await axios.post('/live/submit', {state: 'location-code', answer: state === 'location-code'? data.locationcode : data.riddleanswer, questionNo: attempted}); if(response.status === 420){ localStorage.setItem('encountered-error', true); @@ -335,6 +338,7 @@ } else if (state === 'location-code') { if (response.status === 469) { + console.log('Incorrect Location Code'); const locationButton = document.getElementById('location-submit-button'); locationButton.disabled = true; const messageBox = document.getElementById("message-box"); @@ -354,10 +358,10 @@ const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; state = 'riddle-question'; - setTimeout(() => { + setTimeout(async () => { messageCover.style.display = "none"; locationButton.disabled = false; - renderForm(); + await renderForm(); }, 2000); } } else { @@ -370,10 +374,10 @@ const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; state = 'riddle-timeout'; - setTimeout(() => { + setTimeout(async () => { messageCover.style.display = "none"; riddleButton.disabled = false; - renderForm(); + await renderForm(); }, 5000); } else { @@ -384,17 +388,15 @@ const messageCover = document.getElementById("message-cover"); messageCover.style.display = 'block'; state = 'location-code'; - setTimeout(() => { + setTimeout(async () => { messageCover.style.display = "none"; document.getElementById('locationcode').value= ''; riddleButton.disabled = false; - renderForm(); + await renderForm(); }, 5000); } } - } catch (error) { - console.error(error); - } + } {% endblock %} \ No newline at end of file From 44d1e7913769a69b8b691e08b78f6917a08eef28 Mon Sep 17 00:00:00 2001 From: Ankan <93087057+ItsAnkan@users.noreply.github.com> Date: Wed, 13 Mar 2024 20:42:06 +0530 Subject: [PATCH 04/14] stufff works now ? --- routers/live-router.js | 10 +-- templates/live/interface.njk | 114 +++++++++++++++++------------------ 2 files changed, 61 insertions(+), 63 deletions(-) diff --git a/routers/live-router.js b/routers/live-router.js index 03ac62f..38f568c 100644 --- a/routers/live-router.js +++ b/routers/live-router.js @@ -56,11 +56,11 @@ router.post('/get-location-question', (req, res) => { router.post('/get-riddle-question', (req, res) => { const attempted = req.team.questionsAttempted; if (attempted >= ques) return res.status(418).send('Completed'); - return res.send( - req.team.order.map((o) => - handlerContext.locations.find((l) => l._id === o.location) - )[attempted].questions[req.team.order[attempted].question].question - ); + const location = req.team.order.map((o) => handlerContext.locations.find((l) => l._id === o.location))[attempted]; + return res.send({ + question: location.questions[req.team.order[attempted].question].question, + keywords: location.keywords + }); }); router.post('/submit', async (req, res) => { diff --git a/templates/live/interface.njk b/templates/live/interface.njk index 73b9c1b..855cb20 100644 --- a/templates/live/interface.njk +++ b/templates/live/interface.njk @@ -221,13 +221,12 @@ await renderForm(); } - function getRiddleQuestion () { - return axios.post('/live/get-riddle-question').then((res) => res.data); + async function getRiddleQuestion () { + return (await axios.post('/live/get-riddle-question')).data; } async function getLocationQuestion () { - const response = await axios.post('/live/get-location-question'); - return response.data; + return (await axios.post('/live/get-location-question')).data; } async function renderForm() { @@ -247,7 +246,8 @@ if (state === 'location-code') { document.getElementById('location-question').textContent = await getLocationQuestion(); } else if (state === 'riddle-question') { - const riddleQuestion = getRiddleQuestion(); + const riddleQuestion = await getRiddleQuestion(); + console.log(riddleQuestion); if (riddleQuestion) { document.getElementById('riddle-question').textContent = riddleQuestion.question; } @@ -306,19 +306,16 @@ document.getElementById('continue').textContent = 'Continue' - axios.post('/live/get-data', {teamID: {{ team._id }}}).then(async (res) => { - if (res.data.status === "riddle-timeout") { - return timeout(res.data.timeout); - } - else{ - //console.log each of the following - console.log(res.data); - state = res.data.status; - attempted = res.data.questionsAttempted; - endOfTimeout = res.data.timeout; - await renderForm(); - } - }) + const response = await axios.post('/live/get-data', {teamID: {{ team._id }}}); + if (response.data.status === "riddle-timeout") { + return timeout(response.data.timeout); + } + else{ + state = response.data.status; + attempted = response.data.questionsAttempted; + endOfTimeout = response.data.timeout; + await renderForm(); + } }); async function submit() { @@ -326,19 +323,50 @@ console.log(data); console.log(state); console.log(attempted); - const response = await axios.post('/live/submit', {state: 'location-code', answer: state === 'location-code'? data.locationcode : data.riddleanswer, questionNo: attempted}); - if(response.status === 420){ - localStorage.setItem('encountered-error', true); - location.reload(); + try { + const response = await axios.post('/live/submit', {state: state, answer: state === 'location-code'? data.locationcode : data.riddleanswer, questionNo: attempted}); + if (state === 'location-code') { + const locationButton = document.getElementById('location-submit-button'); + locationButton.disabled = true; + const messageBox = document.getElementById("message-box"); + messageBox.innerHTML = '

Correct Location!

'; + const messageCover = document.getElementById("message-cover"); + messageCover.style.display = 'block'; + state = 'riddle-question'; + setTimeout(async () => { + messageCover.style.display = "none"; + locationButton.disabled = false; + await renderForm(); + }, 2000); } - else if(response.status === 418){ + else { + const riddleButton = document.getElementById('riddle-submit-button'); + riddleButton.disabled = true; + const messageBox = document.getElementById("message-box"); + messageBox.innerHTML = '

Correct Answer!

'; + const messageCover = document.getElementById("message-cover"); + messageCover.style.display = 'block'; + state = 'location-code'; + setTimeout(async () => { + messageCover.style.display = "none"; + document.getElementById('locationcode').value= ''; + riddleButton.disabled = false; + await renderForm(); + }, 5000); + } + } + catch ( e ) { + if ( e.response.status === 418 ) { state='done'; document.getElementById('container').style.display = 'none'; document.getElementById('done-cover').style.display = 'flex'; } - else if (state === 'location-code') { - if (response.status === 469) { - console.log('Incorrect Location Code'); + else if ( e.response.status === 420 ) { + localStorage.setItem('encountered-error', true); + location.reload(); + } + else if ( e.response.status === 469 ) { + if ( state === 'location-code' ) { const locationButton = document.getElementById('location-submit-button'); locationButton.disabled = true; const messageBox = document.getElementById("message-box"); @@ -350,22 +378,7 @@ locationButton.disabled = false; }, 2000); } - else { - const locationButton = document.getElementById('location-submit-button'); - locationButton.disabled = true; - const messageBox = document.getElementById("message-box"); - messageBox.innerHTML = '

Correct Location!

'; - const messageCover = document.getElementById("message-cover"); - messageCover.style.display = 'block'; - state = 'riddle-question'; - setTimeout(async () => { - messageCover.style.display = "none"; - locationButton.disabled = false; - await renderForm(); - }, 2000); - } - } else { - if (response.status === 469) { + else if ( state === 'riddle-question' ) { endOfTimeout = new Date(Date.now() + 120 * 1000); const riddleButton = document.getElementById('riddle-submit-button'); riddleButton.disabled = true; @@ -380,23 +393,8 @@ await renderForm(); }, 5000); } - else { - const riddleButton = document.getElementById('riddle-submit-button'); - riddleButton.disabled = true; - const messageBox = document.getElementById("message-box"); - messageBox.innerHTML = '

Correct Answer!

'; - const messageCover = document.getElementById("message-cover"); - messageCover.style.display = 'block'; - state = 'location-code'; - setTimeout(async () => { - messageCover.style.display = "none"; - document.getElementById('locationcode').value= ''; - riddleButton.disabled = false; - await renderForm(); - }, 5000); - } } - + } } {% endblock %} \ No newline at end of file From f53a4f4379807b977b0eb92e7b92ef18a39a1e7c Mon Sep 17 00:00:00 2001 From: destryptor Date: Wed, 13 Mar 2024 22:54:28 +0530 Subject: [PATCH 05/14] Onkon... frick you --- database/Schemas/Team.js | 2 +- database/handler.js | 2 +- routers/live-router.js | 9 +++++-- templates/live/interface.njk | 52 +++++++++++++++++++++++++----------- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/database/Schemas/Team.js b/database/Schemas/Team.js index 06ce24b..5073651 100644 --- a/database/Schemas/Team.js +++ b/database/Schemas/Team.js @@ -12,7 +12,7 @@ const teamSchema = new mongoose.Schema({ phone: { type: String, required: true } } ], - status: { type: String, required: true, enum: ['location-code', 'riddle-question', 'riddle-timeout'] }, + status: { type: String, required: true, enum: ['location-code', 'riddle-question', 'riddle-timeout', 'completed'] }, questionsAttempted: { type: Number, required: true, default: 0 }, order: [ { diff --git a/database/handler.js b/database/handler.js index d2633cf..209755a 100644 --- a/database/handler.js +++ b/database/handler.js @@ -214,7 +214,7 @@ async function updateTeamStatus (ctx) { team.status = 'riddle-question'; team.timeout = null; await team.save(); - }, 120 * 1000); + }, 110 * 1000); } else { team.questionsAttempted = ctx.questionNo; } diff --git a/routers/live-router.js b/routers/live-router.js index 38f568c..9d9a436 100644 --- a/routers/live-router.js +++ b/routers/live-router.js @@ -67,12 +67,17 @@ router.post('/submit', async (req, res) => { const attempted = req.team.questionsAttempted; if (req.body.questionNo !== attempted) return res.status(420).send('Koi bkl hi hoga'); if (req.body.state !== req.team.status) return res.status(420).send('Koi bkl hi hoga'); - if (attempted >= ques) return res.status(418).send('Completed'); + if (req.body.state === 'riddle-question') { + if (attempted + 1 >= ques) { + await dbh.updateTeamStatus({ _id: req.team._id, status: 'completed', questionNo: 6 }); + return res.status(418).send('Completed'); + } + } if (req.body.state === 'riddle-question') { if ( req.team.order.map((o) => handlerContext.locations.find((l) => l._id === o.location) - )[attempted].questions[req.team.order[attempted].question].answer === req.body.answer + )[attempted].questions[req.team.order[attempted].question].answer === parseInt(req.body.answer) ) { await dbh.updateTeamStatus({ _id: req.team._id, status: 'location-code', questionNo: req.body.questionNo + 1 }); return res.status(200).send('correct answer'); diff --git a/templates/live/interface.njk b/templates/live/interface.njk index 855cb20..a2ba2be 100644 --- a/templates/live/interface.njk +++ b/templates/live/interface.njk @@ -30,10 +30,6 @@