server/www/session.js

49 lines
969 B
JavaScript
Raw Normal View History

function Session(modules) {
var key = null;
var playerKey = null;
var name = null;
var loggedIn = false;
modules.messaging.addEventListener(["Welcome"], function(o) {
playerKey = o.key;
modules.save.set('player.id', o.key);
});
modules.messaging.addEventListener(["LogIn"], function(o) {
if(is(o.from)) {
name = o.as;
loggedIn = true;
}
});
return {
is: is,
getKey: getKey,
isLoggedIn: isLoggedIn,
start: start
};
function is(somePlayerKey) {
return playerKey == somePlayerKey;
}
function getKey() {
return key;
}
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});
playerKey = myID;
} else {
modules.messaging.send({tag: 'Hello', name: name});
}
modules.save.set('player.name', name);
}
}