1
0
Fork 0

Change to Ethics Enum

This commit is contained in:
Neshura 2023-06-03 03:01:38 +02:00
parent b427d07b2e
commit 053b0366f8
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
3 changed files with 66 additions and 130 deletions

View file

@ -1,5 +1,5 @@
'use client'; 'use client';
import { Ethics, Scale } from '../../types/stellaris'; import { Ethics, EthicsDataG15, Scale } from '../../types/stellaris';
import { Radar } from 'react-chartjs-2' import { Radar } from 'react-chartjs-2'
import { Chart as ChartJS, registerables } from 'chart.js'; import { Chart as ChartJS, registerables } from 'chart.js';
import useSWR from 'swr'; import useSWR from 'swr';
@ -7,28 +7,18 @@ import useSWR from 'swr';
const fetcher = async (url:any) => await fetch(url).then((res) => res.json()); const fetcher = async (url:any) => await fetch(url).then((res) => res.json());
export const RadarChart = (props: { weighted: boolean }) => { export const RadarChart = (props: { weighted: boolean }) => {
const {data: tmpData} = useSWR('/api/radar',fetcher); const {data: tmpData} = useSWR('/api/ethics',fetcher);
const parseData = (data: Array<Array<any>>) => { const parseData = (data: Array<Array<any>>) => {
let parsedData = [ let parsedData: EthicsDataG15[] = new Array<EthicsDataG15>;
new Ethics.ega([{ type: Scale.normal, value: data[0][0] }, { type: Scale.fanatic, value: data[0][1] }]), // 0 Object.keys(Ethics).forEach((ethic: number | string) => {
new Ethics.aut([{ type: Scale.normal, value: data[1][0] }, { type: Scale.fanatic, value: data[1][1] }]), // 1 if (Number.isInteger(Number(ethic))) {
new Ethics.mil([{ type: Scale.normal, value: data[2][0] }, { type: Scale.fanatic, value: data[2][1] }]), // 2 parsedData[Number(ethic)] = new EthicsDataG15(data[Number(ethic)])
new Ethics.pac([{ type: Scale.normal, value: data[3][0] }, { type: Scale.fanatic, value: data[3][1] }]), // 3 }
new Ethics.pho([{ type: Scale.normal, value: data[4][0] }, { type: Scale.fanatic, value: data[4][1] }]), // 4 });
new Ethics.phi([{ type: Scale.normal, value: data[5][0] }, { type: Scale.fanatic, value: data[5][1] }]), // 5
new Ethics.com([{ type: Scale.normal, value: data[6][0] }, { type: Scale.fanatic, value: data[6][1] }]), // 6
new Ethics.coo([{ type: Scale.normal, value: data[7][0] }, { type: Scale.fanatic, value: data[7][1] }]), // 7
new Ethics.eli([{ type: Scale.normal, value: data[8][0] }, { type: Scale.fanatic, value: data[8][1] }]), // 8
new Ethics.plu([{ type: Scale.normal, value: data[9][0] }, { type: Scale.fanatic, value: data[9][1] }]), // 9
new Ethics.mat([{ type: Scale.normal, value: data[10][0] }, { type: Scale.fanatic, value: data[10][1] }]), // 10
new Ethics.spi([{ type: Scale.normal, value: data[11][0] }, { type: Scale.fanatic, value: data[11][1] }]), // 11
new Ethics.eco([{ type: Scale.normal, value: data[12][0] }, { type: Scale.fanatic, value: data[12][1] }]), // 12
new Ethics.ind([{ type: Scale.normal, value: data[13][0] }, { type: Scale.fanatic, value: data[13][1] }]), // 13
];
return parsedData; return parsedData;
} }
let ethicsData; let ethicsData: EthicsDataG15[];
let data; let data;
let tEntries = 0; let tEntries = 0;
let scaleLimit = 0; let scaleLimit = 0;
@ -37,27 +27,32 @@ export const RadarChart = (props: { weighted: boolean }) => {
if (tmpData) { if (tmpData) {
let sheetData = tmpData.sheetData let sheetData = tmpData.sheetData
ethicsData = parseData(sheetData); ethicsData = parseData(sheetData);
let labelsArray = [ Ethics.Egalitarian, Ethics.Militarist, Ethics.Xenophobe,
Ethics.Competitive, Ethics.Elitist, Ethics.Materialist, Ethics.Ecologist,
Ethics.Authoritarian, Ethics.Pacifist, Ethics.Xenophile,
Ethics.Cooperative, Ethics.Pluralist, Ethics.Spiritualist, Ethics.Industrialist]
let sumArray = new Array<number>
let sumRegularArray = new Array<number>
labelsArray.forEach((ethic: Ethics, index: number) => {
sumArray[index] = ethicsData[ethic].sum(props.weighted);
sumRegularArray[index] = ethicsData[ethic].sumRegular();
})
data = { data = {
labels: [ ethicsData[0].getKey(), ethicsData[2].getKey(), ethicsData[4].getKey(), labels: labelsArray.map(ethic => Ethics[ethic]),
ethicsData[6].getKey(), ethicsData[8].getKey(), ethicsData[10].getKey(), ethicsData[12].getKey(),
ethicsData[1].getKey(), ethicsData[3].getKey(), ethicsData[5].getKey(),
ethicsData[7].getKey(), ethicsData[9].getKey(), ethicsData[11].getKey(), ethicsData[13].getKey()],
datasets: [{ datasets: [{
label: "Total Ethics", label: "Total Ethics",
data: [ ethicsData[0].sum(props.weighted), ethicsData[2].sum(props.weighted), ethicsData[4].sum(props.weighted), ethicsData[6].sum(props.weighted), data: sumArray,
ethicsData[8].sum(props.weighted), ethicsData[10].sum(props.weighted), ethicsData[12].sum(props.weighted),
ethicsData[1].sum(props.weighted), ethicsData[3].sum(props.weighted), ethicsData[5].sum(props.weighted), ethicsData[7].sum(props.weighted),
ethicsData[9].sum(props.weighted), ethicsData[11].sum(props.weighted), ethicsData[13].sum(props.weighted)],
fill: true, fill: true,
backgroundColor: '#254A6FAA', backgroundColor: '#254A6FAA',
borderColor: '#356A9F', borderColor: '#356A9F',
radius: 3 radius: 3
}, { }, {
label: "Regular Ethics", label: "Regular Ethics",
data: [ ethicsData[0].sumRegular(), ethicsData[2].sumRegular(), ethicsData[4].sumRegular(), data: sumRegularArray,
ethicsData[6].sumRegular(), ethicsData[8].sumRegular(), ethicsData[10].sumRegular(), ethicsData[12].sumRegular(),
ethicsData[1].sumRegular(), ethicsData[3].sumRegular(), ethicsData[5].sumRegular(),
ethicsData[7].sumRegular(), ethicsData[9].sumRegular(), ethicsData[11].sumRegular(), ethicsData[13].sumRegular()],
fill: true, fill: true,
backgroundColor: '#000000AA', backgroundColor: '#000000AA',
borderColor: '#254A6F', borderColor: '#254A6F',
@ -67,7 +62,7 @@ export const RadarChart = (props: { weighted: boolean }) => {
ethicsData.forEach(elem => { ethicsData.forEach(elem => {
tEntries = tEntries + elem.entries(); tEntries = tEntries + elem.sum(false);
if (elem.sum(props.weighted) >= scaleLimit) { if (elem.sum(props.weighted) >= scaleLimit) {
scaleLimit = elem.sum(props.weighted) + 1; scaleLimit = elem.sum(props.weighted) + 1;
} }

View file

@ -1,111 +1,52 @@
import { element } from 'prop-types'; // Ethics Data should be stored differently after Game 15, class will stil be needed for compat reasons afterwards
export namespace Ethics { export class EthicsDataG15 {
// Array index determines fanatic or regular, Index value represents number of occurences
class eth_template { private data: Array<number> = [0]
protected key: string = "";
private data: Array<{type: Scale, value: number}> = []
constructor(data: Array<{type: Scale, value: number}>) {
this.data = data;
}
sum(weigthed: boolean): number {
let sum = 0;
this.data.forEach((element) => {
if (weigthed) {
sum = sum + element.value * element.type;
}
else {
sum = sum + element.value * 1;
}
});
return sum;
}
sumRegular(): number {
let sum = 0;
this.data.forEach((element) => {
if (element.type == Scale.normal) {
sum = sum + element.value;
}
});
return sum;
}
entries(): number {
let sum = 0;
this.data.forEach((element) => {
sum = sum + element.value;
});
return sum;
}
getKey(): string {
return this.key
}
constructor(data: Array<number>) {
this.data[Scale.normal] = data[0],
this.data[Scale.fanatic] = data[1]
} }
export class ega extends eth_template { sum(weigthed: boolean): number {
protected key = "Egalitarian"; let sum = 0;
// skip 0 index since it isn't used
this.data.slice(1,this.data.length).forEach((value, index) => {
// Since the index is accessed via scale enum this works, if not weighted override to 1
let factor = index + 1;
if (!weigthed) {
factor = 1;
}
sum = sum + value * factor;
});
return sum;
} }
export class aut extends eth_template { sumRegular(): number {
protected key = "Authoritarian"; return this.data[Scale.normal];
} }
}
export class mil extends eth_template { export enum Ethics {
protected key = "Militarist"; Egalitarian = 0,
} Authoritarian = 1,
Militarist = 2,
export class pac extends eth_template { Pacifist = 3,
protected key = "Pacifist"; Xenophobe = 4,
} Xenophile = 5,
Competitive = 6,
export class pho extends eth_template { Cooperative = 7,
protected key = "Xenophobe"; Elitist = 8,
} Pluralist = 9,
Materialist = 10,
export class phi extends eth_template { Spiritualist = 11,
protected key = "Xenophile"; Ecologist = 12,
} Industrialist = 13,
export class com extends eth_template {
protected key = "Competitive";
}
export class coo extends eth_template {
protected key = "Cooperative";
}
export class eli extends eth_template {
protected key = "Elitist";
}
export class plu extends eth_template {
protected key = "Pluralist";
}
export class mat extends eth_template {
protected key = "Materialist";
}
export class spi extends eth_template {
protected key = "Spiritualist";
}
export class eco extends eth_template {
protected key = "Ecologist";
}
export class ind extends eth_template {
protected key = "Industrialist";
}
} }
export enum Scale { export enum Scale {
normal = 1, normal = 1,
fanatic = 2 fanatic = 2,
} }
export enum Species { export enum Species {
@ -121,5 +62,5 @@ export enum Species {
Necroid = 9, Necroid = 9,
Aquatic = 10, Aquatic = 10,
Toxoid = 11, Toxoid = 11,
Machine = 12 Machine = 12,
} }