Files
sf2e-silver-to-credits/scripts/convert-prices-to-credits.js
T

84 lines
2.5 KiB
JavaScript

Hooks.on("renderActorSheet", (app, html, data) => {
convertPricesToCredits(html);
});
Hooks.on("updateActor", (actor, data) => {
const sheets = actor.apps;
sheets.forEach((sheet) => {
if (sheet.element) {
convertPricesToCredits(sheet.element);
}
});
});
function convertPricesToCredits(html) {
// Find all price displays in the sheet
const priceElements = html.find("[data-price]");
priceElements.each(function () {
const $element = $(this);
let priceText = $element.text();
// Convert pp to cr (1 pp = 100 cr)
priceText = priceText.replace(/(\d+(?:\.\d+)?)\s*pp/gi, (match, amount) => {
const creditAmount = parseFloat(amount) * 100;
return `${creditAmount} cr`;
});
// Convert gp to cr (1 gp = 10 cr)
priceText = priceText.replace(/(\d+(?:\.\d+)?)\s*gp/gi, (match, amount) => {
const creditAmount = parseFloat(amount) * 10;
return `${creditAmount} cr`;
});
// Convert cp to cr (1 cp = 0.1 cr)
priceText = priceText.replace(/(\d+(?:\.\d+)?)\s*cp/gi, (match, amount) => {
const creditAmount = parseFloat(amount) / 10;
return `${creditAmount} cr`;
});
// Replace sp with cr (no conversion needed, 1 sp = 1 cr)
priceText = priceText.replace(/(\d+(?:\.\d+)?)\s*sp/gi, "$1 cr");
$element.text(priceText);
});
// Also handle price text in tooltips and other text content
const allElements = html.find("*");
allElements.each(function () {
const $element = $(this);
// Skip elements that have child elements (to avoid processing parent containers)
if ($element.children().length === 0) {
let text = $element.text();
const originalText = text;
// Convert pp to cr (1 pp = 100 cr)
text = text.replace(/(\d+(?:\.\d+)?)\s*pp/gi, (match, amount) => {
const creditAmount = parseFloat(amount) * 100;
return `${creditAmount} cr`;
});
// Convert gp to cr (1 gp = 10 cr)
text = text.replace(/(\d+(?:\.\d+)?)\s*gp/gi, (match, amount) => {
const creditAmount = parseFloat(amount) * 10;
return `${creditAmount} cr`;
});
// Convert cp to cr (1 cp = 0.1 cr)
text = text.replace(/(\d+(?:\.\d+)?)\s*cp/gi, (match, amount) => {
const creditAmount = parseFloat(amount) / 10;
return `${creditAmount} cr`;
});
// Replace sp with cr (no conversion needed, 1 sp = 1 cr)
text = text.replace(/(\d+(?:\.\d+)?)\s*sp/gi, "$1 cr");
// Only update if text changed
if (text !== originalText) {
$element.text(text);
}
}
});
}