auto_join_3.3
Håvar Aambø Fosstveit 2020-03-24 01:23:17 +01:00
parent 4135be9789
commit d756dd4721
2 changed files with 20 additions and 35 deletions

View File

@ -14,7 +14,7 @@ class Lobby extends EventEmitter
// Closed flag.
this._closed = false;
this._peers = new Map();
this._peers = {};
}
close()
@ -23,27 +23,28 @@ class Lobby extends EventEmitter
this._closed = true;
this._peers.forEach((peer) =>
// Close the peers.
for (const peer in this._peers)
{
if (!peer.closed)
peer.close();
});
}
this._peers.clear();
this._peers = null;
}
checkEmpty()
{
logger.info('checkEmpty()');
return this._peers.size === 0;
return Object.keys(this._peers).length === 0;
}
peerList()
{
logger.info('peerList()');
return Array.from(this._peers.values()).map((peer) =>
return Object.values(this._peers).map((peer) =>
({
peerId : peer.id,
displayName : peer.displayName
@ -52,25 +53,25 @@ class Lobby extends EventEmitter
hasPeer(peerId)
{
return this._peers.has(peerId);
return this._peers[peerId] != null;
}
promoteAllPeers()
{
logger.info('promoteAllPeers()');
this._peers.forEach((peer) =>
for (const peer in this._peers)
{
if (peer.socket)
this.promotePeer(peer.id);
});
}
}
promotePeer(peerId)
{
logger.info('promotePeer() [peer:"%s"]', peerId);
const peer = this._peers.get(peerId);
const peer = this._peers[peerId];
if (peer)
{
@ -87,7 +88,7 @@ class Lobby extends EventEmitter
peer.closeHandler = null;
this.emit('promotePeer', peer);
this._peers.delete(peerId);
delete this._peers[peerId];
}
}
@ -146,7 +147,7 @@ class Lobby extends EventEmitter
this.emit('peerClosed', peer);
this._peers.delete(peer.id);
delete this._peers[peer.id];
if (this.checkEmpty())
this.emit('lobbyEmpty');
@ -154,7 +155,7 @@ class Lobby extends EventEmitter
this._notification(peer.socket, 'enteredLobby');
this._peers.set(peer.id, peer);
this._peers[peer.id] = peer;
peer.on('gotRole', peer.gotRoleHandler);
peer.on('displayNameChanged', peer.displayNameChangeHandler);

View File

@ -100,11 +100,8 @@ class Room extends EventEmitter
// Close the peers.
for (const peer in this._peers)
{
if (Object.prototype.hasOwnProperty.call(this._peers, peer))
{
if (!peer.closed)
peer.close();
}
if (!peer.closed)
peer.close();
}
this._peers = null;
@ -313,7 +310,6 @@ class Room extends EventEmitter
}, 10000);
}
// checks both room and lobby
checkEmpty()
{
return Object.keys(this._peers).length === 0;
@ -333,12 +329,8 @@ class Room extends EventEmitter
{
peer.socket.join(this._roomId);
const index = this._lastN.indexOf(peer.id);
if (index === -1) // We don't have this peer, add to end
{
this._lastN.push(peer.id);
}
// If we don't have this peer, add to end
!this._lastN.includes(peer.id) && this._lastN.push(peer.id);
this._peers[peer.id] = peer;
@ -372,25 +364,17 @@ class Room extends EventEmitter
// If the Peer was joined, notify all Peers.
if (peer.joined)
{
this._notification(peer.socket, 'peerClosed', { peerId: peer.id }, true);
}
const index = this._lastN.indexOf(peer.id);
if (index > -1) // We have this peer in the list, remove
{
this._lastN.splice(index, 1);
}
// Remove from lastN
this._lastN = this._lastN.filter((id) => id !== peer.id);
delete this._peers[peer.id];
// If this is the last Peer in the room and
// lobby is empty, close the room after a while.
if (this.checkEmpty() && this._lobby.checkEmpty())
{
this.selfDestructCountdown();
}
});
peer.on('displayNameChanged', ({ oldDisplayName }) =>