From 8258f53a3a16a005e3b506384a4a0a2d84be8f04 Mon Sep 17 00:00:00 2001 From: macniel Date: Sun, 2 Nov 2025 20:53:01 +0100 Subject: [PATCH] enables buying of goods and reducing the wealth of the actor who bought the good and proclaiming it loudly in chat. --- src/module/documents/character.mjs | 129 +++++++++++++++++++++++++++- src/module/sheets/merchantSheet.mjs | 32 +++---- 2 files changed, 144 insertions(+), 17 deletions(-) diff --git a/src/module/documents/character.mjs b/src/module/documents/character.mjs index ed225f78..68926b5d 100644 --- a/src/module/documents/character.mjs +++ b/src/module/documents/character.mjs @@ -310,8 +310,133 @@ export class Character extends Actor { return updateObject; } - reduceWealth(by) { - console.log('Reichtum reduziert um ', by) + /** + * reduce the wealth owned by this character by the given amount in Silver + * @param by amount of Silver to reduce the wealth by the character + * @returns {boolean} + */ + + async reduceWealth(by) { + + const ducats = this.itemTypes["Equipment"].filter(p => p.name === "Dukate") + const silver = this.itemTypes["Equipment"].filter(p => p.name === "Silbertaler") + const kreutzer = this.itemTypes["Equipment"].filter(p => p.name === "Kreuzer") + const heller = this.itemTypes["Equipment"].filter(p => p.name === "Heller") + + let ducatsAmount = 0 + let silverAmount = 0 + let kreutzerAmount = 0 + let hellerAmount = 0 + + ducats.forEach(d => { + ducatsAmount += d.system.quantity ?? 1 + }) + + silver.forEach(s => { + silverAmount += s.system.quantity ?? 1 + }) + + kreutzer.forEach(k => { + kreutzerAmount += k.system.quantity ?? 1 + }) + + heller.forEach(h => { + hellerAmount += h.system.quantity ?? 1 + }) + + // Convert total wealth to silver + let totalSilver = ducatsAmount * 10 + silverAmount + kreutzerAmount * 0.1 + hellerAmount * 0.01; + + if (totalSilver < by) { + return false; // Indicate that the reduction can't be performed + } + + // Subtract the given sum from total silver + totalSilver -= by; + + // Handle if the total goes below zero + if (totalSilver < 0) totalSilver = 0; + + // Convert back to coinages + let newDucats = Math.floor(totalSilver / 10); + totalSilver %= 10; + + let newSilver = Math.floor(totalSilver); + totalSilver %= 1; + + let newKreutzer = Math.floor(totalSilver / 0.1); + totalSilver %= 0.1; + + let newHeller = Math.round(totalSilver / 0.01); + + // remove all coinage items + + let deleteDocuments = [] + ducats.forEach(d => deleteDocuments.push(d._id)) + silver.forEach(s => deleteDocuments.push(s._id)) + kreutzer.forEach(k => deleteDocuments.push(k._id)) + heller.forEach(h => deleteDocuments.push(h._id)) + + await this.deleteEmbeddedDocuments('Item', deleteDocuments) + + // rebuild coinage documents + const compendiumOfCoins = game.packs.get('DSA_4-1.Currency'); + + if (newDucats > 0) { + let coin = compendiumOfCoins.index.find(coin => coin.name === "Dukate") + const ducatDocument = await compendiumOfCoins.getDocument(coin._id); + try { + this.createEmbeddedDocuments('Item', [ducatDocument]).then( + embeddedDocuments => { + embeddedDocuments[0].update({"system.quantity": newDucats}).then(_ => { + console.log("created new Ducats with qty of", newDucats) + }) + }) + } catch (err) { + } + } + if (newSilver > 0) { + let coin = compendiumOfCoins.index.find(coin => coin.name === "Silbertaler") + const silverDocument = await compendiumOfCoins.getDocument(coin._id); + try { + this.createEmbeddedDocuments('Item', [silverDocument]).then( + embeddedDocuments => { + embeddedDocuments[0].update({"system.quantity": newSilver}).then(_ => { + console.log("created new Silver with qty of", newSilver) + }) + }) + } catch (err) { + } + } + + if (newKreutzer > 0) { + let coin = compendiumOfCoins.index.find(coin => coin.name === "Kreuzer") + const kreutzerDocument = await compendiumOfCoins.getDocument(coin._id); + try { + this.createEmbeddedDocuments('Item', [kreutzerDocument]).then( + embeddedDocuments => { + embeddedDocuments[0].update({"system.quantity": newKreutzer}).then(_ => { + console.log("created new Kreutzer with qty of", newKreutzer) + }) + }) + } catch (err) { + } + } + + if (newHeller > 0) { + let coin = compendiumOfCoins.index.find(coin => coin.name === "Heller") + const hellerDocument = await compendiumOfCoins.getDocument(coin._id); + try { + this.createEmbeddedDocuments('Item', [hellerDocument]).then( + embeddedDocuments => { + embeddedDocuments[0].update({"system.quantity": newHeller}).then(_ => { + console.log("created new Heller with qty of", newHeller) + }) + }) + } catch (err) { + } + } + return true } diff --git a/src/module/sheets/merchantSheet.mjs b/src/module/sheets/merchantSheet.mjs index 7c147f94..15066116 100644 --- a/src/module/sheets/merchantSheet.mjs +++ b/src/module/sheets/merchantSheet.mjs @@ -102,22 +102,24 @@ export class MerchantSheet extends HandlebarsApplicationMixin(ActorSheetV2) { }); const actor = game.actors.get(actorId) - - - let wealth = 0 - - actor.itemTypes["Equipment"].forEach(coin => { - if (coin.system.category.indexOf("Währung") !== -1) { - wealth += (coin.system.quantity * coin.system.currencyDenominator) + const canBuy = await actor.reduceWealth(item.system.price) + if (canBuy) { // returns false when the wealth cant be reduced sufficiently + actor.createEmbeddedDocuments('Item', [item]).then(documents => { + documents[0].update({'system.quantity': 1}) + }) + if (item.system.quantity != -1) { // -1 means infinite + if (item.system.quantity > 1) { + item.update({'system.quantity': item.system.quantity - 1}) + } else { + this.document.deleteEmbeddedDocuments('Item', [item._id]) // delete when the quantity is equal to 0 + } } - }) - - if (wealth >= item.system.price) { - - actor.reduceWealth(item.system.price) - actor.createEmbeddedDocuments('Item', [item]) - this.document.deleteEmbeddedDocuments('Item', [item._id]) - + ChatMessage.create({ + user: game.user._id, + speaker: {actor}, + content: `hat ${item.name} für ${game.DSA41.displayCurrency(item.system.price)} gekauft`, + type: CONST.CHAT_MESSAGE_TYPES.IC + }) } else { ui.notifications.error(item.name + " ist zu teuer für " + actor.name) }