Creates player state and uses it to get and set values from local storage
This commit is contained in:
parent
89cd292527
commit
bf69b55e99
10 changed files with 153 additions and 159 deletions
|
@ -1153,19 +1153,6 @@ ${escapedMessage}
|
|||
};
|
||||
});
|
||||
|
||||
//TODO : move Player Properties related-code
|
||||
iframeListener.registerAnswerer("setPlayerProperty", (event) => {
|
||||
localUserStore.setUserProperty(event.propertyName, event.propertyValue as string);
|
||||
});
|
||||
|
||||
iframeListener.registerAnswerer("getPlayerProperty", (event) => {
|
||||
return {
|
||||
propertyName: event,
|
||||
propertyValue: localUserStore.getUserProperty(event),
|
||||
};
|
||||
});
|
||||
//END TODO
|
||||
|
||||
iframeListener.registerAnswerer("getState", async () => {
|
||||
// The sharedVariablesManager is not instantiated before the connection is established. So we need to wait
|
||||
// for the connection to send back the answer.
|
||||
|
@ -1178,6 +1165,7 @@ ${escapedMessage}
|
|||
roomId: this.roomUrl,
|
||||
tags: this.connection ? this.connection.getAllTags() : [],
|
||||
variables: this.sharedVariablesManager.variables,
|
||||
playerVariables: localUserStore.getAllUserProperties(),
|
||||
};
|
||||
});
|
||||
this.iframeSubscriptionList.push(
|
||||
|
@ -1267,6 +1255,22 @@ ${escapedMessage}
|
|||
})
|
||||
);
|
||||
|
||||
iframeListener.registerAnswerer("setVariable", (event, source) => {
|
||||
switch (event.target) {
|
||||
case "global": {
|
||||
this.sharedVariablesManager.setVariable(event, source);
|
||||
break;
|
||||
}
|
||||
case "player": {
|
||||
localUserStore.setUserProperty(event.key, event.value);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const _exhaustiveCheck: never = event.target;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
iframeListener.registerAnswerer("removeActionMessage", (message) => {
|
||||
layoutManagerActionStore.removeAction(message.uuid);
|
||||
});
|
||||
|
@ -1391,6 +1395,7 @@ ${escapedMessage}
|
|||
iframeListener.unregisterAnswerer("removeActionMessage");
|
||||
iframeListener.unregisterAnswerer("openCoWebsite");
|
||||
iframeListener.unregisterAnswerer("getCoWebsites");
|
||||
iframeListener.unregisterAnswerer("setVariable");
|
||||
this.sharedVariablesManager?.close();
|
||||
this.embeddedWebsiteManager?.close();
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import { iframeListener } from "../../Api/IframeListener";
|
|||
import type { GameMap } from "./GameMap";
|
||||
import type { ITiledMapLayer, ITiledMapObject, ITiledMapObjectLayer } from "../Map/ITiledMap";
|
||||
import { GameMapProperties } from "./GameMapProperties";
|
||||
import type { SetVariableEvent } from "../../Api/Events/SetVariableEvent";
|
||||
|
||||
interface Variable {
|
||||
defaultValue: unknown;
|
||||
|
@ -48,51 +49,51 @@ export class SharedVariablesManager {
|
|||
iframeListener.setVariable({
|
||||
key: name,
|
||||
value: value,
|
||||
target: "global",
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// When a variable is modified from an iFrame
|
||||
iframeListener.registerAnswerer("setVariable", (event, source) => {
|
||||
const key = event.key;
|
||||
public setVariable(event: SetVariableEvent, source: MessageEventSource | null): void {
|
||||
const key = event.key;
|
||||
|
||||
const object = this.variableObjects.get(key);
|
||||
const object = this.variableObjects.get(key);
|
||||
|
||||
if (object === undefined) {
|
||||
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);
|
||||
}
|
||||
if (object === undefined) {
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
// Let's stop any propagation of the value we set is the same as the existing value.
|
||||
if (JSON.stringify(event.value) === JSON.stringify(this._variables.get(key))) {
|
||||
return;
|
||||
}
|
||||
// Let's stop any propagation of the value we set is the same as the existing value.
|
||||
if (JSON.stringify(event.value) === JSON.stringify(this._variables.get(key))) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._variables.set(key, event.value);
|
||||
this._variables.set(key, event.value);
|
||||
|
||||
// Dispatch to the room connection.
|
||||
this.roomConnection.emitSetVariableEvent(key, event.value);
|
||||
// Dispatch to the room connection.
|
||||
this.roomConnection.emitSetVariableEvent(key, event.value);
|
||||
|
||||
// Dispatch to other iframes
|
||||
iframeListener.dispatchVariableToOtherIframes(key, event.value, source);
|
||||
});
|
||||
// Dispatch to other iframes
|
||||
iframeListener.dispatchVariableToOtherIframes(key, event.value, source);
|
||||
}
|
||||
|
||||
private static findVariablesInMap(gameMap: GameMap): Map<string, Variable> {
|
||||
|
@ -164,10 +165,6 @@ export class SharedVariablesManager {
|
|||
return variable;
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
iframeListener.unregisterAnswerer("setVariable");
|
||||
}
|
||||
|
||||
get variables(): Map<string, unknown> {
|
||||
return this._variables;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue