feat: changed stats storage location, refactored code

This commit is contained in:
2025-11-21 15:47:56 +01:00
parent 043812606d
commit 052819a7c7
4 changed files with 115 additions and 64 deletions

View File

@@ -27,7 +27,7 @@ export interface ModeStats {
}
export interface GameResult {
mode: 'letters' | 'numbers' | 'words' | 'phrases';
mode: "letters" | "numbers" | "words" | "phrases";
rounds: number;
correct: number;
incorrect: number;
@@ -43,20 +43,39 @@ const DEFAULT_STATS: GameStats = {
averageTimePerRound: 0,
bestStreak: 0,
modeStats: {
letters: { gamesPlayed: 0, correctAnswers: 0, incorrectAnswers: 0, averageAccuracy: 0 },
numbers: { gamesPlayed: 0, correctAnswers: 0, incorrectAnswers: 0, averageAccuracy: 0 },
words: { gamesPlayed: 0, correctAnswers: 0, incorrectAnswers: 0, averageAccuracy: 0 },
phrases: { gamesPlayed: 0, correctAnswers: 0, incorrectAnswers: 0, averageAccuracy: 0 },
letters: {
gamesPlayed: 0,
correctAnswers: 0,
incorrectAnswers: 0,
averageAccuracy: 0,
},
numbers: {
gamesPlayed: 0,
correctAnswers: 0,
incorrectAnswers: 0,
averageAccuracy: 0,
},
words: {
gamesPlayed: 0,
correctAnswers: 0,
incorrectAnswers: 0,
averageAccuracy: 0,
},
phrases: {
gamesPlayed: 0,
correctAnswers: 0,
incorrectAnswers: 0,
averageAccuracy: 0,
},
},
};
function getStatsPath(): string {
const homeDir = Deno.env.get("HOME") || Deno.env.get("USERPROFILE") || ".";
return join(homeDir, ".morse-game", "stats.json");
return join(Deno.cwd(), "data", "stats.json");
}
/**
* Load stats from disk
* Load stats from JSON file
*/
export async function loadStats(): Promise<GameStats> {
try {
@@ -69,13 +88,13 @@ export async function loadStats(): Promise<GameStats> {
}
/**
* Save stats to disk
* Save stats to JSON file
*/
export async function saveStats(stats: GameStats): Promise<void> {
const statsPath = getStatsPath();
const dir = join(Deno.env.get("HOME") || Deno.env.get("USERPROFILE") || ".", ".morse-game");
const dataDir = join(Deno.cwd(), "data");
await ensureDir(dir);
await ensureDir(dataDir);
await Deno.writeTextFile(statsPath, JSON.stringify(stats, null, 2));
}
@@ -92,7 +111,8 @@ export async function updateStats(result: GameResult): Promise<GameStats> {
stats.incorrectAnswers += result.incorrect;
stats.averageTimePerRound =
(stats.averageTimePerRound * (stats.totalRounds - result.rounds) +
result.averageTime * result.rounds) / stats.totalRounds;
result.averageTime * result.rounds) /
stats.totalRounds;
stats.bestStreak = Math.max(stats.bestStreak, result.streak);
stats.lastPlayed = new Date().toISOString();
@@ -102,9 +122,8 @@ export async function updateStats(result: GameResult): Promise<GameStats> {
modeStats.correctAnswers += result.correct;
modeStats.incorrectAnswers += result.incorrect;
const totalAnswers = modeStats.correctAnswers + modeStats.incorrectAnswers;
modeStats.averageAccuracy = totalAnswers > 0
? (modeStats.correctAnswers / totalAnswers) * 100
: 0;
modeStats.averageAccuracy =
totalAnswers > 0 ? (modeStats.correctAnswers / totalAnswers) * 100 : 0;
await saveStats(stats);
return stats;