From 88f2bfdf3974815c67e9b485d960c26f1498697b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 23 Jul 2021 12:19:47 +0200 Subject: [PATCH 1/2] Taking into account persist property The "persist" property was not taken into account and all variables were stored in DB. This change makes sure only variables tagged with "persist" are actually persisted. --- back/src/Services/VariablesManager.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/back/src/Services/VariablesManager.ts b/back/src/Services/VariablesManager.ts index c6b6f5fe..e8aaef25 100644 --- a/back/src/Services/VariablesManager.ts +++ b/back/src/Services/VariablesManager.ts @@ -51,6 +51,17 @@ export class VariablesManager { } const variables = await variablesRepository.loadVariables(this.roomUrl); for (const key in variables) { + // Let's only set variables if they are in the map (if the map has changed, maybe stored variables do not exist anymore) + if (this.variableObjects) { + const variableObject = this.variableObjects.get(key); + if (variableObject === undefined) { + continue; + } + if (!variableObject.persist) { + continue; + } + } + this._variables.set(key, variables[key]); } return this; @@ -146,8 +157,9 @@ export class VariablesManager { */ setVariable(name: string, value: string, user: User): string | undefined | false { let readableBy: string | undefined; + let variableObject: Variable | undefined; if (this.variableObjects) { - const variableObject = this.variableObjects.get(name); + variableObject = this.variableObjects.get(name); if (variableObject === undefined) { throw new Error('Trying to set a variable "' + name + '" that is not defined as an object in the map.'); } @@ -175,9 +187,13 @@ export class VariablesManager { } this._variables.set(name, value); - variablesRepository - .saveVariable(this.roomUrl, name, value) - .catch((e) => console.error("Error while saving variable in Redis:", e)); + + if (variableObject !== undefined && variableObject.persist) { + variablesRepository + .saveVariable(this.roomUrl, name, value) + .catch((e) => console.error("Error while saving variable in Redis:", e)); + } + return readableBy; } From c1cd464a7bdd312bb8cc8c7ed1b74e2a591f9877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 23 Jul 2021 12:26:18 +0200 Subject: [PATCH 2/2] Fixing reference to deprecated method in doc --- docs/maps/scripting.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/maps/scripting.md b/docs/maps/scripting.md index 5f645b81..5be57ee1 100644 --- a/docs/maps/scripting.md +++ b/docs/maps/scripting.md @@ -55,10 +55,10 @@ Start by testing this with a simple message sent to the chat. **script.js** ```javascript -WA.sendChatMessage('Hello world', 'Mr Robot'); +WA.chat.sendChatMessage('Hello world', 'Mr Robot'); ``` -The `WA` objects contains a number of useful methods enabling you to interact with the WorkAdventure game. For instance, `WA.sendChatMessage` opens the chat and adds a message in it. +The `WA` objects contains a number of useful methods enabling you to interact with the WorkAdventure game. For instance, `WA.chat.sendChatMessage` opens the chat and adds a message in it. In your browser console, when you open the map, the chat message should be displayed right away.