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

192 lines
6.5 KiB
TypeScript
Raw Normal View History

import ChellarisDataStore from '$lib/stores/ChellarisData';
import SelectedGameStore from '$lib/stores/GameFilter';
import SelectedGameGroupsStore, { type SelectedChellarisGroups } 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();
// 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.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 + '/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 + '/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 + '/ethics')).json();
ethics.sort((a, b) => (a.id < b.id ? -1 : 1));
2023-08-15 22:51:41 +02:00
ethics.forEach(ethic => {
const newEthic: Ethic = { id: ethic.id, displayName: ethic.name, machine: ethic.machine_ethic };
chellarisData.ethics[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[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;
}
}
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 = { id: portraitGroup.id, displayName: portraitGroup.name, portraits: [] };
chellarisData.species[portraitGroup.id] = newPortraitGroup;
2023-08-21 00:48:23 +02:00
});
const portraits: {
id: number,
hires: string,
lores: string,
group_id: number
2023-08-21 00:48:23 +02:00
}[] = await (await fetch(apiBaseUrl + '/portraits')).json();
portraits.sort((a, b) => (a.id < b.id ? -1 : 1));
portraits.forEach(portrait => {
const portraitGroupData = chellarisData.species[portrait.group_id];
2023-08-21 00:48:23 +02:00
if (typeof portraitGroupData !== "undefined") {
const newPortraitData = { id: portrait.id, hires: portrait.hires, lores: portrait.lores };
2023-08-21 00:48:23 +02:00
portraitGroupData.portraits[portrait.id] = newPortraitData;
2023-08-21 00:48:23 +02:00
}
})
ChellarisDataStore.set(chellarisData);
// Local Storage Code
let gameGroupSelections: Array<SelectedChellarisGroups> = [];
let gameSelection: number | undefined;
if (typeof localStorage !== 'undefined') {
// Tab Selection
store = localStorage.getItem('graphsTab');
if (typeof store === 'string') {
GraphsTabStore.set(store);
}
// Game Selection
store = localStorage.getItem('gameSelection');
if (typeof store === 'string' && store != "\"\"") {
gameSelection = JSON.parse(store);
}
if (typeof gameSelection === 'undefined') {
gameSelection = chellarisData.games[0].id;
}
// Game Groups Selection
store = localStorage.getItem('gameGroupSelection');
if (typeof store === 'string') {
gameGroupSelections = JSON.parse(store);
if (typeof gameGroupSelections[gameSelection] === 'undefined') {
// Default to all available groups
gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: chellarisData.games[gameSelection].groups.map(group => group.id) };
// Set Local Storage to default Values if not previously defined
localStorage.setItem('gameGroupSelection', JSON.stringify(gameGroupSelections));
}
else {
Object.keys(gameGroupSelections).forEach(
gKey => {
if (gameGroupSelections[+gKey] == null) {
delete gameGroupSelections[+gKey];
}
else {
Object.keys(gameGroupSelections[+gKey].selectedGroups).forEach(
key => gameGroupSelections[+gKey].selectedGroups[+key] != null || delete gameGroupSelections[+gKey].selectedGroups[+key]
)
}
});
}
}
}
if (typeof gameSelection === 'undefined') {
gameSelection = chellarisData.games[0].id;
}
SelectedGameStore.set(gameSelection);
if (typeof gameGroupSelections[gameSelection] === 'undefined') {
// Default to all available groups
gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: chellarisData.games[gameSelection].groups.map(group => group.id) };
}
SelectedGameGroupsStore.set(gameGroupSelections);
gameGroupSelections = []; // TODO: actually assing a value
2023-08-15 22:51:13 +02:00
return { chellarisData }
}