webclient/js/Room.js

34 lines
767 B
JavaScript

import Messaging;
import Session;
import Table;
var players = Table.make(function(o) {return o.value});
initMessageHandlers();
return {
player: player,
players: players
};
function player(key) {
return {id: key, name: players.get(key)};
}
function enterAll(o) {
for(key in o) {
enterPlayer(key, o[key]);
}
}
function enterPlayer(key, name) {
if(!Session.is(key)) {
players.insert(key, name);
}
}
function initMessageHandlers() {
Messaging.addEventListener(["Okaeri"], function(o) {enterAll(o.room);});
Messaging.addEventListener(["Welcome"], function(o) {enterAll(o.room);});
Messaging.addEventListener(["LogIn"], function(o) {enterPlayer(o.from, o.as);});
Messaging.addEventListener(["LogOut"], function(o) {players.remove(o.from);});
}