Improve docblock

This commit is contained in:
David Négrier 2021-07-22 11:24:30 +02:00
parent ae5617f3a0
commit 31811ab906

View file

@ -1,25 +1,29 @@
/** import type { RoomConnection } from "../../Connexion/RoomConnection";
* Handles variables shared between the scripting API and the server. import { iframeListener } from "../../Api/IframeListener";
*/ import type { Subscription } from "rxjs";
import type {RoomConnection} from "../../Connexion/RoomConnection"; import type { GameMap } from "./GameMap";
import {iframeListener} from "../../Api/IframeListener"; import type { ITile, ITiledMapObject } from "../Map/ITiledMap";
import type {Subscription} from "rxjs"; import type { Var } from "svelte/types/compiler/interfaces";
import type {GameMap} from "./GameMap"; import { init } from "svelte/internal";
import type {ITile, ITiledMapObject} from "../Map/ITiledMap";
import type {Var} from "svelte/types/compiler/interfaces";
import {init} from "svelte/internal";
interface Variable { interface Variable {
defaultValue: unknown, defaultValue: unknown;
readableBy?: string, readableBy?: string;
writableBy?: string, writableBy?: string;
} }
/**
* Stores variables and provides a bridge between scripts and the pusher server.
*/
export class SharedVariablesManager { export class SharedVariablesManager {
private _variables = new Map<string, unknown>(); private _variables = new Map<string, unknown>();
private variableObjects: Map<string, Variable>; private variableObjects: Map<string, Variable>;
constructor(private roomConnection: RoomConnection, private gameMap: GameMap, serverVariables: Map<string, unknown>) { constructor(
private roomConnection: RoomConnection,
private gameMap: GameMap,
serverVariables: Map<string, unknown>
) {
// We initialize the list of variable object at room start. The objects cannot be edited later // 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) // (otherwise, this would cause a security issue if the scripting API can edit this list of objects)
this.variableObjects = SharedVariablesManager.findVariablesInMap(gameMap); this.variableObjects = SharedVariablesManager.findVariablesInMap(gameMap);
@ -46,24 +50,34 @@ export class SharedVariablesManager {
iframeListener.setVariable({ iframeListener.setVariable({
key: name, key: name,
value: value, value: value,
}) });
}); });
// When a variable is modified from an iFrame // When a variable is modified from an iFrame
iframeListener.registerAnswerer('setVariable', (event) => { iframeListener.registerAnswerer("setVariable", (event) => {
const key = event.key; const key = event.key;
const object = this.variableObjects.get(key); const object = this.variableObjects.get(key);
if (object === undefined) { if (object === undefined) {
const errMsg = 'A script is trying to modify variable "'+key+'" but this variable is not defined in the map.' + const errMsg =
'There should be an object in the map whose name is "'+key+'" and whose type is "variable"'; '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); console.error(errMsg);
throw new Error(errMsg); throw new Error(errMsg);
} }
if (object.writableBy && !this.roomConnection.hasTag(object.writableBy)) { 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+'".'; 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); console.error(errMsg);
throw new Error(errMsg); throw new Error(errMsg);
} }
@ -78,11 +92,13 @@ export class SharedVariablesManager {
private static findVariablesInMap(gameMap: GameMap): Map<string, Variable> { private static findVariablesInMap(gameMap: GameMap): Map<string, Variable> {
const objects = new Map<string, Variable>(); const objects = new Map<string, Variable>();
for (const layer of gameMap.getMap().layers) { for (const layer of gameMap.getMap().layers) {
if (layer.type === 'objectgroup') { if (layer.type === "objectgroup") {
for (const object of layer.objects) { for (const object of layer.objects) {
if (object.type === 'variable') { if (object.type === "variable") {
if (object.template) { if (object.template) {
console.warn('Warning, a variable object is using a Tiled "template". WorkAdventure does not support objects generated from Tiled templates.') console.warn(
'Warning, a variable object is using a Tiled "template". WorkAdventure does not support objects generated from Tiled templates.'
);
} }
// We store a copy of the object (to make it immutable) // We store a copy of the object (to make it immutable)
@ -96,27 +112,31 @@ export class SharedVariablesManager {
private static iTiledObjectToVariable(object: ITiledMapObject): Variable { private static iTiledObjectToVariable(object: ITiledMapObject): Variable {
const variable: Variable = { const variable: Variable = {
defaultValue: undefined defaultValue: undefined,
}; };
if (object.properties) { if (object.properties) {
for (const property of object.properties) { for (const property of object.properties) {
const value = property.value; const value = property.value;
switch (property.name) { switch (property.name) {
case 'default': case "default":
variable.defaultValue = value; variable.defaultValue = value;
break; break;
case 'writableBy': case "writableBy":
if (typeof value !== 'string') { if (typeof value !== "string") {
throw new Error('The writableBy property of variable "'+object.name+'" must be a string'); throw new Error(
'The writableBy property of variable "' + object.name + '" must be a string'
);
} }
if (value) { if (value) {
variable.writableBy = value; variable.writableBy = value;
} }
break; break;
case 'readableBy': case "readableBy":
if (typeof value !== 'string') { if (typeof value !== "string") {
throw new Error('The readableBy property of variable "'+object.name+'" must be a string'); throw new Error(
'The readableBy property of variable "' + object.name + '" must be a string'
);
} }
if (value) { if (value) {
variable.readableBy = value; variable.readableBy = value;
@ -130,7 +150,7 @@ export class SharedVariablesManager {
} }
public close(): void { public close(): void {
iframeListener.unregisterAnswerer('setVariable'); iframeListener.unregisterAnswerer("setVariable");
} }
get variables(): Map<string, unknown> { get variables(): Map<string, unknown> {