From 95523edb60eadb029d3cdd2dab7300dddb23c854 Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Fri, 28 Jul 2023 10:47:44 -0300 Subject: [PATCH] fix comparing data 'Of Type'. cast values on expressions to usable C3 types. closes #27 --- plugin/addon.json | 2 +- plugin/c3runtime/conditions.js | 6 +++--- plugin/c3runtime/expressions.js | 12 ++++++------ plugin/c3runtime/instance.js | 23 ++++++++++++++++++++--- plugin/plugin.js | 2 +- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/plugin/addon.json b/plugin/addon.json index 48da71f..c9db3ec 100755 --- a/plugin/addon.json +++ b/plugin/addon.json @@ -3,7 +3,7 @@ "type": "plugin", "name": "Colyseus SDK for Construct 3", "id": "Colyseus_SDK", - "version": "0.15.0.9", + "version": "0.15.0.10", "author": "Endel Dreyer", "website": "https://colyseus.io/", "documentation": "https://docs.colyseus.io", diff --git a/plugin/c3runtime/conditions.js b/plugin/c3runtime/conditions.js index 585d3d6..98f3e63 100755 --- a/plugin/c3runtime/conditions.js +++ b/plugin/c3runtime/conditions.js @@ -1,8 +1,8 @@ "use strict"; { - const operations = ['any', 'add', 'replace', 'remove']; const ANY = ":any:"; + const typesByIndex = ["string", "number", "boolean", "undefined", "object"]; function checkPath (lastPath, path) { if (lastPath === path) { @@ -86,8 +86,8 @@ CompareMessageType(cmp, type) { return C3.compare(this.lastType, cmp, type); }, CompareMessageValue(cmp, value) { return C3.compare(this.lastMessage, cmp, value); }, CompareMessageValueAt(path, cmp, value) { return C3.compare(this.getDeepVariable(path, this.lastMessage), cmp, value); }, - CompareMessageValueOfType(cmp, type) { return C3.compare(typeof (this.lastMessage), cmp, type); }, - CompareMessageValueAtOfType(path, cmp, type) { return C3.compare(typeof (this.getDeepVariable(path, this.lastMessage)), cmp, type); }, + CompareMessageValueOfType(cmp, type) { return C3.compare(typeof (this.lastMessage), cmp, typesByIndex[type]); }, + CompareMessageValueAtOfType(path, cmp, type) { return C3.compare(typeof (this.getDeepVariable(path, this.lastMessage)), cmp, typesByIndex[type]); }, // State/Schema OnChangeAtPath(path) { return checkPath(this.lastPath, path); }, diff --git a/plugin/c3runtime/expressions.js b/plugin/c3runtime/expressions.js index edb98dc..4d3466b 100755 --- a/plugin/c3runtime/expressions.js +++ b/plugin/c3runtime/expressions.js @@ -15,20 +15,20 @@ JSON(data) { return JSON.stringify(eval(`(${data})`)); }, // Messages - MessageValue() { return this.lastMessage; }, - MessageValueAt(path) { return this.getDeepVariable(path, this.lastMessage); }, + MessageValue() { return this.castType(this.lastMessage); }, + MessageValueAt(path) { return this.castType(this.getDeepVariable(path, this.lastMessage)); }, MessageType() { return this.lastType; }, MessageValueType() { return typeof (this.lastMessage); }, MessageValueAtType(path) { return typeof (this.getDeepVariable(path, this.lastMessage)); }, // State - State(variablePath) { return this.getDeepVariable(variablePath, (this.room && this.room.state) || {}); }, + State(variablePath) { return this.castType(this.getDeepVariable(variablePath, (this.room && this.room.state) || {})); }, CurrentStatePath() { return this.lastPath; }, CurrentKey() { return this.lastKey; }, - CurrentValue() { return this.lastValue; }, - CurrentValueAt(path) { return this.getDeepVariable(path, this.lastValue); }, - PreviousValue() { return this.lastPreviousValue; }, + CurrentValue() { return this.castType(this.lastValue); }, + CurrentValueAt(path) { return this.castType(this.getDeepVariable(path, this.lastValue)); }, + PreviousValue() { return this.castType(this.lastPreviousValue); }, // Collections CurrentItemsCount() { diff --git a/plugin/c3runtime/instance.js b/plugin/c3runtime/instance.js index 19ec43f..32911d8 100755 --- a/plugin/c3runtime/instance.js +++ b/plugin/c3runtime/instance.js @@ -227,9 +227,26 @@ value = ""; } - return (typeof(value) === "boolean") - ? Number(value) // convert boolean to number - : (value ?? ""); // everything else (in case of undefined, convert it to empty string) + return value; + } + + castType (value) { + // + // Some types are not easily usable by C3, so we need to cast them. + // + switch (typeof(value)) { + case "boolean": + // convert boolean to number + return Number(value); + + case "object": + return JSON.stringify(value); + + default: + // Everything else + // (in case of undefined, convert it to empty string) + return value || ""; + } } }; diff --git a/plugin/plugin.js b/plugin/plugin.js index a75cadc..26812ef 100755 --- a/plugin/plugin.js +++ b/plugin/plugin.js @@ -12,7 +12,7 @@ const PLUGIN_ID = "Colyseus_SDK"; //////////////////////////////////////////// - const PLUGIN_VERSION = "0.15.0.9"; + const PLUGIN_VERSION = "0.15.0.10"; const PLUGIN_CATEGORY = "web"; const PLUGIN_AUTHOR = "Endel Dreyer";