Add start routine in session module picking the correct message and add an explicit loggedIn private boolean, rename the getter

This commit is contained in:
Tissevert 2019-11-24 22:54:46 +01:00
parent 0b834b4876
commit 403ee2da92
3 changed files with 20 additions and 7 deletions

View file

@ -36,7 +36,7 @@ function Login(modules) {
function initDOMEvents() {
root.addEventListener('submit', function(e) {
e.preventDefault();
if(modules.session.loggedIn()) {
if(modules.session.isLoggedIn()) {
modules.messaging.send({tag: "Invitation", to: them});
} else {
modules.session.start(root.you.value);
@ -100,7 +100,7 @@ function Login(modules) {
function refreshPlayers() {
modules.dom.clear(players);
if(modules.session.loggedIn()) {
if(modules.session.isLoggedIn()) {
refreshThem();
} else {
refreshYou();
@ -151,7 +151,7 @@ function Login(modules) {
}
function setMode() {
var loggedIn = modules.session.loggedIn()
var loggedIn = modules.session.isLoggedIn()
root.join.disabled = loggedIn;
root.invite.disabled = !loggedIn;
if(loggedIn) {

View file

@ -45,7 +45,7 @@ function Room(modules) {
};
function filter(name) {
if(modules.session.loggedIn()) {
if(modules.session.isLoggedIn()) {
var keep = function(player) {
return player.name.match(name) && !modules.session.is(player.key) && player.alone;
};

View file

@ -1,9 +1,11 @@
function Session(modules) {
var key = null;
var name = null;
var loggedIn = false;
modules.messaging.addEventListener(["Welcome"], function(o) {
key = o.key;
modules.save.set('player.id', key);
});
modules.messaging.addEventListener(["Relay", "LogIn"], function(o) {
@ -15,7 +17,8 @@ function Session(modules) {
return {
is: is,
getKey: getKey,
loggedIn: loggedIn
loggedIn: loggedIn,
start: start
};
function is(sessionKey) {
@ -26,7 +29,17 @@ function Session(modules) {
return key;
}
function loggedIn() {
return name != undefined;
function isLoggedIn() {
return loggedIn;
}
function start(name) {
var myID = modules.save.get('player.id');
if(myID != undefined) {
modules.messaging.send({tag: 'Tadaima', myID: myID, name: name});
} else {
modules.messaging.send({tag: 'Hello', name: name});
}
modules.save.set('player.name', name);
}
}