From 274009d3fa9f9d70a422ca348b2f633d7cd619d9 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sun, 1 Jan 2023 22:16:26 +0100 Subject: [PATCH] Suppression de vente depuis un "service" --- module/actor.js | 18 +++++++----------- module/actor/base-actor-sheet.js | 7 +++++-- module/dialog-item-achat.js | 7 ++----- module/dialog-item-vente.js | 12 +++++------- module/item-service.js | 2 +- module/item-sheet.js | 2 +- module/item.js | 3 +-- templates/dialog-item-achat.html | 4 +--- 8 files changed, 23 insertions(+), 32 deletions(-) diff --git a/module/actor.js b/module/actor.js index f532d894..8b85cdce 100644 --- a/module/actor.js +++ b/module/actor.js @@ -3661,12 +3661,11 @@ export class RdDActor extends RdDBaseActor { const cout = Number(achat.prixTotal ?? 0); const vendeur = achat.vendeurId ? game.actors.get(achat.vendeurId) : undefined; - const service = achat.serviceId ? (vendeur?.getItem(achat.serviceId) ?? game.items.get(achat.serviceId)) : undefined; const acheteur = achat.acheteurId ? game.actors.get(achat.acheteurId) : undefined; const vente = achat.vente; const quantite = (achat.choix.nombreLots ?? 1) * (vente.tailleLot); const itemVendu = vendeur?.getItem(vente.item._id) ?? (await RdDItem.getCorrespondingItem(vente.item)); - if (!this.verifierQuantite(service, vente.serviceSubItem, vendeur, itemVendu, quantite)) { + if (!this.verifierQuantite(vendeur, itemVendu, quantite)) { ChatUtility.notifyUser(achat.userId, 'warn', `Le vendeur n'a pas assez de ${itemVendu.name} !`); return } @@ -3675,7 +3674,7 @@ export class RdDActor extends RdDBaseActor { ChatUtility.notifyUser(achat.userId, 'warn', `Vous n'avez pas assez d'argent pour payer ${Math.ceil(cout / 100)} sols !`); return; } - await this.decrementerVente(service, vendeur, itemVendu, quantite, cout); + await this.decrementerVente(vendeur, itemVendu, quantite, cout); if (acheteur) { await acheteur.depenserSols(cout); let createdItemId = await acheteur.creerQuantiteItem(vente.item, quantite); @@ -3697,7 +3696,7 @@ export class RdDActor extends RdDBaseActor { if (vente.quantiteNbLots <= achat.choix.nombreLots) { ChatUtility.removeChatMessageId(achat.chatMessageIdVente); } - else if (!service) { + else { vente["properties"] = itemVendu.getProprietes(); vente.quantiteNbLots -= achat.choix.nombreLots; vente.jsondata = JSON.stringify(vente.item); @@ -3708,18 +3707,15 @@ export class RdDActor extends RdDBaseActor { } } - async decrementerVente(service, vendeur, itemVendu, quantite, cout) { - if (service) { - await service.venteRefItem(itemVendu, quantite, cout) - } - else if (vendeur) { + async decrementerVente(vendeur, itemVendu, quantite, cout) { + if (vendeur) { await vendeur.ajouterSols(cout); await vendeur.decrementerQuantiteItem(itemVendu, quantite); } } - verifierQuantite(service, serviceSubItem, vendeur, item, quantiteTotal) { - const disponible = service ? service.getQuantiteDisponible(serviceSubItem, quantiteTotal) : (vendeur ? (item?.getQuantite() ?? 0) : quantiteTotal); + verifierQuantite(vendeur, item, quantiteTotal) { + const disponible = vendeur ? (item?.getQuantite() ?? 0) : quantiteTotal; return disponible >= quantiteTotal; } diff --git a/module/actor/base-actor-sheet.js b/module/actor/base-actor-sheet.js index 30dcf9a7..7ffa9259 100644 --- a/module/actor/base-actor-sheet.js +++ b/module/actor/base-actor-sheet.js @@ -153,7 +153,7 @@ export class RdDBaseActorSheet extends ActorSheet { RdDSheetUtility.splitItem(item, this.actor); }); this.html.find('.item-delete').click(async event => RdDUtility.confirmActorItemDelete(this, RdDSheetUtility.getItem(event, this.actor))); - this.html.find('.item-vendre').click(async event => RdDSheetUtility.getItem(event, this.actor)?.proposerVente()); + this.html.find('.item-vendre').click(async event => RdDSheetUtility.getItem(event, this.actor)?.proposerVente(this.getQuantiteMax(item))); this.html.find('.creer-un-objet').click(async event => { this.selectObjetTypeToCreate(); @@ -170,7 +170,10 @@ export class RdDBaseActorSheet extends ActorSheet { } - + getQuantiteMax(item) { + return item.system.quantite; + } + /* -------------------------------------------- */ _getHeaderButtons() { let buttons = super._getHeaderButtons(); diff --git a/module/dialog-item-achat.js b/module/dialog-item-achat.js index 12648644..e43ea5d8 100644 --- a/module/dialog-item-achat.js +++ b/module/dialog-item-achat.js @@ -30,15 +30,13 @@ export class DialogItemAchat extends Dialog { } - static async onAcheter({ item, vendeur, acheteur, service, serviceSubItem, tailleLot, prixLot, nbLots, quantiteIllimite, chatMessageIdVente }) { + static async onAcheter({ item, vendeur, acheteur, tailleLot, prixLot, nbLots, quantiteIllimite, chatMessageIdVente }) { const venteData = { item, actingUserId: game.user.id, vendeurId: vendeur?.id, vendeur, acheteur, - serviceSubItem: serviceSubItem, - service, tailleLot, quantiteIllimite, quantiteNbLots: nbLots, @@ -77,7 +75,7 @@ export class DialogItemAchat extends Dialog { buttons[actionAchat] = { label: actionAchat, callback: it => { this.onAchat(); } }; buttons["decliner"] = { label: "Décliner", callback: it => { } }; const acheteur = venteData.acheteur?.name ?? 'Un acheteur'; - const vendeur = (venteData.service ?? venteData.vendeur)?.name ?? 'Un vendeur'; + const vendeur = venteData.vendeur?.name ?? 'Un vendeur'; let conf = { title: `${acheteur} - ${actionAchat} à ${vendeur}`, content: html, @@ -93,7 +91,6 @@ export class DialogItemAchat extends Dialog { await this.html.find(".nombreLots").change(); (this.venteData.vendeur ?? this.venteData.acheteur).achatVente({ userId: game.user.id, - serviceId: this.venteData.service?.id, vendeurId: this.venteData.vendeur?.id, acheteurId: this.venteData.acheteur?.id, prixTotal: this.venteData.prixTotal, diff --git a/module/dialog-item-vente.js b/module/dialog-item-vente.js index f0e7055c..a027ef61 100644 --- a/module/dialog-item-vente.js +++ b/module/dialog-item-vente.js @@ -2,15 +2,13 @@ import { HtmlUtility } from "./html-utility.js"; export class DialogItemVente extends Dialog { - static async display({ item, callback, service = undefined, quantiteMax = undefined }) { - const quantite = quantiteMax ?? item.getQuantite(); + static async display({ item, callback, quantiteMax = undefined }) { + const quantite = quantiteMax ?? item.getQuantite() ?? 1; const isOwned = item.isOwned; - // const isOwned = item.isOwned || service?.actor; const venteData = { item: item, - alias: item.actor?.name ?? service?.name ?? game.user.name, - serviceId: service?.id, - vendeurId: item.actor?.id ?? service?.actor?.id, + alias: item.actor?.name ?? game.user.name, + vendeurId: item.actor?.id , prixOrigine: item.system.cout, prixUnitaire: item.system.cout, prixLot: item.system.cout, @@ -18,7 +16,7 @@ export class DialogItemVente extends Dialog { quantiteNbLots: quantite, quantiteMaxLots: quantite, quantiteMax: quantite, - quantiteIllimite: (service && service.system && service.system.illimite) ? service.system.illimite : !isOwned, + quantiteIllimite: !isOwned || quantiteMax == undefined, isOwned: isOwned, }; const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-vente.html`, venteData); diff --git a/module/item-service.js b/module/item-service.js index bc00ded4..58582a43 100644 --- a/module/item-service.js +++ b/module/item-service.js @@ -47,7 +47,7 @@ export class RdDItemService extends RdDItem { async vendre(subItem) { const item = await RdDItem.getCorrespondingItem(subItem); const quantiteMax = this.system.illimite ? undefined : subItem.system.quantite; - await item.proposerVente({ service: this, quantiteMax }); + await item.proposerVente(quantiteMax); } async acheter(acheteur, subItem) { diff --git a/module/item-sheet.js b/module/item-sheet.js index 2eb610a0..b803de32 100644 --- a/module/item-sheet.js +++ b/module/item-sheet.js @@ -60,7 +60,7 @@ export class RdDItemSheet extends ItemSheet { buttons.unshift({ class: "vendre", icon: "fas fa-comments-dollar", - onclick: ev => this.item.proposerVente({service: this, quantiteMax: 1}) + onclick: ev => this.item.proposerVente(1) }); } buttons.unshift({ diff --git a/module/item.js b/module/item.js index 8455a733..4deec4a1 100644 --- a/module/item.js +++ b/module/item.js @@ -420,7 +420,7 @@ export class RdDItem extends Item { return [true, undefined]; } - async proposerVente({ service = undefined, quantiteMax = undefined }) { + async proposerVente(quantiteMax = undefined) { console.log(this); if (this.isConteneurNonVide()) { ui.notifications.warn(`Votre ${this.name} n'est pas vide, pas possible de le proposer`); @@ -428,7 +428,6 @@ export class RdDItem extends Item { } await DialogItemVente.display({ item: this, - service: service, quantiteMax, callback: async (vente) => { vente["properties"] = this.getProprietes(); diff --git a/templates/dialog-item-achat.html b/templates/dialog-item-achat.html index 6bdff205..03b83a2d 100644 --- a/templates/dialog-item-achat.html +++ b/templates/dialog-item-achat.html @@ -2,9 +2,7 @@
- {{#if service}} - {{service.name}} - {{else if vendeur}} + {{#if vendeur}} {{vendeur.name}} {{else}} Vendeur MJ