From 05c3d3d6ec68edd36ddc9f8a4950d9461366001a Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 27 Aug 2023 21:00:56 +0200 Subject: [PATCH] Switch to Rust API. Migration from Arrays to Objects (API compatability) --- src/lib/stores/GameGroupFilter.ts | 4 +- src/lib/types/chellaris.ts | 15 ++-- src/lib/types/stellaris.ts | 2 +- src/routes/Header.svelte | 2 +- src/routes/graphs/+layout.ts | 29 +++++--- src/routes/graphs/GameGroupSelection.svelte | 10 +-- src/routes/graphs/GameSelection.svelte | 2 +- src/routes/graphs/excel-style/+page.svelte | 79 ++++++++++++--------- src/routes/graphs/tab/+page.svelte | 8 +-- 9 files changed, 84 insertions(+), 67 deletions(-) diff --git a/src/lib/stores/GameGroupFilter.ts b/src/lib/stores/GameGroupFilter.ts index ce6062f..5c176a8 100644 --- a/src/lib/stores/GameGroupFilter.ts +++ b/src/lib/stores/GameGroupFilter.ts @@ -2,9 +2,9 @@ import { writable, type Writable } from "svelte/store"; export type SelectedChellarisGroups = { gameId: number, - selectedGroups: Array, + selectedGroups: { [key: number]: number }, } -const SelectedGameGroupsStore: Writable> = 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; \ No newline at end of file diff --git a/src/lib/types/chellaris.ts b/src/lib/types/chellaris.ts index e9837ae..14083ce 100644 --- a/src/lib/types/chellaris.ts +++ b/src/lib/types/chellaris.ts @@ -1,15 +1,16 @@ import type { Ethic, Species } from './stellaris'; -export interface ChellarisInfo { - games: Array, // Key is Game Id - ethics: Array, // Key is Ethic Id - species: Array, // Key is Species Group Id + +export type ChellarisInfo = { + games: { [key: number]: ChellarisGame }, // Key is Game Id + ethics: { [key: number]: Ethic }, // Key is Ethic Id + species: { [key: number]: Species }, // Key is Species Group Id } export type ChellarisGame = { id: number, name: string, - groups: Array, // Key is Group Id - empires: Array, // Key is Empire Id + groups: { [key: number]: ChellarisGameGroup }, // Key is Group Id + empires: { [key: number]: ChellarisEmpire }, // Key is Empire Id } export type ChellarisGameGroup = { @@ -25,7 +26,7 @@ export type ChellarisEmpire = { empire_portrait: number, // TODO replace with Enum? empire_portrait_group: number, // TODO replace with Enum? discord_user?: string, - ethics: Array, + ethics: { [key: number]: Ethic }, } export const createChellarisInfo = (): ChellarisInfo => { diff --git a/src/lib/types/stellaris.ts b/src/lib/types/stellaris.ts index 4702000..1b38988 100644 --- a/src/lib/types/stellaris.ts +++ b/src/lib/types/stellaris.ts @@ -74,7 +74,7 @@ export type Ethic = { export type Species = { id: number, displayName: string, - portraits: Array, // Key is Portrait Id + portraits: { [key: number]: Portrait }, // Key is Portrait Id } export type Portrait = { diff --git a/src/routes/Header.svelte b/src/routes/Header.svelte index f63e32f..a7a9a36 100644 --- a/src/routes/Header.svelte +++ b/src/routes/Header.svelte @@ -37,7 +37,7 @@ diff --git a/src/routes/graphs/+layout.ts b/src/routes/graphs/+layout.ts index d103b55..394c574 100644 --- a/src/routes/graphs/+layout.ts +++ b/src/routes/graphs/+layout.ts @@ -5,15 +5,19 @@ import GraphsTabStore from '$lib/stores/GraphsTab'; import { createChellarisInfo, type ChellarisGame, createChellarisGame, createChellarisGameGroup, createChellarisEmpire } from "$lib/types/chellaris"; import type { LayoutLoad } from "./$types"; import type { Ethic } from '../../lib/types/stellaris'; +import type { ChellarisInfo } from '../../lib/types/chellaris'; export const load: LayoutLoad = async ({ fetch }) => { let store: string | null; - const apiBaseUrl = 'https://www.chellaris.net/api/v2'; + const apiBaseUrl = 'https://wip.chellaris.net/api'; const chellarisData = createChellarisInfo(); // Chellaris Data Code - const games: { id: number, name: string }[] = await (await fetch(apiBaseUrl + '/games')).json(); + const data: ChellarisInfo = await (await fetch(apiBaseUrl + '/v3/full_view_data')).json(); + + const start = new Date(); // DEBUG + 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 => { @@ -23,7 +27,7 @@ export const load: LayoutLoad = async ({ fetch }) => { chellarisData.games[game.id] = newGame; }); - const groups: { id: number, name: string, game_id: number }[] = await (await fetch(apiBaseUrl + '/game_groups')).json(); + 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 => { @@ -45,7 +49,7 @@ export const load: LayoutLoad = async ({ fetch }) => { empire_portrait_id: number, empire_portrait_group_id: number, group_game_id: number - }[] = await (await fetch(apiBaseUrl + '/empires')).json(); + }[] = await (await fetch(apiBaseUrl + '/v2/empires')).json(); empires.sort((a, b) => (a.id < b.id ? -1 : 1)); empires.forEach(empire => { @@ -57,7 +61,7 @@ export const load: LayoutLoad = async ({ fetch }) => { } }); - const ethics: { id: number, name: string, machine_ethic: boolean }[] = await (await fetch(apiBaseUrl + '/ethics')).json(); + 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 => { @@ -72,7 +76,7 @@ export const load: LayoutLoad = async ({ fetch }) => { empires_group_game_id: number, ethics_id: number, ethics_fanatic: boolean - }[] = await (await fetch(apiBaseUrl + '/empire_ethics')).json(); + }[] = await (await fetch(apiBaseUrl + '/v2/empire_ethics')).json(); empireEthics.forEach(empireEthic => { const gameData = chellarisData.games[empireEthic.empires_group_game_id]; @@ -93,7 +97,7 @@ export const load: LayoutLoad = async ({ fetch }) => { const portraitGroups: { id: number, name: string - }[] = await (await fetch(apiBaseUrl + '/portrait_groups')).json(); + }[] = await (await fetch(apiBaseUrl + '/v2/portrait_groups')).json(); portraitGroups.sort((a, b) => (a.id < b.id ? -1 : 1)); @@ -107,7 +111,7 @@ export const load: LayoutLoad = async ({ fetch }) => { hires: string, lores: string, group_id: number - }[] = await (await fetch(apiBaseUrl + '/portraits')).json(); + }[] = await (await fetch(apiBaseUrl + '/v2/portraits')).json(); portraits.sort((a, b) => (a.id < b.id ? -1 : 1)); @@ -121,7 +125,10 @@ export const load: LayoutLoad = async ({ fetch }) => { } }) - ChellarisDataStore.set(chellarisData); + console.log(new Date().getTime() - start.getTime(), "ms"); // DEBUG + + console.log(data); // DEBUG + ChellarisDataStore.set(data); // Local Storage Code let gameGroupSelections: Array = []; @@ -153,7 +160,7 @@ export const load: LayoutLoad = async ({ fetch }) => { if (typeof gameGroupSelections[gameSelection] === 'undefined') { // Default to all available groups - gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: chellarisData.games[gameSelection].groups.map(group => group.id) }; + gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: Object.values(chellarisData.games[gameSelection].groups).map((group) => group.id) }; // Set Local Storage to default Values if not previously defined localStorage.setItem('gameGroupSelection', JSON.stringify(gameGroupSelections)); @@ -182,7 +189,7 @@ export const load: LayoutLoad = async ({ fetch }) => { if (typeof gameGroupSelections[gameSelection] === 'undefined') { // Default to all available groups - gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: chellarisData.games[gameSelection].groups.map(group => group.id) }; + gameGroupSelections[gameSelection] = { gameId: gameSelection, selectedGroups: Object.values(chellarisData.games[gameSelection].groups).map((group) => group.id) }; } SelectedGameGroupsStore.set(gameGroupSelections); diff --git a/src/routes/graphs/GameGroupSelection.svelte b/src/routes/graphs/GameGroupSelection.svelte index 3246563..cc7e6fd 100644 --- a/src/routes/graphs/GameGroupSelection.svelte +++ b/src/routes/graphs/GameGroupSelection.svelte @@ -9,7 +9,7 @@ SelectedGameStore.subscribe((selection) => { if (typeof $SelectedGameGroupsStore[selection] === 'undefined') { // Default to all available groups - $SelectedGameGroupsStore[selection] = { gameId: $SelectedGameStore, selectedGroups: $ChellarisDataStore.games[$SelectedGameStore].groups.map((group) => group.id) }; + $SelectedGameGroupsStore[selection] = { gameId: $SelectedGameStore, selectedGroups: Object.values($ChellarisDataStore.games[$SelectedGameStore].groups).map((group) => group.id) }; SelectedGameGroupsStore.update(() => $SelectedGameGroupsStore); } }) @@ -22,7 +22,7 @@ } $: selectedGameData = $SelectedGameGroupsStore[$SelectedGameStore]; - $: groupJoiner = selectedGameData.selectedGroups.length > 2 ? ', ' : ' & '; + $: groupJoiner = Object.values(selectedGameData.selectedGroups).length > 2 ? ', ' : ' & '; const updateGroupStore = () => { SelectedGameGroupsStore.update(() => $SelectedGameGroupsStore); @@ -33,12 +33,12 @@ 1 ? 'Groups ' : 'Group ') + - selectedGameData.selectedGroups + dropdownTitle={(Object.values(selectedGameData.selectedGroups).length > 1 ? 'Groups ' : 'Group ') + + Object.values(selectedGameData.selectedGroups) .map((selection) => $ChellarisDataStore.games[$SelectedGameStore].groups[selection]?.name) .join(groupJoiner)} > - {#each $ChellarisDataStore.games[$SelectedGameStore].groups as group} + {#each Object.values($ChellarisDataStore.games[$SelectedGameStore].groups) as group} {#if group} - {#each $ChellarisDataStore.games as game} + {#each Object.values($ChellarisDataStore.games) as game} 1 ? 'Groups' : 'Group'; - $: groupJoiner = selectedGameGroupData.selectedGroups.length > 2 ? ', ' : ' & '; + $: groupNoun = Object.entries(selectedGameGroupData.selectedGroups).length > 1 ? 'Groups' : 'Group'; + $: groupJoiner = Object.entries(selectedGameGroupData.selectedGroups).length > 2 ? ', ' : ' & '; let groupPortraitLimit: number; $: { let containsNA = false; - selectedGameGroupData.selectedGroups.forEach((selection) => { + Object.values(selectedGameGroupData.selectedGroups).forEach((selection) => { if ($ChellarisDataStore.games[$SelectedGameStore].groups[selection]?.name == 'N/A') { containsNA = true; } }); - groupPortraitLimit = containsNA ? $ChellarisDataStore.games[$SelectedGameStore].groups.length - 1 : selectedGameGroupData.selectedGroups.length; + groupPortraitLimit = containsNA + ? Object.entries($ChellarisDataStore.games[$SelectedGameStore].groups).length - 1 + : Object.entries(selectedGameGroupData.selectedGroups).length; } let pageData: { init: boolean; empireCount: number; gestaltCount: { total: number; machines: number }; - ethicsData: Array<{ - id: number; - machine: boolean; - displayName: string; - regular: number; - fanatic: number; - }>; + ethicsData: { + [key: number]: { + id: number; + machine: boolean; + displayName: string; + regular: number; + fanatic: number; + }; + }; takenPortraits: Array]>>; // >>> }; $: { @@ -42,25 +46,27 @@ init: boolean; empireCount: number; gestaltCount: { total: number; machines: number }; - ethicsData: Array<{ - id: number; - machine: boolean; - displayName: string; - regular: number; - fanatic: number; - }>; + ethicsData: { + [key: number]: { + id: number; + machine: boolean; + displayName: string; + regular: number; + fanatic: number; + }; + }; takenPortraits: Array]>>; // >>> } = { init: false, empireCount: 0, gestaltCount: { total: 0, machines: 0 }, - ethicsData: [], + ethicsData: {}, takenPortraits: [] }; if (selectedGame) { // Empty init of Ethics Data - $ChellarisDataStore.ethics.forEach((ethic) => { + Object.values($ChellarisDataStore.ethics).forEach((ethic) => { const newEthicsData: { id: number; machine: boolean; @@ -77,15 +83,15 @@ newPageData.ethicsData[ethic.id] = newEthicsData; }); - selectedGame.empires.forEach((empire, index) => { - if (selectedGameGroupData.selectedGroups.includes(empire.group)) { + Object.values(selectedGame.empires).forEach((empire, index) => { + if (Object.values(selectedGameGroupData.selectedGroups).includes(empire.group)) { newPageData.empireCount = newPageData.empireCount + 1; if (empire.gestalt) { newPageData.gestaltCount.total = newPageData.gestaltCount.total + 1; let machine = false; - empire.ethics.forEach((ethic) => { + Object.values(empire.ethics).forEach((ethic) => { if (ethic.machine) { machine = true; } @@ -96,8 +102,8 @@ } } - empire.ethics.forEach((ethic, id) => { - const tmpEthicPageData = newPageData.ethicsData[id]; + Object.values(empire.ethics).forEach((ethic) => { + const tmpEthicPageData = newPageData.ethicsData[ethic.id]; if (typeof tmpEthicPageData !== 'undefined') { tmpEthicPageData.displayName = ethic.displayName; @@ -107,7 +113,7 @@ tmpEthicPageData.fanatic = tmpEthicPageData.fanatic + 1; } - newPageData.ethicsData[id] = tmpEthicPageData; + newPageData.ethicsData[ethic.id] = tmpEthicPageData; } else { let newEthicsData: { id: number; @@ -129,7 +135,7 @@ newEthicsData.fanatic = 1; } - newPageData.ethicsData[id] = newEthicsData; + newPageData.ethicsData[ethic.id] = newEthicsData; } }); @@ -156,22 +162,23 @@ }); // Fill undefined Portrait Info with default information. - for (let i = 0; i < $ChellarisDataStore.species.length; i++) { + for (let i = 0; i < Object.values($ChellarisDataStore.species).length; i++) { if (typeof newPageData.takenPortraits[i] === 'undefined') { newPageData.takenPortraits[i] = []; } - for (let ii = 0; ii < $ChellarisDataStore.species[i].portraits.length; ii++) { + for (let ii = 0; ii < Object.values($ChellarisDataStore.species[i].portraits).length; ii++) { if (typeof newPageData.takenPortraits[i][ii] === 'undefined') { newPageData.takenPortraits[i][ii] = [0, []]; } } } - + newPageData.init = true; } pageData = newPageData; + console.log(pageData); // DEBUG } // Save Tab to Store @@ -186,7 +193,9 @@ {#if pageData.init}

{selectedGame.name} Sign-Up Info for {groupNoun} - {selectedGameGroupData.selectedGroups.map((selection) => $ChellarisDataStore.games[$SelectedGameStore].groups[selection]?.name).join(groupJoiner)} + {Object.values(selectedGameGroupData.selectedGroups) + .map((selection) => $ChellarisDataStore.games[$SelectedGameStore].groups[selection]?.name) + .join(groupJoiner)}

@@ -201,7 +210,7 @@ # Regular # Fanatic - {#each pageData.ethicsData as ethicData} + {#each Object.values(pageData.ethicsData) as ethicData} {#if ethicData && !ethicData.machine} {ethicData.displayName} @@ -221,7 +230,7 @@ Machine Ethic # - {#each pageData.ethicsData as ethicData} + {#each Object.values(pageData.ethicsData) as ethicData} {#if ethicData && ethicData.machine} {ethicData.displayName} @@ -241,11 +250,11 @@ {index + 1} {/each} - {#each $ChellarisDataStore.species ?? false as portraitGroup} + {#each Object.values($ChellarisDataStore.species) ?? false as portraitGroup} {#if portraitGroup} {portraitGroup.displayName} - {#each portraitGroup.portraits ?? false as portrait} + {#each Object.values(portraitGroup.portraits) ?? false as portrait} {#if portrait} 'tab'); $: selectedGroups = $SelectedGameGroupsStore[$SelectedGameStore].selectedGroups; - $: groupJoiner = selectedGroups.length > 2 ? ', ' : ' & '; + $: groupJoiner = Object.values(selectedGroups).length > 2 ? ', ' : ' & '; @@ -19,7 +19,7 @@

Example Tab

- {selectedGroups.length > 1 ? 'Groups' : 'Group'} - {selectedGroups.map((selection) => $ChellarisDataStore.games[$SelectedGameStore].groups[selection]?.name).join(groupJoiner)} - {selectedGroups.length > 1 ? 'are' : 'is'} selected + {Object.values(selectedGroups).length > 1 ? 'Groups' : 'Group'} + {Object.values(selectedGroups).map((selection) => $ChellarisDataStore.games[$SelectedGameStore].groups[selection]?.name).join(groupJoiner)} + {Object.values(selectedGroups).length > 1 ? 'are' : 'is'} selected