Skip to content

Commit 36765a1

Browse files
committed
Added fuzzy match for switch scenes
1 parent 9591a71 commit 36765a1

File tree

6 files changed

+114
-13
lines changed

6 files changed

+114
-13
lines changed

lib/components/Chat.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var _fs = _interopRequireDefault(require("fs"));
1313

1414
var _signale = _interopRequireDefault(require("signale"));
1515

16+
var _fastFuzzy = require("fast-fuzzy");
17+
1618
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1719

1820
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
@@ -420,33 +422,40 @@ function () {
420422
var _switch2 = _asyncToGenerator(
421423
/*#__PURE__*/
422424
regeneratorRuntime.mark(function _callee4(sceneName) {
425+
var res;
423426
return regeneratorRuntime.wrap(function _callee4$(_context4) {
424427
while (1) {
425428
switch (_context4.prev = _context4.next) {
426429
case 0:
427-
_context4.prev = 0;
428-
_context4.next = 3;
430+
res = (0, _fastFuzzy.search)(sceneName, this.obsProps.scenes, {
431+
keySelector: function keySelector(obj) {
432+
return obj.name;
433+
}
434+
}); // switch scene
435+
436+
_context4.prev = 1;
437+
_context4.next = 4;
429438
return this.obs.setCurrentScene({
430-
"scene-name": sceneName
439+
"scene-name": res[0].name
431440
});
432441

433-
case 3:
434-
this.say("Scene successfully switched to \"".concat(sceneName, "\""));
435-
_context4.next = 10;
442+
case 4:
443+
this.say("Scene successfully switched to \"".concat(res[0].name, "\""));
444+
_context4.next = 11;
436445
break;
437446

438-
case 6:
439-
_context4.prev = 6;
440-
_context4.t0 = _context4["catch"](0);
447+
case 7:
448+
_context4.prev = 7;
449+
_context4.t0 = _context4["catch"](1);
441450
log.error(_context4.t0);
442451
this.say(_context4.t0.error);
443452

444-
case 10:
453+
case 11:
445454
case "end":
446455
return _context4.stop();
447456
}
448457
}
449-
}, _callee4, this, [[0, 6]]);
458+
}, _callee4, this, [[1, 7]]);
450459
}));
451460

452461
function _switch(_x2) {

lib/components/ObsSwitcher.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function (_EventEmitter) {
7777
_this.currentScene = null;
7878
_this.nginxSettings;
7979
_this.previousScene = _this.lowBitrateScene;
80+
_this.scenes = null;
8081

8182
_this.obs.connect({
8283
address: _this.address,
@@ -102,6 +103,8 @@ function (_EventEmitter) {
102103
return _this.heartbeat = heartbeat;
103104
});
104105

106+
_this.obs.on("ScenesChanged", _this.scenesChanged.bind(_assertThisInitialized(_assertThisInitialized(_this))));
107+
105108
log.info("Connecting & authenticating");
106109
return _this;
107110
}
@@ -115,6 +118,7 @@ function (_EventEmitter) {
115118
this.obs.SetHeartbeat({
116119
enable: true
117120
});
121+
this.getSceneList();
118122
this.interval = setInterval(
119123
/*#__PURE__*/
120124
_asyncToGenerator(
@@ -289,6 +293,43 @@ function (_EventEmitter) {
289293
value: function streamStarted() {
290294
this.obsStreaming = true;
291295
}
296+
}, {
297+
key: "getSceneList",
298+
value: function () {
299+
var _getSceneList = _asyncToGenerator(
300+
/*#__PURE__*/
301+
regeneratorRuntime.mark(function _callee3() {
302+
var list;
303+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
304+
while (1) {
305+
switch (_context3.prev = _context3.next) {
306+
case 0:
307+
_context3.next = 2;
308+
return this.obs.GetSceneList();
309+
310+
case 2:
311+
list = _context3.sent;
312+
this.scenes = list.scenes;
313+
314+
case 4:
315+
case "end":
316+
return _context3.stop();
317+
}
318+
}
319+
}, _callee3, this);
320+
}));
321+
322+
function getSceneList() {
323+
return _getSceneList.apply(this, arguments);
324+
}
325+
326+
return getSceneList;
327+
}()
328+
}, {
329+
key: "scenesChanged",
330+
value: function scenesChanged() {
331+
this.getSceneList();
332+
}
292333
}]);
293334

294335
return ObsSwitcher;

package-lock.json

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "Brian Spit",
1313
"license": "MIT",
1414
"dependencies": {
15+
"fast-fuzzy": "^1.8.4",
1516
"node-fetch": "^2.3.0",
1617
"obs-websocket-js": "^1.2.0",
1718
"signale": "^1.3.0",

src/components/Chat.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import WebSocket from "ws";
22
import config from "../../config";
33
import fs from "fs";
44
import signale from "signale";
5+
import { search } from "fast-fuzzy";
56

67
signale.config({
78
displayTimestamp: true,
@@ -306,12 +307,14 @@ class Chat {
306307
}
307308

308309
async switch(sceneName) {
310+
const res = search(sceneName, this.obsProps.scenes, { keySelector: obj => obj.name });
311+
309312
// switch scene
310313
try {
311314
await this.obs.setCurrentScene({
312-
"scene-name": sceneName
315+
"scene-name": res[0].name
313316
});
314-
this.say(`Scene successfully switched to "${sceneName}"`);
317+
this.say(`Scene successfully switched to "${res[0].name}"`);
315318
} catch (e) {
316319
log.error(e);
317320
this.say(e.error);

src/components/ObsSwitcher.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ObsSwitcher extends EventEmitter {
3333
this.currentScene = null;
3434
this.nginxSettings;
3535
this.previousScene = this.lowBitrateScene;
36+
this.scenes = null;
3637

3738
this.obs.connect({ address: this.address, password: this.password }).catch(e => {
3839
// handle this somewhere else
@@ -46,13 +47,15 @@ class ObsSwitcher extends EventEmitter {
4647
this.obs.on("StreamStopped", this.streamStopped.bind(this));
4748
this.obs.on("StreamStarted", this.streamStarted.bind(this));
4849
this.obs.on("Heartbeat", heartbeat => (this.heartbeat = heartbeat));
50+
this.obs.on("ScenesChanged", this.scenesChanged.bind(this));
4951

5052
log.info("Connecting & authenticating");
5153
}
5254

5355
onAuth() {
5456
log.success(`Successfully connected`);
5557
this.obs.SetHeartbeat({ enable: true });
58+
this.getSceneList();
5659

5760
this.interval = setInterval(async () => {
5861
const currentScene = await this.obs.GetCurrentScene();
@@ -181,6 +184,15 @@ class ObsSwitcher extends EventEmitter {
181184
streamStarted() {
182185
this.obsStreaming = true;
183186
}
187+
188+
async getSceneList() {
189+
const list = await this.obs.GetSceneList();
190+
this.scenes = list.scenes;
191+
}
192+
193+
scenesChanged() {
194+
this.getSceneList();
195+
}
184196
}
185197

186198
export default ObsSwitcher;

0 commit comments

Comments
 (0)