foundryvtt-reve-de-dragon/module/item-sheet.js

275 lines
10 KiB
JavaScript
Raw Normal View History

2020-12-04 20:52:04 +01:00
import { RdDItemSort } from "./item-sort.js";
import { RdDUtility } from "./rdd-utility.js";
2021-01-07 20:04:10 +01:00
import { RdDAlchimie } from "./rdd-alchimie.js";
import { RdDItemCompetence } from "./item-competence.js";
2021-04-04 18:37:16 +02:00
import { RdDHerbes } from "./rdd-herbes.js";
2021-10-29 09:53:35 +02:00
import { RdDGemme } from "./rdd-gemme.js";
import { HtmlUtility } from "./html-utility.js";
2022-11-06 21:39:03 +01:00
import { ReglesOptionelles } from "./settings/regles-optionelles.js";
import { SYSTEM_RDD } from "./constants.js";
import { RdDSheetUtility } from "./rdd-sheet-utility.js";
import { SystemCompendiums } from "./settings/system-compendiums.js";
import { Misc } from "./misc.js";
2020-12-04 20:52:04 +01:00
2020-05-21 21:48:20 +02:00
/**
* Extend the basic ItemSheet for RdD specific items
2020-05-21 21:48:20 +02:00
*/
2020-05-22 00:48:43 +02:00
export class RdDItemSheet extends ItemSheet {
2020-05-21 21:48:20 +02:00
static get ITEM_TYPE() {
return undefined
}
static defaultTemplate(type) {
return type ?
`systems/foundryvtt-reve-de-dragon/templates/item-${type}-sheet.html` :
"systems/foundryvtt-reve-de-dragon/templates/item-sheet.html";
}
static register(sheetClass) {
Items.registerSheet(SYSTEM_RDD, sheetClass, {
label: Misc.typeName('Item', sheetClass.ITEM_TYPE),
types: [sheetClass.ITEM_TYPE],
makeDefault: true
})
}
2020-05-21 21:48:20 +02:00
/** @override */
2021-05-07 17:27:02 +02:00
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: [SYSTEM_RDD, "sheet", "item"],
template: RdDItemSheet.defaultTemplate(RdDItemSheet.ITEM_TYPE),
2021-05-07 17:27:02 +02:00
width: 550,
height: 550
});
2020-05-21 21:48:20 +02:00
}
/* -------------------------------------------- */
get template() {
return RdDItemSheet.defaultTemplate(this.item.type);
}
get title() {
return `${Misc.typeName('Item', this.item.type)}: ${this.item.name}`;
}
2021-12-05 16:48:18 +01:00
2020-05-21 21:48:20 +02:00
/* -------------------------------------------- */
2021-01-01 21:11:56 +01:00
_getHeaderButtons() {
let buttons = super._getHeaderButtons();
if (this.item.isInventaire() && this.item.isVideOuNonConteneur()) {
2021-05-07 17:27:02 +02:00
buttons.unshift({
class: "vendre",
2021-05-07 17:27:02 +02:00
icon: "fas fa-comments-dollar",
onclick: ev => this.item.proposerVente()
});
}
buttons.unshift({
class: "montrer",
icon: "fas fa-comment",
onclick: ev => this.item.postItem()
});
2021-01-01 21:11:56 +01:00
return buttons
}
2020-05-21 21:48:20 +02:00
2021-01-01 21:11:56 +01:00
/* -------------------------------------------- */
2020-05-21 21:48:20 +02:00
/** @override */
2021-05-07 17:27:02 +02:00
setPosition(options = {}) {
2020-05-21 21:48:20 +02:00
const position = super.setPosition(options);
const sheetHeader = this.element.find(".sheet-header");
2020-05-21 21:48:20 +02:00
const sheetBody = this.element.find(".sheet-body");
const bodyHeight = position.height - sheetHeader[0].clientHeight;
2020-05-21 21:48:20 +02:00
sheetBody.css("height", bodyHeight);
return position;
}
2021-05-07 17:27:02 +02:00
2020-12-04 20:52:04 +01:00
/* -------------------------------------------- */
async getData() {
2021-05-07 17:27:02 +02:00
let formData = {
id: this.item.id,
title: this.item.name,
type: this.item.type,
img: this.item.img,
name: this.item.name,
system: this.item.system,
2021-03-29 18:08:18 +02:00
isGM: game.user.isGM,
actorId: this.actor?.id,
isOwned: this.actor ? true : false,
owner: this.item.isOwner,
2021-03-29 18:08:18 +02:00
editable: this.isEditable,
cssClass: this.isEditable ? "editable" : "locked",
2022-09-27 21:03:18 +02:00
isSoins: false,
description: await TextEditor.enrichHTML(this.item.system.description, { async: true }),
descriptionmj: await TextEditor.enrichHTML(this.item.system.descriptionmj, { async: true }),
isComestible: this.item.isComestible()
2021-03-25 09:28:36 +01:00
}
2021-04-10 21:07:53 +02:00
const competences = await SystemCompendiums.getCompetences(this.actor?.type);
2022-04-30 23:42:55 +02:00
formData.categorieCompetences = RdDItemCompetence.getCategorieCompetences()
2022-09-17 16:07:38 +02:00
if (this.item.type == 'tache' || this.item.type == 'livre' || this.item.type == 'meditation' || this.item.type == 'oeuvre') {
2022-04-30 23:42:55 +02:00
formData.caracList = duplicate(game.system.model.Actor.personnage.carac)
2022-06-12 08:17:59 +02:00
formData.caracList["reve-actuel"] = duplicate(game.system.model.Actor.personnage.reve.reve)
formData.competences = competences;
}
2022-09-17 16:07:38 +02:00
if (this.item.type == 'arme') {
formData.competences = competences.filter(it => RdDItemCompetence.isCompetenceArme(it));
}
if (['sort', 'sortreserve'].includes(this.item.type)) {
formData.competences = competences.filter(it => RdDItemCompetence.isDraconic(it));
}
2022-09-27 21:03:18 +02:00
if (this.item.type == 'recettecuisine') {
formData.ingredients = await TextEditor.enrichHTML(this.object.system.ingredients, { async: true })
}
if (this.item.type == 'extraitpoetique') {
formData.extrait = await TextEditor.enrichHTML(this.object.system.extrait, { async: true })
formData.texte = await TextEditor.enrichHTML(this.object.system.texte, { async: true })
2022-09-27 21:03:18 +02:00
}
2022-09-17 16:07:38 +02:00
if (this.item.type == 'recettealchimique') {
RdDAlchimie.processManipulation(this.item, this.actor && this.actor.id);
formData.manipulation_update = await TextEditor.enrichHTML(this.object.system.manipulation_update, { async: true })
formData.utilisation = await TextEditor.enrichHTML(this.object.system.utilisation, { async: true })
formData.enchantement = await TextEditor.enrichHTML(this.object.system.enchantement, { async: true })
formData.sureffet = await TextEditor.enrichHTML(this.object.system.sureffet, { async: true })
2021-01-07 20:04:10 +01:00
}
2022-09-17 16:07:38 +02:00
if (this.item.type == 'gemme') {
2021-10-29 09:53:35 +02:00
formData.gemmeTypeList = RdDGemme.getGemmeTypeOptionList();
RdDGemme.calculDataDerivees(this.item);
2021-10-29 09:53:35 +02:00
}
2022-09-17 16:07:38 +02:00
if (this.item.type == 'potion') {
2021-04-04 18:37:16 +02:00
if (this.dateUpdated) {
formData.system.prdate = this.dateUpdated;
2021-04-04 18:37:16 +02:00
this.dateUpdated = undefined;
}
await RdDHerbes.updatePotionData(formData);
2021-05-07 17:27:02 +02:00
}
2022-09-17 16:07:38 +02:00
if (formData.isOwned && this.item.type == 'herbe' && (formData.system.categorie == 'Soin' || formData.system.categorie == 'Repos')) {
2021-05-07 17:27:02 +02:00
formData.isIngredientPotionBase = true;
}
2022-09-17 16:07:38 +02:00
if (this.item.type == 'sortreserve') {
const sortId = this.item.system.sortid;
formData.sort = formData.isOwned ? this.item.actor.items.get(sortId) : game.items.get(sortId);
}
2021-03-16 22:56:57 +01:00
formData.bonusCaseList = RdDItemSort.getBonusCaseList(formData, true);
2020-12-04 20:52:04 +01:00
2021-03-16 22:56:57 +01:00
return formData;
2020-12-04 20:52:04 +01:00
}
2021-05-07 17:27:02 +02:00
2020-05-21 21:48:20 +02:00
/* -------------------------------------------- */
/** @override */
2021-05-07 17:27:02 +02:00
activateListeners(html) {
2020-05-21 21:48:20 +02:00
super.activateListeners(html);
let itemSheetDialog = this;
HtmlUtility._showControlWhen($(".item-cout"), ReglesOptionelles.isUsing('afficher-prix-joueurs') || game.user.isGM || !this.item.isOwned);
HtmlUtility._showControlWhen($(".item-magique"), this.item.isMagique());
2020-05-21 21:48:20 +02:00
// Everything below here is only needed if the sheet is editable
if (!this.options.editable) return;
this.form.ondragstart = (event) => this._onDragStart(event);
this.form.ondrop = (event) => this._onDrop(event);
2020-05-22 19:28:01 +02:00
// Select competence categorie
2021-05-06 21:34:07 +02:00
html.find(".categorie").change(event => this._onSelectCategorie(event));
2021-03-29 18:08:18 +02:00
html.find('.sheet-competence-xp').change((event) => {
if (this.item.isCompetencePersonnage()) {
RdDUtility.checkThanatosXP(this.item.name);
2021-02-06 23:51:04 +01:00
}
2021-05-07 17:27:02 +02:00
});
2021-02-06 23:51:04 +01:00
2021-04-04 18:37:16 +02:00
html.find('.enchanteDate').change((event) => {
let jour = Number($('#jourMois').val());
let mois = $('#nomMois').val();
this.dateUpdated = game.system.rdd.calendrier.getIndexFromDate(jour, mois);
});
2022-12-05 15:29:00 +01:00
html.find('.creer-tache-livre').click((event) => this._getEventActor(event).creerTacheDepuisLivre(this.item));
html.find('.consommer-potion').click((event) => this._getEventActor(event).consommerPotion(this.item));
html.find('.creer-potion-base').click((event) => this._getEventActor(event).dialogFabriquerPotion(this.item));
2021-05-07 17:27:02 +02:00
2021-01-07 20:04:10 +01:00
html.find('.alchimie-tache a').click((event) => {
2022-12-05 15:29:00 +01:00
let actor = this._getEventActor(event);
2021-05-07 17:27:02 +02:00
if (actor) {
2022-12-05 15:29:00 +01:00
let recetteId = event.currentTarget.attributes['data-recette-id'].value;
let tacheName = event.currentTarget.attributes['data-alchimie-tache'].value;
let tacheData = event.currentTarget.attributes['data-alchimie-data'].value;
2021-01-07 20:04:10 +01:00
actor.effectuerTacheAlchimie(recetteId, tacheName, tacheData);
} else {
2022-06-12 13:58:55 +02:00
ui.notifications.info("Impossible trouver un acteur pour réaliser cette tache Alchimique.");
2021-01-07 20:04:10 +01:00
}
});
2021-05-07 17:27:02 +02:00
2022-12-05 15:29:00 +01:00
html.find('.item-split').click(async event => RdDSheetUtility.splitItem(RdDSheetUtility.getItem(event, this.actor), this.actor, async () => itemSheetDialog.render(true)));
html.find('.item-edit').click(async event => RdDSheetUtility.getItem(event, this.actor)?.sheet.render(true));
html.find('.item-delete').click(async event => RdDUtility.confirmerSuppressionItem(this, RdDSheetUtility.getItem(event, this.actor)));
html.find('.item-vendre').click(async event => RdDSheetUtility.getItem(event, this.actor)?.proposerVente());
html.find('.item-montrer').click(async event => RdDSheetUtility.getItem(event, this.actor)?.postItem());
html.find('.item-action').click(async event => RdDSheetUtility.getItem(event, this.actor)?.actionPrincipale(this.actor, async () => itemSheetDialog.render(true)));
2022-12-05 15:29:00 +01:00
}
_getEventActor(event) {
let actorId = event.currentTarget.attributes['data-actor-id'].value;
let actor = game.actors.get(actorId);
return actor;
2020-05-21 21:48:20 +02:00
}
2021-05-07 17:27:02 +02:00
2020-05-21 21:48:20 +02:00
/* -------------------------------------------- */
2021-05-06 21:34:07 +02:00
async _onSelectCategorie(event) {
2020-05-21 21:48:20 +02:00
event.preventDefault();
2021-05-06 21:34:07 +02:00
if (this.item.isCompetence()) {
2021-05-06 21:34:07 +02:00
let level = RdDItemCompetence.getNiveauBase(event.currentTarget.value);
this.item.system.base = level;
2021-05-06 21:34:07 +02:00
$("#base").val(level);
}
2020-05-22 19:28:01 +02:00
}
2020-05-21 21:48:20 +02:00
2020-05-22 19:28:01 +02:00
/* -------------------------------------------- */
2020-05-21 21:48:20 +02:00
/** @override */
_updateObject(event, formData) {
if (this.item.type == 'sort') {
// Données de bonus de cases ?
formData['system.bonuscase'] = RdDItemSort.buildBonusCaseStringFromFormData(formData.bonusValue, formData.caseValue);
}
2021-05-07 17:27:02 +02:00
return this.item.update(formData);
2020-05-21 21:48:20 +02:00
}
/* -------------------------------------------- */
2021-12-05 16:48:18 +01:00
async _onDragStart(event) {
}
async _onDrop(event) {
// Try to extract the dragData
let dragData = RdDItemSheet.$extractDragData(event);
if (!dragData) return false;
const allowed = Hooks.call("dropActorSheetData", this.actor, this, dragData);
if (allowed === false) return false;
2021-12-05 16:48:18 +01:00
// Handle different dragData types
switch (dragData.type) {
2021-12-05 16:48:18 +01:00
case "Item":
return this._onDropItem(event, dragData);
case "Actor":
return this._onDropActor(event, dragData);
2021-12-05 16:48:18 +01:00
}
return super._onDrop(event);
}
static $extractDragData(event) {
try {
const eventData = event?.dataTransfer?.getData('text/plain');
if (eventData) {
return JSON.parse(eventData);
}
} catch (err) { }
return undefined;
}
2021-12-05 16:48:18 +01:00
async _onDropItem(event, dragData) {
}
async _onDropActor(event, dragData) {
}
2020-05-21 21:48:20 +02:00
}