'use client'; import { Ethic, Game, GameData, GameGroup } from "@/types/stellaris"; import { Dispatch, Key, SetStateAction, Suspense, useEffect, useState } from 'react'; import { Empire } from '../../../types/stellaris'; import { generateUrl } from "@/components/server/fetchers"; type SelectionType = "all" | Set; export const GameInfoTabs = ( props: { game: Game, groups: GameGroup[] }) => { const [currentTab, setCurrentTab] = useState(1); // won't work with a class, needs to be a type const [gameData, setGameData] = useState({info: props.game, groups: props.groups, empires: new Array}); useEffect(() => { const getEmpires = async () => { const tmpEmpires = await fetch( generateUrl('/empires', [ //{ key: "group_id", val: this.gr }, // TODO: wait for firq to implement multi calls, for now return all empires in game and manual parse { key: "group_game_id", val: gameData.info.id },] )).then((res) => res.json()); let empires: Empire[] = [] tmpEmpires.forEach((empire: Empire) => { if (gameData.groups.map(group => group.id).includes(empire.group_id)) { empires.push(empire) } }); return empires; } const fetchData = async () => { let tmpData = gameData tmpData.groups = props.groups; tmpData.empires = await getEmpires(); setGameData((prevGameData) => ({ ...prevGameData, groups: tmpData.groups, empires: tmpData.empires })); } if (gameData.groups != props.groups) { fetchData() } }, [gameData, props.groups]) let currentTabView; switch (currentTab) { case (1): case (2): { currentTabView =

{currentTab}

break; } case (3): { const gestaltSums = getGestaltCount(gameData.empires, 13); currentTabView =

Legacy View for {props.groups.length > 1 ? "Groups" : "Group"} {props.groups.map(elem => elem.name).join(", ")}

Empires: {gameData.empires.length}

Ethics

Total Gestalts: {gestaltSums.total}

Hive Minds: {gestaltSums.total - gestaltSums.machines}

Machines: {gestaltSums.machines}

Machine Ethics

Portrait 1

Portrait 2

Portrait 3

Portrait 1

Portrait 2

Portrait 3

; break; } default: currentTabView =

Oops, something went wrong

} return ( <>
{currentTabView} ) } function getEthicSum(ethic: Ethic, empires: Empire[]) { let sum = 0; empires.forEach(empire => { empire.ethic_ids.includes(ethic.id) ? sum = sum + 1 : sum }); return sum; } function getGestaltCount(empires: Empire[], machineId: number) { let sum = 0; let sumMachines = 0; empires.forEach(empire => { if(empire.gestalt) { if(empire.empire_portrait_group_id == machineId) { sumMachines = sumMachines + 1; sum = sum + 1; } else { sum = sum + 1 } }}); return {total: sum, machines: sumMachines} }