From e8b7e395e20e37397f76185076471e82d774c76a Mon Sep 17 00:00:00 2001 From: Neshura Date: Wed, 16 Aug 2023 00:47:58 +0200 Subject: [PATCH] Ethic Parsing, Added "machine" modifier to Empire and Ethics --- src/lib/types/chellaris.ts | 9 +- src/lib/types/stellaris.ts | 3 +- src/routes/graphs/+layout.ts | 31 ++++++- src/routes/graphs/GameGroupSelection.svelte | 8 +- src/routes/graphs/excel-style/+page.svelte | 93 ++++++++++++++++++--- 5 files changed, 120 insertions(+), 24 deletions(-) diff --git a/src/lib/types/chellaris.ts b/src/lib/types/chellaris.ts index eedd056..78efdfc 100644 --- a/src/lib/types/chellaris.ts +++ b/src/lib/types/chellaris.ts @@ -1,6 +1,7 @@ +import type { Ethic } from './stellaris'; export interface ChellarisInfo { games: Map, // Key is Game Id - ethics: Array, // TODO implement + ethics: Map, // TODO implement portraits: Array, // TODO implement } @@ -16,16 +17,18 @@ export type ChellarisGameGroup = { export type ChellarisEmpire = { gestalt: boolean, + machine: boolean, group: number, empire_portrait: number, // TODO replace with Enum? empire_portrait_group: number, // TODO replace with Enum? discord_user?: string, + ethics: Map, } export const createChellarisInfo = (): ChellarisInfo => { const newChellarisInfo = { games: new Map(), - ethics: [], + ethics: new Map(), portraits: [] }; @@ -64,9 +67,11 @@ export const createChellarisEmpire = ( }): ChellarisEmpire => { const newChellarisEmpire = { gestalt: gestalt, + machine: false, group: group_id, empire_portrait: empire_portrait_id, empire_portrait_group: empire_portrait_group_id, + ethics: new Map(), }; return newChellarisEmpire; diff --git a/src/lib/types/stellaris.ts b/src/lib/types/stellaris.ts index c6526ae..66a0f1c 100644 --- a/src/lib/types/stellaris.ts +++ b/src/lib/types/stellaris.ts @@ -33,7 +33,8 @@ export enum LegacyEthics { export type Ethic = { displayName: string, - fanatic: boolean + machine: boolean + fanatic?: boolean, } export enum Scale { diff --git a/src/routes/graphs/+layout.ts b/src/routes/graphs/+layout.ts index 91c195d..f026545 100644 --- a/src/routes/graphs/+layout.ts +++ b/src/routes/graphs/+layout.ts @@ -4,6 +4,7 @@ import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter"; 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'; export const load: LayoutLoad = async () => { let store: string | null; @@ -54,12 +55,38 @@ export const load: LayoutLoad = async () => { } }); - const ethics: {id: number, name: string, fanatic: boolean}[] = await (await fetch(apiBaseUrl + '/ethics')).json(); + 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)); ethics.forEach(ethic => { - console.log(ethic.name); + const newEthic: Ethic = {displayName: ethic.name, machine: ethic.machine_ethic}; + + chellarisData.ethics.set(ethic.id, newEthic); }); + 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); + } + } + }) + ChellarisDataStore.set(chellarisData); // Local Storage Code diff --git a/src/routes/graphs/GameGroupSelection.svelte b/src/routes/graphs/GameGroupSelection.svelte index f55fa3d..4e037c3 100644 --- a/src/routes/graphs/GameGroupSelection.svelte +++ b/src/routes/graphs/GameGroupSelection.svelte @@ -4,18 +4,14 @@ import SelectedGameGroupsStore from '$lib/stores/GameGroupFilter'; import SelectedGameStore from '$lib/stores/GameFilter'; import ChellarisDataStore from '$lib/stores/ChellarisData'; - import type { ChellarisGame, ChellarisGameGroup, ChellarisInfo } from '$lib/types/chellaris'; + import { createChellarisInfo, type ChellarisGame, type ChellarisGameGroup, type ChellarisInfo } from '$lib/types/chellaris'; let selectedGame: number; let selectedGameGroups: Array = []; let selectedGameGroupsMap: Map> = new Map(); let gameGroups: Map = new Map(); - let chellarisData: ChellarisInfo = { - games: new Map(), - ethics: [], - portraits: [] - }; + let chellarisData: ChellarisInfo = createChellarisInfo(); // Chellaris Data Code const updateGameGroups = () => { diff --git a/src/routes/graphs/excel-style/+page.svelte b/src/routes/graphs/excel-style/+page.svelte index f2f1f0a..a27ffc1 100644 --- a/src/routes/graphs/excel-style/+page.svelte +++ b/src/routes/graphs/excel-style/+page.svelte @@ -12,6 +12,8 @@ import ChellarisDataStore from '$lib/stores/ChellarisData'; import GraphsTabStore from '$lib/stores/GraphsTab'; import type { LayoutData } from '../$types'; + import type { Ethic } from '$lib/types/stellaris'; + import { page } from '$app/stores'; export let data: LayoutData; let selectedGameGroups: Array = []; @@ -19,13 +21,23 @@ let gameGroups: Map = new Map(); let chellarisData: ChellarisInfo = data.chellarisData; let selectedGameIdx: number; - let selectedGame: ChellarisGame = createChellarisGame();; + let selectedGame: ChellarisGame = createChellarisGame(); let pageData: { empireCount: number; gestaltCount: { total: number; machines: number }; + ethicsData: Map< + number, + { + machine: boolean; + displayName: string; + regular: number; + fanatic: number; + } + >; } = { empireCount: 0, - gestaltCount: { total: 0, machines: 0 } + gestaltCount: { total: 0, machines: 0 }, + ethicsData: new Map() }; // Save Tab to Store @@ -45,10 +57,11 @@ }; const updatePageData = () => { - let tmpGameData = chellarisData.games.get(selectedGameIdx); + const tmpGameData = chellarisData.games.get(selectedGameIdx); if (typeof tmpGameData !== 'undefined') { - let groupEmpires: Map = new Map(); + const groupEmpires: Map = new Map(); + pageData.ethicsData = new Map(); pageData.gestaltCount = { total: 0, machines: 0 }; tmpGameData.empires.forEach((empire, index) => { @@ -57,11 +70,53 @@ if (empire.gestalt) { pageData.gestaltCount.total = pageData.gestaltCount.total + 1; - if (empire.empire_portrait_group == 13) { - // TODO replace static number with generated one in case Machine Portrait ID changes + + let machine = false; + empire.ethics.forEach(ethic => { + if (ethic.machine) { + machine = true; + } + }); + + if (machine) { pageData.gestaltCount.machines = pageData.gestaltCount.machines + 1; } } + + empire.ethics.forEach((ethic, id) => { + const tmpEthicPageData = pageData.ethicsData.get(id); + + if (typeof tmpEthicPageData !== 'undefined') { + tmpEthicPageData.displayName = ethic.displayName; + if (!ethic.fanatic) { + tmpEthicPageData.regular = tmpEthicPageData.regular + 1; + } else { + tmpEthicPageData.fanatic = tmpEthicPageData.fanatic + 1; + } + + pageData.ethicsData.set(id, tmpEthicPageData); + } else { + let newEthicsData: { + machine: boolean; + displayName: string; + regular: number; + fanatic: number; + } = { + machine: ethic.machine, + displayName: ethic.displayName, + regular: 0, + fanatic: 0 + }; + + if (!ethic.fanatic) { + newEthicsData.regular = 1; + } else { + newEthicsData.fanatic = 1; + } + + pageData.ethicsData.set(id, newEthicsData); + } + }); } }); pageData.empireCount = groupEmpires.size; @@ -78,10 +133,10 @@ SelectedGameStore.subscribe((selection) => { selectedGameIdx = selection; const tmpGameData = chellarisData.games.get(selectedGameIdx); - if (typeof tmpGameData !== "undefined") { + if (typeof tmpGameData !== 'undefined') { selectedGame = tmpGameData; } - + updateGameGroups(); if (selectedGameGroupsMap.size != 0) { @@ -129,11 +184,15 @@ # Regular # Fanatic - - - - - + {#each pageData.ethicsData as ethicData} + {#if !ethicData[1].machine} + + {ethicData[1].displayName} + {ethicData[1].regular} + {ethicData[1].fanatic} + + {/if} + {/each}
@@ -145,6 +204,14 @@ Machine Ethic # + {#each pageData.ethicsData as ethicData} + {#if ethicData[1].machine} + + {ethicData[1].displayName} + {ethicData[1].regular} + + {/if} + {/each}