foundryvtt-reve-de-dragon/module/actor/commerce-sheet.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-01-01 22:21:30 +01:00
import { DialogItemAchat } from "../dialog-item-achat.js";
import { RdDItem } from "../item.js";
import { RdDUtility } from "../rdd-utility.js";
import { RdDBaseActorSheet } from "./base-actor-sheet.js";
/**
* Extend the basic ActorSheet with some very simple modifications
* @extends {ActorSheet}
*/
export class RdDCommerceSheet extends RdDBaseActorSheet {
/** @override */
static get defaultOptions() {
2024-05-01 09:13:21 +02:00
return foundry.utils.mergeObject(super.defaultOptions, {
2023-01-01 22:21:30 +01:00
template: "systems/foundryvtt-reve-de-dragon/templates/actor/commerce-actor-sheet.html",
2023-11-06 23:02:22 +01:00
width: 600, height: 720,
tabs: []
2023-01-01 22:21:30 +01:00
});
}
get title() {
if (this.actor.token && this.actor.token != this.actor.prototypeToken) {
return this.actor.token.name;
}
return super.title
}
2023-01-01 22:21:30 +01:00
async getData() {
const formData = await super.getData();
if (this.actor.token && this.actor.token != this.actor.prototypeToken) {
2024-05-01 09:13:21 +02:00
foundry.utils.mergeObject(formData,
{
title: this.actor.token.name,
token: {
img: this.actor.token.texture.src
}
},
{ overwrite: true });
}
return formData;
}
2023-01-01 22:21:30 +01:00
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
super.activateListeners(html);
this.html.find('a.item-acheter').click(async event => await this.vente(this.getItem(event)));
2023-01-01 22:21:30 +01:00
if (!this.options.editable) return;
this.html.find('a.item-quantite-moins').click(async event => await this.getItem(event)?.quantiteIncDec(-1, { supprimerSiZero: false }));
2023-01-01 22:21:30 +01:00
this.html.find('a.item-quantite-plus').click(async event => await this.getItem(event)?.quantiteIncDec(1));
this.html.find('input.item-quantite').change(async event => {
const newQuantite = Math.max(0, Number.parseInt(this.html.find(event.currentTarget).val()));
await this.getItem(event)?.update({ "system.quantite": newQuantite });
})
this.html.find('input.item-cout').change(async event => {
const newCout = Math.max(0, Number(this.html.find(event.currentTarget).val()));
await this.getItem(event)?.update({ "system.cout": newCout });
})
}
getTypesInventaire() {
return RdDItem.getItemTypesInventaire('all');
}
2023-01-01 22:21:30 +01:00
async vente(item) {
const acheteur = RdDUtility.getSelectedActor();
if (!acheteur) {
ui.notifications.warn(`Pas d'acheteur sélectionné`);
return;
}
const disponible = this.actor.getQuantiteDisponible(item)
if (disponible == 0) {
ui.notifications.warn(`${this.name} n'a plus de ${item.name} en vente`);
return;
}
await DialogItemAchat.onAcheter({
item,
vendeur: this.actor,
acheteur,
quantiteIllimite: disponible == undefined,
nbLots: disponible ?? 1,
tailleLot: 1,
prixLot: item.calculerPrixCommercant()
2023-01-01 22:21:30 +01:00
});
}
}