Allow the host to transfer hosting to another user
The host can now appoint a new host by double-clicking on another user's avatar. Useful for giving control to another user.
This commit is contained in:
parent
fa8127386f
commit
f0990dfbfa
62
server.js
62
server.js
|
@ -187,6 +187,16 @@ ptserver_io.on('connection', function(socket){
|
|||
type: msg.type
|
||||
})
|
||||
})
|
||||
socket.on('transfer_host',function(data){
|
||||
if (socket.ourRoom == null){
|
||||
//console.log('This user should join a room first')
|
||||
socket.emit('flowerror','You aren\' connected to a room! Use join')
|
||||
socket.emit('rejoin')
|
||||
return
|
||||
}
|
||||
console.log("Hi there", data)
|
||||
transferHost(socket.selfUser, function (user){ return user.username == data.username })
|
||||
})
|
||||
socket.on('connect_timeout',function(){
|
||||
//console.log('timeout')
|
||||
handleDisconnect(true)
|
||||
|
@ -198,14 +208,8 @@ ptserver_io.on('connection', function(socket){
|
|||
if (socket.selfUser === undefined || socket.selfUser === null){
|
||||
return
|
||||
}
|
||||
//console.log('User left: ' + socket.selfUser.username)
|
||||
if (socket.selfUser.role == 'host'){
|
||||
//Our Host has left, lets give the next Guest the Host role
|
||||
var newHost = transferHost(socket.selfUser.room)
|
||||
//console.log('The new host is ' + newHost)
|
||||
//console.log(JSON.stringify(newHost,null,4))
|
||||
socket.broadcast.to(socket.selfUser.room).emit('host-swap',newHost)
|
||||
}
|
||||
//console.log('User left: ' + socket.selfUser.username)
|
||||
transferHost(socket.selfUser, function (user){ return user != socket.selfUser })
|
||||
removeUser(socket.selfUser.room,socket.selfUser.username)
|
||||
if (ptserver_io.sockets.adapter.rooms[socket.selfUser.room]){
|
||||
socket.broadcast.to(socket.selfUser.room).emit('user-left',ptserver_io.sockets.adapter.rooms[socket.selfUser.room].users,socket.selfUser)
|
||||
|
@ -230,39 +234,23 @@ ptserver_io.on('connection', function(socket){
|
|||
}
|
||||
}
|
||||
}
|
||||
function transferHost(roomName){
|
||||
console.log('Transfering the host in the room ' + roomName)
|
||||
var room = ptserver_io.sockets.adapter.rooms[roomName]
|
||||
if (room === undefined){
|
||||
//Room has already been destroyed!
|
||||
function transferHost(user, newHostPredicate){
|
||||
if (user.role != 'host') {
|
||||
console.log('Not transfering host in room', user.room, 'from', user.username, 'because its role is', user.role)
|
||||
return
|
||||
}
|
||||
var oldHost = removeHost(room)
|
||||
if (oldHost === null || oldHost === undefined) {
|
||||
var room = ptserver_io.sockets.adapter.rooms[user.room]
|
||||
var newHost = room.users.find(newHostPredicate)
|
||||
if (!newHost) {
|
||||
console.log('Not transfering host in room', user.room, 'from', user.username, 'because suitable user found')
|
||||
return
|
||||
}
|
||||
for (var i in room.users){
|
||||
if (room.users[i].username != oldHost.username){
|
||||
//This is a valid user
|
||||
//console.log('Transferred host to ' + room.users[i].username)
|
||||
room.users[i].role = 'host'
|
||||
room.hostUser = room.users[i]
|
||||
room.hostUsername = room.users[i].username
|
||||
return (room.users[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
function removeHost(room){
|
||||
if (room === undefined){
|
||||
//Room has already been destroyed!
|
||||
return
|
||||
}
|
||||
for (var i in room.users){
|
||||
if (room.users[i].role == 'host'){
|
||||
room.users[i].role = 'guest'
|
||||
return(room.users[i])
|
||||
}
|
||||
}
|
||||
console.log('Transfering host in room', user.room, 'from', user.username, 'to', newHost.username)
|
||||
user.role = 'guest'
|
||||
newHost.role = 'host'
|
||||
room.hostUser = newHost
|
||||
room.hostUsername = newHost.username
|
||||
socket.broadcast.to(user.room).emit('host-swap', newHost)
|
||||
}
|
||||
function removeUser(roomname,username){
|
||||
var room = ptserver_io.sockets.adapter.rooms[roomname]
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<v-list-item style="height:4em" class="mb-0 pb-0">
|
||||
<v-list-tile avatar class="pb-0 mb-0" tag="div" >
|
||||
<v-list-tile-avatar>
|
||||
<img v-bind:src="user.avatarUrl"/>
|
||||
<img v-bind:src="user.avatarUrl" v-on:dblclick="transferHost(user.username)"/>
|
||||
</v-list-tile-avatar>
|
||||
<v-list-tile-content>
|
||||
<v-list-tile-title> {{ user.username }}</v-list-tile-title>
|
||||
|
@ -176,7 +176,15 @@
|
|||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
},
|
||||
transferHost: function (username) {
|
||||
window.x = {
|
||||
username: username,
|
||||
this: this
|
||||
}
|
||||
console.log("transfering host", window.x)
|
||||
this.$store.dispatch('transferHost', username)
|
||||
},
|
||||
handleDisconnect: function () {
|
||||
this.$store.dispatch('disconnectServer')
|
||||
},
|
||||
|
|
|
@ -814,6 +814,13 @@ const plexTogether = {
|
|||
})
|
||||
}
|
||||
},
|
||||
transferHost ({state, commit, rootState}, username) {
|
||||
if (state._socket.connected) {
|
||||
state._socket.emit('transfer_host', {
|
||||
username: username
|
||||
})
|
||||
}
|
||||
},
|
||||
getServerList () {
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue