diff --git a/src/routes/graphs/+layout.ts b/src/routes/graphs/+layout.ts new file mode 100644 index 0000000..e16c8be --- /dev/null +++ b/src/routes/graphs/+layout.ts @@ -0,0 +1,76 @@ +import type { PageLoad } from "../$types"; +import ChellarisDataStore from '$lib/stores/ChellarisData'; +import SelectedGameStore from '$lib/stores/GameFilter'; +import type { ChellarisInfo, ChellarisGame, ChellarisGameGroup } from '../../lib/types/chellaris'; +import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter"; + +export const load: PageLoad = async () => { + // Chellaris Data Code + // Game Dummy Data + const dummyData: ChellarisInfo = { + games: new Map(), + ethics: [], + portraits: [] + } + + dummyData.games.set("1", { + name: "Game 16", + groups: new Map(), + empires: [], + }); + + dummyData.games.set("2", { + name: "Game 17", + groups: new Map(), + empires: [], + }); + + // Group Dummy Data + dummyData.games.get("1")?.groups.set("1", {name: "A"}); + dummyData.games.get("1")?.groups.set("2", {name: "B"}); + dummyData.games.get("1")?.groups.set("3", {name: "None"}); + + dummyData.games.get("2")?.groups.set("4", {name: "A"}); + dummyData.games.get("2")?.groups.set("5", {name: "B"}); + dummyData.games.get("2")?.groups.set("6", {name: "None2"}); + + // + ChellarisDataStore.set(dummyData) + + // Local Storage Code + let store: string | null; + + if (typeof localStorage !== 'undefined') { + // Game Selection + store = localStorage.getItem('gameSelection'); + + let selectedGame = ""; + if (typeof store == 'string') { + selectedGame = JSON.parse(store); + SelectedGameStore.set(selectedGame); + } + + // Game Groups Selection + store = localStorage.getItem('gameGroupSelection'); + + if (typeof store == 'string') { + const gameGroupSelectionMap = new Map>(JSON.parse(store)); + const tmp = gameGroupSelectionMap.get(selectedGame); + let selectedGameGroups: Array = []; + if (typeof tmp !== 'undefined') { + selectedGameGroups = [...tmp.values()]; + } else { + const tmpGameData = dummyData.games.get(selectedGame); + // If this fails an empty array is precisely what we want + if (typeof tmpGameData !== "undefined") { + // Default to all available groups + selectedGameGroups = [...tmpGameData.groups.keys()]; + // Set Local Storage to default Values if not previously defined + localStorage.setItem('gameGroupSelection', JSON.stringify(Array.from(selectedGameGroups.entries()))); + } + } + gameGroupSelectionMap.set(selectedGame, selectedGameGroups); + SelectedGameGroupsStore.set(gameGroupSelectionMap); + } + } +} \ No newline at end of file