webclient/js/Session.js

49 lines
805 B
JavaScript
Raw Normal View History

import Messaging;
import Save;
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 playerKey;
}
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);
}