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

View File

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