2018-05-11 12:31:53 +02:00
|
|
|
function Room(modules) {
|
|
|
|
|
2019-12-08 23:01:24 +01:00
|
|
|
function Player(key, name) {
|
2018-05-11 12:31:53 +02:00
|
|
|
this.key = key;
|
|
|
|
this.name = name;
|
2018-05-26 21:26:12 +02:00
|
|
|
this.dom = modules.dom.make('li', {textContent: name});
|
2018-05-11 12:31:53 +02:00
|
|
|
this.position = null;
|
|
|
|
}
|
|
|
|
|
2018-04-12 23:01:40 +02:00
|
|
|
var players = {};
|
2018-04-13 10:42:11 +02:00
|
|
|
var sortedKeys = [];
|
2018-04-12 23:01:40 +02:00
|
|
|
var session = {
|
|
|
|
key: null,
|
|
|
|
loggedIn: false,
|
|
|
|
selected: null
|
|
|
|
};
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-12-08 23:01:24 +01:00
|
|
|
modules.messaging.addEventListener(["Welcome"], populate);
|
|
|
|
modules.messaging.addEventListener(["Okaeri"], populate);
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2019-12-08 23:01:24 +01:00
|
|
|
modules.messaging.addEventListener(["LogIn"], function(o) {
|
|
|
|
enter(o.from, o.as);
|
2018-05-11 12:31:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Relay", "LogOut"], function(o) {
|
|
|
|
leave(o.from);
|
|
|
|
});
|
|
|
|
|
2018-05-13 18:08:12 +02:00
|
|
|
var compareKeysByLogin = modules.fun.map(function(key) {return players[key].name;}, modules.fun.defaultCompare);
|
2018-04-12 23:01:40 +02:00
|
|
|
|
|
|
|
return {
|
2018-05-11 12:31:53 +02:00
|
|
|
filter: filter,
|
2019-12-08 23:01:24 +01:00
|
|
|
/*enter: enter,*/
|
2018-04-12 23:01:40 +02:00
|
|
|
leave: leave,
|
|
|
|
name: name
|
|
|
|
};
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
function filter(name) {
|
2019-11-24 22:54:46 +01:00
|
|
|
if(modules.session.isLoggedIn()) {
|
2018-05-11 12:31:53 +02:00
|
|
|
var keep = function(player) {
|
2019-12-08 23:01:24 +01:00
|
|
|
return player.name.match(name) && !modules.session.is(player.key);
|
2018-05-11 12:31:53 +02:00
|
|
|
};
|
2018-04-12 23:01:40 +02:00
|
|
|
} else {
|
2018-05-11 12:31:53 +02:00
|
|
|
var keep = function(player) {return player.name.match(name);};
|
2018-04-12 23:01:40 +02:00
|
|
|
}
|
2018-05-13 18:08:12 +02:00
|
|
|
return modules.fun.mapFilter(modules.fun.of(players), keep)(sortedKeys);
|
2018-04-12 23:01:40 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:01:24 +01:00
|
|
|
function enter(key, name) {
|
|
|
|
name = name || "anon";
|
|
|
|
var player = new Player(key, name);
|
2018-04-12 23:01:40 +02:00
|
|
|
players[key] = player;
|
2018-05-13 18:08:12 +02:00
|
|
|
player.position = modules.fun.insert(key, sortedKeys, compareKeysByLogin);
|
2018-04-13 10:42:11 +02:00
|
|
|
sortedKeys.splice(player.position, 0, key);
|
2018-04-12 23:01:40 +02:00
|
|
|
}
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2019-12-08 23:01:24 +01:00
|
|
|
function populate(o) {
|
|
|
|
for(var key in o.room) {
|
|
|
|
enter(key, o.room[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 23:01:40 +02:00
|
|
|
function leave(key) {
|
|
|
|
var player = players[key];
|
|
|
|
if(player != undefined) {
|
2018-04-13 10:42:11 +02:00
|
|
|
sortedKeys.splice(player.position, 1);
|
2018-04-12 23:01:40 +02:00
|
|
|
delete players[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function name(key) {
|
|
|
|
player = players[key];
|
|
|
|
return player && player.name;
|
|
|
|
}
|
2018-04-11 13:25:24 +02:00
|
|
|
}
|