feat: added new features, refactored code
This commit is contained in:
105
game.ts
105
game.ts
@@ -2,12 +2,18 @@
|
||||
|
||||
import { textToMorse, compareMorse, normalizeMorse } from "./morse.ts";
|
||||
|
||||
export type GameMode = 'letters' | 'numbers' | 'words' | 'phrases';
|
||||
export type GameMode =
|
||||
| "letters"
|
||||
| "alphanumeric"
|
||||
| "full"
|
||||
| "words"
|
||||
| "phrases";
|
||||
|
||||
export interface GameConfig {
|
||||
mode: GameMode;
|
||||
rounds: number;
|
||||
timePerRound: number; // seconds
|
||||
dynamicTime?: boolean; // For words/phrases: multiply time by character count
|
||||
}
|
||||
|
||||
export interface RoundResult {
|
||||
@@ -26,26 +32,50 @@ export interface GameSession {
|
||||
bestStreak: number;
|
||||
}
|
||||
|
||||
// Word banks for different difficulty levels
|
||||
const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
|
||||
const NUMBERS = '0123456789'.split('');
|
||||
// Character sets for different difficulty levels
|
||||
const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
||||
const NUMBERS = "0123456789".split("");
|
||||
const PUNCTUATION = ".,?!-/()@".split("");
|
||||
const ALPHANUMERIC = [...LETTERS, ...NUMBERS];
|
||||
const FULL_SET = [...LETTERS, ...NUMBERS, ...PUNCTUATION];
|
||||
const WORDS = [
|
||||
'HELLO', 'WORLD', 'CODE', 'TIME', 'GAME', 'TEST', 'PLAY',
|
||||
'FAST', 'SLOW', 'HELP', 'GOOD', 'BEST', 'NICE', 'COOL',
|
||||
'MORSE', 'SIGNAL', 'RADIO', 'SEND', 'MESSAGE', 'QUICK',
|
||||
'LEARN', 'PRACTICE', 'SKILL', 'MASTER', 'EXPERT',
|
||||
"HELLO",
|
||||
"WORLD",
|
||||
"CODE",
|
||||
"TIME",
|
||||
"GAME",
|
||||
"TEST",
|
||||
"PLAY",
|
||||
"FAST",
|
||||
"SLOW",
|
||||
"HELP",
|
||||
"GOOD",
|
||||
"BEST",
|
||||
"NICE",
|
||||
"COOL",
|
||||
"MORSE",
|
||||
"SIGNAL",
|
||||
"RADIO",
|
||||
"SEND",
|
||||
"MESSAGE",
|
||||
"QUICK",
|
||||
"LEARN",
|
||||
"PRACTICE",
|
||||
"SKILL",
|
||||
"MASTER",
|
||||
"EXPERT",
|
||||
];
|
||||
const PHRASES = [
|
||||
'HELLO WORLD',
|
||||
'GOOD MORNING',
|
||||
'HOW ARE YOU',
|
||||
'THANK YOU',
|
||||
'SEE YOU SOON',
|
||||
'HAVE A NICE DAY',
|
||||
'MORSE CODE',
|
||||
'QUICK BROWN FOX',
|
||||
'THE END',
|
||||
'WELL DONE',
|
||||
"HELLO WORLD",
|
||||
"GOOD MORNING",
|
||||
"HOW ARE YOU",
|
||||
"THANK YOU",
|
||||
"SEE YOU SOON",
|
||||
"HAVE A NICE DAY",
|
||||
"MORSE CODE",
|
||||
"QUICK BROWN FOX",
|
||||
"THE END",
|
||||
"WELL DONE",
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -53,13 +83,15 @@ const PHRASES = [
|
||||
*/
|
||||
export function getChallenge(mode: GameMode): string {
|
||||
switch (mode) {
|
||||
case 'letters':
|
||||
case "letters":
|
||||
return LETTERS[Math.floor(Math.random() * LETTERS.length)];
|
||||
case 'numbers':
|
||||
return NUMBERS[Math.floor(Math.random() * NUMBERS.length)];
|
||||
case 'words':
|
||||
case "alphanumeric":
|
||||
return ALPHANUMERIC[Math.floor(Math.random() * ALPHANUMERIC.length)];
|
||||
case "full":
|
||||
return FULL_SET[Math.floor(Math.random() * FULL_SET.length)];
|
||||
case "words":
|
||||
return WORDS[Math.floor(Math.random() * WORDS.length)];
|
||||
case 'phrases':
|
||||
case "phrases":
|
||||
return PHRASES[Math.floor(Math.random() * PHRASES.length)];
|
||||
}
|
||||
}
|
||||
@@ -124,12 +156,13 @@ export function isGameComplete(session: GameSession): boolean {
|
||||
*/
|
||||
export function getGameSummary(session: GameSession) {
|
||||
const totalRounds = session.results.length;
|
||||
const correct = session.results.filter(r => r.correct).length;
|
||||
const correct = session.results.filter((r) => r.correct).length;
|
||||
const incorrect = totalRounds - correct;
|
||||
const accuracy = totalRounds > 0 ? (correct / totalRounds) * 100 : 0;
|
||||
const averageTime = totalRounds > 0
|
||||
? session.results.reduce((sum, r) => sum + r.timeSpent, 0) / totalRounds
|
||||
: 0;
|
||||
const averageTime =
|
||||
totalRounds > 0
|
||||
? session.results.reduce((sum, r) => sum + r.timeSpent, 0) / totalRounds
|
||||
: 0;
|
||||
|
||||
return {
|
||||
totalRounds,
|
||||
@@ -146,13 +179,15 @@ export function getGameSummary(session: GameSession) {
|
||||
*/
|
||||
export function getDifficultyDescription(mode: GameMode): string {
|
||||
switch (mode) {
|
||||
case 'letters':
|
||||
return 'Single letters A-Z';
|
||||
case 'numbers':
|
||||
return 'Single digits 0-9';
|
||||
case 'words':
|
||||
return 'Common 4-6 letter words';
|
||||
case 'phrases':
|
||||
return 'Short phrases';
|
||||
case "letters":
|
||||
return "Single letters A-Z";
|
||||
case "alphanumeric":
|
||||
return "Letters A-Z and numbers 0-9";
|
||||
case "full":
|
||||
return "Letters, numbers, and punctuation";
|
||||
case "words":
|
||||
return "Common 4-6 letter words";
|
||||
case "phrases":
|
||||
return "Short phrases";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user