2020-01-10 08:36:57 +01:00
|
|
|
import Messaging;
|
|
|
|
import Session;
|
|
|
|
import Table;
|
|
|
|
|
2020-01-13 08:39:32 +01:00
|
|
|
var players = Table.make(player, 'name');
|
2020-01-10 08:36:57 +01:00
|
|
|
initMessageHandlers();
|
|
|
|
|
|
|
|
return {
|
2020-01-11 19:46:41 +01:00
|
|
|
opponent: opponent,
|
2020-01-13 08:39:32 +01:00
|
|
|
players: players
|
2020-01-10 08:36:57 +01:00
|
|
|
};
|
|
|
|
|
2020-01-11 19:46:41 +01:00
|
|
|
function opponent(key) {
|
2020-01-13 08:39:32 +01:00
|
|
|
return {id: key, name: players.get(key).name};
|
2020-01-11 19:46:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 08:36:57 +01:00
|
|
|
function player(key, name) {
|
|
|
|
return {
|
|
|
|
key: key,
|
|
|
|
name: name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initMessageHandlers() {
|
|
|
|
Messaging.addEventListener(["Okaeri"], function(o) {
|
2020-01-13 08:39:32 +01:00
|
|
|
players.insertAll(o.room);
|
2020-01-10 08:36:57 +01:00
|
|
|
});
|
|
|
|
Messaging.addEventListener(["Welcome"], function(o) {
|
2020-01-13 08:39:32 +01:00
|
|
|
players.insertAll(o.room);
|
2020-01-10 08:36:57 +01:00
|
|
|
});
|
|
|
|
Messaging.addEventListener(["LogIn"], function(o) {
|
|
|
|
if(!Session.is(o.from)) {
|
2020-01-13 08:39:32 +01:00
|
|
|
players.insert(o.from, o.as);
|
2020-01-10 08:36:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Messaging.addEventListener(["LogOut"], function(o) {
|
2020-01-13 08:39:32 +01:00
|
|
|
players.remove(o.from);
|
2020-01-10 08:36:57 +01:00
|
|
|
});
|
|
|
|
}
|