Remove intermediate name->key hash by using custom comparison function
This commit is contained in:
parent
eb2fd7a7cb
commit
a8f951fad4
1 changed files with 6 additions and 8 deletions
14
www/room.js
14
www/room.js
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue