import { QuestKey } from "./Quests" ; enum GlobalStateKey { BEAT_GAME_ONCE = "beatGame" , ACHIEVED_ENDINGS = "achievedEndings" } export class GlobalState { private static getParsedValueByKey < T > ( key , fallback ) { const serialized = localStorage . getItem ( key ) ; if ( serialized ) { try { const parsed = JSON . parse ( serialized ) as T ; return parsed || fallback ; } catch ( e ) { console . error ( e ) ; return fallback ; } } else { return fallback ; } } private static setKeyValuePair < T > ( key , value ) { try { const serialized = JSON . stringify ( value ) ; localStorage . setItem ( key , serialized ) ; } catch ( e ) { console . error ( e ) ; } } public static getHasBeatenGame ( ) { return GlobalState . getParsedValueByKey < boolean > ( GlobalStateKey . BEAT_GAME_ONCE , false ) ; } public static setHasBeatenGame ( ) { GlobalState . setKeyValuePair < boolean > ( GlobalStateKey . BEAT_GAME_ONCE , true ) ; } public static getAchievedEndings ( ) { return GlobalState . getParsedValueByKey < QuestKey [ ] > ( GlobalStateKey . ACHIEVED_ENDINGS , [ ] ) ; } public static setAchievedEnding ( questKey ) { const achievedEndings = GlobalState . getParsedValueByKey < QuestKey [ ] > ( GlobalStateKey . ACHIEVED_ENDINGS , [ ] ) ; if ( ! achievedEndings . includes ( questKey ) ) { achievedEndings . push ( questKey ) ; GlobalState . setKeyValuePair < QuestKey [ ] > ( GlobalStateKey . ACHIEVED_ENDINGS , achievedEndings ) ; } } }