foundryvtt-reve-de-dragon/module/sommeil/dialog-chateau-dormant.js

108 lines
3.6 KiB
JavaScript

export class DialogChateauDormant extends Dialog {
static async create() {
const date = game.system.rdd.calendrier.dateCourante();
const actorsSettings = game.actors.filter(actor => actor.hasPlayerOwner && actor.isPersonnage())
.map(actor => ({
actor: actor,
insomnie: actor.system.sommeil?.insomnie,
moral: 'neutre'
}));
const dialogData = {
actorsSettings,
date: date,
motifStress: `Nuit du ${date}`,
finChateauDormant: game.system.rdd.calendrier.getTimestampFinChateauDormant()
};
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/sommeil/dialog-chateau-dormant.hbs",
dialogData);
new DialogChateauDormant(dialogData, html)
.render(true);
}
constructor(dialogData, html) {
const options = {
classes: ["rdd-dialog-chateau-dormant"],
width: 600,
height: 'fit-content',
'z-index': 99999
};
const conf = {
title: "De Chateau dormant à Vaisseau",
content: html,
buttons: {
chateauDormant: { label: "Passer à Vaisseau!", callback: it => { this.onChateauDormant(); } }
}
};
super(conf, options);
this.dialogData = dialogData;
}
activateListeners(html) {
super.activateListeners(html);
this.html = html;
this.html.find('input.sommeil-insomnie').change(event => this.onInsomnie(event));
this._activateListenerOnActorMoral(this.html);
}
_activateListenerOnActorMoral(html) {
html.find(`span.sommeil-actor-moral a`).click(event => this.onActorMoral(event));
}
onInsomnie(event) {
const sommeilInsomnie = this.html.find(event.currentTarget);
const isInsomnie = sommeilInsomnie.is(':checked');
const sommeilHeures = sommeilInsomnie.parents('.set-sommeil-actor').find('input.sommeil-heures');
sommeilHeures.prop('disabled', isInsomnie);
if (isInsomnie) {
sommeilHeures.val('0');
}
}
async onActorMoral(event) {
const selected = this.html.find(event.currentTarget);
const actorRow = selected.parents('.set-sommeil-actor');
const actorId = actorRow.data('actor-id');
const actorSetting = this.getActorSetting(actorId);
if (actorSetting) {
actorSetting.moral = selected.data('moral');
const htmlMoral = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/sommeil/sommeil-actor-moral.hbs', actorSetting)
actorRow.find('.sommeil-actor-moral').html(htmlMoral);
// re-attach listeners for actor row
this._activateListenerOnActorMoral(actorRow);
}
}
getActorSetting(actorId) {
return this.dialogData.actorsSettings.find(it => it.actor.id == actorId);
}
async onChateauDormant() {
const motifStress = this.html.find("form input[name='motifStress']").val();
const sommeilActors = jQuery.map(this.html.find('li.set-sommeil-actor'), it => {
const actorRow = this.html.find(it);
const actorId = actorRow.data('actor-id');
const actorSetting = this.getActorSetting(actorId);
return {
actorId,
ignorer: actorRow.find('input.sommeil-ignorer').is(':checked'),
stress: {
motif: motifStress,
valeur: Number.parseInt(actorRow.find('input.sommeil-stress').val()),
},
sommeil: {
insomnie: actorRow.find('input.sommeil-insomnie').is(':checked'),
heures: Number.parseInt(actorRow.find('input.sommeil-heures').val()),
moral: actorSetting.moral,
}
}
});
await Promise.all(
sommeilActors.filter(it => !it.ignorer)
.map(async it => await game.actors.get(it.actorId)?.prepareChateauDormant(this.dialogData.finChateauDormant, it))
)
}
}