Keep separating hall and login, simplifying both, generalize listSelector and add one for games

This commit is contained in:
Tissevert 2019-12-12 22:16:49 +01:00
parent 0147ca0135
commit d496bca168
3 changed files with 41 additions and 80 deletions

View file

@ -32,10 +32,11 @@
<label for="you"></label><input type="text" name="you"/>
<input type="submit" name="join" disabled/>
</p>
</form>
</div>
<div id="hall">
<form id="room">
</form>
</div>
<div id="hall">
<form id="room">
<input type="submit" name="submitButton" hidden disabled/>
<p id="invite">
<label for="them"></label><input type="text" name="them"/>
<input type="submit" name="invite" disabled/>
@ -44,9 +45,10 @@
<span class="message"></span>
<ul class="list"></ul>
</div>
<!--p id="leave">
<input type="button" name="leave"/>
</p-->
<div id="games">
<span class="message"></span>
<ul class="list"></ul>
</div>
</form>
</div>
<div id="game">

View file

@ -1,8 +1,7 @@
Screen.Hall = function(modules) {
var root = document.getElementById('login');
var playersMessage = root.getElementsByClassName('message')[0];
var players = document.getElementById('players').getElementsByClassName('list')[0];
var join = document.getElementById("join");
var root = document.getElementById('room');
var players = listSelector('players');
var games = listSelector('games');
var invite = document.getElementById("invite");
var submit = root.submitButton;
var them = null;
@ -23,61 +22,49 @@ Screen.Hall = function(modules) {
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();
var name = modules.save.get('player.name');
if(name != undefined && name.length > 0) {
root.you.value = name;
formDisable('join', false);
}
}
function initDOMEvents() {
root.addEventListener('submit', function(e) {
e.preventDefault();
if(modules.session.isLoggedIn()) {
modules.messaging.send({tag: "Invitation", to: them});
} else {
modules.session.start(root.you.value);
}
modules.messaging.send({tag: "Invitation", to: them});
});
/*
root.leave.addEventListener('click', function(e) {
e.preventDefault();
lib.send({tag: "LogOut"})
});
*/
root.you.addEventListener("input", function() {refreshPlayers();});
//root.them.addEventListener("input", function() {refreshPlayers();});
root.them.addEventListener("input", function() {refreshPlayers();});
}
function initMessageHandlers() {
modules.messaging.addEventListener(["Okaeri"], function() {
refreshPlayers();
refreshGames();
});
modules.messaging.addEventListener(["Welcome"], function() {
refreshPlayers();
});
modules.messaging.addEventListener(["Update"], function(o) {
refreshPlayers();
refreshGames();
});
modules.messaging.addEventListener(["LogIn"], function(o) {
if(modules.session.is(o.from)) {
modules.screen.select('hall');
}
});
modules.messaging.addEventListener(["Relay", "LogOut"], function() {
playersChanged();
console.log("Someone joined in, they should be added to the lists");
console.log(o);
});
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) {
@ -89,6 +76,7 @@ Screen.Hall = function(modules) {
} else {
modules.messaging.send({tag: "Answer", accept: false});
}
*/
});
modules.messaging.addEventListener(["Relay", "Answer"], function(o) {
@ -101,28 +89,13 @@ Screen.Hall = function(modules) {
modules.messaging.addEventListener('close', refreshPlayers);
}
function playersChanged() {
setMode();
refreshPlayers();
}
function refreshPlayers() {
modules.dom.clear(players);
if(modules.session.isLoggedIn()) {
refreshThem();
} else {
refreshYou();
}
modules.dom.clear(players.list);
refreshThem();
}
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 refreshGames() {
modules.dom.clear(games.list);
}
function refreshThem() {
@ -133,21 +106,21 @@ Screen.Hall = function(modules) {
players.appendChild(player.dom);
});
var exact = filtered.find(exactMatch(name));
playersMessage.textContent = '';
players.message.textContent = '';
if(exact != undefined) {
them = exact.key;
} else if(filtered.length == 1) {
them = filtered[0].key;
} else if(filtered.length == 0) {
playersMessage.textContent = modules.i18n.get(
players.message.textContent = modules.i18n.get(
name.length > 0 ? "notFound" : "alone"
);
}
formDisable("invite", them == undefined);
formDisable(them == undefined);
}
function formDisable(name, disabled) {
[submit, root[name]].forEach(function(button) {
function formDisable(disabled) {
[submit, root.invite].forEach(function(button) {
button.disabled = disabled || !modules.messaging.isOn();
});
}
@ -158,18 +131,4 @@ Screen.Hall = function(modules) {
};
}
function setMode() {
var loggedIn = modules.session.isLoggedIn()
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();
}
}
}

View file

@ -35,7 +35,7 @@ Screen.Login = function(modules) {
}
function formDisable(disabled) {
[submit, root.name].forEach(function(button) {
[submit, root.join].forEach(function(button) {
button.disabled = disabled || !modules.messaging.isOn();
});
}