server/www/screen/hall.js

128 lines
3.3 KiB
JavaScript
Raw Normal View History

Screen.Hall = function(modules) {
var form = modules.ui.connectedForm('room');
var players = listSelector('players');
var games = listSelector('games');
var them = null;
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});
}
}];
init();
return {};
function listSelector(id) {
var root = document.getElementById(id);
return {
root: root,
message: root.getElementsByClassName('message')[0],
list: root.getElementsByClassName('list')[0]
}
}
function init() {
initDOMEvents();
initMessageHandlers();
}
function initDOMEvents() {
form.addEventListener('submit', function(e) {
e.preventDefault();
modules.messaging.send({tag: "Invitation", to: them});
});
form.them.addEventListener("input", function() {refreshPlayers();});
}
function initMessageHandlers() {
modules.messaging.addEventListener(["Okaeri"], function(o) {
modules.room.populate(o);
refreshPlayers();
refreshGames();
});
modules.messaging.addEventListener(["Welcome"], function(o) {
modules.room.populate(o);
refreshPlayers();
refreshGames();
});
modules.messaging.addEventListener(["LogIn"], function(o) {
modules.room.enter(o.from, o.as);
refreshPlayers();
});
modules.messaging.addEventListener(["LogOut"], function(o) {
modules.room.leave(o.from);
refreshPlayers();
});
modules.messaging.addEventListener(["Relay", "Invitation"], function(o) {
console.log("Received an invitation, should be added to games list");
console.log(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");
}
});
}
function refreshPlayers() {
modules.dom.clear(players.list);
players.message.textContent = '';
var name = form.them.value;
them = null;
var filtered = modules.room.filter(name);
filtered.forEach(function(player) {
players.list.appendChild(player.dom);
});
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) {
players.message.textContent = modules.i18n.get(
name.length > 0 ? "notFound" : "alone"
);
}
modules.ui.enableForm('room', them != undefined);
}
function refreshGames() {
modules.dom.clear(games.list);
2019-12-23 22:36:13 +01:00
games.message.textContent = modules.i18n.get('noGames');
}
2018-04-11 13:25:24 +02:00
function exactMatch(name) {
return function(player) {
return player.name === name;
};
}
2018-04-11 13:25:24 +02:00
}