Finer granularity points
This commit is contained in:
parent
3a22b42492
commit
a7e37024f5
3 changed files with 16 additions and 17 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { Quiz, Player, GameState, GameRole, NetworkMessage, AnswerOption, Question } from '../types';
|
||||
import { generateQuiz } from '../services/geminiService';
|
||||
import { POINTS_PER_QUESTION, QUESTION_TIME, PLAYER_COLORS } from '../constants';
|
||||
import { POINTS_PER_QUESTION, QUESTION_TIME, QUESTION_TIME_MS, PLAYER_COLORS } from '../constants';
|
||||
import { Peer, DataConnection } from 'peerjs';
|
||||
|
||||
export const useGame = () => {
|
||||
|
|
@ -147,7 +147,7 @@ export const useGame = () => {
|
|||
|
||||
if (!currentPlayer || currentPlayer.lastAnswerCorrect !== null) return;
|
||||
|
||||
const points = isCorrect ? Math.round(POINTS_PER_QUESTION * (timeLeftRef.current / QUESTION_TIME)) : 0;
|
||||
const points = isCorrect ? Math.round(POINTS_PER_QUESTION * (timeLeftRef.current / QUESTION_TIME_MS)) : 0;
|
||||
const newScore = currentPlayer.score + points;
|
||||
|
||||
setPlayers(prev => prev.map(p => {
|
||||
|
|
@ -197,16 +197,14 @@ export const useGame = () => {
|
|||
setHasAnswered(false);
|
||||
setLastPointsEarned(null);
|
||||
setSelectedOption(null);
|
||||
setTimeLeft(QUESTION_TIME);
|
||||
setTimeLeft(QUESTION_TIME_MS);
|
||||
setPlayers(prev => prev.map(p => ({ ...p, lastAnswerCorrect: null })));
|
||||
|
||||
// Use refs to get the latest state inside this async callback
|
||||
const currentQuiz = quizRef.current;
|
||||
const currentIndex = currentQuestionIndexRef.current;
|
||||
|
||||
if (currentQuiz) {
|
||||
const currentQ = currentQuiz.questions[currentIndex];
|
||||
// Ensure options exist
|
||||
const options = currentQ.options || [];
|
||||
const correctOpt = options.find(o => o.isCorrect);
|
||||
const correctShape = correctOpt?.shape || 'triangle';
|
||||
|
|
@ -214,7 +212,7 @@ export const useGame = () => {
|
|||
|
||||
const optionsForClient = options.map(o => ({
|
||||
...o,
|
||||
isCorrect: false // Masked
|
||||
isCorrect: false
|
||||
}));
|
||||
|
||||
broadcast({
|
||||
|
|
@ -233,10 +231,10 @@ export const useGame = () => {
|
|||
if (timerRef.current) clearInterval(timerRef.current);
|
||||
timerRef.current = setInterval(() => {
|
||||
setTimeLeft(prev => {
|
||||
if (prev <= 1) { endQuestion(); return 0; }
|
||||
return prev - 1;
|
||||
if (prev <= 100) { endQuestion(); return 0; }
|
||||
return prev - 100;
|
||||
});
|
||||
}, 1000);
|
||||
}, 100);
|
||||
};
|
||||
|
||||
const endQuestion = () => {
|
||||
|
|
@ -303,7 +301,7 @@ export const useGame = () => {
|
|||
setLastPointsEarned(null);
|
||||
setSelectedOption(null);
|
||||
setCurrentQuestionIndex(data.payload.currentQuestionIndex);
|
||||
setTimeLeft(data.payload.timeLimit);
|
||||
setTimeLeft(data.payload.timeLimit * 1000);
|
||||
setCurrentCorrectShape(data.payload.correctShape);
|
||||
|
||||
setQuiz(prev => {
|
||||
|
|
@ -321,7 +319,7 @@ export const useGame = () => {
|
|||
});
|
||||
|
||||
if (timerRef.current) clearInterval(timerRef.current);
|
||||
timerRef.current = setInterval(() => setTimeLeft(prev => Math.max(0, prev - 1)), 1000);
|
||||
timerRef.current = setInterval(() => setTimeLeft(prev => Math.max(0, prev - 100)), 100);
|
||||
}
|
||||
|
||||
if (data.type === 'RESULT') {
|
||||
|
|
@ -356,7 +354,7 @@ export const useGame = () => {
|
|||
const option = arg as AnswerOption;
|
||||
const isCorrect = option.isCorrect;
|
||||
setSelectedOption(option);
|
||||
const points = isCorrect ? Math.round(POINTS_PER_QUESTION * (timeLeftRef.current / QUESTION_TIME)) : 0;
|
||||
const points = isCorrect ? Math.round(POINTS_PER_QUESTION * (timeLeftRef.current / QUESTION_TIME_MS)) : 0;
|
||||
setLastPointsEarned(points);
|
||||
|
||||
const hostPlayer = playersRef.current.find(p => p.id === 'host');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue