Skip to content

Commit

Permalink
Add buildable list to unit stats
Browse files Browse the repository at this point in the history
  • Loading branch information
KingDarBoja committed Apr 26, 2024
1 parent ac2674b commit d193a83
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
20 changes: 0 additions & 20 deletions pages/explorer/races/[raceId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,6 @@ const BuildingMapping = (
);
};

// function getBuildingTrainableUnits(
// building: EbpsType,
// sbpsData: SbpsType[],
// ebpsData: EbpsType[],
// ): BuildingSchema["units"] {
// return Object.entries(getResolvedSquads(building.spawnItems, sbpsData, ebpsData)).map(
// ([id, { ui, time_cost }]) => ({
// id,
// desc: {
// screen_name: ui.screenName,
// help_text: ui.helpText,
// brief_text: ui.briefText,
// symbol_icon_name: ui.symbolIconName,
// icon_name: ui.iconName,
// },
// time_cost,
// }),
// );
// }

function getBuildingUpgrades(
building: EbpsType,
upgradesData: UpgradesType[],
Expand Down
57 changes: 55 additions & 2 deletions pages/explorer/races/[raceId]/units/[unitId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ import { GetStaticPaths, GetStaticProps, NextPage } from "next";
import Head from "next/head";
import Error from "next/error";
import { useRouter } from "next/router";
import { Card, Container, Flex, Grid, List, Space, Stack, Text, Title } from "@mantine/core";
import {
Card,
Container,
Flex,
Grid,
List,
SimpleGrid,
Space,
Stack,
Text,
Title,
} from "@mantine/core";
import {
AbilitiesType,
EbpsType,
getResolvedAbilities,
getResolvedConstruction,
getResolvedUpgrades,
getSquadTotalCost,
getSquadTotalUpkeepCost,
Expand Down Expand Up @@ -45,6 +57,7 @@ interface UnitDetailProps {
resolvedEntities: EbpsType[];
upgrades: UpgradesType[];
abilities: AbilitiesType[];
buildables: EbpsType[];
};
}

Expand Down Expand Up @@ -117,7 +130,7 @@ const UnitDetail: NextPage<UnitDetailProps> = ({ calculatedData }) => {
const { totalUpkeepCost } = calculatedData;

// Obtain the squad weapons loadout (ignoring non-damage dealing ones like smoke).
const { squadWeapons, upgrades, abilities } = calculatedData;
const { squadWeapons, upgrades, abilities, buildables } = calculatedData;

// Use default weapon for max range.
const rangeValues = {
Expand Down Expand Up @@ -217,6 +230,7 @@ const UnitDetail: NextPage<UnitDetailProps> = ({ calculatedData }) => {
</Grid.Col>
</Grid>
<Grid>
<Grid.Col>{UnitBuildingSection(buildables)}</Grid.Col>
<Grid.Col>{UnitWeaponSection(squadWeapons)}</Grid.Col>
</Grid>
</Container>
Expand Down Expand Up @@ -255,6 +269,42 @@ const UnitUpgradeSection = (upgrades: UpgradesType[]) => {
);
};

const UnitBuildingSection = (buildings: EbpsType[]) => {
// Resolve unit buildables.
if (!buildings || !buildings.length) return <></>;
return (
<Stack>
<Title order={4}>Can construct</Title>
<SimpleGrid cols={3}>
{Object.values(buildings).map(({ id, ui, cost }) => {
// If we are missing the name of the ability --> it's most likely broken
if (ui.screenName) {
return (
<Card key={id} p="lg" radius="md" withBorder>
{UnitUpgradeCard({
id,
desc: {
screen_name: ui.screenName,
help_text: ui.helpText,
extra_text: ui.extraText,
brief_text: ui.briefText,
icon_name: ui.iconName,
extra_text_formatter: "",
brief_text_formatter: "",
},
time_cost: cost,
})}
</Card>
);
} else {
return null;
}
})}
</SimpleGrid>
</Stack>
);
};

const UnitAbilitySection = (abilities: AbilitiesType[]) => {
// Resolve unit abilities.
if (!abilities || !abilities.length) return <></>;
Expand Down Expand Up @@ -354,6 +404,8 @@ const createdCalculateValuesForUnits = (

const abilities = Object.values(getResolvedAbilities(resolvedSquad.abilities, abilitiesData));

const buildables = Object.values(getResolvedConstruction(resolvedSquad.construction, ebpsData));

// console.log('Calculated abilities', resolvedSquad);

const resolvedEntities: EbpsType[] = [];
Expand All @@ -374,6 +426,7 @@ const createdCalculateValuesForUnits = (
resolvedEntities,
upgrades,
abilities,
buildables,
};
};

Expand Down
12 changes: 12 additions & 0 deletions src/unitStats/faction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ export function getResolvedAbilities(refs: string[], abilities: AbilitiesType[])
return foundAbilities;
}

export function getResolvedConstruction(refs: string[], ebpsData: EbpsType[]) {
// The key is the ebps id.
const foundConstructions: Record<string, EbpsType> = {};
for (const refId of refs) {
const foundItem = ebpsData.find((x) => x.id === refId);
if (!foundItem) continue;

foundConstructions[refId] ??= foundItem;
}
return foundConstructions;
}

export function getResolvedUpgrades(refs: string[], upgradesData: UpgradesType[]) {
// The key is the upgrade id.
const researchableUpgrades: Record<string, UpgradesType> = {};
Expand Down
18 changes: 18 additions & 0 deletions src/unitStats/mappingSbps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type SbpsType = {
requirements: string[];
/** Found at `squad_ability_ext`. This contains unit abilities references. */
abilities: string[];
/** Found at `squad_engineer_ext/construction_groups/construction_items` list.
* This conatins the ebps reference to the building list this squad can
* construct. */
construction: string[];
/** Found at `squad_reinforce_ext`. This contains reinforcement
* information
*/
Expand Down Expand Up @@ -135,6 +139,7 @@ const mapSbpsData = (filename: string, subtree: any, jsonPath: string, parent: s
capture_revert: 0,
requirements: [],
abilities: [],
construction: [],
};

mapExtensions(subtree, sbpsEntity);
Expand Down Expand Up @@ -282,6 +287,19 @@ const mapExtensions = (root: any, sbps: SbpsType) => {
}
}
break;
case "squad_engineer_ext":
if (!extension.construction_groups?.length) break;
for (const conGroup of extension.construction_groups) {
if (!conGroup.construction_group.construction_items?.length) break;
for (const conItem of conGroup.construction_group.construction_items) {
if (conItem.construction_item.ebp.instance_reference) {
sbps.construction.push(
conItem.construction_item.ebp.instance_reference.split("/").slice(-1)[0],
);
}
}
}
break;
default:
break;
}
Expand Down

0 comments on commit d193a83

Please sign in to comment.