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

@ -12,4 +12,32 @@ export type ChellarisGame = {
export type ChellarisGameGroup = { export type ChellarisGameGroup = {
name: string, 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,66 +1,69 @@
import type { PageLoad } from "../$types"; import type { PageLoad } from "../$types";
import ChellarisDataStore from '$lib/stores/ChellarisData'; import ChellarisDataStore from '$lib/stores/ChellarisData';
import SelectedGameStore from '$lib/stores/GameFilter'; 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 SelectedGameGroupsStore from "$lib/stores/GameGroupFilter";
import GraphsTabStore from '$lib/stores/GraphsTab';
export const load: PageLoad = async () => { export const load: PageLoad = async () => {
// Chellaris Data Code const apiBaseUrl = 'https://www.chellaris.net/api/v2';
// Game Dummy Data const chellarisData = createChellarisInfo();
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; let store: string | null;
if (typeof localStorage !== 'undefined') { // Chellaris Data Code
// Game Selection const games: {id: number, name: string}[] = await (await fetch(apiBaseUrl + '/games')).json();
store = localStorage.getItem('gameSelection');
let selectedGame = ""; games.sort((a, b) => (a.name < b.name ? -1 : 1));
if (typeof store == 'string') { games.forEach(game => {
selectedGame = JSON.parse(store); const newGame: ChellarisGame = createChellarisGame();
SelectedGameStore.set(selectedGame); 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 = 0;
if (typeof store == 'string') {
selectedGame = JSON.parse(store);
SelectedGameStore.set(selectedGame);
}
// Game Groups Selection // Game Groups Selection
store = localStorage.getItem('gameGroupSelection'); store = localStorage.getItem('gameGroupSelection');
if (typeof store == 'string') { 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); const tmp = gameGroupSelectionMap.get(selectedGame);
let selectedGameGroups: Array<string> = []; let selectedGameGroups: Array<number> = [];
if (typeof tmp !== 'undefined') { if (typeof tmp !== 'undefined') {
selectedGameGroups = [...tmp.values()]; selectedGameGroups = [...tmp.values()];
} else { } 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 this fails an empty array is precisely what we want
if (typeof tmpGameData !== "undefined") { if (typeof tmpGameData !== "undefined") {
// Default to all available groups // Default to all available groups
@ -71,6 +74,6 @@ export const load: PageLoad = async () => {
} }
gameGroupSelectionMap.set(selectedGame, selectedGameGroups); gameGroupSelectionMap.set(selectedGame, selectedGameGroups);
SelectedGameGroupsStore.set(gameGroupSelectionMap); SelectedGameGroupsStore.set(gameGroupSelectionMap);
} }
} }
} }