Files

82 lines
2.3 KiB
JavaScript

Hooks.on("renderPartySheet", (app, html, data) => {
convertPartySheetPricesToCredits(html);
});
Hooks.on("updateActor", (actor, data) => {
// Also update when actors are updated
const sheets = actor.apps;
sheets.forEach((sheet) => {
if (
sheet.constructor.name === "PartySheet" ||
sheet.element?.closest(".party-sheet")
) {
convertPartySheetPricesToCredits(sheet.element);
}
});
});
// Fallback: use MutationObserver to catch party sheet updates
const partySheetObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length || mutation.type === "characterData") {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) {
// Element node
const $node = $(node);
// Check if this contains party sheet value spans
if ($node.find(".value").length > 0 || $node.hasClass("value")) {
convertPartySheetPricesToCredits($node);
}
}
});
}
});
});
partySheetObserver.observe(document.body, {
childList: true,
subtree: true,
characterData: true,
});
function convertPartySheetPricesToCredits(html) {
console.log("Converting party sheet prices to credits");
// Target the .value spans specifically
const valueSpans = html.find(".value");
valueSpans.each(function () {
const $span = $(this);
let text = $span.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) {
console.log("Converting:", originalText, "to:", text);
$span.text(text);
}
});
}