Add Word Wrangler demos

This commit is contained in:
Mark Backman
2025-04-24 14:29:19 -04:00
parent 09ff836ef6
commit c80d09f66c
54 changed files with 12209 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
/**
* Formats seconds into MM:SS format
*/
export function formatTime(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
}

View File

@@ -0,0 +1,37 @@
/**
* Safely clears any type of timer
* @param timer The timer to clear
* @returns null to reassign to the timer reference
*/
export function clearTimer(timer: NodeJS.Timeout | null): null {
if (timer) {
clearTimeout(timer);
}
return null;
}
/**
* Creates a countdown timer that calls the callback every second
* @returns A function to stop the timer
*/
export function createCountdownTimer(
durationSeconds: number,
onTick: (secondsLeft: number) => void,
onComplete: () => void
): () => void {
let secondsLeft = durationSeconds;
const timer = setInterval(() => {
secondsLeft--;
onTick(secondsLeft);
if (secondsLeft <= 0) {
clearInterval(timer);
onComplete();
}
}, 1000);
return () => {
clearInterval(timer);
};
}

View File

@@ -0,0 +1,37 @@
import { TRANSCRIPT_PATTERNS } from '@/constants/gameConstants';
/**
* Checks if a transcript contains a correct guess for the target word
*/
export function detectWordGuess(transcript: string, targetWord: string) {
const currentWordLower = targetWord.toLowerCase().trim();
// Primary detection: Look for explicit guesses
const guessPattern = TRANSCRIPT_PATTERNS.GUESS_PATTERN;
const guessMatch = transcript.match(guessPattern);
if (guessMatch) {
// Extract the guessed word from whichever group matched (group 1 or 2)
let guessedWord = (guessMatch[1] || guessMatch[2] || '')
.toLowerCase()
.trim();
// Remove articles ("a", "an", "the") from the beginning of the guessed word
guessedWord = guessedWord.replace(/^(a|an|the)\s+/i, '');
return {
isCorrect: guessedWord === currentWordLower,
isExplicitGuess: true,
guessedWord,
};
}
// Secondary detection: Check if word appears in transcript
const containsWord = transcript.toLowerCase().includes(currentWordLower);
return {
isCorrect: containsWord,
isExplicitGuess: false,
guessedWord: containsWord ? targetWord : null,
};
}