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) { if(is(o.from)) { name = o.message.name; } }); return { is: is, getKey: getKey, loggedIn: loggedIn, start: start }; function is(sessionKey) { return key == sessionKey; } 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}); } else { modules.messaging.send({tag: 'Hello', name: name}); } modules.save.set('player.name', name); } }