on('ready', () => { const MAX_WEIGHT = 500; const COIN_ORDER = ['PP', 'GP', 'EP', 'SP', 'CP']; const QUANTITY_COLOR = 'blue';
// Create or get the handout by name const getOrCreateHandout = (handoutName) => { let handout = findObjs({ type: 'handout', name: handoutName })[0]; if (!handout) { log(`Creating handout: ${handoutName}`); handout = createObj('handout', { name: handoutName, inplayerjournals: 'all', notes: '<b>Total Weight: 0 lbs</b><br><br>' // Initialize with basic content }); } else { log(`Found existing handout: ${handoutName}`); } return handout; };
// Parse the content of the handout notes const parseHandoutContent = (notes) => { let items = {}; (notes || '').split('<br>').forEach(line => { let match = line.match(/<b>(.+?)<\/b>: <span style='color:(.*?)'>(\d+)<\/span> \((.*?) lbs\)/); if (match) { let [, item, , quantity, weight] = match; items[item] = { quantity: parseInt(quantity, 10), weight: parseFloat(weight) }; } }); return items; };
// Update the handout notes with new item details const updateHandout = (handoutName, items) => { let totalWeight = Object.entries(items).reduce((sum, [_, { quantity, weight }]) => sum + (quantity * weight), 0); let sortedItems = Object.entries(items).sort(([a], [b]) => { let aIndex = COIN_ORDER.indexOf(a); let bIndex = COIN_ORDER.indexOf(b); if (aIndex !== -1 && bIndex !== -1) return aIndex - bIndex; if (aIndex !== -1) return -1; if (bIndex !== -1) return 1; return a.localeCompare(b); });
let content = `<b>Total Weight: ${totalWeight.toFixed(2)} lbs</b><br><br>`; sortedItems.forEach(([item, { quantity, weight }]) => { content += `<b>${item}</b>: <span style='color:${QUANTITY_COLOR}'>${quantity}</span> (${weight} lbs)<br>`; });
let handout = getOrCreateHandout(handoutName); handout.set('notes', content); // Update the notes field with the new content log(`Handout updated for ${handoutName}`); };
// Add an item to the handout const addItem = (count, item, weight, handoutName) => { let handout = getOrCreateHandout(handoutName); handout.get('notes', (notes) => { let items = parseHandoutContent(notes); count = parseInt(count, 10); weight = parseFloat(weight); let currentWeight = Object.entries(items).reduce((sum, [_, { quantity, weight }]) => sum + (quantity * weight), 0); if (currentWeight + (count * weight) > MAX_WEIGHT) { sendChat('Dimension Bag', `/w gm &{template:default} {{name=Dimension Bag}} {{Warning=Cannot add items! Exceeds 500 lbs limit.}}`); return; } items[item] = items[item] || { quantity: 0, weight }; items[item].quantity += count; updateHandout(handoutName, items); }); };
// Remove an item from the handout const removeItem = (count, item, handoutName) => { let handout = getOrCreateHandout(handoutName); handout.get('notes', (notes) => { let items = parseHandoutContent(notes); count = parseInt(count, 10); if (!items[item] || items[item].quantity < count) { sendChat('Dimension Bag', `/w gm &{template:default} {{name=Dimension Bag}} {{Warning=Not enough ${item} to remove!}}`); return; } items[item].quantity -= count; if (items[item].quantity <= 0) delete items[item]; updateHandout(handoutName, items); }); };
// Listen for chat commands on('chat:message', (msg) => { if (msg.type !== 'api') return;
// Use a regular expression to capture bag name in quotes and the rest of the command let args = msg.content.match(/^!dimensionbag (add|remove) "(.+?)" (.+)$/);
if (!args) { sendChat('Dimension Bag', '/w gm &{template:default} {{name=Dimension Bag}} {{Warning=Invalid command syntax. Please use: !dimensionbag add "Bag Name" [quantity] [item] [weight] or !dimensionbag remove "Bag Name" [quantity] [item].}}'); return; }
let action = args[1]; // 'add' or 'remove' let bagName = args[2]; // Bag name with spaces in quotes let commandArgs = args[3].split(' '); // Rest of the command for quantity, item name, weight
if (action === 'add' && commandArgs.length >= 3) { let count = commandArgs.shift(); let weight = commandArgs.pop(); let item = commandArgs.join(' '); // Join remaining parts as item name addItem(count, item, weight, bagName); } else if (action === 'remove' && commandArgs.length >= 2) { let count = commandArgs.shift(); let item = commandArgs.join(' '); // Join remaining parts as item name removeItem(count, item, bagName); } else { sendChat('Dimension Bag', '/w gm &{template:default} {{name=Dimension Bag}} {{Warning=Invalid command syntax.}}'); } }); });
|