Optimize server code a bit

This commit is contained in:
Uriziel 2016-02-07 01:04:55 +01:00
parent c1300464a5
commit b98ba59886

View File

@ -239,6 +239,8 @@ class Room(object):
self._setBy = None self._setBy = None
self._playlist = [] self._playlist = []
self._playlistIndex = None self._playlistIndex = None
self.__lastUpdate = time.time()
self.__position = 0
def __str__(self, *args, **kwargs): def __str__(self, *args, **kwargs):
return self.getName() return self.getName()
@ -247,18 +249,22 @@ class Room(object):
return self._name return self._name
def getPosition(self): def getPosition(self):
if self._watchers: age = time.time() - self.__lastUpdate
if self._watchers and age > 1:
watcher = min(self._watchers.values()) watcher = min(self._watchers.values())
self._setBy = watcher self._setBy = watcher
return watcher.getPosition() self.__position = watcher.getPosition()
self.__lastUpdate = time.time()
return self.__position
else: else:
return 0 return self.__position + age if self._playState == self.STATE_PLAYING else 0
def setPaused(self, paused=STATE_PAUSED, setBy=None): def setPaused(self, paused=STATE_PAUSED, setBy=None):
self._playState = paused self._playState = paused
self._setBy = setBy self._setBy = setBy
def setPosition(self, position, setBy=None): def setPosition(self, position, setBy=None):
self.__position = position
for watcher in self._watchers.itervalues(): for watcher in self._watchers.itervalues():
watcher.setPosition(position) watcher.setPosition(position)
self._setBy = setBy self._setBy = setBy
@ -283,6 +289,8 @@ class Room(object):
return return
del self._watchers[watcher.getName()] del self._watchers[watcher.getName()]
watcher.setRoom(None) watcher.setRoom(None)
if not self._watchers:
self.__position = 0
def isEmpty(self): def isEmpty(self):
return not bool(self._watchers) return not bool(self._watchers)
@ -311,12 +319,15 @@ class ControlledRoom(Room):
self._controllers = {} self._controllers = {}
def getPosition(self): def getPosition(self):
if self._controllers: age = time.time() - self.__lastUpdate
if self._controllers and age > 1:
watcher = min(self._controllers.values()) watcher = min(self._controllers.values())
self._setBy = watcher self._setBy = watcher
return watcher.getPosition() self.__position = watcher.getPosition()
self.__lastUpdate = time.time()
return self.__position
else: else:
return 0 return self.__position + age if self._playState == self.STATE_PLAYING else 0
def addController(self, watcher): def addController(self, watcher):
self._controllers[watcher.getName()] = watcher self._controllers[watcher.getName()] = watcher