Merge pull request #1310 from thecodingmachine/fix_persist_property

Taking into account persist property
This commit is contained in:
David Négrier 2021-07-23 12:27:08 +02:00 committed by GitHub
commit 5a56c20221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -51,6 +51,17 @@ export class VariablesManager {
} }
const variables = await variablesRepository.loadVariables(this.roomUrl); const variables = await variablesRepository.loadVariables(this.roomUrl);
for (const key in variables) { 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]); this._variables.set(key, variables[key]);
} }
return this; return this;
@ -146,8 +157,9 @@ export class VariablesManager {
*/ */
setVariable(name: string, value: string, user: User): string | undefined | false { setVariable(name: string, value: string, user: User): string | undefined | false {
let readableBy: string | undefined; let readableBy: string | undefined;
let variableObject: Variable | undefined;
if (this.variableObjects) { if (this.variableObjects) {
const variableObject = this.variableObjects.get(name); variableObject = this.variableObjects.get(name);
if (variableObject === undefined) { if (variableObject === undefined) {
throw new Error('Trying to set a variable "' + name + '" that is not defined as an object in the map.'); 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); this._variables.set(name, value);
variablesRepository
.saveVariable(this.roomUrl, name, value) if (variableObject !== undefined && variableObject.persist) {
.catch((e) => console.error("Error while saving variable in Redis:", e)); variablesRepository
.saveVariable(this.roomUrl, name, value)
.catch((e) => console.error("Error while saving variable in Redis:", e));
}
return readableBy; return readableBy;
} }

View File

@ -55,10 +55,10 @@ Start by testing this with a simple message sent to the chat.
**script.js** **script.js**
```javascript ```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. In your browser console, when you open the map, the chat message should be displayed right away.