Remove intermediate name->key hash by using custom comparison function

This commit is contained in:
Sasha 2018-04-13 10:42:11 +02:00
parent eb2fd7a7cb
commit a8f951fad4

View file

@ -1,7 +1,6 @@
function Room(domElem, lib) {
var players = {};
var keysByName = {};
var logins = [];
var sortedKeys = [];
var session = {
key: null,
loggedIn: false,
@ -12,6 +11,7 @@ function Room(domElem, lib) {
e.preventDefault();
lib.send({tag: "Invitation", to: session.selected})
});
var compareKeysByLogin = lib.funMap(function(key) {return players[key].name;}, lib.defaultCompare);
return {
populate: populate,
@ -51,12 +51,11 @@ function Room(domElem, lib) {
function enter(key, name) {
var player = Player(key, name);
keysByName[name] = key;
players[key] = player;
player.position = lib.insert(name, logins);
beforePlayer = players[keysByName[logins[player.position]]];
player.position = lib.insert(key, sortedKeys, compareKeysByLogin);
beforePlayer = players[sortedKeys[player.position]];
playersList.insertBefore(player.dom, beforePlayer && beforePlayer.dom);
logins.splice(player.position, 0, name);
sortedKeys.splice(player.position, 0, key);
}
function leave(key) {
@ -68,8 +67,7 @@ function Room(domElem, lib) {
}
if(player != undefined) {
playersList.removeChild(player.dom);
logins.splice(player.position, 1);
delete keysByName[player.name]
sortedKeys.splice(player.position, 1);
delete players[key];
}
}