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 {
games: Map<number, ChellarisGame>, // Key is Game Id
ethics: Array<null>, // TODO implement
ethics: Map<number, Ethic>, // TODO implement
portraits: Array<null>, // 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<number, Ethic>,
}
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;

View file

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

View file

@ -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

View file

@ -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<number> = [];
let selectedGameGroupsMap: Map<number, Array<number>> = new Map();
let gameGroups: Map<number, ChellarisGameGroup> = new Map();
let chellarisData: ChellarisInfo = {
games: new Map(),
ethics: [],
portraits: []
};
let chellarisData: ChellarisInfo = createChellarisInfo();
// Chellaris Data Code
const updateGameGroups = () => {

View file

@ -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<number> = [];
@ -19,13 +21,23 @@
let gameGroups: Map<number, ChellarisGameGroup> = 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<number, ChellarisEmpire> = new Map();
const groupEmpires: Map<number, ChellarisEmpire> = 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,7 +133,7 @@
SelectedGameStore.subscribe((selection) => {
selectedGameIdx = selection;
const tmpGameData = chellarisData.games.get(selectedGameIdx);
if (typeof tmpGameData !== "undefined") {
if (typeof tmpGameData !== 'undefined') {
selectedGame = tmpGameData;
}
@ -129,11 +184,15 @@
<th># Regular</th>
<th># Fanatic</th>
</tr>
<tr>
<td />
<td />
<td />
</tr>
{#each pageData.ethicsData as ethicData}
{#if !ethicData[1].machine}
<tr>
<td>{ethicData[1].displayName}</td>
<td>{ethicData[1].regular}</td>
<td>{ethicData[1].fanatic}</td>
</tr>
{/if}
{/each}
</table>
</div>
<div class="ethics-column">
@ -145,6 +204,14 @@
<th>Machine Ethic</th>
<th>#</th>
</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>
</div>
</section>