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.
StorageService is the sole abstraction over browser localStorage in TriviaPP. It serialises and deserialises four independent data domains — player profile, game history, a local leaderboard, and cumulative statistics — each under its own namespaced key. All reads return sensible defaults when data is absent, so callers never need to guard against null. The local ranking additionally features an automatic 24-hour reset: a timestamp stored in sw_ranking_reset_time is checked on every ranking read or write, and if 24 hours have elapsed, the ranking is wiped so the daily leaderboard stays fresh.
Constructor / Dependencies
StorageService is provided in the root injector and has no constructor dependencies.
Storage Keys
| Key | Data type | Description |
|---|---|---|
sw_profile | Profile | Player display name, avatar, and haptic preference |
sw_history | HistoryItem[] | Ordered list of past game sessions (most recent first) |
sw_ranking | RankingItem[] | Local leaderboard, capped at 20 entries, sorted by score descending |
sw_stats | { gamesPlayed, correctAnswers, maxScore } | Cumulative lifetime statistics |
sw_ranking_reset_time | number (epoch ms) | Unix timestamp of the last ranking reset; used to gate 24-hour resets |
Models
Methods — Profile
saveProfile()
Serialises the given Profile object to localStorage under the sw_profile key.
The full profile object to persist. All three fields (
displayName, avatar, vibrateOnError) should be present.getProfile()
Reads and deserialises the stored profile. Returns a default profile object when no data is found.
Methods — History
addHistory()
Prepends a new HistoryItem to the stored history array, keeping the most recent session at index 0.
The completed game session record to add.
getHistory()
Returns the full history array. Returns an empty array [] if no history has been recorded yet.
saveHistory()
Persists a complete, pre-built history array to localStorage, overwriting any existing data. Prefer addHistory() for appending individual records.
The full history array to store.
Methods — Local Ranking
addRankingItem()
Adds or updates an entry in the local leaderboard, applying merge-by-name, sorting, and a 20-entry cap.
The ranking entry to add.
name is used as the identity key (case-insensitive, trimmed).- Calls
checkAndResetRanking()— wipes the board if 24 hours have passed. - Searches the existing ranking for an entry whose
namematchesitem.name(case-insensitive). - If a match is found, replaces it only if the new score is higher than the stored score.
- If no match is found, appends the new item.
- Sorts the resulting array by
scoredescending. - Caps the board at the top 20 entries.
getRanking()
Returns the current local leaderboard, checking for a 24-hour reset before reading.
[] if the ranking is empty or has just been reset. The array is pre-sorted by score descending.
saveRanking()
Overwrites the stored ranking array. Prefer addRankingItem() for individual updates.
The full ranking array to store.
clearRanking()
Removes the sw_ranking key from localStorage, effectively resetting the local leaderboard to empty.
clearRanking() does not update sw_ranking_reset_time. Use it for explicit manual resets (e.g., a “Clear leaderboard” button). The automatic 24-hour reset is handled internally by checkAndResetRanking().Methods — Stats
updateStats()
Increments gamesPlayed and correctAnswers, and updates maxScore if the new score is a personal best. All updates are applied atomically to the stored stats object.
The total score achieved in the completed session.
The number of questions answered correctly in the completed session.
getStats()
Returns the cumulative stats object. Returns zeroed defaults when no data is stored.
saveStats()
Persists a complete stats object to localStorage, overwriting any existing data. Prefer updateStats() for post-game recording.
The stats object to store. Should include
gamesPlayed, correctAnswers, and maxScore.