Screen.Hall = function(modules) { var form = modules.ui.connectedForm('room'); var players = listSelector('players'); var games = listSelector('games'); var them = null; var invitationAnswers = [{ label: 'accept', action: function() { modules.messaging.send({tag: "Answer", accept: true}); modules.screen.select("game"); } }, { label: 'decline', action: function() { modules.messaging.send({tag: "Answer", accept: false}); } }]; init(); return {}; function listSelector(id) { var root = document.getElementById(id); return { root: root, message: root.getElementsByClassName('message')[0], list: root.getElementsByClassName('list')[0] } } function init() { initDOMEvents(); initMessageHandlers(); } function initDOMEvents() { form.addEventListener('submit', function(e) { e.preventDefault(); modules.messaging.send({tag: "Invitation", to: them}); }); form.them.addEventListener("input", function() {refreshPlayers();}); } function initMessageHandlers() { modules.messaging.addEventListener(["Okaeri"], function() { refreshPlayers(); refreshGames(); }); modules.messaging.addEventListener(["Welcome"], function() { refreshPlayers(); refreshGames(); }); modules.messaging.addEventListener(["LogIn"], function(o) { console.log("Someone joined in, they should be added to the lists"); console.log(o); refreshPlayers(); }); modules.messaging.addEventListener(["Relay", "Invitation"], function(o) { console.log("Received an invitation, should be added to games list"); console.log(o); /* var name = modules.room.name(o.from); // invitations should come only from known players, in doubt say «no» if(name) { modules.statusHandler.set("🎴"); modules.screen.dialog({ text: modules.i18n.get('invited')(name), answers: invitationAnswers }); } else { modules.messaging.send({tag: "Answer", accept: false}); } */ }); modules.messaging.addEventListener(["Relay", "Answer"], function(o) { if(o.message.accept) { modules.screen.select("game"); } }); } function refreshPlayers() { modules.dom.clear(players.list); players.message.textContent = ''; var name = form.them.value; them = null; var filtered = modules.room.filter(name); filtered.forEach(function(player) { players.list.appendChild(player.dom); }); var exact = filtered.find(exactMatch(name)); if(exact != undefined) { them = exact.key; } else if(filtered.length == 1) { them = filtered[0].key; } else if(filtered.length == 0) { players.message.textContent = modules.i18n.get( name.length > 0 ? "notFound" : "alone" ); } modules.ui.enableForm('room', them != undefined); } function refreshGames() { modules.dom.clear(games.list); } function refreshThem() { players.message.textContent = ''; formDisable(them == undefined); } function exactMatch(name) { return function(player) { return player.name === name; }; } }