2021-07-02 11:31:44 +02:00
|
|
|
/**
|
|
|
|
* Handles variables shared between the scripting API and the server.
|
|
|
|
*/
|
|
|
|
import type {RoomConnection} from "../../Connexion/RoomConnection";
|
|
|
|
import {iframeListener} from "../../Api/IframeListener";
|
|
|
|
import type {Subscription} from "rxjs";
|
|
|
|
import type {GameMap} from "./GameMap";
|
2021-07-05 17:25:23 +02:00
|
|
|
import type {ITile, ITiledMapObject} from "../Map/ITiledMap";
|
|
|
|
import type {Var} from "svelte/types/compiler/interfaces";
|
2021-07-07 17:17:28 +02:00
|
|
|
import {init} from "svelte/internal";
|
2021-07-05 17:25:23 +02:00
|
|
|
|
|
|
|
interface Variable {
|
2021-07-06 10:58:12 +02:00
|
|
|
defaultValue: unknown,
|
|
|
|
readableBy?: string,
|
|
|
|
writableBy?: string,
|
2021-07-05 17:25:23 +02:00
|
|
|
}
|
2021-07-02 11:31:44 +02:00
|
|
|
|
|
|
|
export class SharedVariablesManager {
|
|
|
|
private _variables = new Map<string, unknown>();
|
2021-07-05 17:25:23 +02:00
|
|
|
private variableObjects: Map<string, Variable>;
|
2021-07-02 11:31:44 +02:00
|
|
|
|
2021-07-07 17:17:28 +02:00
|
|
|
constructor(private roomConnection: RoomConnection, private gameMap: GameMap, serverVariables: Map<string, unknown>) {
|
2021-07-02 11:31:44 +02:00
|
|
|
// We initialize the list of variable object at room start. The objects cannot be edited later
|
|
|
|
// (otherwise, this would cause a security issue if the scripting API can edit this list of objects)
|
|
|
|
this.variableObjects = SharedVariablesManager.findVariablesInMap(gameMap);
|
|
|
|
|
2021-07-05 17:25:23 +02:00
|
|
|
// Let's initialize default values
|
|
|
|
for (const [name, variableObject] of this.variableObjects.entries()) {
|
|
|
|
this._variables.set(name, variableObject.defaultValue);
|
|
|
|
}
|
|
|
|
|
2021-07-07 17:17:28 +02:00
|
|
|
// Override default values with the variables from the server:
|
|
|
|
for (const [name, value] of serverVariables) {
|
|
|
|
this._variables.set(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
roomConnection.onSetVariable((name, value) => {
|
|
|
|
console.log('Set Variable received from server');
|
|
|
|
this._variables.set(name, value);
|
|
|
|
|
|
|
|
// On server change, let's notify the iframes
|
|
|
|
iframeListener.setVariable({
|
|
|
|
key: name,
|
|
|
|
value: value,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-07-02 11:31:44 +02:00
|
|
|
// When a variable is modified from an iFrame
|
2021-07-05 18:29:34 +02:00
|
|
|
iframeListener.registerAnswerer('setVariable', (event) => {
|
2021-07-02 11:31:44 +02:00
|
|
|
const key = event.key;
|
|
|
|
|
2021-07-06 10:58:12 +02:00
|
|
|
const object = this.variableObjects.get(key);
|
|
|
|
|
|
|
|
if (object === undefined) {
|
2021-07-02 11:31:44 +02:00
|
|
|
const errMsg = 'A script is trying to modify variable "'+key+'" but this variable is not defined in the map.' +
|
|
|
|
'There should be an object in the map whose name is "'+key+'" and whose type is "variable"';
|
|
|
|
console.error(errMsg);
|
|
|
|
throw new Error(errMsg);
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:58:12 +02:00
|
|
|
if (object.writableBy && !this.roomConnection.hasTag(object.writableBy)) {
|
|
|
|
const errMsg = 'A script is trying to modify variable "'+key+'" but this variable is only writable for users with tag "'+object.writableBy+'".';
|
|
|
|
console.error(errMsg);
|
|
|
|
throw new Error(errMsg);
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:31:44 +02:00
|
|
|
this._variables.set(key, event.value);
|
2021-07-07 17:17:28 +02:00
|
|
|
|
|
|
|
// Dispatch to the room connection.
|
2021-07-06 10:58:12 +02:00
|
|
|
this.roomConnection.emitSetVariableEvent(key, event.value);
|
2021-07-02 11:31:44 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:25:23 +02:00
|
|
|
private static findVariablesInMap(gameMap: GameMap): Map<string, Variable> {
|
|
|
|
const objects = new Map<string, Variable>();
|
2021-07-02 11:31:44 +02:00
|
|
|
for (const layer of gameMap.getMap().layers) {
|
|
|
|
if (layer.type === 'objectgroup') {
|
|
|
|
for (const object of layer.objects) {
|
|
|
|
if (object.type === 'variable') {
|
2021-07-05 18:48:26 +02:00
|
|
|
if (object.template) {
|
|
|
|
console.warn('Warning, a variable object is using a Tiled "template". WorkAdventure does not support objects generated from Tiled templates.')
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:31:44 +02:00
|
|
|
// We store a copy of the object (to make it immutable)
|
2021-07-05 17:25:23 +02:00
|
|
|
objects.set(object.name, this.iTiledObjectToVariable(object));
|
2021-07-02 11:31:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
2021-07-05 17:25:23 +02:00
|
|
|
private static iTiledObjectToVariable(object: ITiledMapObject): Variable {
|
|
|
|
const variable: Variable = {
|
|
|
|
defaultValue: undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
if (object.properties) {
|
|
|
|
for (const property of object.properties) {
|
2021-07-06 10:58:12 +02:00
|
|
|
const value = property.value;
|
|
|
|
switch (property.name) {
|
|
|
|
case 'default':
|
|
|
|
variable.defaultValue = value;
|
|
|
|
break;
|
|
|
|
case 'writableBy':
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
throw new Error('The writableBy property of variable "'+object.name+'" must be a string');
|
|
|
|
}
|
|
|
|
if (value) {
|
|
|
|
variable.writableBy = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'readableBy':
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
throw new Error('The readableBy property of variable "'+object.name+'" must be a string');
|
|
|
|
}
|
|
|
|
if (value) {
|
|
|
|
variable.readableBy = value;
|
|
|
|
}
|
|
|
|
break;
|
2021-07-05 17:25:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return variable;
|
|
|
|
}
|
2021-07-02 11:31:44 +02:00
|
|
|
|
|
|
|
public close(): void {
|
2021-07-05 18:29:34 +02:00
|
|
|
iframeListener.unregisterAnswerer('setVariable');
|
2021-07-02 11:31:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get variables(): Map<string, unknown> {
|
|
|
|
return this._variables;
|
|
|
|
}
|
|
|
|
}
|