diff --git a/package-lock.json b/package-lock.json index 182f532..7131bf3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,9 @@ "node-sass-middleware": "0.11.0", "pug": "2.0.0-beta11", "socket.io": "^3.1.2" + }, + "devDependencies": { + "node-key-sender": "^1.0.11" } }, "node_modules/@types/babel-types": { @@ -1617,6 +1620,12 @@ "node": ">= 0.8.0" } }, + "node_modules/node-key-sender": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/node-key-sender/-/node-key-sender-1.0.11.tgz", + "integrity": "sha512-vv2IXd8QdZBFYXaIy02uy2rK6EKj+tOTEuoTxJKS9l8zw8Cz6DeLffR8ompj7N2A3h6XK7aiy+YAcTaeOqwp2Q==", + "dev": true + }, "node_modules/node-sass": { "version": "4.14.1", "resolved": "https://registry.npmjs.org/node-sass/-/node-sass-4.14.1.tgz", @@ -4284,6 +4293,12 @@ "which": "1" } }, + "node-key-sender": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/node-key-sender/-/node-key-sender-1.0.11.tgz", + "integrity": "sha512-vv2IXd8QdZBFYXaIy02uy2rK6EKj+tOTEuoTxJKS9l8zw8Cz6DeLffR8ompj7N2A3h6XK7aiy+YAcTaeOqwp2Q==", + "dev": true + }, "node-sass": { "version": "4.14.1", "resolved": "https://registry.npmjs.org/node-sass/-/node-sass-4.14.1.tgz", diff --git a/package.json b/package.json index 2424ee2..59d6f1b 100644 --- a/package.json +++ b/package.json @@ -15,5 +15,8 @@ "node-sass-middleware": "0.11.0", "pug": "2.0.0-beta11", "socket.io": "^3.1.2" + }, + "devDependencies": { + "node-key-sender": "^1.0.11" } } diff --git a/public/javascripts/settings/decks.js b/public/javascripts/settings/decks.js index 1ebfb79..a5b78d3 100644 --- a/public/javascripts/settings/decks.js +++ b/public/javascripts/settings/decks.js @@ -159,6 +159,8 @@ function customFields(values) { d.classList.add("input-field"); d.insertAdjacentElement("beforeend", e); d.insertAdjacentHTML("beforeend", ``); + if (field.helper) + d.insertAdjacentHTML("beforeend", `${field.helper}`); customs.insertAdjacentElement("beforeend", d); if (values && name in values) { e.value = values[name]; diff --git a/sockets/trigger.js b/sockets/trigger.js index 444de9c..106c505 100644 --- a/sockets/trigger.js +++ b/sockets/trigger.js @@ -2,7 +2,7 @@ const db = require("../db.json"); const getSlot = require("../types").getSlot; module.exports = socket => { - return data => { + return async data => { let err = null; if (data && Array.isArray(data) && data.length === 3) { if (db.decks[data[0]]) { @@ -10,7 +10,10 @@ module.exports = socket => { if (el) { try { - el.trigger(socket); + if (el.trigger.constructor.name === "AsyncFunction") + await el.trigger(socket); + else + el.trigger(socket); } catch (exc) { err = "unknown"; console.error(exc) diff --git a/types/Keys.js b/types/Keys.js new file mode 100644 index 0000000..354f3cc --- /dev/null +++ b/types/Keys.js @@ -0,0 +1,54 @@ +const Base = require("./Base"); +const ks = require('node-key-sender'); + + +class Keys extends Base { + static name = "Keys"; + static type = "keys"; + static fields = { + keys: { + type: "text", + name: "Keys", + helper: "Key separated by a comma, if combo use +" + } + }; + + constructor(text, image = null, options = null) { + super(text, image, options); + } + + /** + * @override + */ + static staticToJSON() { + return super.staticToJSON(Keys.name, Keys.type, Keys.fields); + } + + /** + * @override + */ + toJSON() { + return super.toJSON(Keys.type) + } + + /** + * @override + */ + async trigger() { + let keys = this.options.keys.split(","); + let toSend = []; + + for (const key of keys) + if (key.match(/\S\+\S/)) + toSend.push([ks.sendCombination, key.split("+")]); + else if (toSend.length !== 0 && toSend[toSend.length-1][0] === ks.sendKeys) + toSend[toSend.length-1][1].push(key); + else + toSend.push([ks.sendKeys, [key]]); + + for (const send of toSend) + await send[0](send[1]); + } +} + +module.exports = Keys; diff --git a/types/index.js b/types/index.js index b93e8e8..60bcde2 100644 --- a/types/index.js +++ b/types/index.js @@ -2,7 +2,8 @@ const db = require("../db.json"); const types = { "execCommand": require("./ExecCommand"), - "deck": require("./Deck") + "deck": require("./Deck"), + "keys": require("./Keys") }; module.exports.types = types;