2018-05-11 12:31:53 +02:00
|
|
|
function Login(modules) {
|
|
|
|
var root = document.getElementById('login');
|
2019-01-01 12:57:27 +01:00
|
|
|
var playersMessage = root.getElementsByClassName('message')[0];
|
|
|
|
var players = root.getElementsByClassName('list')[0];
|
2018-05-11 12:31:53 +02:00
|
|
|
var join = document.getElementById("join");
|
|
|
|
var invite = document.getElementById("invite");
|
|
|
|
var submit = root.submitButton;
|
|
|
|
var them = null;
|
2019-01-01 12:57:27 +01:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
}];
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
init();
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
initDOMEvents();
|
|
|
|
initMessageHandlers();
|
|
|
|
var name = modules.save.get('player.name');
|
|
|
|
if(name != undefined && name.length > 0) {
|
|
|
|
root.you.value = name;
|
|
|
|
formDisable('join', false);
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
2019-11-24 22:48:43 +01:00
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
function initDOMEvents() {
|
|
|
|
root.addEventListener('submit', function(e) {
|
|
|
|
e.preventDefault();
|
2019-11-24 22:54:46 +01:00
|
|
|
if(modules.session.isLoggedIn()) {
|
2019-11-24 22:48:43 +01:00
|
|
|
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);
|
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
|
|
|
|
function playersChanged() {
|
2019-11-24 22:48:43 +01:00
|
|
|
setMode();
|
|
|
|
refreshPlayers();
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
function refreshPlayers() {
|
2018-05-11 12:31:53 +02:00
|
|
|
modules.dom.clear(players);
|
2019-11-24 22:54:46 +01:00
|
|
|
if(modules.session.isLoggedIn()) {
|
2018-05-11 12:31:53 +02:00
|
|
|
refreshThem();
|
|
|
|
} else {
|
|
|
|
refreshYou();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshYou() {
|
|
|
|
var nameTaken = false;
|
|
|
|
var name = root.you.value;
|
|
|
|
modules.room.filter(name).forEach(function(player) {
|
|
|
|
players.appendChild(player.dom);
|
|
|
|
nameTaken = nameTaken || name == player.name;
|
|
|
|
});
|
|
|
|
formDisable("join", name.length < 1 || nameTaken);
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshThem() {
|
|
|
|
them = null;
|
|
|
|
var name = root.them.value;
|
|
|
|
var filtered = modules.room.filter(name);
|
|
|
|
filtered.forEach(function(player) {
|
|
|
|
players.appendChild(player.dom);
|
|
|
|
});
|
|
|
|
var exact = filtered.find(exactMatch(name));
|
2019-01-01 12:57:27 +01:00
|
|
|
playersMessage.textContent = '';
|
2018-05-11 12:31:53 +02:00
|
|
|
if(exact != undefined) {
|
|
|
|
them = exact.key;
|
|
|
|
} else if(filtered.length == 1) {
|
|
|
|
them = filtered[0].key;
|
|
|
|
} else if(filtered.length == 0) {
|
2019-01-01 12:57:27 +01:00
|
|
|
playersMessage.textContent = modules.i18n.get(
|
|
|
|
name.length > 0 ? "notFound" : "alone"
|
|
|
|
);
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
formDisable("invite", them == undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formDisable(name, disabled) {
|
|
|
|
[submit, root[name]].forEach(function(button) {
|
2019-11-24 22:48:43 +01:00
|
|
|
button.disabled = disabled || !modules.messaging.isOn();
|
2018-05-11 12:31:53 +02:00
|
|
|
});
|
|
|
|
}
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
function exactMatch(name) {
|
|
|
|
return function(player) {
|
|
|
|
return player.name === name;
|
|
|
|
};
|
2018-04-12 23:01:40 +02:00
|
|
|
}
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
function setMode() {
|
2019-11-24 22:54:46 +01:00
|
|
|
var loggedIn = modules.session.isLoggedIn()
|
2018-05-11 12:31:53 +02:00
|
|
|
root.join.disabled = loggedIn;
|
|
|
|
root.invite.disabled = !loggedIn;
|
|
|
|
if(loggedIn) {
|
|
|
|
join.className = "";
|
|
|
|
invite.className = "on";
|
|
|
|
root.them.focus();
|
|
|
|
} else {
|
|
|
|
join.className = "on";
|
|
|
|
invite.className = "";
|
|
|
|
root.you.focus();
|
|
|
|
}
|
2018-04-12 23:01:40 +02:00
|
|
|
}
|
2018-04-11 13:25:24 +02:00
|
|
|
}
|