webclient/js/Session.js

50 lines
815 B
JavaScript
Raw Normal View History

import Messaging;
import Save;
var key = null;
var playerKey = null;
var name = null;
var loggedIn = false;
Messaging.addEventListener(["Welcome"], function(o) {
playerKey = o.key;
Save.set('player.id', o.key);
});
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 = Save.get('player.id');
if(myID != undefined) {
Messaging.send({tag: 'Tadaima', myID: myID, name: name});
playerKey = myID;
} else {
Messaging.send({tag: 'Hello', name: name});
}
Save.set('player.name', name);
}