Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/dreancaste/TriviaPP/llms.txt

Use this file to discover all available pages before exploring further.

TriviaPP maintains two parallel ranking systems: an online leaderboard stored in Firebase Firestore that aggregates scores from all players on a given day, and a local leaderboard stored in the device’s localStorage that persists the top 20 scores for the current 24-hour window. Both are written simultaneously at the end of every trivia session.
The Firebase online ranking requires valid Firebase credentials configured in src/app/firebase.config.ts. Without them, calls to RankingService.addScore() will throw and online scores will not be persisted. The local ranking and the rest of the game will continue to function normally.

Online Ranking (Firebase Firestore)

RankingService is the Angular service responsible for all Firestore interactions. It initializes the Firebase app on first use (or reuses an existing app instance) and holds a reference to the Firestore database. Writing a scoreaddScore(name, score) creates a new document in the ranking collection:
await addDoc(collection(this.db, 'ranking'), {
  name,
  score,
  date: today  // ISO format: YYYY-MM-DD
});
Reading today’s rankinggetDailyRanking() queries the ranking collection ordered by score descending, then filters the results in memory to entries whose date field matches today’s ISO date string:
const q = query(
  collection(this.db, 'ranking'),
  orderBy('score', 'desc')
);
const snapshot = await getDocs(q);
return snapshot.docs
  .map(doc => doc.data())
  .filter(item => item['date'] === today);
RankingPage calls getDailyRanking() in ngOnInit and binds the returned array to the leaderboard view.

Local Ranking (localStorage)

StorageService maintains a local copy of the leaderboard under the storage key sw_ranking. Key behaviors:
  • Top 20 cap — after every insert the array is sorted by score descending and trimmed to 20 entries.
  • Name-based upsert — if a player with the same display name (case-insensitive) already exists, only their score is updated — and only if the new score is higher.
  • Automatic 24-hour reset — a companion key sw_ranking_reset_time records the Unix timestamp of the last reset. Every call to getRanking() or addRankingItem() checks whether 24 hours have elapsed; if so, sw_ranking is cleared and the reset timestamp is refreshed.

RankingItem Interface

Both the Firebase documents and the local storage entries are shaped by the RankingItem model:
export interface RankingItem {
  name: string;
  score: number;
  date?: string;
}
The date field is optional on the interface; the Firestore writer always populates it, while the local storage writer omits it.

When Scores Are Saved

Scores are written at the very end of a trivia session inside TriviaPage.finishGame(). The sequence is:
  1. StorageService.addHistory(historyItem) — session saved to history.
  2. StorageService.addRankingItem(rankingItem) — local leaderboard updated.
  3. StorageService.updateStats(score, correctCount) — cumulative stats updated.
  4. RankingService.addScore(displayName, score) — entry posted to Firestore.
All four operations run for every completed game, so the local and online rankings are always in sync (assuming Firebase is reachable).

Build docs developers (and LLMs) love