Animate points
This commit is contained in:
parent
c38a3e1fdf
commit
156c210dea
3 changed files with 45 additions and 8 deletions
|
|
@ -17,6 +17,8 @@ export const useGame = () => {
|
|||
const [currentCorrectShape, setCurrentCorrectShape] = useState<string | null>(null);
|
||||
const [lastPointsEarned, setLastPointsEarned] = useState<number | null>(null);
|
||||
const [selectedOption, setSelectedOption] = useState<AnswerOption | null>(null);
|
||||
const [currentPlayerScore, setCurrentPlayerScore] = useState(0);
|
||||
const [currentStreak, setCurrentStreak] = useState(0);
|
||||
|
||||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const peerRef = useRef<Peer | null>(null);
|
||||
|
|
@ -302,6 +304,12 @@ export const useGame = () => {
|
|||
|
||||
if (data.type === 'RESULT') {
|
||||
setLastPointsEarned(data.payload.scoreAdded);
|
||||
setCurrentPlayerScore(data.payload.newScore);
|
||||
if (data.payload.isCorrect) {
|
||||
setCurrentStreak(prev => prev + 1);
|
||||
} else {
|
||||
setCurrentStreak(0);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.type === 'TIME_UP') {
|
||||
|
|
@ -329,9 +337,15 @@ export const useGame = () => {
|
|||
const points = isCorrect ? Math.round(POINTS_PER_QUESTION * (timeLeftRef.current / QUESTION_TIME)) : 0;
|
||||
setLastPointsEarned(points);
|
||||
|
||||
const hostPlayer = playersRef.current.find(p => p.id === 'host');
|
||||
const newScore = (hostPlayer?.score || 0) + points;
|
||||
const newStreak = isCorrect ? (hostPlayer?.streak || 0) + 1 : 0;
|
||||
setCurrentPlayerScore(newScore);
|
||||
setCurrentStreak(newStreak);
|
||||
|
||||
setPlayers(prev => prev.map(p => {
|
||||
if (p.id !== 'host') return p;
|
||||
return { ...p, score: p.score + points, streak: isCorrect ? p.streak + 1 : 0, lastAnswerCorrect: isCorrect };
|
||||
return { ...p, score: newScore, streak: newStreak, lastAnswerCorrect: isCorrect };
|
||||
}));
|
||||
} else {
|
||||
const option = arg as AnswerOption;
|
||||
|
|
@ -355,7 +369,7 @@ export const useGame = () => {
|
|||
}, []);
|
||||
|
||||
return {
|
||||
role, gameState, quiz, players, currentQuestionIndex, timeLeft, error, gamePin, hasAnswered, lastPointsEarned, currentCorrectShape, selectedOption,
|
||||
role, gameState, quiz, players, currentQuestionIndex, timeLeft, error, gamePin, hasAnswered, lastPointsEarned, currentCorrectShape, selectedOption, currentPlayerScore, currentStreak,
|
||||
startQuizGen, startManualCreation, finalizeManualQuiz, joinGame, startGame: startHostGame, handleAnswer, nextQuestion
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue