feat: added game active page
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
const STORAGE_KEY = 'nowchess.games';
|
||||
const MAX_ENTRIES = 50;
|
||||
|
||||
interface GameEntry {
|
||||
id: string;
|
||||
addedAt: number;
|
||||
}
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class GameHistoryService {
|
||||
recordGame(gameId: string): void {
|
||||
const entries = this.load().filter((e) => e.id !== gameId);
|
||||
entries.unshift({ id: gameId, addedAt: Date.now() });
|
||||
this.save(entries.slice(0, MAX_ENTRIES));
|
||||
}
|
||||
|
||||
getGameIds(): string[] {
|
||||
return this.load().map((e) => e.id);
|
||||
}
|
||||
|
||||
removeGame(gameId: string): void {
|
||||
this.save(this.load().filter((e) => e.id !== gameId));
|
||||
}
|
||||
|
||||
private load(): GameEntry[] {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
return raw ? (JSON.parse(raw) as GameEntry[]) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private save(entries: GameEntry[]): void {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(entries));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user