Simplify guest selection and handle disconnections

This commit is contained in:
Sasha 2018-04-13 09:57:36 +02:00
parent 315db97abb
commit d022ab9aa4
2 changed files with 17 additions and 12 deletions

View file

@ -22,7 +22,6 @@
<form id="room" class="off"> <form id="room" class="off">
<ul class="players"> <ul class="players">
</ul> </ul>
<input type="number" hidden name="guest"/>
<input type="submit" name="invite" value="Invite to a game" disabled/> <input type="submit" name="invite" value="Invite to a game" disabled/>
</form> </form>
<p id="debug"></p> <p id="debug"></p>

View file

@ -10,7 +10,7 @@ function Room(domElem, lib) {
var playersList = domElem.getElementsByClassName('players')[0]; var playersList = domElem.getElementsByClassName('players')[0];
domElem.addEventListener('submit', function(e) { domElem.addEventListener('submit', function(e) {
e.preventDefault(); e.preventDefault();
lib.send({tag: "Invitation", to: parseInt(domElem.guest.value)}) lib.send({tag: "Invitation", to: session.selected})
}); });
return { return {
@ -63,6 +63,8 @@ function Room(domElem, lib) {
var player = players[key]; var player = players[key];
if(key == session.key) { if(key == session.key) {
off(); off();
} else if(key == session.selected) {
reset();
} }
if(player != undefined) { if(player != undefined) {
playersList.removeChild(player.dom); playersList.removeChild(player.dom);
@ -85,25 +87,29 @@ function Room(domElem, lib) {
function off() { function off() {
domElem.className = "off"; domElem.className = "off";
session.loggedIn = false; session.loggedIn = false;
reset();
} }
function select(key) { function select(key) {
if(key == domElem.guest.value) { if(key === session.selected) {
unselect(players[key].dom); unselect(key);
} else { } else {
if(session.selected) { reset();
unselect(session.selected);
}
players[key].dom.className = "selected"; players[key].dom.className = "selected";
session.selected = players[key].dom; session.selected = key;
domElem.guest.value = key;
domElem.invite.disabled = false; domElem.invite.disabled = false;
} }
} }
function unselect(dom) { function reset() {
dom.className = ""; if(session.selected) {
domElem.guest.value = ""; unselect(session.selected);
}
}
function unselect(key) {
players[key].dom.className = "";
session.selected = null;
domElem.invite.disabled = true; domElem.invite.disabled = true;
} }
} }