2018-05-11 12:31:53 +02:00
|
|
|
function Room(modules) {
|
|
|
|
|
|
|
|
function Player(key, name, alone) {
|
|
|
|
this.key = key;
|
|
|
|
this.name = name;
|
|
|
|
this.alone = alone;
|
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
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Welcome"], function(o) {
|
|
|
|
for(var key in o.room) {
|
|
|
|
enter(parseInt(key), o.room[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Update"], function(o) {
|
|
|
|
o.alone.forEach(function(key) {players[key].alone = true;});
|
|
|
|
o.paired.forEach(function(key) {players[key].alone = false;});
|
2018-04-12 23:01:40 +02:00
|
|
|
});
|
2018-05-11 12:31:53 +02:00
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Relay", "LogIn"], function(o) {
|
|
|
|
enter(o.from, o.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2018-04-12 23:01:40 +02:00
|
|
|
enter: enter,
|
|
|
|
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) {
|
|
|
|
return player.name.match(name) && !modules.session.is(player.key) && player.alone;
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
function enter(key, obj) {
|
|
|
|
var name = obj.name || "anon";
|
|
|
|
var alone = obj.alone != undefined ? obj.alone : true;
|
|
|
|
var player = new Player(key, name, alone);
|
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
|
|
|
|
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
|
|
|
}
|