Skip to content

Commit 92e14ae

Browse files
author
Alan Shaw
committed
Listen for pass/fail events and broadcast progress
1 parent 6984b4a commit 92e14ae

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

nodebot-workshop.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env node
22

3-
const workshopper = require('workshopper')
3+
const dgram = require('dgram')
4+
, workshopper = require('workshopper')
45
, path = require('path')
5-
, menu = require('./exercises/menu')
6-
76
, name = 'nodebot-workshop'
87
, title = 'Nodebot Workshop'
98
, subtitle = '\x1b[23mSelect an exercise and hit \x1b[3mEnter\x1b[23m to begin'
@@ -12,13 +11,42 @@ function fpath (f) {
1211
return path.join(__dirname, f)
1312
}
1413

15-
workshopper({
14+
var nodebot = workshopper({
1615
name : name
1716
, title : title
1817
, subtitle : subtitle
1918
, exerciseDir : fpath('./exercises/')
2019
, appDir : __dirname
2120
, helpFile : fpath('help.txt')
2221
, menuItems : []
23-
, menu : {fg: "black", bg: /^win/.test(process.platform) ? "yellow" : 220}
22+
, menu : {fg: 'black', bg: /^win/.test(process.platform) ? 'yellow' : 220}
23+
})
24+
25+
function broadcastProgress (event, exercise, mode, msg) {
26+
var sock = dgram.createSocket('udp4')
27+
, packet = new Buffer(JSON.stringify({
28+
workshop: title,
29+
exercise: exercise.title,
30+
mode: mode,
31+
msg: msg,
32+
event: event
33+
}))
34+
, port = process.env.PROGRESS_BROADCAST_PORT || 1337
35+
36+
sock.bind(port, '0.0.0.0', function (er) {
37+
if (er) return;
38+
sock.setBroadcast(true)
39+
40+
sock.send(packet, 0, packet.length, port, '255.255.255.255', function () {
41+
sock.close()
42+
})
43+
})
44+
}
45+
46+
nodebot.on('pass', function (exercise, mode) {
47+
broadcastProgress('pass', exercise, mode)
48+
})
49+
50+
nodebot.on('fail', function (exercise, mode) {
51+
broadcastProgress('fail', exercise, mode)
2452
})

0 commit comments

Comments
 (0)