Dynamically get Game and Group data via API

This commit is contained in:
Neshura 2023-08-14 20:18:36 +02:00
parent 704133a460
commit e3c5a9c60f
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 80 additions and 49 deletions

View file

@ -13,3 +13,31 @@ export type ChellarisGame = {
export type ChellarisGameGroup = {
name: string,
}
export const createChellarisInfo = (): ChellarisInfo => {
const newChellarisInfo = {
games: new Map(),
ethics: [],
portraits: []
};
return newChellarisInfo;
}
export const createChellarisGame = (): ChellarisGame => {
const newChellarisGame = {
name: "",
groups: new Map(),
empires: [],
};
return newChellarisGame;
}
export const createChellarisGameGroup = (): ChellarisGameGroup => {
const newChellarisGameGroup = {
name: "",
};
return newChellarisGameGroup;
}

View file

@ -1,50 +1,53 @@
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 { type ChellarisInfo, type ChellarisGame, type ChellarisGameGroup, createChellarisInfo, createChellarisGameGroup, createChellarisGame } from '../../lib/types/chellaris';
import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter";
import GraphsTabStore from '$lib/stores/GraphsTab';
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
const apiBaseUrl = 'https://www.chellaris.net/api/v2';
const chellarisData = createChellarisInfo();
let store: string | null;
// 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);
}
})
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 = "";
let selectedGame = 0;
if (typeof store == 'string') {
selectedGame = JSON.parse(store);
SelectedGameStore.set(selectedGame);
@ -54,13 +57,13 @@ export const load: PageLoad = async () => {
store = localStorage.getItem('gameGroupSelection');
if (typeof store == 'string') {
const gameGroupSelectionMap = new Map<string, Array<string>>(JSON.parse(store));
const gameGroupSelectionMap = new Map<number, Array<number>>(JSON.parse(store));
const tmp = gameGroupSelectionMap.get(selectedGame);
let selectedGameGroups: Array<string> = [];
let selectedGameGroups: Array<number> = [];
if (typeof tmp !== 'undefined') {
selectedGameGroups = [...tmp.values()];
} else {
const tmpGameData = dummyData.games.get(selectedGame);
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