Merge game history feature: Add multi-game save and analysis mode

This commit is contained in:
Stefan
2025-11-27 09:33:23 +01:00
8 changed files with 967 additions and 57 deletions
+120
View File
@@ -41,6 +41,13 @@ export interface Translations {
playAsWhite: string;
playAsBlack: string;
randomColor: string;
analyzeGame: string;
savedGamesTitle: string;
savedGamesEmpty: string;
opponentLabel: string;
evaluationLabel: string;
noEvaluation: string;
deleteGame: string;
};
// Game
@@ -90,6 +97,23 @@ export interface Translations {
evaluation: string;
bestMove: string;
aiAnalysis: string;
modeTitle: string;
modeDescription: string;
pasteLabel: string;
pastePlaceholder: string;
chooseCoach: string;
orientation: string;
startButton: string;
next: string;
previous: string;
step: string;
missedTactics: string;
cpLoss: string;
none: string;
opening: string;
enginePending: string;
coachPending: string;
importError: string;
};
// API Key Input
@@ -172,6 +196,13 @@ const en: Translations = {
playAsWhite: 'Play as White',
playAsBlack: 'Play as Black',
randomColor: 'Random',
analyzeGame: 'Analyze a Game',
savedGamesTitle: 'Unfinished games',
savedGamesEmpty: 'No unfinished games yet.',
opponentLabel: 'Opponent',
evaluationLabel: 'Evaluation',
noEvaluation: 'No evaluation yet',
deleteGame: 'Delete game',
},
game: {
playingAs: 'Playing as',
@@ -213,6 +244,23 @@ const en: Translations = {
evaluation: 'Evaluation',
bestMove: 'Best Move',
aiAnalysis: 'AI Analysis',
modeTitle: 'Analyze an Existing Game',
modeDescription: 'Upload a PGN or FEN and let your coach walk you through every move with engine-backed insights.',
pasteLabel: 'PGN or FEN Input',
pastePlaceholder: 'Paste PGN or FEN here to review the game move by move...',
chooseCoach: 'Choose Coach Personality',
orientation: 'Board Orientation',
startButton: 'Start Analysis',
next: 'Next Move',
previous: 'Previous Move',
step: 'Move',
missedTactics: 'Missed Tactics',
cpLoss: 'Evaluation Change',
none: 'None detected',
opening: 'Opening',
enginePending: 'Running engine evaluation...',
coachPending: 'Coach is preparing feedback...',
importError: 'Could not load that PGN or FEN. Please check the notation.',
},
apiKeyInput: {
title: 'API Key Required',
@@ -296,6 +344,13 @@ const de: Translations = {
playAsWhite: 'Als Weiß spielen',
playAsBlack: 'Als Schwarz spielen',
randomColor: 'Zufällig',
analyzeGame: 'Partie analysieren',
savedGamesTitle: 'Unfertige Partien',
savedGamesEmpty: 'Keine unfertigen Partien vorhanden.',
opponentLabel: 'Gegner',
evaluationLabel: 'Bewertung',
noEvaluation: 'Keine Bewertung',
deleteGame: 'Partie löschen',
},
game: {
playingAs: 'Spielst als',
@@ -337,6 +392,23 @@ const de: Translations = {
evaluation: 'Bewertung',
bestMove: 'Bester Zug',
aiAnalysis: 'KI-Analyse',
modeTitle: 'Bestehende Partie analysieren',
modeDescription: 'PGN oder FEN hochladen und vom Coach mit Engine-Unterstützung durch die Partie führen lassen.',
pasteLabel: 'PGN- oder FEN-Eingabe',
pastePlaceholder: 'PGN oder FEN hier einfügen, um die Partie Zug für Zug anzusehen...',
chooseCoach: 'Coach-Persönlichkeit wählen',
orientation: 'Brettausrichtung',
startButton: 'Analyse starten',
next: 'Nächster Zug',
previous: 'Vorheriger Zug',
step: 'Zug',
missedTactics: 'Verpasste Taktiken',
cpLoss: 'Bewertungsänderung',
none: 'Keine erkannt',
opening: 'Eröffnung',
enginePending: 'Engine-Bewertung läuft...',
coachPending: 'Coach bereitet Feedback vor...',
importError: 'PGN oder FEN konnte nicht geladen werden. Bitte Notation prüfen.',
},
apiKeyInput: {
title: 'API-Schlüssel erforderlich',
@@ -420,6 +492,13 @@ const fr: Translations = {
playAsWhite: 'Jouer Blancs',
playAsBlack: 'Jouer Noirs',
randomColor: 'Aléatoire',
analyzeGame: 'Analyser une partie',
savedGamesTitle: 'Parties inachevées',
savedGamesEmpty: 'Aucune partie en cours.',
opponentLabel: 'Adversaire',
evaluationLabel: 'Évaluation',
noEvaluation: 'Pas d\'évaluation',
deleteGame: 'Supprimer la partie',
},
game: {
playingAs: 'Jouant',
@@ -461,6 +540,23 @@ const fr: Translations = {
evaluation: 'Évaluation',
bestMove: 'Meilleur coup',
aiAnalysis: 'Analyse IA',
modeTitle: 'Analyser une partie existante',
modeDescription: 'Importez un PGN ou un FEN et laissez le coach commenter chaque coup avec laide du moteur.',
pasteLabel: 'Saisie PGN ou FEN',
pastePlaceholder: 'Collez ici un PGN ou un FEN pour revoir la partie coup par coup...',
chooseCoach: 'Choisir la personnalité du coach',
orientation: 'Orientation de l’échiquier',
startButton: 'Lancer lanalyse',
next: 'Coup suivant',
previous: 'Coup précédent',
step: 'Coup',
missedTactics: 'Tactiques manquées',
cpLoss: 'Changement d’évaluation',
none: 'Aucune détectée',
opening: 'Ouverture',
enginePending: 'Évaluation du moteur en cours...',
coachPending: 'Le coach prépare son retour...',
importError: 'Impossible de charger ce PGN ou FEN. Merci de vérifier la notation.',
},
apiKeyInput: {
title: 'Clé API requise',
@@ -544,6 +640,13 @@ const it: Translations = {
playAsWhite: 'Gioca Bianco',
playAsBlack: 'Gioca Nero',
randomColor: 'Casuale',
analyzeGame: 'Analizza una partita',
savedGamesTitle: 'Partite non finite',
savedGamesEmpty: 'Nessuna partita in corso.',
opponentLabel: 'Avversario',
evaluationLabel: 'Valutazione',
noEvaluation: 'Nessuna valutazione',
deleteGame: 'Elimina partita',
},
game: {
playingAs: 'Giocando',
@@ -585,6 +688,23 @@ const it: Translations = {
evaluation: 'Valutazione',
bestMove: 'Mossa migliore',
aiAnalysis: 'Analisi IA',
modeTitle: 'Analizza una partita esistente',
modeDescription: 'Carica un PGN o un FEN e lascia che il coach commenti ogni mossa con il supporto del motore.',
pasteLabel: 'Input PGN o FEN',
pastePlaceholder: 'Incolla qui PGN o FEN per rivedere la partita mossa per mossa...',
chooseCoach: 'Scegli la personalità del coach',
orientation: 'Orientamento della scacchiera',
startButton: 'Avvia analisi',
next: 'Mossa successiva',
previous: 'Mossa precedente',
step: 'Mossa',
missedTactics: 'Tattiche mancate',
cpLoss: 'Variazione di valutazione',
none: 'Nessuna rilevata',
opening: 'Apertura',
enginePending: 'Valutazione del motore in corso...',
coachPending: 'Il coach sta preparando il feedback...',
importError: 'Impossibile caricare questo PGN o FEN. Controlla la notazione.',
},
apiKeyInput: {
title: 'Chiave API richiesta',
+92
View File
@@ -0,0 +1,92 @@
import { Personality } from "./personalities";
import { StockfishEvaluation } from "./stockfish";
import { SupportedLanguage } from "./i18n/translations";
export type SavedGame = {
id: string;
fen: string;
pgn?: string;
selectedPersonality: Personality;
playerColor: "white" | "black";
updatedAt: number;
evaluation?: Pick<StockfishEvaluation, "score" | "mate" | "depth"> | null;
language?: SupportedLanguage;
apiKey?: string | null;
};
const STORAGE_KEY = "chess_tutor_saves";
const LEGACY_KEY = "chess_tutor_save";
const parseSavedGames = (): SavedGame[] => {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return [];
try {
const data = JSON.parse(raw);
if (!Array.isArray(data)) return [];
return data.filter(Boolean);
} catch (e) {
console.error("Failed to parse saved games", e);
return [];
}
};
const persistSavedGames = (games: SavedGame[]) => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(games));
};
const loadLegacySave = (): SavedGame[] => {
const legacy = localStorage.getItem(LEGACY_KEY);
if (!legacy) return [];
try {
const parsed = JSON.parse(legacy);
if (parsed && parsed.fen && parsed.selectedPersonality) {
const legacyGame: SavedGame = {
id: parsed.id || `legacy-${Date.now()}`,
fen: parsed.fen,
pgn: parsed.pgn,
selectedPersonality: parsed.selectedPersonality,
playerColor: parsed.playerColor || "white",
updatedAt: parsed.updatedAt || Date.now(),
evaluation: parsed.evaluation || null,
};
return [legacyGame];
}
} catch (e) {
console.error("Failed to migrate legacy save", e);
}
return [];
};
export const loadSavedGames = (): SavedGame[] => {
const existing = parseSavedGames();
if (existing.length > 0) {
return existing.sort((a, b) => b.updatedAt - a.updatedAt);
}
const legacy = loadLegacySave();
if (legacy.length > 0) {
persistSavedGames(legacy);
localStorage.removeItem(LEGACY_KEY);
return legacy.sort((a, b) => b.updatedAt - a.updatedAt);
}
return [];
};
export const upsertSavedGame = (game: SavedGame) => {
const games = parseSavedGames();
const index = games.findIndex(g => g.id === game.id);
const updatedGames = index >= 0
? games.map(g => (g.id === game.id ? game : g))
: [...games, game];
persistSavedGames(updatedGames);
};
export const deleteSavedGame = (id: string) => {
const games = parseSavedGames().filter(g => g.id !== id);
persistSavedGames(games);
};