forked from public/foundryvtt-reve-de-dragon
Gestion de la périodicité/temporalité
This commit is contained in:
@ -39,6 +39,12 @@ const FORMULES_DUREE = [
|
||||
// { code: "chateaudormant", label: "Fin Chateau dormant", calcul: async (t, actor) => t.nouveauJour() },
|
||||
// { code: "special", label: "Spéciale", calcul: async (t, actor) => t.addJours(100 * RDD_JOURS_PAR_AN) },
|
||||
]
|
||||
const FORMULES_PERIODE = [
|
||||
{ code: 'rounds', label: "Rounds", calcul: async (t, nombre) => t.addMinutes(nombre / 10) },
|
||||
{ code: 'minutes', label: "Minutes", calcul: async (t, nombre) => t.addMinutes(nombre) },
|
||||
{ code: 'heures', label: "Heures", calcul: async (t, nombre) => t.addHeures(nombre) },
|
||||
{ code: 'jours', label: "Jours", calcul: async (t, nombre) => t.addJours(nombre) },
|
||||
]
|
||||
|
||||
export class RdDTimestamp {
|
||||
|
||||
@ -80,6 +86,9 @@ export class RdDTimestamp {
|
||||
static formulesDuree() {
|
||||
return FORMULES_DUREE
|
||||
}
|
||||
static formulesPeriode() {
|
||||
return FORMULES_PERIODE
|
||||
}
|
||||
|
||||
static imgSigneHeure(heure) {
|
||||
return RdDTimestamp.imgSigne(RdDTimestamp.definition(heure));
|
||||
@ -103,23 +112,9 @@ export class RdDTimestamp {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
static signeHeure(key, value) {
|
||||
const signe = RdDTimestamp.definition(value);
|
||||
if (signe && ['key', 'webp', 'label', 'lettreFont', 'saison', 'heure', 'icon'].includes(key)) {
|
||||
return signe[key];
|
||||
}
|
||||
console.error(`Appel à getSigneAs('${key}', ${value}) avec une clé/heure incorrects`);
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
static getCalendrier(indexDate, indexMinute = 0) {
|
||||
return new RdDTimestamp({ indexDate, indexMinute }).toOldCalendrier();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param indexMinute: la version formattée de la date
|
||||
* @param indexDate: la date (depuis le jour 0)
|
||||
* @return la version formattée de la date
|
||||
*/
|
||||
static formatIndexDate(indexDate) {
|
||||
return new RdDTimestamp({ indexDate }).formatDate()
|
||||
@ -142,9 +137,9 @@ export class RdDTimestamp {
|
||||
indexMinute: worldTime.heureRdD * 120 + worldTime.minutesRelative
|
||||
};
|
||||
RdDTimestamp.setWorldTime(new RdDTimestamp(worldTime))
|
||||
|
||||
|
||||
}
|
||||
return new RdDTimestamp(worldTime);
|
||||
return new RdDTimestamp(worldTime);
|
||||
}
|
||||
|
||||
static setWorldTime(timestamp) {
|
||||
@ -175,6 +170,11 @@ export class RdDTimestamp {
|
||||
this.indexMinute = indexMinute ?? 0
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convertit le timestamp en une structure avec les informations utiles
|
||||
* pour afficher la date et l'heure
|
||||
*/
|
||||
toCalendrier() {
|
||||
return {
|
||||
timestamp: this,
|
||||
@ -187,21 +187,6 @@ export class RdDTimestamp {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertit un timestamp en donnée utile à l'affichage d'un calendrier
|
||||
*/
|
||||
toOldCalendrier() {
|
||||
return {
|
||||
indexJour: this.indexDate,
|
||||
annee: this.annee,
|
||||
moisRdD: this.mois,
|
||||
jour: this.jour,
|
||||
heureRdD: this.heure,
|
||||
moisLabel: RdDTimestamp.definition(this.mois).label,
|
||||
heureLabel: RdDTimestamp.definition(this.heure).label,
|
||||
minutesRelative: this.minute,
|
||||
};
|
||||
}
|
||||
get annee() { return Math.floor(this.indexDate / RDD_JOURS_PAR_AN) }
|
||||
get mois() { return Math.floor((this.indexDate % RDD_JOURS_PAR_AN) / RDD_JOURS_PAR_MOIS) }
|
||||
get jour() { return (this.indexDate % RDD_JOURS_PAR_AN) % RDD_JOURS_PAR_MOIS }
|
||||
@ -256,11 +241,12 @@ export class RdDTimestamp {
|
||||
}
|
||||
|
||||
addPeriode(nombre, unite) {
|
||||
switch (unite) {
|
||||
case 'heures': return this.addHeures(nombre)
|
||||
case 'minutes': return this.addMinutes(nombre)
|
||||
case 'jours': return this.addJours(nombre)
|
||||
case 'rounds': return this.addMinutes(nombre / 10)
|
||||
const formule = FORMULES_PERIODE.find(it => it.code == unite);
|
||||
if (formule) {
|
||||
return formule.calcul(this, nombre);
|
||||
}
|
||||
else {
|
||||
ui.notifications.info(`Pas de période pour ${unite ?? 'Aucune uinité définie'}`)
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@ -275,7 +261,10 @@ export class RdDTimestamp {
|
||||
}
|
||||
|
||||
compare(timestamp) {
|
||||
let diff = (this.indexDate - timestamp.indexDate) ?? (this.indexMinute - timestamp.indexMinute);
|
||||
let diff = this.indexDate - timestamp.indexDate
|
||||
if (diff == 0) {
|
||||
diff = this.indexMinute - timestamp.indexMinute
|
||||
}
|
||||
return diff < 0 ? -1 : diff > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user