UNPKG

hw-server

Version:
206 lines (178 loc) 5.16 kB
/*global module,setTimeout,clearTimeout,setInterval,clearInterval,require*/ var homework = undefined; var utils = undefined; function Event(name, cmp, value) { if (arguments.length != 3) { throw "Variable event needs three arguments, name, comparator and value"; } if (!(name in variables.variables)) { throw "Variable " + name + " does not exist"; } if (cmp !== "==" && cmp !== "!=") { throw "Variable invalid comparator " + cmp; } this._initOns(); this._name = name; this._cmp = cmp; this._value = value; variables.on("changed", (name) => { if (this._name == name) { if ((this._cmp == "==" && variables.variables[name] == this._value) || (this._cmp == "!=" && variables.variables[name] != this._value)) { homework.Console.log("triggering variable event", this._name, this._value); this._emit("triggered", this); } } }); variables.on("destroyed", (name) => { if (this._name == name) { this._emit("cancel"); } }); } Event.prototype = { _name: undefined, _value: undefined, _cmp: undefined, check: function() { if (this._name in variables.variables) { if (this._cmp == "==") return variables.variables[this._name] == this._value; else return variables.variables[this._name] != this._value; } return false; }, serialize: function() { return { type: "VariableEvent", name: this._name, cmp: this._cmp, value: this._value }; }, format: function() { return ["Variable", this._name, this._cmp, this._value]; } }; function Action(name, value) { if (arguments.length != 2) { throw "Variable action needs at least two arguments, name and value"; } if (!(name in variables.variables)) { throw "Variable " + name + " does not exist"; } this._initOns(); this._name = name; this._value = value; variables.on("destroyed", (name) => { if (this._name == name) { this._emit("cancel"); } }); } Action.prototype = { trigger: function(pri) { if (pri === homework.rulePriorities.High) { homework.Console.log("trigger variable action", this._name, this._value); variables.change(this._name, this._value); } }, serialize: function() { return { type: "VariableAction", name: this._name, value: this._value }; }, format: function() { return ["Variable", this._name, this._value]; } }; function eventCompleter() { if (!arguments.length) { return { type: "array", values: Object.keys(variables.variables) }; } else if (arguments.length === 1 || (arguments.length === 2 && arguments[1].length < 2)) { return { type: "array", values: ["==", "!="] }; } else if (arguments.length === 2) { return { type: "string", values: [] }; } return { values: [] }; } function eventDeserializer(e) { if (!(typeof e === "object")) return null; if (e.type !== "VariableEvent") return null; var event; try { variables.create(e.name); event = new Event(e.name, e.cmp, e.value); } catch (e) { return null; } return event; } function actionCompleter() { if (!arguments.length) { return { type: "array", values: Object.keys(variables.variables) }; } else if (arguments.length === 1) { return { type: "string", values: [] }; } return { values: [] }; } function actionDeserializer(e) { if (!(typeof e === "object")) return null; if (e.type !== "VariableAction") return null; var event; try { variables.create(e.name); event = new Action(e.name, e.value); } catch (e) { return null; } return event; } const variables = { variables: Object.create(null), init: function(hw) { homework = hw; utils = hw.utils; utils.onify(Event.prototype); utils.onify(Action.prototype); homework.registerEvent("Variable", Event, eventCompleter, eventDeserializer); homework.registerAction("Variable", Action, actionCompleter, actionDeserializer); homework.loadRules(); utils.onify(this); this._initOns(); }, names: function() { return Object.keys(this.variables); }, create: function(name) { if (name in this.variables) return false; this.variables[name] = undefined; return true; }, destroy: function(name) { if (name in this.variables) { delete this.variables[name]; this._emit("destroyed", name); return true; } return false; }, change: function(name, value) { if (!(name in this.variables)) { return false; } this.variables[name] = value; this._emit("changed", name); return true; } }; module.exports = variables;