foundryvtt-reve-de-dragon/module/rdd-calendrier.js

505 lines
18 KiB
JavaScript
Raw Normal View History

2020-12-08 21:40:41 +01:00
import { RdDCalendrierEditeur } from "./rdd-calendrier-editeur.js";
2020-12-10 20:14:35 +01:00
import { RdDAstrologieEditeur } from "./rdd-astrologie-editeur.js";
2020-12-12 23:31:19 +01:00
import { RdDResolutionTable } from "./rdd-resolution-table.js";
import { RdDUtility } from "./rdd-utility.js";
import { RdDDice } from "./rdd-dice.js";
2021-06-09 00:00:15 +02:00
import { Misc } from "./misc.js";
2022-01-29 22:49:34 +01:00
import { HIDE_DICE, SHOW_DICE, SYSTEM_RDD, SYSTEM_SOCKET_ID } from "./constants.js";
2022-11-04 20:41:16 +01:00
import { DialogChronologie } from "./dialog-chronologie.js";
import { RdDTimestamp, WORLD_TIMESTAMP_SETTING } from "./rdd-timestamp.js";
2021-06-09 00:00:15 +02:00
const RDD_JOUR_PAR_MOIS = 28;
2021-06-09 00:00:15 +02:00
const RDD_HEURES_PAR_JOUR = 12;
2020-12-11 08:29:24 +01:00
const MAX_NOMBRE_ASTRAL = 12;
const JOURS_DU_MOIS = Array(RDD_JOUR_PAR_MOIS).fill().map((item, index) => 1 + index);
2020-09-20 16:36:39 +02:00
/* -------------------------------------------- */
export class RdDCalendrier extends Application {
2020-12-08 21:40:41 +01:00
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
template: "systems/foundryvtt-reve-de-dragon/templates/calendar-template.html",
popOut: false,
resizable: false
});
}
static createCalendrierPos() {
return { top: 200, left: 200 };
}
2021-07-09 01:01:12 +02:00
constructor() {
super();
2020-12-08 21:40:41 +01:00
// position
this.calendrierPos = duplicate(game.settings.get(SYSTEM_RDD, "calendrier-pos"));
if (this.calendrierPos == undefined || this.calendrierPos.top == undefined) {
this.calendrierPos = RdDCalendrier.createCalendrierPos();
game.settings.set(SYSTEM_RDD, "calendrier-pos", this.calendrierPos);
2021-07-09 01:01:12 +02:00
}
// Calendrier
this.timestamp = RdDTimestamp.getWorldTime();
2021-07-09 01:01:12 +02:00
if (Misc.isUniqueConnectedGM()) { // Uniquement si GM
RdDTimestamp.setWorldTime(this.timestamp);
2021-07-09 01:01:12 +02:00
this.listeNombreAstral = this.getListeNombreAstral();
this.rebuildListeNombreAstral(HIDE_DICE); // Ensure always up-to-date
}
console.log('RdDCalendrier.constructor()', this.timestamp, this.timestamp.toCalendrier(), this.calendrierPos, this.listeNombreAstral);
Hooks.on('updateSetting', async (setting, update, options, id) => this.onUpdateSetting(setting, update, options, id));
}
async onUpdateSetting(setting, update, options, id) {
if (setting.key == SYSTEM_RDD + '.' + WORLD_TIMESTAMP_SETTING) {
this.timestamp = RdDTimestamp.getWorldTime();
this.updateDisplay();
}
2020-12-08 21:40:41 +01:00
}
/* -------------------------------------------- */
/** @override */
async activateListeners(html) {
super.activateListeners(html);
this.html = html;
this.updateDisplay();
this.html.find('.ajout-chronologie').click(ev => DialogChronologie.create());
this.html.find('.calendar-btn').click(ev => this.onCalendarButton(ev));
this.html.find('.calendar-btn-edit').click(ev => {
ev.preventDefault();
this.showCalendarEditor();
});
this.html.find('.astrologie-btn-edit').click(ev => {
ev.preventDefault();
this.showAstrologieEditor();
2021-07-09 01:01:12 +02:00
});
this.html.find('#calendar-move-handle').mousedown(ev => {
ev.preventDefault();
ev = ev || window.event;
let isRightMB = false;
if ("which" in ev) { // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
isRightMB = ev.which == 3;
} else if ("button" in ev) { // IE, Opera
isRightMB = ev.button == 2;
}
if (!isRightMB) {
dragElement(document.getElementById("calendar-time-container"));
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
function dragElement(elmnt) {
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.bottom = undefined
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released:
elmnt.onmousedown = undefined;
document.onmouseup = undefined;
document.onmousemove = undefined;
let xPos = (elmnt.offsetLeft - pos1) > window.innerWidth ? window.innerWidth - 200 : (elmnt.offsetLeft - pos1);
let yPos = (elmnt.offsetTop - pos2) > window.innerHeight - 20 ? window.innerHeight - 100 : (elmnt.offsetTop - pos2)
xPos = xPos < 0 ? 0 : xPos;
yPos = yPos < 0 ? 0 : yPos;
if (xPos != (elmnt.offsetLeft - pos1) || yPos != (elmnt.offsetTop - pos2)) {
elmnt.style.top = (yPos) + "px";
elmnt.style.left = (xPos) + "px";
}
game.system.rdd.calendrier.calendrierPos.top = yPos;
game.system.rdd.calendrier.calendrierPos.left = xPos;
if (game.user.isGM) {
game.settings.set(SYSTEM_RDD, "calendrier-pos", duplicate(game.system.rdd.calendrier.calendrierPos));
}
}
}
} else if (isRightMB) {
game.system.rdd.calendrier.calendrierPos.top = 200;
game.system.rdd.calendrier.calendrierPos.left = 200;
if (game.user.isGM) {
game.settings.set(SYSTEM_RDD, "calendrier-pos", duplicate(game.system.rdd.calendrier.calendrierPos));
}
this.setPos(game.system.rdd.calendrier.calendrierPos);
}
});
}
/* -------------------------------------------- */
getListeNombreAstral() {
return game.settings.get(SYSTEM_RDD, "liste-nombre-astral") ?? [];
2020-12-08 21:40:41 +01:00
}
2020-12-08 23:28:29 +01:00
2020-12-10 20:14:35 +01:00
/* -------------------------------------------- */
dateCourante() {
return this.timestamp.formatDate();
2020-12-10 20:14:35 +01:00
}
isAfterIndexDate(indexDate) {
// TODO: standardize
return indexDate < this.timestamp.indexDate;
2021-04-04 18:37:16 +02:00
}
2021-01-16 18:54:07 +01:00
/* -------------------------------------------- */
heureCourante() { return RdDTimestamp.definition(this.timestamp.heure); }
2021-01-16 18:54:07 +01:00
2020-12-09 00:01:02 +01:00
/* -------------------------------------------- */
getCurrentMinute() { return this.timestamp.indexMinute; }
getTimestampFinChateauDormant(nbJours = 0) {
return this.timestamp.nouveauJour().addJour(nbJours);
}
getTimestampFinHeure(nbHeures = 0) {
return this.timestamp.nouvelleHeure().addHeures(nbHeures);
2020-12-09 00:01:02 +01:00
}
2021-04-04 18:37:16 +02:00
/* -------------------------------------------- */
getIndexFromDate(jour, mois) {
const addYear = mois < this.timestamp.mois || (mois == this.timestamp.mois && jour < this.timestamp.jour)
const time = RdDTimestamp.timestamp(this.timestamp.annee + (addYear ? 1 : 0), mois, jour);
return time.indexDate;
2021-04-04 18:37:16 +02:00
}
2020-12-12 23:31:19 +01:00
/* -------------------------------------------- */
getJoursSuivants(count) {
2020-12-12 23:31:19 +01:00
let jours = [];
let indexDate = this.timestamp.indexDate;
for (let i = 0; i < count; i++, indexDate++) {
jours[i] = { label: RdDTimestamp.formatIndexDate(indexDate), index: indexDate };
2020-12-12 23:31:19 +01:00
}
return jours;
}
2020-12-08 23:28:29 +01:00
/* -------------------------------------------- */
async ajouterNombreAstral(indexDate, showDice = SHOW_DICE) {
const nombreAstral = await RdDDice.rollTotal("1dh", { showDice: showDice, rollMode: "selfroll" });
const dateFuture = RdDTimestamp.formatIndexDate(indexDate);
if (showDice != HIDE_DICE) {
2021-07-09 01:01:12 +02:00
ChatMessage.create({
whisper: ChatMessage.getWhisperRecipients("GM"),
content: `Le chiffre astrologique du ${dateFuture} sera le ${nombreAstral}`
});
}
2020-12-08 23:28:29 +01:00
return {
nombreAstral: nombreAstral,
2020-12-08 23:28:29 +01:00
valeursFausses: [],
index: indexDate
2020-12-08 23:28:29 +01:00
}
}
2021-04-25 10:08:40 +02:00
/* -------------------------------------------- */
resetNombreAstral() {
2021-04-25 10:08:40 +02:00
this.listeNombreAstral = [];
game.settings.set(SYSTEM_RDD, "liste-nombre-astral", []);
2022-01-29 22:49:34 +01:00
game.socket.emit(SYSTEM_SOCKET_ID, {
msg: "msg_reset_nombre_astral",
data: {}
});
2021-04-25 10:08:40 +02:00
}
2021-06-09 00:00:15 +02:00
/* -------------------------------------------- */
/**
*
* @param {*} indexDate la date pour laquelle obtenir le nombre astral. Si undefined, on prend la date du jour
* @returns le nombre astral pour la date, ou pour la date du jour si la date n'est pas fournie.
* Si aucun nombre astral n'est trouvé, retourne 0 (cas où l'on demanderait un nombre astral en dehors des 12 jours courant et à venir)
*/
getNombreAstral(indexDate = undefined) {
if (indexDate == undefined) {
indexDate = this.timestamp.indexDate;
}
const listNombreAstral = this.getListeNombreAstral();
let astralData = listNombreAstral.find((nombreAstral, i) => nombreAstral.index == indexDate);
return astralData?.nombreAstral ?? 0;
2020-12-12 23:31:19 +01:00
}
2020-12-08 23:28:29 +01:00
/* -------------------------------------------- */
async rebuildListeNombreAstral(showDice = HIDE_DICE) {
if (Misc.isUniqueConnectedGM()) {
let newList = [];
2021-06-09 00:00:15 +02:00
for (let i = 0; i < MAX_NOMBRE_ASTRAL; i++) {
let dayIndex = this.timestamp.indexDate + i;
2021-06-09 00:00:15 +02:00
let na = this.listeNombreAstral.find(n => n.index == dayIndex);
if (na) {
2021-07-09 01:01:12 +02:00
newList[i] = na;
} else {
2021-07-09 01:01:12 +02:00
newList[i] = await this.ajouterNombreAstral(dayIndex, showDice);
}
}
this.listeNombreAstral = newList;
game.settings.set(SYSTEM_RDD, "liste-nombre-astral", newList);
2020-12-08 23:28:29 +01:00
}
}
2021-06-09 16:38:52 +02:00
/* -------------------------------------------- */
async setNewTimestamp(newTimestamp) {
game.actors.forEach(actor => actor.onTimeChanging(this.timestamp, newTimestamp));
RdDTimestamp.setWorldTime(newTimestamp);
this.timestamp = newTimestamp;
await this.rebuildListeNombreAstral();
2021-06-05 20:53:43 +02:00
this.updateDisplay();
2020-12-08 23:28:29 +01:00
}
2020-12-08 21:40:41 +01:00
/* -------------------------------------------- */
async onCalendarButton(ev) {
ev.preventDefault();
const calendarAvance = ev.currentTarget.attributes['data-calendar-avance'];
const calendarSet = ev.currentTarget.attributes['data-calendar-set'];
if (calendarAvance) {
await this.incrementTime(Number(calendarAvance.value));
2022-12-29 02:31:29 +01:00
}
else if (calendarSet) {
this.positionnerHeure(Number(calendarSet.value));
2020-12-08 21:40:41 +01:00
}
this.updateDisplay();
}
/* -------------------------------------------- */
async incrementTime(minutes = 0) {
await this.setNewTimestamp(this.timestamp.addMinutes(minutes));
2020-12-08 21:40:41 +01:00
}
2020-12-08 23:28:29 +01:00
/* -------------------------------------------- */
2021-07-09 01:01:12 +02:00
async incrementerJour() {
await this.setNewTimestamp(this.timestamp.nouveauJour());
2020-12-08 23:28:29 +01:00
}
2020-12-08 21:40:41 +01:00
/* -------------------------------------------- */
async positionnerHeure(heure) {
const indexDate = this.timestamp.indexDate;
const addDay = this.timestamp.heure < heure ? 0 : 1;
await this.setNewTimestamp(new RdDTimestamp({
indexDate: indexDate + addDay, indexHeure: 0
})
.addHeures(heure))
2020-12-08 21:40:41 +01:00
}
/* -------------------------------------------- */
2021-03-16 22:56:57 +01:00
fillCalendrierData(formData = {}) {
mergeObject(formData, this.timestamp.toCalendrier());
2021-03-16 22:56:57 +01:00
formData.isGM = game.user.isGM;
return formData;
2020-12-08 21:40:41 +01:00
}
2020-12-12 23:31:19 +01:00
/* -------------------------------------------- */
getLectureAstrologieDifficulte(dateIndex) {
let indexNow = this.timestamp.indexDate;
2020-12-12 23:31:19 +01:00
let diffDay = dateIndex - indexNow;
return - Math.floor(diffDay / 2);
}
/* -------------------------------------------- */
async requestNombreAstral(request) {
2022-12-29 02:31:29 +01:00
const actor = game.actors.get(request.id);
if (Misc.isUniqueConnectedGM()) { // Only once
console.log(request);
let jourDiff = this.getLectureAstrologieDifficulte(request.date);
2022-06-12 12:14:55 +02:00
let niveau = Number(request.astrologie.system.niveau) + Number(request.conditions) + Number(jourDiff) + Number(request.etat);
let rollData = {
2020-12-17 00:05:32 +01:00
caracValue: request.carac_vue,
finalLevel: niveau,
showDice: HIDE_DICE,
rollMode: "blindroll"
};
await RdDResolutionTable.rollData(rollData);
request.rolled = rollData.rolled;
2022-12-29 02:31:29 +01:00
request.isValid = request.rolled.isSuccess;
request.nbAstral = this.getNombreAstral(request.date);
2022-12-29 02:31:29 +01:00
if (request.rolled.isSuccess) {
if (request.rolled.isPart) {
2022-12-29 02:31:29 +01:00
// Gestion expérience (si existante)
request.competence = actor.getCompetence("astrologie")
request.selectedCarac = actor.system.carac["vue"];
actor.appliquerAjoutExperience(request, 'hide');
}
}
else {
request.nbAstral = await RdDDice.rollTotal("1dhr" + request.nbAstral, {
rollMode: "selfroll", showDice: HIDE_DICE
});
2020-12-12 23:41:04 +01:00
// Mise à jour des nombres astraux du joueur
2022-12-29 02:31:29 +01:00
this.addNbAstralIncorect(request.id, request.date, request.nbAstral);
2020-12-12 23:31:19 +01:00
}
2022-12-29 02:31:29 +01:00
if (Misc.getActiveUser(request.userId)?.isGM) {
RdDUtility.responseNombreAstral(request);
} else {
2022-01-29 22:49:34 +01:00
game.socket.emit(SYSTEM_SOCKET_ID, {
2020-12-12 23:31:19 +01:00
msg: "msg_response_nombre_astral",
data: request
});
2020-12-12 23:31:19 +01:00
}
}
}
2022-12-29 02:31:29 +01:00
addNbAstralIncorect(actorId, date, nbAstral) {
let astralData = this.listeNombreAstral.find((nombreAstral, i) => nombreAstral.index == date);
astralData.valeursFausses.push({ actorId: actorId, nombreAstral: nbAstral });
game.settings.set(SYSTEM_RDD, "liste-nombre-astral", this.listeNombreAstral);
}
static ecartHeureChance(heureNaissance, nombreAstral, heure) {
return (heureNaissance + nombreAstral - heure) % RDD_HEURES_PAR_JOUR;
2021-10-11 11:09:30 +02:00
}
2020-12-11 16:19:19 +01:00
/* -------------------------------------------- */
getAjustementAstrologique(heureNaissance, name = undefined) {
let defHeure = RdDTimestamp.findHeure(heureNaissance);
if (defHeure) {
return RdDCalendrier.ajustementAstrologiqueHeure(defHeure.heure, this.getNombreAstral(), this.timestamp.heure);
}
else if (name) {
2021-01-16 18:54:07 +01:00
ui.notifications.warn(name + " n'a pas d'heure de naissance, ou elle est incorrecte : " + heureNaissance);
2020-12-11 03:23:34 +01:00
}
2021-06-09 00:00:15 +02:00
else {
ui.notifications.warn(heureNaissance + " ne correspond pas à une heure de naissance");
}
2020-12-11 03:23:34 +01:00
return 0;
}
static ajustementAstrologiqueHeure(hn, nbAstral, heure) {
switch (RdDCalendrier.ecartHeureChance(hn, nbAstral, heure)) {
case 0: return 4;
case 4: case 8: return 2;
case 6: return -4;
case 3: case 9: return -2;
}
return 0;
}
2020-12-08 21:40:41 +01:00
/* -------------------------------------------- */
getData() {
2021-03-16 22:56:57 +01:00
let formData = super.getData();
this.fillCalendrierData(formData);
this.setPos(this.calendrierPos);
2021-03-16 22:56:57 +01:00
return formData;
2020-12-08 21:40:41 +01:00
}
/* -------------------------------------------- */
setPos(pos) {
return new Promise(resolve => {
function check() {
let elmnt = document.getElementById("calendar-time-container");
if (elmnt) {
2022-11-05 18:06:30 +01:00
elmnt.style.bottom = undefined;
let xPos = (pos.left) > window.innerWidth ? window.innerWidth - 200 : pos.left;
let yPos = (pos.top) > window.innerHeight - 20 ? window.innerHeight - 100 : pos.top;
2020-12-08 21:40:41 +01:00
elmnt.style.top = (yPos) + "px";
elmnt.style.left = (xPos) + "px";
resolve();
} else {
setTimeout(check, 30);
}
}
check();
});
}
/* -------------------------------------------- */
updateDisplay() {
let calendrier = this.fillCalendrierData();
// Rebuild text du calendrier
2023-01-08 20:41:50 +01:00
let dateHTML = `${calendrier.jourDuMois} ${calendrier.mois.label} (${calendrier.mois.saison}) de l'année ${calendrier.annee}`
2020-12-21 20:54:24 +01:00
if (game.user.isGM) {
dateHTML = dateHTML + "<br>Nombre Astral: " + (this.getNombreAstral() ?? "?");
}
2021-03-31 23:59:31 +02:00
for (let handle of document.getElementsByClassName("calendar-date-rdd")) {
handle.innerHTML = dateHTML;
}
for (let heure of document.getElementsByClassName("calendar-heure-texte")) {
heure.innerHTML = calendrier.heure.label;
}
for (const minute of document.getElementsByClassName("calendar-time-disp")) {
minute.innerHTML = `${calendrier.minute} minutes`;
}
for (const heureImg of document.getElementsByClassName("calendar-heure-img")) {
heureImg.src = calendrier.heure.icon;
2020-12-21 20:54:24 +01:00
}
2020-12-08 21:40:41 +01:00
}
/* -------------------------------------------- */
async saveEditeur(calendrierData) {
const newTimestamp = RdDTimestamp.timestamp(
Number.parseInt(calendrierData.annee),
calendrierData.mois.heure,
Number.parseInt(calendrierData.jourMois),
calendrierData.heure.heure,
Number.parseInt(calendrierData.minutes)
);
await this.setNewTimestamp(newTimestamp);
2020-12-08 21:40:41 +01:00
}
2020-12-08 21:40:41 +01:00
/* -------------------------------------------- */
async showCalendarEditor() {
let calendrierData = this.fillCalendrierData();
if (this.editeur == undefined) {
let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/calendar-editor-template.html', calendrierData);
this.editeur = new RdDCalendrierEditeur(html, this, calendrierData)
2020-12-08 21:40:41 +01:00
}
this.editeur.updateData(calendrierData);
2020-12-08 21:40:41 +01:00
this.editeur.render(true);
}
static buildJoursMois() { return JOURS_DU_MOIS; }
2021-06-09 00:00:15 +02:00
2020-12-10 20:14:35 +01:00
/* -------------------------------------------- */
async showAstrologieEditor() {
const calendrierData = duplicate(this.fillCalendrierData());
2021-12-18 09:45:36 +01:00
this.listeNombreAstral = this.listeNombreAstral || [];
calendrierData.astrologieData = this.listeNombreAstral.map(astro => {
const timestamp = new RdDTimestamp({ indexDate: astro.index });
astro.date = { mois: timestamp.mois, jour: timestamp.jour + 1 }
for (let vf of astro.valeursFausses) {
let actor = game.actors.get(vf.actorId);
2020-12-13 23:11:58 +01:00
vf.actorName = (actor) ? actor.name : "Inconnu";
}
return astro;
});
const nbAstral = this.getNombreAstral()
calendrierData.heures = Array.from(Array(RDD_HEURES_PAR_JOUR).keys());
calendrierData.ajustementsActeur = game.actors.filter(it => it.isPersonnage() && it.hasPlayerOwner).map(actor => {
return {
actor,
ajustements: calendrierData.heures.map(heure => {
const hn = RdDTimestamp.findHeure(actor.getHeureNaissance())?.heure;
return {
heure,
ajustement: RdDCalendrier.ajustementAstrologiqueHeure(hn, nbAstral, heure)
}
})
2022-12-29 02:31:29 +01:00
}
});
let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/calendar-astrologie-template.html', calendrierData);
let astrologieEditeur = new RdDAstrologieEditeur(html, this, calendrierData)
astrologieEditeur.updateData(calendrierData);
2020-12-11 08:29:24 +01:00
astrologieEditeur.render(true);
2020-12-10 20:14:35 +01:00
}
2020-09-20 16:36:39 +02:00
}