Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra text formatter to abilities and upgrades #262

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/unit-cards/battlegroup-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const BattlegroupBranchMapping = (branch: BattlegroupResolvedBranchType, faction
extra_text: upg.ui.helpText,
brief_text: upg.ui.briefText,
icon_name: upg.ui.iconName,
extra_text_formatter: upg.ui.extraTextFormatter,
}}
time_cost={{
manpower: ability.cost.manpower,
Expand Down Expand Up @@ -166,6 +167,7 @@ export const BattlegroupCard = (
screen_name: uiParent.screenName,
help_text: "",
extra_text: "",
extra_text_formatter: "",
brief_text: uiParent.briefText,
icon_name: uiParent.iconName,
},
Expand Down
9 changes: 6 additions & 3 deletions components/unit-cards/unit-upgrade-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type UnitUpgradeDescription = {
help_text: string | null;
/** Locstring value. Found at `extra_text/locstring/value`. */
extra_text: string | null;
/** Locstring with formatter variables. Found at `extra_text_formatter`, which list each parameter. */
extra_text_formatter: string;
/** Locstring value. Found at `brief_text/locstring/value`. */
brief_text: string | null;
/** File path. Found at `icon_name`. */
Expand Down Expand Up @@ -116,9 +118,9 @@ const UnitUpgradeCardHeader = ({ desc, cfg }: Pick<UnitUpgrade, "desc" | "cfg">)
{desc.brief_text}
</Text>
</Tooltip>
<Tooltip label={desc.extra_text}>
<Text fz="sm" lineClamp={1}>
{desc.extra_text}
<Tooltip label={desc.extra_text || desc.extra_text_formatter}>
<Text fz="sm" lineClamp={2}>
{desc.extra_text || desc.extra_text_formatter}
</Text>
</Tooltip>
</Flex>
Expand Down Expand Up @@ -156,6 +158,7 @@ export const UnitUpgradeCard = ({ desc, time_cost, cfg }: UnitUpgrade) => {
help_text: desc.help_text,
brief_text: desc.brief_text,
extra_text: desc.extra_text,
extra_text_formatter: desc.extra_text_formatter,
icon_name: desc.icon_name,
}}
cfg={cfg}
Expand Down
2 changes: 2 additions & 0 deletions pages/explorer/races/[raceId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ function getBuildingUpgrades(
extra_text: ui.extraText,
brief_text: ui.briefText,
icon_name: ui.iconName,
extra_text_formatter: ui.extraTextFormatter,
},
time_cost: {
fuel: cost.fuel,
Expand All @@ -230,6 +231,7 @@ function generateAfrikaKorpsCallIns(abilitiesData: AbilitiesType[]): BuildingSch
help_text: ui.helpText || "",
extra_text: ui.extraText || "",
brief_text: ui.briefText || "",
extra_text_formatter: ui.extraTextFormatter,
icon_name: ui.iconName,
},
time_cost: {
Expand Down
2 changes: 2 additions & 0 deletions pages/explorer/races/[raceId]/units/[unitId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ const UnitUpgradeSection = (upgrades: UpgradesType[]) => {
extra_text: ui.extraText,
brief_text: ui.briefText,
icon_name: ui.iconName,
extra_text_formatter: ui.extraTextFormatter,
},
time_cost: cost,
})}
Expand Down Expand Up @@ -251,6 +252,7 @@ const UnitAbilitySection = (abilities: AbilitiesType[]) => {
extra_text: ui.extraText,
brief_text: ui.briefText,
icon_name: ui.iconName,
extra_text_formatter: ui.extraTextFormatter,
},
time_cost: cost,
})}
Expand Down
33 changes: 31 additions & 2 deletions src/unitStats/locstring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@ import config from "../../config";
let unitStatsLocString: Record<string, string | null>;

type LocstringSchema = {
id: number;
name: string;
value: string;
};

type LocstringObjectSchema = {
locstring: LocstringSchema;
};

type TextFormatterSchema = {
// template_reference: { name: string; value: string };
formatter: LocstringObjectSchema;
formatter_arguments: Array<{ int_value: number }>;
};

// The input comes from the weapon / ebps / sbps / upgrade json.
const resolveLocstring = (inLocstring: LocstringObjectSchema) => {
if (!unitStatsLocString) return null;
// unitStatsLocString is a object (Record<string, string>
return unitStatsLocString[inLocstring?.locstring?.value] ?? null;
};

const resolveTextFormatterLocstring = (inFormatter: TextFormatterSchema) => {
if (!inFormatter || !inFormatter?.formatter?.locstring?.value) return null;
// We lookup for the formatter locstring and replace the values with the argument list.
const foundFormatterLoc = unitStatsLocString[inFormatter.formatter.locstring.value] ?? null;
if (!foundFormatterLoc) return null;

const valRegex = /(\%\d+\%)/g;
const replacements = inFormatter.formatter_arguments.map((x) => `${x.int_value}`);
const formattedLoc = foundFormatterLoc.replace(valRegex, () => replacements.shift() ?? "0");

// Now replace the double % symbol via regex to preserve a single percentage symbol.
const perRegex = /(\%\%)/g;
const finalFormattedLoc = formattedLoc.replace(perRegex, "%");

return finalFormattedLoc;
};

const fetchLocstring = async () => {
if (unitStatsLocString) return unitStatsLocString;

Expand All @@ -43,4 +66,10 @@ const setLocstring = (locstring: any) => {
unitStatsLocString = locstring;
};

export { resolveLocstring, fetchLocstring, unitStatsLocString, setLocstring };
export {
resolveLocstring,
resolveTextFormatterLocstring,
fetchLocstring,
unitStatsLocString,
setLocstring,
};
27 changes: 18 additions & 9 deletions src/unitStats/mappingAbilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import config from "../../config";
import { internalSlash } from "../utils";
import { resolveLocstring } from "./locstring";
import { resolveLocstring, resolveTextFormatterLocstring } from "./locstring";
import { traverseTree } from "./unitStatsLib";

// Need to be extended by all required fields
Expand Down Expand Up @@ -37,10 +37,15 @@ type AbilitiesUiData = {
iconName: string; // Could be empty.
symbolIconName: string; // Could be empty.
/* Locstring fields. */
helpText: string | null;
briefText: string | null;
screenName: string | null;
extraText: string | null; // Could be empty (Set as $0).
helpText: string;
briefText: string;
screenName: string;
extraText: string; // Could be empty (Set as $0).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one thing I am not sure about.

image

I added these checks that if it's null it should not be displayed, so we can be sure what

if("") is false, so it's going to work, but it's something we can think off how to handle across the app.

Copy link
Collaborator Author

@KingDarBoja KingDarBoja Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does work without issues for both null or empty string checks. :)

extraTextFormatter: string;
// extraTextFormatter?: {
// formatter: string; // The default string which contains the formatted variables.
// args: number[]; // Formatter arguments (ordered list). Kept for reference.
// }
};

/**
Expand Down Expand Up @@ -73,6 +78,7 @@ const mapAbilitiesData = (filename: string, subtree: any, jsonPath: string, pare
briefText: "",
screenName: "",
extraText: "",
extraTextFormatter: "",
},
rechargeTime: 0,
cost: {
Expand All @@ -98,10 +104,13 @@ const mapAbilityBag = (root: any, ability: AbilitiesType) => {
ability.ui.iconName = abilityBag.ui_info?.icon_name || "";
ability.ui.symbolIconName = abilityBag.ui_info?.symbol_icon_name || "";
// When it is empty, it has a value of "0".
ability.ui.screenName = resolveLocstring(abilityBag.ui_info?.screen_name);
ability.ui.helpText = resolveLocstring(abilityBag.ui_info?.help_text);
ability.ui.extraText = resolveLocstring(abilityBag.ui_info?.extra_text);
ability.ui.briefText = resolveLocstring(abilityBag.ui_info?.brief_text);
ability.ui.screenName = resolveLocstring(abilityBag.ui_info?.screen_name) || "";
ability.ui.helpText = resolveLocstring(abilityBag.ui_info?.help_text) || "";
ability.ui.extraText = resolveLocstring(abilityBag.ui_info?.extra_text) || "";
ability.ui.briefText = resolveLocstring(abilityBag.ui_info?.brief_text) || "";

ability.ui.extraTextFormatter =
resolveTextFormatterLocstring(abilityBag.ui_info?.extra_text_formatter) || "";

/* --------- COST SECTION --------- */
ability.cost.fuel = abilityBag.cost_to_player?.fuel || 0;
Expand Down
7 changes: 6 additions & 1 deletion src/unitStats/mappingUpgrades.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// type description of mapped data

import { resolveLocstring } from "./locstring";
import { resolveLocstring, resolveTextFormatterLocstring } from "./locstring";
import { traverseTree } from "./unitStatsLib";
import config from "../../config";
import { internalSlash } from "../utils";
Expand Down Expand Up @@ -36,6 +36,7 @@ type UpgradeUiData = {
briefText: string;
screenName: string;
extraText: string; // Could be empty (Set as $0).
extraTextFormatter: string;
};

/**
Expand Down Expand Up @@ -72,6 +73,7 @@ const mapUpgradesData = (filename: string, subtree: any, jsonPath: string, paren
briefText: "",
screenName: "",
extraText: "",
extraTextFormatter: "",
},
cost: {
fuel: 0,
Expand Down Expand Up @@ -108,6 +110,9 @@ const mapUpgradeBag = (root: any, upgrade: UpgradesType) => {
const briefText = upgradeBag.ui_info?.brief_text;
upgrade.ui.briefText = resolveLocstring(briefText) || "";

upgrade.ui.extraTextFormatter =
resolveTextFormatterLocstring(upgradeBag.ui_info?.extra_text_formatter) || "";

/* --------- COST SECTION --------- */
upgrade.cost.time = upgradeBag.time_cost?.time_seconds || 0;
upgrade.cost.fuel = upgradeBag.time_cost?.cost?.fuel || 0;
Expand Down