110 lines
4.1 KiB
JavaScript
110 lines
4.1 KiB
JavaScript
import { renderTemplate } from "../constants.js";
|
|
import { ReglesOptionnelles } from "../settings/regles-optionnelles.js";
|
|
import { EffetsDraconiques } from "../tmr/effets-draconiques.js";
|
|
import { RdDTimestamp } from "../time/rdd-timestamp.js";
|
|
|
|
export class DialogRepos extends Dialog {
|
|
|
|
static async create(actor) {
|
|
if (!actor.isPersonnage()) {
|
|
return
|
|
}
|
|
if (!ReglesOptionnelles.isUsing("chateau-dormant-gardien") || !actor.isPersonnageJoueur()) {
|
|
foundry.utils.mergeObject(actor.system.sommeil, {
|
|
"nouveaujour": true,
|
|
"insomnie": EffetsDraconiques.isSujetInsomnie(actor),
|
|
"moral": "neutre",
|
|
"heures": 4
|
|
})
|
|
}
|
|
const hourDefs = RdDTimestamp.definitions();
|
|
const heures = Math.max(1, actor.system.sommeil.heures ?? 4)
|
|
const nuitHeureDebut = 7
|
|
const nuitHeureFin = Math.min(11, Math.max(nuitHeureDebut, nuitHeureDebut + heures - 1))
|
|
const nuitDuree = Math.max(1, nuitHeureFin - nuitHeureDebut + 1)
|
|
const data = {
|
|
name: actor.name,
|
|
img: actor.img,
|
|
system: actor.system,
|
|
hourDefinitions: hourDefs,
|
|
nuitHeureDebut,
|
|
nuitHeureFin,
|
|
nuitDuree,
|
|
};
|
|
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/sommeil/dialog-repos.hbs", data);
|
|
const dialog = new DialogRepos(html, actor);
|
|
dialog.render(true);
|
|
}
|
|
|
|
constructor(html, actor) {
|
|
let options = { classes: ["dialog-repos"], width: 400, height: 'fit-content', 'z-index': 99999 };
|
|
let conf = {
|
|
title: "Se reposer",
|
|
content: html,
|
|
default: "repos",
|
|
buttons: {
|
|
"repos": { label: "Se reposer", callback: async it => { this.repos(); } }
|
|
}
|
|
};
|
|
super(conf, options);
|
|
this.actor = actor;
|
|
}
|
|
activateListeners(html) {
|
|
super.activateListeners(html);
|
|
this.html = $(html);
|
|
this.html.find(`.sommeil-actor-moral a`).click(event => this.onActorMoral(event));
|
|
this.html.find('[name="sommeil.heure.debut"], [name="sommeil.heure.fin"]').change(() => {
|
|
const debut = Number.parseInt(this.html.find('[name="sommeil.heure.debut"]').val());
|
|
const fin = Number.parseInt(this.html.find('[name="sommeil.heure.fin"]').val());
|
|
this.html.find('[name="sommeil.duree"]').text(Math.max(1, fin - debut + 1));
|
|
});
|
|
}
|
|
/* -------------------------------------------- */
|
|
|
|
async repos() {
|
|
const selection = await this.html.find("[name='repos']:checked").val();
|
|
switch (selection) {
|
|
case "sieste": return await this.sieste();
|
|
case "nuit": return await this.nuit();
|
|
case "chateau-dormant": return await this.chateauDormant();
|
|
case "gris-reve": return await this.grisReve();
|
|
}
|
|
}
|
|
|
|
async grisReve() {
|
|
await this.html.find("[name='nb-jours']").change();
|
|
const nbJours = Number.parseInt(await this.html.find("[name='nb-jours']").val());
|
|
await this.actor.grisReve(nbJours);
|
|
}
|
|
|
|
async chateauDormant() {
|
|
await this.actor.dormirChateauDormant();
|
|
}
|
|
|
|
async nuit() {
|
|
const debut = Number.parseInt(this.html.find("[name='sommeil.heure.debut']").val());
|
|
const fin = Number.parseInt(this.html.find("[name='sommeil.heure.fin']").val());
|
|
const sommeilHeures = Math.max(1, fin - debut + 1);
|
|
await this.actor.update({ 'system.sommeil.heures': sommeilHeures });
|
|
await this.actor.dormir(sommeilHeures, { chateauDormant: true });
|
|
}
|
|
|
|
async sieste() {
|
|
await this.html.find("[name='sieste.heures']").change();
|
|
const siesteHeures = Number.parseInt(await this.html.find("[name='sieste.heures']").val());
|
|
await this.actor.dormir(siesteHeures);
|
|
}
|
|
|
|
async onActorMoral(event) {
|
|
const selected = this.html.find(event.currentTarget);
|
|
const parentDiv = selected.parents().find('.sommeil-actor-moral');
|
|
const situationMoral = selected.data('moral');
|
|
await this.actor.setInfoSommeilMoral(situationMoral);
|
|
const htmlMoral = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/sommeil/sommeil-actor-moral.hbs', {
|
|
moral: situationMoral
|
|
});
|
|
parentDiv.html(htmlMoral);
|
|
this.html.find(`.sommeil-actor-moral a`).click(event => this.onActorMoral(event));
|
|
}
|
|
}
|