Hussien Haider commited on
Commit
d264e15
·
1 Parent(s): 39591f4
Files changed (1) hide show
  1. src/routes/challenges.ts +18 -4
src/routes/challenges.ts CHANGED
@@ -18,7 +18,7 @@ const batchStatusSchema = z.object({
18
  const submitSchema = z.object({
19
  challengeId: z.string().uuid(),
20
  code: z.string().min(1).max(50000),
21
- output: z.string().max(100000).optional(),
22
  });
23
 
24
  const practiceSchema = z.object({
@@ -258,7 +258,7 @@ router.post('/submit', authenticate, async (req: AuthRequest, res: Response) =>
258
  return;
259
  }
260
 
261
- const { challengeId, code } = result.data;
262
 
263
  const { data: challenge, error: challengeError } = await supabase
264
  .from('challenges')
@@ -359,13 +359,25 @@ router.post('/submit', authenticate, async (req: AuthRequest, res: Response) =>
359
  let score: number;
360
  let feedback = '';
361
  let suggestions = '';
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
  if (challenge.type === 'multiple_choice') {
364
  // MCQ: validate answer server-side using test_cases.correct
365
  const selectedIndex = parseInt(code, 10);
366
  const correctIndex = challenge.test_cases?.correct;
367
  isCorrect = !isNaN(selectedIndex) && selectedIndex === correctIndex;
368
- score = isCorrect ? challenge.points : 0;
369
  feedback = isCorrect ? 'Correct!' : 'Not quite right. Try again!';
370
  } else {
371
  // AI-powered evaluation for all other challenge types
@@ -376,7 +388,7 @@ router.post('/submit', authenticate, async (req: AuthRequest, res: Response) =>
376
  challenge.points
377
  );
378
  isCorrect = evaluation.isCorrect;
379
- score = evaluation.score;
380
  feedback = evaluation.feedback;
381
  suggestions = evaluation.suggestions;
382
  }
@@ -433,6 +445,8 @@ router.post('/submit', authenticate, async (req: AuthRequest, res: Response) =>
433
  score,
434
  feedback,
435
  suggestions,
 
 
436
  },
437
  message: isCorrect ? 'Challenge completed!' : 'Keep trying!',
438
  });
 
18
  const submitSchema = z.object({
19
  challengeId: z.string().uuid(),
20
  code: z.string().min(1).max(50000),
21
+ timeSpent: z.number().min(0).max(300).optional(),
22
  });
23
 
24
  const practiceSchema = z.object({
 
258
  return;
259
  }
260
 
261
+ const { challengeId, code, timeSpent } = result.data;
262
 
263
  const { data: challenge, error: challengeError } = await supabase
264
  .from('challenges')
 
359
  let score: number;
360
  let feedback = '';
361
  let suggestions = '';
362
+ const TIMER_DURATION = 300; // 5 minutes
363
+
364
+ // Calculate time-based score multiplier
365
+ const getTimeMultiplier = (time?: number): number => {
366
+ if (!time) return 1; // No timer = full points
367
+ const ratio = (TIMER_DURATION - time) / TIMER_DURATION;
368
+ if (ratio >= 0.8) return 1; // Full points if solved in first 20% time
369
+ if (ratio >= 0.6) return 0.8; // 80% points
370
+ if (ratio >= 0.4) return 0.6; // 60% points
371
+ if (ratio >= 0.2) return 0.4; // 40% points
372
+ return 0.2; // Minimum 20% points
373
+ };
374
 
375
  if (challenge.type === 'multiple_choice') {
376
  // MCQ: validate answer server-side using test_cases.correct
377
  const selectedIndex = parseInt(code, 10);
378
  const correctIndex = challenge.test_cases?.correct;
379
  isCorrect = !isNaN(selectedIndex) && selectedIndex === correctIndex;
380
+ score = isCorrect ? Math.round(challenge.points * getTimeMultiplier(timeSpent)) : 0;
381
  feedback = isCorrect ? 'Correct!' : 'Not quite right. Try again!';
382
  } else {
383
  // AI-powered evaluation for all other challenge types
 
388
  challenge.points
389
  );
390
  isCorrect = evaluation.isCorrect;
391
+ score = isCorrect ? Math.round(evaluation.score * getTimeMultiplier(timeSpent)) : evaluation.score;
392
  feedback = evaluation.feedback;
393
  suggestions = evaluation.suggestions;
394
  }
 
445
  score,
446
  feedback,
447
  suggestions,
448
+ timeSpent: timeSpent || 0,
449
+ basePoints: challenge.points,
450
  },
451
  message: isCorrect ? 'Challenge completed!' : 'Keep trying!',
452
  });