Keyed Object Fixes

This commit is contained in:
Neshura 2023-08-28 07:28:43 +02:00
parent d5229a2ab4
commit c15f96916e
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
5 changed files with 30 additions and 126 deletions

View file

@ -13,10 +13,11 @@
<style> <style>
.backdrop { .backdrop {
z-index: 200;
width: 100%; width: 100%;
height: 100%; height: 100%;
position: fixed; position: fixed;
background: rgba(0, 0, 0, 0.8); background-color: rgba(0, 0, 0, 0.8);
} }
.modal { .modal {
padding: 10px; padding: 10px;

View file

@ -5,6 +5,6 @@ export type SelectedChellarisGroups = {
selectedGroups: { [key: number]: number }, selectedGroups: { [key: number]: number },
} }
const SelectedGameGroupsStore: Writable<{ [key: number]: SelectedChellarisGroups }> = writable([]); // Key should always be the same as gameId const SelectedGameGroupsStore: Writable<{ [key: number]: SelectedChellarisGroups }> = writable(); // Key should always be the same as gameId
export default SelectedGameGroupsStore; export default SelectedGameGroupsStore;

View file

@ -10,128 +10,17 @@ import type { ChellarisInfo } from '../../lib/types/chellaris';
export const load: LayoutLoad = async ({ fetch }) => { export const load: LayoutLoad = async ({ fetch }) => {
let store: string | null; let store: string | null;
const apiBaseUrl = 'https://wip.chellaris.net/api'; const apiBaseUrl = 'https://wip.chellaris.net/api';
const chellarisData = createChellarisInfo();
// Auth Cookies
// Chellaris Data Code // Chellaris Data Code
const data: ChellarisInfo = await (await fetch(apiBaseUrl + '/v3/full_view_data')).json(); const chellarisData: ChellarisInfo = await (await fetch(apiBaseUrl + '/v3/full_view_data')).json();
const start = new Date(); // DEBUG ChellarisDataStore.set(chellarisData);
const games: { id: number, name: string }[] = await (await fetch(apiBaseUrl + '/v2/games')).json();
games.sort((a, b) => (a.name < b.name ? -1 : 1));
games.forEach(game => {
const newGame: ChellarisGame = createChellarisGame();
newGame.id = game.id;
newGame.name = game.name;
chellarisData.games[game.id] = newGame;
});
const groups: { id: number, name: string, game_id: number }[] = await (await fetch(apiBaseUrl + '/v2/game_groups')).json();
groups.sort((a, b) => (a.name < b.name ? -1 : 1));
groups.forEach(group => {
const gameData = chellarisData.games[group.game_id];
if (typeof gameData !== "undefined") {
const newGroup = createChellarisGameGroup();
newGroup.id = group.id;
newGroup.name = group.name;
chellarisData.games[group.game_id].groups[group.id] = newGroup;
}
})
const empires: {
id: number,
discord_user?: string,
group_id: number,
gestalt: boolean,
empire_portrait_id: number,
empire_portrait_group_id: number,
group_game_id: number
}[] = await (await fetch(apiBaseUrl + '/v2/empires')).json();
empires.sort((a, b) => (a.id < b.id ? -1 : 1));
empires.forEach(empire => {
const gameData = chellarisData.games[empire.group_game_id];
if (typeof gameData !== "undefined") {
const newEmpire = createChellarisEmpire(empire);
chellarisData.games[empire.group_game_id].empires[empire.id] = newEmpire;
}
});
const ethics: { id: number, name: string, machine_ethic: boolean }[] = await (await fetch(apiBaseUrl + '/v2/ethics')).json();
ethics.sort((a, b) => (a.id < b.id ? -1 : 1));
ethics.forEach(ethic => {
const newEthic: Ethic = { id: ethic.id, displayName: ethic.name, machine: ethic.machine_ethic };
chellarisData.ethics[ethic.id] = newEthic;
});
const empireEthics: {
empires_id: number,
empires_group_id: number,
empires_group_game_id: number,
ethics_id: number,
ethics_fanatic: boolean
}[] = await (await fetch(apiBaseUrl + '/v2/empire_ethics')).json();
empireEthics.forEach(empireEthic => {
const gameData = chellarisData.games[empireEthic.empires_group_game_id];
const ethic = chellarisData.ethics[empireEthic.ethics_id];
if (typeof gameData !== "undefined" && typeof ethic !== "undefined") {
const empireData = gameData.empires[empireEthic.empires_id];
if (typeof empireData !== "undefined") {
const tmpEthic: Ethic = { id: ethic.id, machine: ethic.machine, displayName: ethic.displayName, fanatic: empireEthic.ethics_fanatic };
if (tmpEthic.machine) {
empireData.machine = true;
}
empireData.ethics[empireEthic.ethics_id] = tmpEthic;
}
}
});
const portraitGroups: {
id: number,
name: string
}[] = await (await fetch(apiBaseUrl + '/v2/portrait_groups')).json();
portraitGroups.sort((a, b) => (a.id < b.id ? -1 : 1));
portraitGroups.forEach(portraitGroup => {
const newPortraitGroup = { id: portraitGroup.id, displayName: portraitGroup.name, portraits: [] };
chellarisData.species[portraitGroup.id] = newPortraitGroup;
});
const portraits: {
id: number,
hires: string,
lores: string,
group_id: number
}[] = await (await fetch(apiBaseUrl + '/v2/portraits')).json();
portraits.sort((a, b) => (a.id < b.id ? -1 : 1));
portraits.forEach(portrait => {
const portraitGroupData = chellarisData.species[portrait.group_id];
if (typeof portraitGroupData !== "undefined") {
const newPortraitData = { id: portrait.id, hires: portrait.hires, lores: portrait.lores };
portraitGroupData.portraits[portrait.id] = newPortraitData;
}
})
console.log(new Date().getTime() - start.getTime(), "ms"); // DEBUG
console.log(data); // DEBUG
ChellarisDataStore.set(data);
// Local Storage Code // Local Storage Code
let gameGroupSelections: Array<SelectedChellarisGroups> = []; let gameGroupSelections: { [key: number]: SelectedChellarisGroups } = {};
let gameSelection: number | undefined; let gameSelection: number | undefined;
if (typeof localStorage !== 'undefined') { if (typeof localStorage !== 'undefined') {
@ -160,7 +49,7 @@ export const load: LayoutLoad = async ({ fetch }) => {
if (typeof gameGroupSelections[gameSelection] === 'undefined') { if (typeof gameGroupSelections[gameSelection] === 'undefined') {
// Default to all available groups // Default to all available groups
gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: Object.values(chellarisData.games[gameSelection].groups).map((group) => group.id) }; gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: Object.fromEntries(Object.values(chellarisData.games[gameSelection].groups).map((group) => group.id).entries()) };
// Set Local Storage to default Values if not previously defined // Set Local Storage to default Values if not previously defined
localStorage.setItem('gameGroupSelection', JSON.stringify(gameGroupSelections)); localStorage.setItem('gameGroupSelection', JSON.stringify(gameGroupSelections));
@ -189,7 +78,7 @@ export const load: LayoutLoad = async ({ fetch }) => {
if (typeof gameGroupSelections[gameSelection] === 'undefined') { if (typeof gameGroupSelections[gameSelection] === 'undefined') {
// Default to all available groups // Default to all available groups
gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: Object.values(chellarisData.games[gameSelection].groups).map((group) => group.id) }; gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: Object.fromEntries(Object.values(chellarisData.games[gameSelection].groups).map((group) => group.id).entries()) };
} }
SelectedGameGroupsStore.set(gameGroupSelections); SelectedGameGroupsStore.set(gameGroupSelections);

View file

@ -9,7 +9,7 @@
SelectedGameStore.subscribe((selection) => { SelectedGameStore.subscribe((selection) => {
if (typeof $SelectedGameGroupsStore[selection] === 'undefined') { if (typeof $SelectedGameGroupsStore[selection] === 'undefined') {
// Default to all available groups // Default to all available groups
$SelectedGameGroupsStore[selection] = { gameId: $SelectedGameStore, selectedGroups: Object.values($ChellarisDataStore.games[$SelectedGameStore].groups).map((group) => group.id) }; $SelectedGameGroupsStore[selection] = { gameId: $SelectedGameStore, selectedGroups: Object.fromEntries(Object.values($ChellarisDataStore.games[$SelectedGameStore].groups).map((group) => group.id).entries()) };
SelectedGameGroupsStore.update(() => $SelectedGameGroupsStore); SelectedGameGroupsStore.update(() => $SelectedGameGroupsStore);
} }
}) })
@ -21,11 +21,26 @@
} }
} }
$: selectedGameData = $SelectedGameGroupsStore[$SelectedGameStore]; $: selectedGameData = $SelectedGameGroupsStore[$SelectedGameStore];
let selectionArray = Object.values(selectedGameData ? selectedGameData.selectedGroups : []);
SelectedGameStore.subscribe(newSelection => {
selectionArray = Object.values($SelectedGameGroupsStore[newSelection] ? $SelectedGameGroupsStore[newSelection].selectedGroups : []);
});
$: groupJoiner = Object.values(selectedGameData.selectedGroups).length > 2 ? ', ' : ' & '; $: groupJoiner = Object.values(selectedGameData.selectedGroups).length > 2 ? ', ' : ' & ';
const updateGroupStore = () => { const updateGroupStore = () => {
SelectedGameGroupsStore.update(() => $SelectedGameGroupsStore); let tmpSelection: { [key: number]: number } = {}
selectionArray.forEach(entry => {
tmpSelection[entry] = entry;
});
SelectedGameGroupsStore.update((data) => {
data[$SelectedGameStore].selectedGroups = tmpSelection;
return data;
});
} }
const dropdownId = crypto.randomUUID(); const dropdownId = crypto.randomUUID();
@ -45,7 +60,7 @@
id={'checkbox' + group.id} id={'checkbox' + group.id}
data-dropdown={dropdownId} data-dropdown={dropdownId}
type="checkbox" type="checkbox"
bind:group={selectedGameData.selectedGroups} bind:group={selectionArray}
on:change={updateGroupStore} on:change={updateGroupStore}
value={group.id} value={group.id}
/> />

View file

@ -83,8 +83,8 @@
newPageData.ethicsData[ethic.id] = newEthicsData; newPageData.ethicsData[ethic.id] = newEthicsData;
}); });
Object.values(selectedGame.empires).forEach((empire, index) => { Object.values(selectedGame.empires).forEach((empire) => {
if (Object.values(selectedGameGroupData.selectedGroups).includes(empire.group)) { if (empire.group in selectedGameGroupData.selectedGroups) {
newPageData.empireCount = newPageData.empireCount + 1; newPageData.empireCount = newPageData.empireCount + 1;
if (empire.gestalt) { if (empire.gestalt) {
@ -178,7 +178,6 @@
} }
pageData = newPageData; pageData = newPageData;
console.log(pageData); // DEBUG
} }
// Save Tab to Store // Save Tab to Store