Files
Battle-for-Tismo/shared/text_format.js
T
adrien 7ea5373b60 Moved the economy onto resources and surfaced each building's material bill
Rebalanced the resource rules so nothing is priced primarily in money: steel, electricity and high-tech lead every construction bill, chip foundries are seeded everywhere instead of being hand-placed, and cities open with stores of steel and high-tech. City building rows now lead with the resource bill and keep the money as an aside, and the region's resources expand into the buildings that demand them. Pulled the graph and cost maths into a framework-free shared/economy_graph.js, quoted currency through one helper, and gave every data table the same striping and row highlight.
2026-09-22 23:31:36 +02:00

56 lines
1.7 KiB
JavaScript

// Small formatting helpers shared by the UI, ported from
// scripts/classes/text_format.gd.
// Groups a number's digits in threes. The separators are non-breaking spaces,
// so a money or resource figure never wraps in the middle of a number.
export function groupDigits(value) {
const truncated = Math.trunc(value);
const digits = String(Math.abs(truncated));
let grouped = "";
let count = 0;
for (let i = digits.length - 1; i >= 0; i--) {
grouped = digits[i] + grouped;
count += 1;
if (count % 3 === 0 && i > 0) grouped = "\u00a0" + grouped;
}
return (truncated < 0 ? "-" : "") + grouped;
}
function threeSignificant(value) {
const digits = Math.floor(Math.log(value) / Math.log(10)) + 1;
const decimals = Math.max(0, 3 - digits);
const factor = Math.pow(10, decimals);
const rounded = Math.round(value * factor) / factor;
let text = rounded.toFixed(decimals);
if (text.includes(".")) text = text.replace(/0+$/, "").replace(/\.$/, "");
return text;
}
export function compact(value) {
if (Math.abs(value) < 1e-9) return "0";
const sign = value < 0 ? "-" : "";
let amount = Math.abs(value);
const suffixes = ["", "K", "M", "B", "T"];
let index = 0;
while (amount >= 1000 && index < suffixes.length - 1) {
amount /= 1000;
index += 1;
}
let text = threeSignificant(amount);
if (text === "1000" && index < suffixes.length - 1) {
index += 1;
text = "1";
}
return sign + text + suffixes[index];
}
export function improvements(list) {
if (!list || list.length === 0) return "None";
return list.join(", ");
}
export function hours(value) {
if (value < 1.0) return value.toFixed(2) + " h";
return value.toFixed(1) + " h";
}