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

This commit is contained in:
Tissevert 2019-11-24 22:48:43 +01:00
parent cd517821c3
commit 0b834b4876

View file

@ -19,70 +19,88 @@ function Login(modules) {
} }
}]; }];
root.addEventListener('submit', function(e) { init();
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");
}
});
return {}; return {};
function playersChanged() { function init() {
var loggedIn = modules.session.loggedIn(); initDOMEvents();
setMode(loggedIn); initMessageHandlers();
refreshPlayers(loggedIn); 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); modules.dom.clear(players);
if(loggedIn) { if(modules.session.loggedIn()) {
refreshThem(); refreshThem();
} else { } else {
refreshYou(); refreshYou();
@ -122,7 +140,7 @@ function Login(modules) {
function formDisable(name, disabled) { function formDisable(name, disabled) {
[submit, root[name]].forEach(function(button) { [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.join.disabled = loggedIn;
root.invite.disabled = !loggedIn; root.invite.disabled = !loggedIn;
if(loggedIn) { if(loggedIn) {