Skip to content

Commit

Permalink
fix comparing data 'Of Type'. cast values on expressions to usable C3…
Browse files Browse the repository at this point in the history
… types. closes #27
  • Loading branch information
endel committed Jul 28, 2023
1 parent 2e7fbdc commit 95523ed
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plugin/addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions plugin/c3runtime/conditions.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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); },
Expand Down
12 changes: 6 additions & 6 deletions plugin/c3runtime/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
23 changes: 20 additions & 3 deletions plugin/c3runtime/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "";
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit 95523ed

Please sign in to comment.