Ethic Parsing, Added "machine" modifier to Empire and Ethics

This commit is contained in:
Neshura 2023-08-16 00:47:58 +02:00
parent 74c6b6f688
commit e8b7e395e2
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
5 changed files with 120 additions and 24 deletions

View file

@ -1,6 +1,7 @@
import type { Ethic } from './stellaris';
export interface ChellarisInfo { export interface ChellarisInfo {
games: Map<number, ChellarisGame>, // Key is Game Id games: Map<number, ChellarisGame>, // Key is Game Id
ethics: Array<null>, // TODO implement ethics: Map<number, Ethic>, // TODO implement
portraits: Array<null>, // TODO implement portraits: Array<null>, // TODO implement
} }
@ -16,16 +17,18 @@ export type ChellarisGameGroup = {
export type ChellarisEmpire = { export type ChellarisEmpire = {
gestalt: boolean, gestalt: boolean,
machine: boolean,
group: number, group: number,
empire_portrait: number, // TODO replace with Enum? empire_portrait: number, // TODO replace with Enum?
empire_portrait_group: number, // TODO replace with Enum? empire_portrait_group: number, // TODO replace with Enum?
discord_user?: string, discord_user?: string,
ethics: Map<number, Ethic>,
} }
export const createChellarisInfo = (): ChellarisInfo => { export const createChellarisInfo = (): ChellarisInfo => {
const newChellarisInfo = { const newChellarisInfo = {
games: new Map(), games: new Map(),
ethics: [], ethics: new Map(),
portraits: [] portraits: []
}; };
@ -64,9 +67,11 @@ export const createChellarisEmpire = (
}): ChellarisEmpire => { }): ChellarisEmpire => {
const newChellarisEmpire = { const newChellarisEmpire = {
gestalt: gestalt, gestalt: gestalt,
machine: false,
group: group_id, group: group_id,
empire_portrait: empire_portrait_id, empire_portrait: empire_portrait_id,
empire_portrait_group: empire_portrait_group_id, empire_portrait_group: empire_portrait_group_id,
ethics: new Map(),
}; };
return newChellarisEmpire; return newChellarisEmpire;

View file

@ -33,7 +33,8 @@ export enum LegacyEthics {
export type Ethic = { export type Ethic = {
displayName: string, displayName: string,
fanatic: boolean machine: boolean
fanatic?: boolean,
} }
export enum Scale { export enum Scale {

View file

@ -4,6 +4,7 @@ import SelectedGameGroupsStore from "$lib/stores/GameGroupFilter";
import GraphsTabStore from '$lib/stores/GraphsTab'; import GraphsTabStore from '$lib/stores/GraphsTab';
import { createChellarisInfo, type ChellarisGame, createChellarisGame, createChellarisGameGroup, createChellarisEmpire } from "$lib/types/chellaris"; import { createChellarisInfo, type ChellarisGame, createChellarisGame, createChellarisGameGroup, createChellarisEmpire } from "$lib/types/chellaris";
import type { LayoutLoad } from "./$types"; import type { LayoutLoad } from "./$types";
import type { Ethic } from '../../lib/types/stellaris';
export const load: LayoutLoad = async () => { export const load: LayoutLoad = async () => {
let store: string | null; 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 => { 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); ChellarisDataStore.set(chellarisData);
// Local Storage Code // Local Storage Code

View file

@ -4,18 +4,14 @@
import SelectedGameGroupsStore from '$lib/stores/GameGroupFilter'; import SelectedGameGroupsStore from '$lib/stores/GameGroupFilter';
import SelectedGameStore from '$lib/stores/GameFilter'; import SelectedGameStore from '$lib/stores/GameFilter';
import ChellarisDataStore from '$lib/stores/ChellarisData'; 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 selectedGame: number;
let selectedGameGroups: Array<number> = []; let selectedGameGroups: Array<number> = [];
let selectedGameGroupsMap: Map<number, Array<number>> = new Map(); let selectedGameGroupsMap: Map<number, Array<number>> = new Map();
let gameGroups: Map<number, ChellarisGameGroup> = new Map(); let gameGroups: Map<number, ChellarisGameGroup> = new Map();
let chellarisData: ChellarisInfo = { let chellarisData: ChellarisInfo = createChellarisInfo();
games: new Map(),
ethics: [],
portraits: []
};
// Chellaris Data Code // Chellaris Data Code
const updateGameGroups = () => { const updateGameGroups = () => {

View file

@ -12,6 +12,8 @@
import ChellarisDataStore from '$lib/stores/ChellarisData'; import ChellarisDataStore from '$lib/stores/ChellarisData';
import GraphsTabStore from '$lib/stores/GraphsTab'; import GraphsTabStore from '$lib/stores/GraphsTab';
import type { LayoutData } from '../$types'; import type { LayoutData } from '../$types';
import type { Ethic } from '$lib/types/stellaris';
import { page } from '$app/stores';
export let data: LayoutData; export let data: LayoutData;
let selectedGameGroups: Array<number> = []; let selectedGameGroups: Array<number> = [];
@ -19,13 +21,23 @@
let gameGroups: Map<number, ChellarisGameGroup> = new Map(); let gameGroups: Map<number, ChellarisGameGroup> = new Map();
let chellarisData: ChellarisInfo = data.chellarisData; let chellarisData: ChellarisInfo = data.chellarisData;
let selectedGameIdx: number; let selectedGameIdx: number;
let selectedGame: ChellarisGame = createChellarisGame();; let selectedGame: ChellarisGame = createChellarisGame();
let pageData: { let pageData: {
empireCount: number; empireCount: number;
gestaltCount: { total: number; machines: number }; gestaltCount: { total: number; machines: number };
ethicsData: Map<
number,
{
machine: boolean;
displayName: string;
regular: number;
fanatic: number;
}
>;
} = { } = {
empireCount: 0, empireCount: 0,
gestaltCount: { total: 0, machines: 0 } gestaltCount: { total: 0, machines: 0 },
ethicsData: new Map()
}; };
// Save Tab to Store // Save Tab to Store
@ -45,10 +57,11 @@
}; };
const updatePageData = () => { const updatePageData = () => {
let tmpGameData = chellarisData.games.get(selectedGameIdx); const tmpGameData = chellarisData.games.get(selectedGameIdx);
if (typeof tmpGameData !== 'undefined') { if (typeof tmpGameData !== 'undefined') {
let groupEmpires: Map<number, ChellarisEmpire> = new Map(); const groupEmpires: Map<number, ChellarisEmpire> = new Map();
pageData.ethicsData = new Map();
pageData.gestaltCount = { total: 0, machines: 0 }; pageData.gestaltCount = { total: 0, machines: 0 };
tmpGameData.empires.forEach((empire, index) => { tmpGameData.empires.forEach((empire, index) => {
@ -57,11 +70,53 @@
if (empire.gestalt) { if (empire.gestalt) {
pageData.gestaltCount.total = pageData.gestaltCount.total + 1; 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; 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; pageData.empireCount = groupEmpires.size;
@ -78,7 +133,7 @@
SelectedGameStore.subscribe((selection) => { SelectedGameStore.subscribe((selection) => {
selectedGameIdx = selection; selectedGameIdx = selection;
const tmpGameData = chellarisData.games.get(selectedGameIdx); const tmpGameData = chellarisData.games.get(selectedGameIdx);
if (typeof tmpGameData !== "undefined") { if (typeof tmpGameData !== 'undefined') {
selectedGame = tmpGameData; selectedGame = tmpGameData;
} }
@ -129,11 +184,15 @@
<th># Regular</th> <th># Regular</th>
<th># Fanatic</th> <th># Fanatic</th>
</tr> </tr>
<tr> {#each pageData.ethicsData as ethicData}
<td /> {#if !ethicData[1].machine}
<td /> <tr>
<td /> <td>{ethicData[1].displayName}</td>
</tr> <td>{ethicData[1].regular}</td>
<td>{ethicData[1].fanatic}</td>
</tr>
{/if}
{/each}
</table> </table>
</div> </div>
<div class="ethics-column"> <div class="ethics-column">
@ -145,6 +204,14 @@
<th>Machine Ethic</th> <th>Machine Ethic</th>
<th>#</th> <th>#</th>
</tr> </tr>
{#each pageData.ethicsData as ethicData}
{#if ethicData[1].machine}
<tr>
<td>{ethicData[1].displayName}</td>
<td>{ethicData[1].regular}</td>
</tr>
{/if}
{/each}
</table> </table>
</div> </div>
</section> </section>