From 19dd3b540cbced1d21dbba46cb8fbcec6786b070 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sun, 12 May 2024 22:40:06 +0200 Subject: [PATCH] Astrologie: garder toutes les lectures d'un acteur --- module/actor.js | 11 ++-- module/actor/base-actor.js | 7 --- module/migrations.js | 21 ++++++- module/rdd-utility.js | 11 +--- module/sommeil/app-astrologie.js | 16 +++-- module/time/rdd-calendrier.js | 79 ++++++++++-------------- templates/sommeil/astrologie-gardien.hbs | 4 +- 7 files changed, 74 insertions(+), 75 deletions(-) diff --git a/module/actor.js b/module/actor.js index f3d2e33d..2be25dcc 100644 --- a/module/actor.js +++ b/module/actor.js @@ -2365,26 +2365,25 @@ export class RdDActor extends RdDBaseActorSang { /* -------------------------------------------- */ - async resetNombresAstraux() { + async deleteNombresAstraux() { const deletions = this.itemTypes['nombreastral'].map(it => it._id); await this.deleteEmbeddedDocuments("Item", deletions); } /* -------------------------------------------- */ - async ajouteNombreAstral(callData) { - const indexDate = Number.parseInt(callData.date); + async ajouteNombreAstral(date, nbAstral, isValid) { + const indexDate = Number.parseInt(date); // Ajout du nombre astral const item = { name: "Nombre Astral", type: "nombreastral", system: { - value: callData.nbAstral, - istrue: callData.isvalid, + value: nbAstral, + istrue: isValid, jourindex: indexDate, jourlabel: RdDTimestamp.formatIndexDate(indexDate) } }; await this.createEmbeddedDocuments("Item", [item]); - game.system.rdd.calendrier.notifyChangeNombresAstraux(); } async supprimerAnciensNombresAstraux() { diff --git a/module/actor/base-actor.js b/module/actor/base-actor.js index 3a148f18..b0a46de0 100644 --- a/module/actor/base-actor.js +++ b/module/actor/base-actor.js @@ -42,13 +42,6 @@ export class RdDBaseActor extends Actor { switch (sockmsg.msg) { case "msg_remote_actor_call": return RdDBaseActor.onRemoteActorCall(sockmsg.data, sockmsg.userId); - case "msg_reset_nombre_astral": - game.user.character.resetNombresAstraux(); - game.system.rdd.calendrier.notifyChangeNombresAstraux(); - return; - case "msg_refresh_nombre_astral": - Hooks.callAll(APP_ASTROLOGIE_REFRESH); - return; } } diff --git a/module/migrations.js b/module/migrations.js index 887ba354..ddd36ec8 100644 --- a/module/migrations.js +++ b/module/migrations.js @@ -5,6 +5,7 @@ import { Monnaie } from "./item-monnaie.js"; import { RdDItem, TYPES } from "./item.js"; import { RdDTimestamp } from "./time/rdd-timestamp.js"; import { RdDRaretes } from "./item/raretes.js"; +import { RdDCalendrier } from "./time/rdd-calendrier.js"; class Migration { get code() { return "sample"; } @@ -514,6 +515,20 @@ class _10_7_19_PossessionsEntiteVictime extends Migration { } } +class _11_2_20_MigrationAstrologie extends Migration { + get code() { return "migration-astrologie" } + get version() { return "11.2.20" } + + async migrate() { + const nombresAstraux = game.system.rdd.calendrier.getNombresAstraux() + nombresAstraux.forEach(na => { + na.lectures = na.valeursFausses + na.valeursFausses = undefined + }) + await game.system.rdd.calendrier.setNombresAstraux(nombresAstraux) + } +} + export class Migrations { static getMigrations() { return [ @@ -532,6 +547,7 @@ export class Migrations { new _10_7_0_MigrationBlessures(), new _10_7_19_CategorieCompetenceCreature(), new _10_7_19_PossessionsEntiteVictime(), + new _11_2_20_MigrationAstrologie(), ]; } @@ -546,7 +562,10 @@ export class Migrations { } migrate() { - const currentVersion = game.settings.get(SYSTEM_RDD, "systemMigrationVersion"); + let currentVersion = game.settings.get(SYSTEM_RDD, "systemMigrationVersion") + if (currentVersion.startsWith("v")) { + currentVersion = currentVersion.substring(1) + } if (isNewerVersion(game.system.version, currentVersion)) { // if (true) { /* comment previous and uncomment here to test before upgrade */ const migrations = Migrations.getMigrations().filter(m => isNewerVersion(m.version, currentVersion)); diff --git a/module/rdd-utility.js b/module/rdd-utility.js index 374c4cfb..884be00c 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -18,6 +18,7 @@ import { RdDRaretes } from "./item/raretes.js"; import { RdDEmpoignade } from "./rdd-empoignade.js"; import { ExperienceLog } from "./actor/experience-log.js"; import { RdDCoeur } from "./coeur/rdd-coeur.js"; +import { APP_ASTROLOGIE_REFRESH } from "./sommeil/app-astrologie.js"; /* -------------------------------------------- */ // This table starts at 0 -> niveau -10 @@ -625,21 +626,15 @@ export class RdDUtility { return perte.total; } - /* -------------------------------------------- */ - static async responseNombreAstral(callData) { - let actor = game.actors.get(callData.id); - actor.ajouteNombreAstral(callData); - } - /* -------------------------------------------- */ static onSocketMessage(sockmsg) { switch (sockmsg.msg) { case "msg_gm_chat_message": return ChatUtility.handleGMChatMessage(sockmsg.data); + case "msg_app_astrologie_refresh": + return Hooks.callAll(APP_ASTROLOGIE_REFRESH); case "msg_request_nombre_astral": return game.system.rdd.calendrier.requestNombreAstral(sockmsg.data); - case "msg_response_nombre_astral": - return RdDUtility.responseNombreAstral(sockmsg.data); case "msg_tmr_move": let actor = game.actors.get(sockmsg.data.actorId); if (actor.isOwner || game.user.isGM) { diff --git a/module/sommeil/app-astrologie.js b/module/sommeil/app-astrologie.js index b407dad3..eca4accb 100644 --- a/module/sommeil/app-astrologie.js +++ b/module/sommeil/app-astrologie.js @@ -31,7 +31,6 @@ export class AppAstrologie extends Application { constructor(actor, options = {}) { super(options); this.actor = actor; - this.hookReference = Hooks.on(APP_ASTROLOGIE_REFRESH, () => this.refreshAstrologie()); } getData(options) { @@ -85,9 +84,10 @@ export class AppAstrologie extends Application { const nbAstral = calendrier.getNombreAstral() const heures = RdDTimestamp.heures(); return { - ajustementsActors:game.actors.filter(actor => actor.isPersonnageJoueur()) + ajustementsActors: game.actors.filter(actor => actor.isPersonnageJoueur()) .map(actor => this.getAjustementActor(actor, nbAstral, heures)), - nombresAstraux: calendrier.getNombresAstraux().map(na => this.getDetailNombreAstral(na)) + nombresAstraux: game.system.rdd.calendrier.getNombresAstraux() + .map(na => this.getDetailNombreAstral(na)) } } return {} @@ -107,12 +107,15 @@ export class AppAstrologie extends Application { const detail = foundry.utils.duplicate(nombreAstral); const timestamp = new RdDTimestamp({ indexDate: nombreAstral.index }); detail.date = { mois: timestamp.mois, jour: timestamp.jour + 1 }; - detail.valeursFausses.forEach(fausse => fausse.actorName = game.actors.get(fausse.actorId).name ?? "Inconnu"); + detail.lectures.forEach(lecture => lecture.actorName = game.actors.get(lecture.actorId).name ?? "Inconnu"); return detail; } /* -------------------------------------------- */ activateListeners(html) { + if (!this.hookReference){ + this.hookReference = Hooks.on(APP_ASTROLOGIE_REFRESH, () => this.refreshAstrologie()); + } super.activateListeners(html); this.html = html; this.html.find('select[name="signe-astral"]').change(event => { @@ -145,8 +148,7 @@ export class AppAstrologie extends Application { /* -------------------------------------------- */ async onRebuild() { - game.system.rdd.calendrier.resetNombresAstraux(); - + await game.system.rdd.calendrier.resetNombresAstraux(); await game.system.rdd.calendrier.rebuildNombresAstraux(); } @@ -195,6 +197,8 @@ export class AppAstrologie extends Application { } refreshAstrologie() { + this.count = (this.count ?? 0)+1 + console.log(`Refreshing ${this.count}`); this.render(true) } diff --git a/module/time/rdd-calendrier.js b/module/time/rdd-calendrier.js index b96f640b..0ff8022c 100644 --- a/module/time/rdd-calendrier.js +++ b/module/time/rdd-calendrier.js @@ -51,7 +51,6 @@ export class RdDCalendrier extends Application { this.timestamp = RdDTimestamp.getWorldTime(); if (Misc.isUniqueConnectedGM()) { // Uniquement si GM RdDTimestamp.setWorldTime(this.timestamp); - this.nombresAstraux = this.getNombresAstraux(); this.rebuildNombresAstraux(); // Ensure always up-to-date } Hooks.on('updateSetting', async (setting, update, options, id) => this.onUpdateSetting(setting, update, options, id)); @@ -108,7 +107,10 @@ export class RdDCalendrier extends Application { this.timestamp = RdDTimestamp.getWorldTime(); this.positionAiguilles() this.render(false); - Hooks.callAll(APP_ASTROLOGIE_REFRESH); + Hooks.callAll(APP_ASTROLOGIE_REFRESH) + } + if (setting.key == SYSTEM_RDD + '.' + "liste-nombre-astral") { + Hooks.callAll(APP_ASTROLOGIE_REFRESH) } } @@ -167,7 +169,11 @@ export class RdDCalendrier extends Application { /* -------------------------------------------- */ getNombresAstraux() { - return game.settings.get(SYSTEM_RDD, "liste-nombre-astral") ?? []; + return game.settings.get(SYSTEM_RDD, "liste-nombre-astral") ?? [] + } + + async setNombresAstraux(nombresAstraux) { + await game.settings.set(SYSTEM_RDD, "liste-nombre-astral", nombresAstraux) } /* -------------------------------------------- */ @@ -225,20 +231,15 @@ export class RdDCalendrier extends Application { const nombreAstral = await RdDDice.rollTotal("1dh", { showDice: HIDE_DICE, rollMode: "selfroll" }); return { nombreAstral: nombreAstral, - valeursFausses: [], + lectures: [], index: indexDate } } /* -------------------------------------------- */ - resetNombresAstraux() { - this.nombresAstraux = []; - game.settings.set(SYSTEM_RDD, "liste-nombre-astral", []); - - game.socket.emit(SYSTEM_SOCKET_ID, { - msg: "msg_reset_nombre_astral", - data: {} - }); + async resetNombresAstraux() { + await Promise.all(game.actors.filter(it => it.type == "personnage").map(async it => await it.deleteNombresAstraux())) + await this.setNombresAstraux([]) } /** @@ -251,39 +252,30 @@ export class RdDCalendrier extends Application { if (indexDate == undefined) { indexDate = this.timestamp.indexDate; } - this.nombresAstraux = this.getNombresAstraux(); - let astralData = this.nombresAstraux.find((nombreAstral, i) => nombreAstral.index == indexDate); + const nombresAstraux = this.getNombresAstraux() + let astralData = nombresAstraux.find(it => it.index == indexDate); return astralData?.nombreAstral ?? 0; } /* -------------------------------------------- */ async rebuildNombresAstraux() { if (Misc.isUniqueConnectedGM()) { - let newList = []; + const nombresAstraux = this.getNombresAstraux() + let newNombresAstraux = []; for (let i = 0; i < MAX_NOMBRE_ASTRAL; i++) { let dayIndex = this.timestamp.indexDate + i; - let na = this.nombresAstraux.find(n => n.index == dayIndex); + let na = nombresAstraux.find(it => it.index == dayIndex); if (na) { - newList[i] = na; + newNombresAstraux[i] = na; } else { - newList[i] = await this.ajouterNombreAstral(dayIndex); + newNombresAstraux[i] = await this.ajouterNombreAstral(dayIndex); } } - this.nombresAstraux = newList; - game.settings.set(SYSTEM_RDD, "liste-nombre-astral", newList); game.actors.filter(it => it.isPersonnage()).forEach(actor => actor.supprimerAnciensNombresAstraux()); - this.notifyChangeNombresAstraux(); + await this.setNombresAstraux(newNombresAstraux); } } - notifyChangeNombresAstraux() { - Hooks.callAll(APP_ASTROLOGIE_REFRESH); - game.socket.emit(SYSTEM_SOCKET_ID, { - msg: "msg_refresh_nombre_astral", - data: {} - }); - } - /* -------------------------------------------- */ async setNewTimestamp(newTimestamp) { const oldTimestamp = this.timestamp; @@ -373,25 +365,22 @@ export class RdDCalendrier extends Application { request.nbAstral = await RdDDice.rollTotal("1dhr" + request.nbAstral, { rollMode: "selfroll", showDice: HIDE_DICE }); - // Mise à jour des nombres astraux du joueur - this.addNbAstralIncorect(request.id, request.date, request.nbAstral); - } - - if (Misc.getActiveUser(request.userId)?.isGM) { - RdDUtility.responseNombreAstral(request); - } else { - game.socket.emit(SYSTEM_SOCKET_ID, { - msg: "msg_response_nombre_astral", - data: request - }); } + // Mise à jour des nombres astraux du joueur + await this.addNbAstralJoueur(actor, request.date, request.nbAstral, request.isValid) + Hooks.callAll(APP_ASTROLOGIE_REFRESH) + game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_app_astrologie_refresh", data: {} }) } } - - addNbAstralIncorect(actorId, date, nbAstral) { - const astralData = this.nombresAstraux.find((nombreAstral, i) => nombreAstral.index == date); - astralData.valeursFausses.push({ actorId: actorId, nombreAstral: nbAstral }); - game.settings.set(SYSTEM_RDD, "liste-nombre-astral", this.nombresAstraux); + + async addNbAstralJoueur(actor, date, nbAstral, isValid) { + const nombresAstraux = this.getNombresAstraux() + const astralData = nombresAstraux.find(it => it.index == date) + if (astralData) { + astralData.lectures.push({ actorId: actor.id, nombreAstral: nbAstral }); + await this.setNombresAstraux(nombresAstraux); + await actor.ajouteNombreAstral(date, nbAstral, isValid); + } } /* -------------------------------------------- */ diff --git a/templates/sommeil/astrologie-gardien.hbs b/templates/sommeil/astrologie-gardien.hbs index 208581e0..592b29b5 100644 --- a/templates/sommeil/astrologie-gardien.hbs +++ b/templates/sommeil/astrologie-gardien.hbs @@ -15,8 +15,8 @@
    {{nba.nombreAstral}} - {{#each nba.valeursFausses as |fausse|}} -
  1. {{fausse.actorName}} - {{fausse.nombreAstral}}
  2. + {{#each nba.lectures as |lecture|}} +
  3. {{lecture.actorName}} - {{lecture.nombreAstral}}
  4. {{/each}}