From 0b834b4876e63141bbcb67032bb8dcaa6bf4759a Mon Sep 17 00:00:00 2001 From: Tissevert Date: Sun, 24 Nov 2019 22:48:43 +0100 Subject: [PATCH] Sort initializers into separate init functions for DOM and WS events, stop passing session.loggedIn() around in refresh and setMode functions and send first message through session --- www/login.js | 139 +++++++++++++++++++++++++++++---------------------- 1 file changed, 79 insertions(+), 60 deletions(-) diff --git a/www/login.js b/www/login.js index 1ac5141..b966523 100644 --- a/www/login.js +++ b/www/login.js @@ -19,70 +19,88 @@ function Login(modules) { } }]; - root.addEventListener('submit', function(e) { - e.preventDefault(); - if(modules.session.loggedIn()) { - modules.messaging.send({tag: "Invitation", to: them}); - } else { - modules.messaging.send({tag: "LogIn", name: root.you.value}); - } - }); - - root.leave.addEventListener('click', function(e) { - e.preventDefault(); - lib.send({tag: "LogOut"}) - }); - - root.you.addEventListener("input", function() {refreshPlayers(false);}); - root.them.addEventListener("input", function() {refreshPlayers(true);}); - - modules.messaging.addEventListener(["Welcome"], function() { - refreshPlayers(modules.session.loggedIn()); - }); - - modules.messaging.addEventListener(["Update"], function(o) { - refreshPlayers(modules.session.loggedIn()); - }); - - modules.messaging.addEventListener(["Relay", "LogIn"], function() { - playersChanged(); - }); - - modules.messaging.addEventListener(["Relay", "LogOut"], function() { - playersChanged(); - }); - - modules.messaging.addEventListener(["Relay", "Invitation"], function(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"); - } - }); + init(); return {}; - function playersChanged() { - var loggedIn = modules.session.loggedIn(); - setMode(loggedIn); - refreshPlayers(loggedIn); + function init() { + initDOMEvents(); + initMessageHandlers(); + var name = modules.save.get('player.name'); + if(name != undefined && name.length > 0) { + root.you.value = name; + formDisable('join', false); + } } - function refreshPlayers(loggedIn) { + function initDOMEvents() { + root.addEventListener('submit', function(e) { + e.preventDefault(); + if(modules.session.loggedIn()) { + modules.messaging.send({tag: "Invitation", to: them}); + } else { + modules.session.start(root.you.value); + } + }); + + root.leave.addEventListener('click', function(e) { + e.preventDefault(); + lib.send({tag: "LogOut"}) + }); + + root.you.addEventListener("input", function() {refreshPlayers();}); + root.them.addEventListener("input", function() {refreshPlayers();}); + } + + function initMessageHandlers() { + modules.messaging.addEventListener(["Welcome"], function() { + refreshPlayers(); + }); + + modules.messaging.addEventListener(["Update"], function(o) { + refreshPlayers(); + }); + + modules.messaging.addEventListener(["Relay", "LogIn"], function() { + playersChanged(); + }); + + modules.messaging.addEventListener(["Relay", "LogOut"], function() { + playersChanged(); + }); + + modules.messaging.addEventListener(["Relay", "Invitation"], function(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"); + } + }); + + modules.messaging.addEventListener('open', refreshPlayers); + modules.messaging.addEventListener('close', refreshPlayers); + } + + function playersChanged() { + setMode(); + refreshPlayers(); + } + + function refreshPlayers() { modules.dom.clear(players); - if(loggedIn) { + if(modules.session.loggedIn()) { refreshThem(); } else { refreshYou(); @@ -122,7 +140,7 @@ function Login(modules) { function formDisable(name, disabled) { [submit, root[name]].forEach(function(button) { - button.disabled = disabled; + button.disabled = disabled || !modules.messaging.isOn(); }); } @@ -132,7 +150,8 @@ function Login(modules) { }; } - function setMode(loggedIn) { + function setMode() { + var loggedIn = modules.session.loggedIn() root.join.disabled = loggedIn; root.invite.disabled = !loggedIn; if(loggedIn) {