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';
import { Ethics, Scale } from '../../types/stellaris';
import { Ethics, EthicsDataG15, Scale } from '../../types/stellaris';
import { Radar } from 'react-chartjs-2'
import { Chart as ChartJS, registerables } from 'chart.js';
import useSWR from 'swr';
@ -7,28 +7,18 @@ import useSWR from 'swr';
const fetcher = async (url:any) => await fetch(url).then((res) => res.json());
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>>) => {
let parsedData = [
new Ethics.ega([{ type: Scale.normal, value: data[0][0] }, { type: Scale.fanatic, value: data[0][1] }]), // 0
new Ethics.aut([{ type: Scale.normal, value: data[1][0] }, { type: Scale.fanatic, value: data[1][1] }]), // 1
new Ethics.mil([{ type: Scale.normal, value: data[2][0] }, { type: Scale.fanatic, value: data[2][1] }]), // 2
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
];
let parsedData: EthicsDataG15[] = new Array<EthicsDataG15>;
Object.keys(Ethics).forEach((ethic: number | string) => {
if (Number.isInteger(Number(ethic))) {
parsedData[Number(ethic)] = new EthicsDataG15(data[Number(ethic)])
}
});
return parsedData;
}
let ethicsData;
let ethicsData: EthicsDataG15[];
let data;
let tEntries = 0;
let scaleLimit = 0;
@ -37,27 +27,32 @@ export const RadarChart = (props: { weighted: boolean }) => {
if (tmpData) {
let sheetData = tmpData.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 = {
labels: [ ethicsData[0].getKey(), ethicsData[2].getKey(), ethicsData[4].getKey(),
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()],
labels: labelsArray.map(ethic => Ethics[ethic]),
datasets: [{
label: "Total Ethics",
data: [ ethicsData[0].sum(props.weighted), ethicsData[2].sum(props.weighted), ethicsData[4].sum(props.weighted), ethicsData[6].sum(props.weighted),
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)],
data: sumArray,
fill: true,
backgroundColor: '#254A6FAA',
borderColor: '#356A9F',
radius: 3
}, {
label: "Regular Ethics",
data: [ ethicsData[0].sumRegular(), ethicsData[2].sumRegular(), ethicsData[4].sumRegular(),
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()],
data: sumRegularArray,
fill: true,
backgroundColor: '#000000AA',
borderColor: '#254A6F',
@ -67,7 +62,7 @@ export const RadarChart = (props: { weighted: boolean }) => {
ethicsData.forEach(elem => {
tEntries = tEntries + elem.entries();
tEntries = tEntries + elem.sum(false);
if (elem.sum(props.weighted) >= scaleLimit) {
scaleLimit = elem.sum(props.weighted) + 1;
}

View file

@ -1,111 +1,52 @@
import { element } from 'prop-types';
export namespace Ethics {
// Ethics Data should be stored differently after Game 15, class will stil be needed for compat reasons afterwards
export class EthicsDataG15 {
// Array index determines fanatic or regular, Index value represents number of occurences
private data: Array<number> = [0]
class eth_template {
protected key: string = "";
private data: Array<{type: Scale, value: number}> = []
constructor(data: Array<{type: Scale, value: number}>) {
this.data = data;
constructor(data: Array<number>) {
this.data[Scale.normal] = data[0],
this.data[Scale.fanatic] = data[1]
}
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;
// 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;
}
sumRegular(): number {
let sum = 0;
this.data.forEach((element) => {
if (element.type == Scale.normal) {
sum = sum + element.value;
return this.data[Scale.normal];
}
});
return sum;
}
entries(): number {
let sum = 0;
this.data.forEach((element) => {
sum = sum + element.value;
});
return sum;
}
getKey(): string {
return this.key
}
}
export class ega extends eth_template {
protected key = "Egalitarian";
}
export class aut extends eth_template {
protected key = "Authoritarian";
}
export class mil extends eth_template {
protected key = "Militarist";
}
export class pac extends eth_template {
protected key = "Pacifist";
}
export class pho extends eth_template {
protected key = "Xenophobe";
}
export class phi extends eth_template {
protected key = "Xenophile";
}
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 Ethics {
Egalitarian = 0,
Authoritarian = 1,
Militarist = 2,
Pacifist = 3,
Xenophobe = 4,
Xenophile = 5,
Competitive = 6,
Cooperative = 7,
Elitist = 8,
Pluralist = 9,
Materialist = 10,
Spiritualist = 11,
Ecologist = 12,
Industrialist = 13,
}
export enum Scale {
normal = 1,
fanatic = 2
fanatic = 2,
}
export enum Species {
@ -121,5 +62,5 @@ export enum Species {
Necroid = 9,
Aquatic = 10,
Toxoid = 11,
Machine = 12
Machine = 12,
}