diff --git a/index.html b/index.html
index fcdf559..46412b2 100644
--- a/index.html
+++ b/index.html
@@ -35,6 +35,11 @@
+
+
Backend, where are thou?
+
+
- Montenegro
diff --git a/irc.png b/irc.png
new file mode 100644
index 0000000..9998c20
Binary files /dev/null and b/irc.png differ
diff --git a/irc.svg b/irc.svg
new file mode 100644
index 0000000..f9910c1
--- /dev/null
+++ b/irc.svg
@@ -0,0 +1,102 @@
+
+
+
+
diff --git a/quiz.css b/quiz.css
index 9554b0c..848e31f 100644
--- a/quiz.css
+++ b/quiz.css
@@ -57,6 +57,14 @@ p {
font-size: 200%;
}
+#irc ul {
+ list-style-type: none;
+ font-family: monospace;
+ font-size: 150%;
+ text-align: left;
+ width: 40em;
+}
+
#answers {
position: absolute;
bottom: 0;
diff --git a/quiz.js b/quiz.js
index 220bc8d..56399b6 100644
--- a/quiz.js
+++ b/quiz.js
@@ -60,7 +60,7 @@ function setupWs() {
};
ws.onclose = function() {
console.error('WebSocket closed');
- setupWs();
+ window.setTimeout(setupWs, 100);
};
ws.onmessage = function(event) {
try {
@@ -141,7 +141,7 @@ function startQuiz() {
if (name) {
playerNames[i] = name;
playerScores[i] = 0;
- $('#scoreboard dl').append('- 0
');
+ $('#scoreboard dl').append('- 0
');
$('#scoreboard dl dt').last().text(name);
$('#players').append('- 0
');
$('#players li.player'+i+' span.name').text(name);
@@ -195,6 +195,10 @@ function takeJoker(activePlayer, joker) {
// Joker already taken
return;
+ /* Hide previous special jokers */
+ $('#nedap').hide();
+ $('#irc').hide();
+
playerJokers[activePlayer][joker] = true;
$('#tier').append('');
$('#scoreboard dd').eq(activePlayer).find('.' + joker).remove();
@@ -253,7 +257,6 @@ function takeJoker(activePlayer, joker) {
/* Fill */
ctx.fillStyle = '#ccc';
var barHeight = (y2 - y1) * scores[i] / total;
-console.log({x1:x1,y1:y1,x2:x2,y2:y2,barHeight:barHeight});
ctx.fillRect(x1, y2 - barHeight, x2 - x1, barHeight);
/* Outline */
@@ -270,10 +273,35 @@ console.log({x1:x1,y1:y1,x2:x2,y2:y2,barHeight:barHeight});
onBackendMessage = function(msg) {
if (msg.nedap && msg.nedap.scores)
scores = msg.nedap.scores;
-console.log('scores: '+JSON.stringify(scores));
+ console.log('scores: '+JSON.stringify(scores));
redraw();
};
}
+ if (joker === 'irc') {
+ sendToBackend({ irc: "activate" });
+ onBackendMessage = function(msg) {
+ if (msg.irc && msg.irc.nick && msg.irc.text) {
+ var ircPane = $('#irc ul');
+ var line = $('');
+ line.text('<' + msg.irc.nick + '> ' + msg.irc.text);
+ line.hide();
+ ircPane.append(line);
+ line.slideDown(200);
+
+ if (ircPane.children().length > 8) {
+ var line1 = ircPane.children().first();
+ line1.slideUp(200, function() {
+ line1.remove();
+ });
+ }
+ }
+ if (msg.irc && msg.irc.server && msg.irc.channel) {
+ $('#irc .caption').text(msg.irc.server + ' ' + msg.irc.channel);
+ }
+ };
+ $('#irc ul').empty();
+ $('#irc').slideDown(500);
+ }
}
function setQuestionContents(q) {
@@ -404,10 +432,14 @@ function switchToGame() {
} else if (activePlayer !== null &&
key === 'n') {
takeJoker(activePlayer, 'nedap');
+ } else if (activePlayer !== null &&
+ key === 'i') {
+ takeJoker(activePlayer, 'irc');
}
};
$('#nedap').hide();
+ $('#irc').hide();
onBackendMessage = null;
// Instantly show the question:
$('#game').show();
diff --git a/server.js b/server.js
index fac8414..2e059ae 100644
--- a/server.js
+++ b/server.js
@@ -1,9 +1,15 @@
var Connect = require('connect');
var wss = require('websocket-server');
var wsc = require('websocket-client');
+var irc = require('irc-js');
var frontend;
+
+/*
+ * Nedap backend connection
+ */
+
/* TODO: url */
var nedap;
function connectNedap() {
@@ -24,7 +30,7 @@ function connectNedap() {
try {
var msg = JSON.parse(data);
console.log({ fromNedap: msg });
- frontend.send(JSON.stringify({ nedap: msg }));
+ sendToFrontend({ nedap: msg });
} catch (e) {
console.error(e.stack);
}
@@ -32,6 +38,56 @@ function connectNedap() {
}
connectNedap();
+
+/*
+ * IRC client
+ */
+
+var IRC_SERVER = 'irc.freenode.net';
+var IRC_CHAN = '#pentanews';
+var chat = new irc({ server: IRC_SERVER,
+ encoding: 'utf-8',
+ nick: '[Ceiling]Cat'
+ });
+function connectChat() {
+ chat.connect();
+}
+connectChat();
+chat.addListener('376', function() {
+ chat.join(IRC_CHAN);
+});
+chat.addListener('366', function(msg) {
+ if (msg.params[1] === IRC_CHAN) {
+ console.log('Successfully joined ' + IRC_CHAN);
+ pushIrcInfo();
+ }
+});
+chat.addListener('privmsg', function(msg) {
+ console.log({PRIVMSG:msg});
+ var nick = msg.person.nick;
+ var channel = msg.params[0];
+ var text = msg.params[1];
+ if (nick && channel === IRC_CHAN && text && frontend) {
+ sendToFrontend({ irc: { nick: nick,
+ text: text
+ } });
+ }
+});
+chat.addListener('disconnected', function() {
+ console.error('Chat disconnected!');
+ process.nextTick(connectChat);
+});
+
+function pushIrcInfo() {
+ sendToFrontend({ irc: { server: IRC_SERVER,
+ channel: IRC_CHAN } });
+}
+
+
+/*
+ * Web server
+ */
+
var server = Connect.createServer(
Connect.logger(),
Connect.bodyDecoder(),
@@ -39,6 +95,10 @@ var server = Connect.createServer(
Connect.errorHandler({ dumpExceptions: true, showStack: true })
);
+/*
+ * WebSocket server
+ */
+
wss.createServer({ server: server }).on('connection', function(conn) {
frontend = conn;
@@ -50,7 +110,9 @@ wss.createServer({ server: server }).on('connection', function(conn) {
console.log({ toNedap: msg.nedap });
nedap.send(JSON.stringify(msg.nedap));
}
-
+ else if (msg.irc === "activate") {
+ pushIrcInfo();
+ }
} catch (e) {
console.error(e.stack);
}
@@ -63,4 +125,11 @@ wss.createServer({ server: server }).on('connection', function(conn) {
conn.on('error', reset);
});
+function sendToFrontend(obj) {
+ if (!frontend)
+ return;
+
+ frontend.send(JSON.stringify(obj));
+}
+
server.listen(8081);