76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
|
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<string, ChellarisGame>(),
|
||
|
ethics: [],
|
||
|
portraits: []
|
||
|
}
|
||
|
|
||
|
dummyData.games.set("1", {
|
||
|
name: "Game 16",
|
||
|
groups: new Map<string, ChellarisGameGroup>(),
|
||
|
empires: [],
|
||
|
});
|
||
|
|
||
|
dummyData.games.set("2", {
|
||
|
name: "Game 17",
|
||
|
groups: new Map<string, ChellarisGameGroup>(),
|
||
|
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<string, Array<string>>(JSON.parse(store));
|
||
|
const tmp = gameGroupSelectionMap.get(selectedGame);
|
||
|
let selectedGameGroups: Array<string> = [];
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|