2019-12-11 22:03:16 +01:00
|
|
|
Screen.Hall = function(modules) {
|
2019-12-12 22:16:49 +01:00
|
|
|
var root = document.getElementById('room');
|
|
|
|
var players = listSelector('players');
|
|
|
|
var games = listSelector('games');
|
2018-05-11 12:31:53 +02:00
|
|
|
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 {};
|
|
|
|
|
2019-12-12 22:16:49 +01:00
|
|
|
function listSelector(id) {
|
|
|
|
var root = document.getElementById(id);
|
|
|
|
return {
|
|
|
|
root: root,
|
|
|
|
message: root.getElementsByClassName('message')[0],
|
|
|
|
list: root.getElementsByClassName('list')[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
function init() {
|
|
|
|
initDOMEvents();
|
|
|
|
initMessageHandlers();
|
|
|
|
}
|
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-12-12 22:16:49 +01:00
|
|
|
modules.messaging.send({tag: "Invitation", to: them});
|
2019-11-24 22:48:43 +01:00
|
|
|
});
|
|
|
|
|
2019-12-12 22:16:49 +01:00
|
|
|
root.them.addEventListener("input", function() {refreshPlayers();});
|
2019-11-24 22:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function initMessageHandlers() {
|
2019-12-08 23:01:24 +01:00
|
|
|
modules.messaging.addEventListener(["Okaeri"], function() {
|
|
|
|
refreshPlayers();
|
2019-12-12 22:16:49 +01:00
|
|
|
refreshGames();
|
2019-12-08 23:01:24 +01:00
|
|
|
});
|
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
modules.messaging.addEventListener(["Welcome"], function() {
|
|
|
|
refreshPlayers();
|
2019-12-12 22:16:49 +01:00
|
|
|
refreshGames();
|
2019-11-24 22:48:43 +01:00
|
|
|
});
|
|
|
|
|
2019-12-08 23:01:24 +01:00
|
|
|
modules.messaging.addEventListener(["LogIn"], function(o) {
|
2019-12-12 22:16:49 +01:00
|
|
|
console.log("Someone joined in, they should be added to the lists");
|
|
|
|
console.log(o);
|
2019-11-24 22:48:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Relay", "Invitation"], function(o) {
|
2019-12-12 22:16:49 +01:00
|
|
|
console.log("Received an invitation, should be added to games list");
|
|
|
|
console.log(o);
|
|
|
|
/*
|
2019-11-24 22:48:43 +01:00
|
|
|
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});
|
|
|
|
}
|
2019-12-12 22:16:49 +01:00
|
|
|
*/
|
2019-11-24 22:48:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-11-24 22:48:43 +01:00
|
|
|
function refreshPlayers() {
|
2019-12-12 22:16:49 +01:00
|
|
|
modules.dom.clear(players.list);
|
|
|
|
refreshThem();
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 22:16:49 +01:00
|
|
|
function refreshGames() {
|
|
|
|
modules.dom.clear(games.list);
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-12 22:16:49 +01:00
|
|
|
players.message.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-12-12 22:16:49 +01:00
|
|
|
players.message.textContent = modules.i18n.get(
|
2019-01-01 12:57:27 +01:00
|
|
|
name.length > 0 ? "notFound" : "alone"
|
|
|
|
);
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
2019-12-12 22:16:49 +01:00
|
|
|
formDisable(them == undefined);
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 22:16:49 +01:00
|
|
|
function formDisable(disabled) {
|
|
|
|
[submit, root.invite].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
|
|
|
|
|
|
|
}
|