39 lines
734 B
JavaScript
39 lines
734 B
JavaScript
import Messaging;
|
|
import Session;
|
|
import Table;
|
|
|
|
var players = Table.make(player, 'name');
|
|
initMessageHandlers();
|
|
|
|
return {
|
|
opponent: opponent,
|
|
players: players
|
|
};
|
|
|
|
function opponent(key) {
|
|
return {id: key, name: players.get(key).name};
|
|
}
|
|
|
|
function player(key, name) {
|
|
return {
|
|
key: key,
|
|
name: name
|
|
};
|
|
}
|
|
|
|
function initMessageHandlers() {
|
|
Messaging.addEventListener(["Okaeri"], function(o) {
|
|
players.insertAll(o.room);
|
|
});
|
|
Messaging.addEventListener(["Welcome"], function(o) {
|
|
players.insertAll(o.room);
|
|
});
|
|
Messaging.addEventListener(["LogIn"], function(o) {
|
|
if(!Session.is(o.from)) {
|
|
players.insert(o.from, o.as);
|
|
}
|
|
});
|
|
Messaging.addEventListener(["LogOut"], function(o) {
|
|
players.remove(o.from);
|
|
});
|
|
}
|