import { Injectable } from '@angular/core' ; import { AchievementHistory } from '../../models/achievement/achievement-history' ; import { CompletedAchievement } from '../../models/completed-achievement' ; import { LocalStorageService } from '../local-storage' ; import { HsAchievementsInfo } from './achievements-info' ; declare let amplitude ; @ Injectable ( ) export class AchievementsStorageService { private achievementsCache : { [ achievementId ] : CompletedAchievement ; } = { } ; constructor ( private readonly localStorageService ) { } public async saveInGameAchievements ( info ) { this . localStorageService . setItem ( LocalStorageService . LOCAL_STORAGE_IN_GAME_ACHIEVEMENTS , info ) ; return info ; } public async retrieveInGameAchievements ( ) { const fromStorage = this . localStorageService . getItem ( LocalStorageService . LOCAL_STORAGE_IN_GAME_ACHIEVEMENTS ) ; return fromStorage ; } public save ( achievement ) { this . achievementsCache [ achievement . id ] = achievement ; return achievement ; } public setAll ( achievements ) { if ( ! achievements ) { return [ ] ; } this . achievementsCache = { } ; [ ... achievements ] . forEach ( ( achievement ) => this . save ( achievement ) ) ; return this . getAll ( ) ; } public getAchievement ( achievementId ) { const achievement = this . achievementsCache [ achievementId ] || CompletedAchievement . create ( { id : achievementId , numberOfCompletions : 0 , } as CompletedAchievement ) ; return achievement ; } public getAll ( ) { return Object . values ( this . achievementsCache ) ; } public async loadAllHistory ( ) { const fromStorage = this . localStorageService . getItem ( LocalStorageService . LOCAL_STORAGE_ACHIEVEMENTS_HISTORY ) ; return fromStorage ?? [ ] ; } public async saveAllHistory ( history ) { this . localStorageService . setItem ( LocalStorageService . LOCAL_STORAGE_ACHIEVEMENTS_HISTORY , history ) ; } public async saveHistory ( history ) { const fromStorage = this . localStorageService . getItem ( LocalStorageService . LOCAL_STORAGE_ACHIEVEMENTS_HISTORY ) ; const historyList = fromStorage ?? [ ] ; const newHistory = [ history , ... historyList ] ; this . saveAllHistory ( newHistory ) ; } }