foundryvtt-reve-de-dragon/module/dialog-create-signedraconiq...

114 lines
4.3 KiB
JavaScript

import { ChatUtility } from "./chat-utility.js";
import { HtmlUtility } from "./html-utility.js";
import { RdDItemSigneDraconique } from "./item-signedraconique.js";
import { Misc } from "./misc.js";
import { TMRType, TMRUtility } from "./tmr-utility.js";
export class DialogCreateSigneDraconiqueForActors extends Dialog {
static async createSigneForActors() {
let dialogData = {
signe: {
name: 'Un signe draconique',
type: "signedraconique",
img: 'systems/foundryvtt-reve-de-dragon/icons/tmr/gift.webp',
data: {
typesTMR: DialogCreateSigneDraconiqueForActors.selectRandomTmrs(),
ephemere: true,
duree: "1 round",
difficulte: -5,
valeur: { norm: 10, sign: 10, part: 15 },
}
},
actors: game.actors.filter(actor => actor.isHautRevant()).map(it => duplicate(Misc.data(it)))
};
dialogData.tmrs = TMRUtility.listSelectedTMR(dialogData.signe.data.typesTMR ?? []);
dialogData.actors.forEach(it => it.selected = false);
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/dialog-create-signedraconique-actors.html", dialogData);
new DialogCreateSigneDraconiqueForActors(dialogData, html)
.render(true);
}
static selectRandomTmrs() {
let tmrs = Object.values(TMRType).map(value => Misc.upperFirst(value.name));
const nbTmr = tmrs.length;
let remove = Math.floor(Math.random() * (nbTmr - 1));
for (let i = nbTmr; i > remove; i--) {
tmrs.splice(Math.floor(Math.random() * i), 1);
}
return tmrs;
}
constructor(dialogData, html, callback) {
let options = { classes: ["DialogCreateSigneDraconiqueActorsActors"], width: 500, height: 650, 'z-index': 99999 };
let conf = {
title: "Créer un signe pour les personnages",
content: html,
default: "Créer le signe",
buttons: { "Créer le signe": { label: "Créer le signe", callback: it => { this._onCreerSigne(); } } }
};
super(conf, options);
this.dialogData = dialogData;
}
async _onCreerSigne() {
this.dialogData.actors.filter(it => it.selected).map(it => game.actors.get(it._id))
.forEach(actor => this._createSigneForActor(actor, this.dialogData.signe));
}
async _createSigneForActor(actor, signe) {
actor.createEmbeddedDocuments("Item", [signe]);
ChatMessage.create({
whisper: ChatUtility.getWhisperRecipientsAndGMs(Misc.data(actor).name),
content: await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/chat-signe-draconique-actor.html", {
signe: signe,
alias: Misc.data(actor).name
})
});
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
this.setEphemere(this.dialogData.signe.data.ephemere);
html.find(".signe-name").change((event) => this.dialogData.signe.name = event.currentTarget.value);
html.find(".signe-data-ephemere").change((event) => this.setEphemere(event.currentTarget.checked));
html.find(".select-tmr").change((event) => this.onSelectTmr(event));
html.find(".select-actor").change((event) => this.onSelectActor(event));
html.find(".valeur-xp-sort").change((event) => this.onValeurXpSort(event));
}
async setEphemere(ephemere){
this.dialogData.signe.data.ephemere = ephemere;
HtmlUtility._showControlWhen($(".signe-data-duree"), ephemere);
}
async onSelectTmr(event) {
event.preventDefault();
this.dialogData.signe.data.typesTMR = $(".select-tmr").val();
}
async onSelectActor(event) {
event.preventDefault();
let selectActor = $(".select-actor");
const options = event.currentTarget.options;
for (var i = 0; i < options.length; i++) { // looping over the options
const actorId = options[i].attributes["data-actor-id"].value;
const actor = this.dialogData.actors.find(it => it._id == actorId);
if (actor) {
actor.selected = options[i].selected;
}
};
}
onValeurXpSort(event) {
const codeReussite = event.currentTarget.attributes['data-typereussite']?.value ?? 0;
const xp = Number(event.currentTarget.value);
const oldValeur = this.dialogData.signe.data.valeur;
this.dialogData.signe.data.valeur = RdDItemSigneDraconique.calculValeursXpSort(codeReussite, xp, oldValeur);
}
}