2019-12-11 22:03:16 +01:00
|
|
|
Screen.Hall = function(modules) {
|
2019-12-14 18:26:24 +01:00
|
|
|
var form = modules.ui.connectedForm('room');
|
2019-12-12 22:16:49 +01:00
|
|
|
var players = listSelector('players');
|
|
|
|
var games = listSelector('games');
|
2018-05-11 12:31:53 +02:00
|
|
|
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() {
|
2019-12-14 18:26:24 +01:00
|
|
|
form.addEventListener('submit', function(e) {
|
2019-11-24 22:48:43 +01:00
|
|
|
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-14 18:26:24 +01:00
|
|
|
form.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-12-21 23:08:38 +01:00
|
|
|
refreshPlayers();
|
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");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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);
|
2019-12-14 18:26:24 +01:00
|
|
|
var name = form.them.value;
|
2018-05-11 12:31:53 +02:00
|
|
|
them = null;
|
|
|
|
var filtered = modules.room.filter(name);
|
|
|
|
filtered.forEach(function(player) {
|
2019-12-14 18:26:24 +01:00
|
|
|
players.list.appendChild(player.dom);
|
2018-05-11 12:31:53 +02:00
|
|
|
});
|
|
|
|
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) {
|
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-14 18:26:24 +01:00
|
|
|
function refreshGames() {
|
|
|
|
modules.dom.clear(games.list);
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshThem() {
|
|
|
|
players.message.textContent = '';
|
|
|
|
formDisable(them == undefined);
|
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
|
|
|
|
|
|
|
}
|