Skip to content

Commit 5d2cc25

Browse files
committed
Implement exact 10-interval bonus questions logic fetching only 15-point questions
1 parent 63594b4 commit 5d2cc25

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

Treasure_Hunt_Backend/src/controllers/teamController.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,23 @@ export const getCurrentQuestion = async (req, res) => {
222222
`;
223223
const tableExists = await pool.query(tableCheckQuery, [`user_answers_${username}`]);
224224

225-
// 3. Get answered count for question numbering
226-
let answeredCount = 0;
225+
// Check if requesting bonus question
226+
const isBonusReq = req.query.is_bonus === 'true';
227+
const pointCondition = isBonusReq ? 'qb.points = 15' : 'qb.points != 15';
228+
229+
// 3. Get answered count for normal vs bonus questions
230+
let normalAnsweredCount = 0;
231+
let bonusAnsweredCount = 0;
227232
if (tableExists.rows[0].exists) {
228233
const answeredResult = await pool.query(
229-
`SELECT COUNT(*) FROM "user_answers_${username}"`
234+
`SELECT
235+
SUM(CASE WHEN qb.points != 15 THEN 1 ELSE 0 END) as normal_count,
236+
SUM(CASE WHEN qb.points = 15 THEN 1 ELSE 0 END) as bonus_count
237+
FROM "user_answers_${username}" ua
238+
JOIN question_bank qb ON ua.question_id = qb.id`
230239
);
231-
answeredCount = parseInt(answeredResult.rows[0].count);
240+
normalAnsweredCount = parseInt(answeredResult.rows[0].normal_count || 0);
241+
bonusAnsweredCount = parseInt(answeredResult.rows[0].bonus_count || 0);
232242
}
233243

234244
let questionResult;
@@ -238,6 +248,7 @@ export const getCurrentQuestion = async (req, res) => {
238248
`SELECT qa.id as assignment_id, qb.* FROM question_assignments qa
239249
JOIN question_bank qb ON qa.question_id = qb.id
240250
WHERE qa.user_id = $1
251+
AND ${pointCondition}
241252
AND NOT EXISTS (
242253
SELECT 1
243254
FROM "user_answers_${username}" ua
@@ -252,6 +263,7 @@ export const getCurrentQuestion = async (req, res) => {
252263
`SELECT qa.id as assignment_id, qb.* FROM question_assignments qa
253264
JOIN question_bank qb ON qa.question_id = qb.id
254265
WHERE qa.user_id = $1
266+
AND ${pointCondition}
255267
ORDER BY qa.id
256268
LIMIT 1`,
257269
[userId]
@@ -268,8 +280,9 @@ export const getCurrentQuestion = async (req, res) => {
268280

269281
res.json({
270282
success: true,
271-
question: questionResult.rows[0],
272-
question_number: answeredCount + 1,
283+
question: { ...questionResult.rows[0], is_bonus: isBonusReq },
284+
question_number: isBonusReq ? bonusAnsweredCount + 1 : normalAnsweredCount + 1,
285+
completed_bonus: bonusAnsweredCount,
273286
// Per-question time limit in seconds (5 minutes per question)
274287
time_limit: 300
275288
});

Treasure_Hunt_Frontend/src/pages/participant/Participant.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ const Participant = () => {
106106
if (response.question_number) setQuestionNumber(prev => Math.max(prev, response.question_number));
107107
setCompletedBonus(response.completed_bonus || 0);
108108
} else if (response.completed) {
109+
if (isBonus) {
110+
setError('No more bonus questions available on this path!');
111+
setIsBonusMode(false);
112+
fetchCurrentQuestion(false);
113+
return;
114+
}
109115
setCurrentQuestion({ completed: true });
110116
} else {
111117
setError('No question available');
@@ -204,8 +210,12 @@ const Participant = () => {
204210
if (image) formData.append('image', image);
205211
const response = await submitAnswer(currentQuestion.id, formData);
206212
if (response.success) {
207-
if (currentQuestion.is_bonus) setCompletedBonus(prev => prev + 1);
208-
setQuestionNumber(prev => prev + 1);
213+
if (currentQuestion.is_bonus) {
214+
setCompletedBonus(prev => prev + 1);
215+
setIsBonusMode(false);
216+
} else {
217+
setQuestionNumber(prev => prev + 1);
218+
}
209219
setSuccess('✨ Answer logged in the admin panel!');
210220
setTextAnswer('');
211221
setImage(null);

Treasure_Hunt_Frontend/src/pages/participant/components/BonusQuestionHandler.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import React from 'react';
22
import { motion } from 'framer-motion';
33

4-
const BonusQuestionHandler = ({ questionNumber, onSwitchToBonus, onSwitchToNormal, isBonusMode }) => {
5-
if (questionNumber !== 5 && questionNumber !== 10 && !isBonusMode) return null;
4+
const BonusQuestionHandler = ({ questionNumber, onSwitchToBonus, onSwitchToNormal, isBonusMode, completedBonus }) => {
5+
const availableBonuses = Math.floor((questionNumber - 1) / 10);
6+
7+
if (completedBonus >= availableBonuses && !isBonusMode) return null;
68

79
return (
810
<div style={{ marginBottom: '1.5rem', width: '100%', textAlign: 'center', position: 'relative', zIndex: 1, padding: '0 0.5rem' }}>

0 commit comments

Comments
 (0)