Sort so host is always on top of user list

This commit is contained in:
Travis Shivers 2020-06-22 17:54:44 -05:00
parent 18550e66eb
commit 1f1b062a3f
1 changed files with 15 additions and 0 deletions

View File

@ -85,6 +85,18 @@ router.get('/users', (req, res) => {
return res.send(JSON.stringify({ users })).end();
});
const hostComparator = (a, b) => {
if (a.role === 'host') {
return -1;
}
if (b.role === 'host') {
return 1;
}
return 0;
};
socketServer.on('connection', (socket) => {
function transferHost(user, newHostPredicate) {
if (user.role !== 'host') {
@ -111,6 +123,9 @@ socketServer.on('connection', (socket) => {
newHost.role = 'host';
room.hostUser = newHost;
room.hostUsername = newHost.username;
room.users.sort(hostComparator);
// Resort to make host on top
socket.broadcast.to(user.room).emit('host-swap', newHost);
}