This repository has been archived on 2024-08-06. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
chellaris-sign-up-site/src/routes/graphs/+layout.ts

164 lines
5.7 KiB
TypeScript
Raw Normal View History

import ChellarisDataStore from '$lib/stores/ChellarisData';
import SelectedGameStore from '$lib/stores/GameFilter';
import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter";
import GraphsTabStore from '$lib/stores/GraphsTab';
2023-08-15 22:51:13 +02:00
import { createChellarisInfo, type ChellarisGame, createChellarisGame, createChellarisGameGroup, createChellarisEmpire } from "$lib/types/chellaris";
import type { LayoutLoad } from "./$types";
import type { Ethic } from '../../lib/types/stellaris';
export const load: LayoutLoad = async ({ fetch }) => {
2023-08-15 22:51:13 +02:00
let store: string | null;
const apiBaseUrl = 'https://www.chellaris.net/api/v2';
const chellarisData = createChellarisInfo();
2023-08-15 22:51:13 +02:00
// Chellaris Data Code
const games: {id: number, name: string}[] = await (await fetch(apiBaseUrl + '/games')).json();
games.sort((a, b) => (a.name < b.name ? -1 : 1));
games.forEach(game => {
const newGame: ChellarisGame = createChellarisGame();
newGame.name = game.name;
chellarisData.games.set(game.id, newGame);
});
const groups: {id: number, name: string, game_id: number}[] = await (await fetch(apiBaseUrl + '/game_groups')).json();
groups.sort((a, b) => (a.name < b.name ? -1 : 1));
groups.forEach(group => {
const gameData = chellarisData.games.get(group.game_id);
if (typeof gameData !== "undefined") {
const newGroup = createChellarisGameGroup();
newGroup.name = group.name;
gameData.groups.set(group.id, newGroup)
chellarisData.games.set(group.game_id, gameData);
}
})
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 + '/empires')).json();
empires.sort((a, b) => (a.id < b.id ? -1 : 1));
empires.forEach(empire => {
const gameData = chellarisData.games.get(empire.group_game_id);
if (typeof gameData !== "undefined") {
const newEmpire = createChellarisEmpire(empire);
gameData.empires.set(empire.id, newEmpire);
}
});
const ethics: {id: number, name: string, machine_ethic: boolean}[] = await (await fetch(apiBaseUrl + '/ethics')).json();
2023-08-15 22:51:41 +02:00
ethics.sort((a, b) => (a.id < b.id ? -1 : 1));
2023-08-15 22:51:41 +02:00
ethics.forEach(ethic => {
const newEthic: Ethic = {displayName: ethic.name, machine: ethic.machine_ethic};
chellarisData.ethics.set(ethic.id, newEthic);
2023-08-15 22:51:41 +02:00
});
const empireEthics: {
empires_id: number,
empires_group_id: number,
empires_group_game_id: number,
ethics_id: number,
ethics_fanatic: boolean}[] = await (await fetch(apiBaseUrl + '/empire_ethics')).json();
empireEthics.forEach(empireEthic => {
const gameData = chellarisData.games.get(empireEthic.empires_group_game_id);
const ethic = chellarisData.ethics.get(empireEthic.ethics_id);
if (typeof gameData !== "undefined" && typeof ethic !== "undefined") {
const empireData = gameData.empires.get(empireEthic.empires_id);
if (typeof empireData !== "undefined") {
const tmpEthic: Ethic = {machine: ethic.machine, displayName: ethic.displayName, fanatic: empireEthic.ethics_fanatic};
if (tmpEthic.machine) {
empireData.machine = true;
}
empireData.ethics.set(empireEthic.ethics_id, tmpEthic);
}
}
2023-08-21 00:48:23 +02:00
});
const portraitGroups: {
id: number,
name: string
}[] = await (await fetch(apiBaseUrl + '/portrait_groups')).json();
portraitGroups.sort((a, b) => (a.id < b.id ? -1 : 1));
portraitGroups.forEach(portraitGroup => {
const newPortraitGroup = { displayName: portraitGroup.name, portraits: new Map() };
chellarisData.species.set(portraitGroup.id, newPortraitGroup);
});
const portraits: {
id: number,
hires: string,
lores: string,
2023-08-21 00:48:23 +02:00
group_id: number
}[] = await (await fetch(apiBaseUrl + '/portraits')).json();
portraits.sort((a, b) => (a.id < b.id ? -1 : 1));
portraits.forEach(portrait => {
const portraitGroupData = chellarisData.species.get(portrait.group_id);
if (typeof portraitGroupData !== "undefined") {
const newPortraitData = { hires: portrait.hires, lores: portrait.lores };
2023-08-21 00:48:23 +02:00
portraitGroupData.portraits.set(portrait.id, newPortraitData);
}
})
ChellarisDataStore.set(chellarisData);
// Local Storage Code
if (typeof localStorage !== 'undefined') {
// Tab Selection
store = localStorage.getItem('graphsTab');
if (typeof store == 'string') {
GraphsTabStore.set(store);
}
// Game Selection
store = localStorage.getItem('gameSelection');
let selectedGame = 1;
if (typeof store == 'string' && store != "\"\"") {
selectedGame = JSON.parse(store);
}
SelectedGameStore.set(selectedGame);
// Game Groups Selection
store = localStorage.getItem('gameGroupSelection');
if (typeof store == 'string') {
let selectedGameGroups: Array<number> = [];
const gameGroupSelectionMap = new Map<number, Array<number>>(JSON.parse(store));
const tmp = gameGroupSelectionMap.get(selectedGame);
if (typeof tmp !== 'undefined') {
selectedGameGroups = tmp;
} else {
const tmpGameData = chellarisData.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);
}
}
2023-08-15 22:51:13 +02:00
return { chellarisData }
}