enables buying of goods and reducing the wealth of the actor who bought the good and proclaiming it loudly in chat.
parent
6d4f8694df
commit
8258f53a3a
|
|
@ -310,8 +310,133 @@ export class Character extends Actor {
|
||||||
return updateObject;
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -102,22 +102,24 @@ export class MerchantSheet extends HandlebarsApplicationMixin(ActorSheetV2) {
|
||||||
});
|
});
|
||||||
|
|
||||||
const actor = game.actors.get(actorId)
|
const actor = game.actors.get(actorId)
|
||||||
|
const canBuy = await actor.reduceWealth(item.system.price)
|
||||||
|
if (canBuy) { // returns false when the wealth cant be reduced sufficiently
|
||||||
let wealth = 0
|
actor.createEmbeddedDocuments('Item', [item]).then(documents => {
|
||||||
|
documents[0].update({'system.quantity': 1})
|
||||||
actor.itemTypes["Equipment"].forEach(coin => {
|
})
|
||||||
if (coin.system.category.indexOf("Währung") !== -1) {
|
if (item.system.quantity != -1) { // -1 means infinite
|
||||||
wealth += (coin.system.quantity * coin.system.currencyDenominator)
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
ChatMessage.create({
|
||||||
|
user: game.user._id,
|
||||||
if (wealth >= item.system.price) {
|
speaker: {actor},
|
||||||
|
content: `hat ${item.name} für ${game.DSA41.displayCurrency(item.system.price)} gekauft`,
|
||||||
actor.reduceWealth(item.system.price)
|
type: CONST.CHAT_MESSAGE_TYPES.IC
|
||||||
actor.createEmbeddedDocuments('Item', [item])
|
})
|
||||||
this.document.deleteEmbeddedDocuments('Item', [item._id])
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ui.notifications.error(item.name + " ist zu teuer für " + actor.name)
|
ui.notifications.error(item.name + " ist zu teuer für " + actor.name)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue