Userlist now shows current time of the file as well

This commit is contained in:
Uriziel 2012-12-11 23:43:06 +01:00
parent 7b02de825f
commit f31bb70e0f
5 changed files with 49 additions and 23 deletions

View File

@ -1,3 +1,3 @@
version = '1.1.9' version = '1.2.0'
milestone = 'Tequila' milestone = 'Tequila'
projectURL = 'http://uriziel.github.com/syncplay/' projectURL = 'http://uriziel.github.com/syncplay/'

View File

@ -49,7 +49,7 @@ class SyncplayClient(object):
def __init__(self, playerClass, ui, args): def __init__(self, playerClass, ui, args):
self.protocolFactory = SyncClientFactory(self) self.protocolFactory = SyncClientFactory(self)
self.ui = UiManager(self, ui) self.ui = UiManager(self, ui)
self.userlist = SyncplayUserlist(self.ui) self.userlist = SyncplayUserlist(self.ui, self)
if(args.room == None or args.room == ''): if(args.room == None or args.room == ''):
args.room = 'default' args.room = 'default'
self.defaultRoom = args.room self.defaultRoom = args.room
@ -266,6 +266,10 @@ class SyncplayClient(object):
def getRoom(self): def getRoom(self):
return self.userlist.currentUser.room return self.userlist.currentUser.room
def getUserList(self):
if(self._protocol and self._protocol.logged):
self._protocol.sendList()
def getPassword(self): def getPassword(self):
return self._serverPassword return self._serverPassword
@ -302,11 +306,12 @@ class SyncplayClient(object):
self.ui.promptFor("Press enter to exit\n") self.ui.promptFor("Press enter to exit\n")
class SyncplayUser(object): class SyncplayUser(object):
def __init__(self, username = None, room = None, file_ = None): def __init__(self, username = None, room = None, file_ = None, position = 0):
self.username = username self.username = username
self.room = room self.room = room
self.file = file_ self.file = file_
self.lastPosition = position
def setFile(self, filename, duration, size): def setFile(self, filename, duration, size):
file_ = { file_ = {
"name": filename, "name": filename,
@ -327,10 +332,11 @@ class SyncplayUser(object):
return self.username < other.username return self.username < other.username
class SyncplayUserlist(object): class SyncplayUserlist(object):
def __init__(self, ui): def __init__(self, ui, client):
self.currentUser = SyncplayUser() self.currentUser = SyncplayUser()
self._users = {} self._users = {}
self.ui = ui self.ui = ui
self._client = client
def __showUserChangeMessage(self, username, room, file_): def __showUserChangeMessage(self, username, room, file_):
if (room and not file_): if (room and not file_):
@ -355,10 +361,10 @@ class SyncplayUserlist(object):
message = "Your file differs in the following way(s): " + ", ".join(differences) message = "Your file differs in the following way(s): " + ", ".join(differences)
self.ui.showMessage(message) self.ui.showMessage(message)
def addUser(self, username, room, file_, noMessage = False): def addUser(self, username, room, file_, position = 0, noMessage = False):
if(username == self.currentUser.username): if(username == self.currentUser.username):
return return
user = SyncplayUser(username, room, file_) user = SyncplayUser(username, room, file_, position)
self._users[username] = user self._users[username] = user
if(not noMessage): if(not noMessage):
self.__showUserChangeMessage(username, room, file_) self.__showUserChangeMessage(username, room, file_)
@ -387,7 +393,11 @@ class SyncplayUserlist(object):
self.addUser(username, room, file_) self.addUser(username, room, file_)
def __addUserWithFileToList(self, rooms, user): def __addUserWithFileToList(self, rooms, user):
file_key = '\'{}\' ({})'.format(user.file['name'], self.ui.formatTime(user.file['duration'])) if(self.currentUser.username == user.username):
currentPosition = self.ui.formatTime(self._client.getGlobalPosition())
else:
currentPosition = self.ui.formatTime(user.lastPosition)
file_key = '\'{}\' ({}/{})'.format(user.file['name'], currentPosition, self.ui.formatTime(user.file['duration']))
if (not rooms[user.room].has_key(file_key)): if (not rooms[user.room].has_key(file_key)):
rooms[user.room][file_key] = {} rooms[user.room][file_key] = {}
rooms[user.room][file_key][user.username] = user rooms[user.room][file_key][user.username] = user
@ -407,7 +417,10 @@ class SyncplayUserlist(object):
self.__addUserWithFileToList(rooms, user) self.__addUserWithFileToList(rooms, user)
else: else:
self.__addUserWithoutFileToList(rooms, user) self.__addUserWithoutFileToList(rooms, user)
self.__addUserWithFileToList(rooms, self.currentUser) if(self.currentUser.file):
self.__addUserWithFileToList(rooms, self.currentUser)
else:
self.__addUserWithoutFileToList(rooms, self.currentUser)
return rooms return rooms
def __addDifferentFileMessageIfNecessary(self, user, message): def __addDifferentFileMessageIfNecessary(self, user, message):

View File

@ -143,8 +143,9 @@ class SyncClientProtocol(JSONCommandProtocol):
roomName = room[0] roomName = room[0]
for user in room[1].iteritems(): for user in room[1].iteritems():
userName = user[0] userName = user[0]
file_ = user[1] if user[1] <> {} else None file_ = user[1]['file'] if user[1]['file'] <> {} else None
self._client.userlist.addUser(userName, roomName, file_, noMessage=True) position = user[1]['position']
self._client.userlist.addUser(userName, roomName, file_, position, noMessage=True)
self._client.userlist.showUserList() self._client.userlist.showUserList()
def sendList(self): def sendList(self):
@ -314,16 +315,20 @@ class SyncServerProtocol(JSONCommandProtocol):
user[username]["event"] = event user[username]["event"] = event
self.sendSet({"user": user}) self.sendSet({"user": user})
def _addUserOnList(self, userlist, watcher): def _addUserOnList(self, userlist, roomPositions, watcher):
if (not userlist.has_key(watcher.room)): if (not userlist.has_key(watcher.room)):
userlist[watcher.room] = {} userlist[watcher.room] = {}
userlist[watcher.room][watcher.name] = watcher.file if watcher.file else {} roomPositions[watcher.room] = watcher.getRoomPosition()
userlist[watcher.room][watcher.name] = {
"file": watcher.file if watcher.file else {},
"position": roomPositions[watcher.room] if roomPositions[watcher.room] else 0
}
def sendList(self): def sendList(self):
userlist = {} userlist = {}
roomPositions = {}
watchers = self._factory.getAllWatchers(self) watchers = self._factory.getAllWatchers(self)
for watcher in watchers.itervalues(): for watcher in watchers.itervalues():
self._addUserOnList(userlist, watcher) self._addUserOnList(userlist, roomPositions, watcher)
self.sendMessage({"List": userlist}) self.sendMessage({"List": userlist})
@requireLogged @requireLogged

View File

@ -74,16 +74,20 @@ class SyncFactory(Factory):
self._rooms.pop(room) self._rooms.pop(room)
self._roomStates.pop(room) self._roomStates.pop(room)
def getRoomPausedAndPosition(self, room):
position = self._roomStates[room]["position"]
paused = self._roomStates[room]["paused"]
if (not paused):
timePassedSinceSet = time.time() - self._roomStates[room]["lastUpdate"]
position += timePassedSinceSet
return paused, position
def sendState(self, watcherProtocol, doSeek = False, senderLatency = 0, forcedUpdate = False): def sendState(self, watcherProtocol, doSeek = False, senderLatency = 0, forcedUpdate = False):
watcher = self.getWatcher(watcherProtocol) watcher = self.getWatcher(watcherProtocol)
if(not watcher): if(not watcher):
return return
room = watcher.room room = watcher.room
position = self._roomStates[room]["position"] paused, position = self.getRoomPausedAndPosition(room)
paused = self._roomStates[room]["paused"]
if(not paused):
timePassedSinceSet = time.time() - self._roomStates[room]["lastUpdate"]
position += timePassedSinceSet
setBy = self._roomStates[room]["setBy"] setBy = self._roomStates[room]["setBy"]
watcher.paused = paused watcher.paused = paused
watcher.position = position watcher.position = position
@ -219,6 +223,10 @@ class Watcher(object):
else: else:
return self.position < b.position return self.position < b.position
def getRoomPosition(self):
_, position = self.factory.getRoomPausedAndPosition(self.room)
return position
def scheduleSendState(self, when=1): def scheduleSendState(self, when=1):
self._sendStateTimer = task.LoopingCall(self.sendState) self._sendStateTimer = task.LoopingCall(self.sendState)
self._sendStateTimer.start(when, True) self._sendStateTimer.start(when, True)
@ -234,4 +242,4 @@ class Watcher(object):
def deactivate(self): def deactivate(self):
if(self._sendStateTimer): if(self._sendStateTimer):
self._sendStateTimer.stop() self._sendStateTimer.stop()

View File

@ -79,7 +79,7 @@ class ConsoleUI(threading.Thread):
self._syncplayClient.setPosition(self._syncplayClient.playerPositionBeforeLastSeek) self._syncplayClient.setPosition(self._syncplayClient.playerPositionBeforeLastSeek)
self._syncplayClient.playerPositionBeforeLastSeek = tmp_pos self._syncplayClient.playerPositionBeforeLastSeek = tmp_pos
elif data == "l": elif data == "l":
self._syncplayClient.userlist.showUserList() self._syncplayClient.getUserList()
elif data == "p": elif data == "p":
self._syncplayClient.setPaused(not self._syncplayClient.getPlayerPaused()) self._syncplayClient.setPaused(not self._syncplayClient.getPlayerPaused())
elif data == 'help': elif data == 'help':