// Load comments at server startup
fs.readFile(state_file, (err, data) => {
if (err)
- state = { images: [], targets: [] };
+ state = {
+ images: [],
+ targets: [],
+ tardis: {
+ companions: {
+ names: [],
+ count: 0
+ }
+ }
+ };
else
state = JSON.parse(data);
});
app.get('/tardis', tardis_app);
app.get('/tardis/', tardis_app);
+const io_tardis = io.of("/tardis");
+
+io_tardis.use(wrap(session_middleware));
+
+var tardis_interval;
+
+function emit_tardis_timer() {
+ const tardis = state.tardis;
+ console.log("Emitting timer at " + tardis.timer);
+ io_tardis.emit('timer', tardis.timer);
+ tardis.timer = tardis.timer - 1;
+ if (tardis.timer < 0) {
+ clearInterval(tardis_interval);
+ tardis.timer = 30;
+ }
+}
+
+io_tardis.on("connection", (socket) => {
+ console.log("In connection handler.");
+ if (! socket.request.session.name) {
+ console.log("Error: Someone showed up at the Tardis without a name.");
+ return;
+ }
+
+ const name = socket.request.session.name;
+ const tardis = state.tardis;
+
+ if (tardis.companions.count === 0) {
+ tardis.timer = 30;
+ emit_tardis_timer();
+ tardis_interval = setInterval(emit_tardis_timer, 1000);
+ }
+
+ if (! tardis.companions.names.includes(name)) {
+ tardis.companions.count = tardis.companions.count + 1;
+ console.log("Adding " + name + " for " + tardis.companions.count + " companions");
+ io_tardis.emit('companions', tardis.companions.count);
+ }
+ tardis.companions.names.push(name);
+
+ socket.on('disconnect', () => {
+ const names = tardis.companions.names;
+
+ names.splice(names.indexOf(name), 1);
+
+ if (! tardis.companions.includes(name)) {
+ tardis.companions.count = tardis.companions.count - 1;
+ io_tardis.emit('companions', tardis.companions.count);
+ }
+ });
+});
+
io.on('connection', (socket) => {
// First things first, tell the client their name (if any)
transform: scale(30);
}
}
- .zoom-now {
+ .zoom-tardis {
animation-name: zoom;
animation-duration: 2s;
animation-timing-function: ease-in;
</p>
</div>
- <p id="welcome-message">
- Waiting to enter Tardis.
+ <div id="welcome-message">
+ <div id="timer_div" style="visibility: hidden">
+ Entering TARDIS in <span id="timer"></span> seconds.
+ </div>
<br>
Companions present: <span id="companions">1</span>/4
- </p>
+ </div>
</div>
<div id="game" style="display: none">
</div>
</div>
+
+ <script src="/socket.io/socket.io.js"></script>
+ <script>
+ const socket = io("/tardis");
+
+ const header = document.getElementById("header");
+ const companions = document.getElementById("companions");
+ const timer_div = document.getElementById("timer_div");
+ const timer = document.getElementById("timer");
+ const welcome_message = document.getElementById("welcome-message");
+
+ socket.on('companions', (count) => {
+ companions.textContent = count.toString();
+ });
+
+ socket.on('timer', (value) => {
+ console.log("Receiving timer value of " + value);
+ timer_div.style.visibility = "visible";
+ timer.textContent = value.toString();
+
+ if (value === 0) {
+ welcome_message.style.visibility = "hidden";
+ timer_div.style.visibility = "hidden";
+ header.className = "zoom-tardis";
+ }
+ });
+
+ </script>
</body>
</html>