def __enrichMessage(msg, withGamePrefix=True): """ Add some optional prefixes to msg: S/C, process id, time, git commit. @param msg: The original message. @type msg: C{str} @param withGamePrefix: If set, prepend the game prefix. @type withGamePrefix: C{Boolean} @rtype: C{str} """ result = msg # set the default if withGamePrefix and Internal.logPrefix: result = '{prefix}{process}: {msg}'.format( prefix=Internal.logPrefix, process=os.getpid() if Debug.process else '', msg=msg) if Debug.time: result = '{:08.4f} {}'.format(elapsedSince(Debug.time), result) if Debug.git: head = gitHead() if head not in ('current', None): result = 'git:{}/p3 {}'.format(head, result) if int(Debug.callers): result = ' ' + result return result
def checkPings(self): """are all clients still alive? If not log them out""" since = elapsedSince(self.lastPing) if self.srvUsers and since > 30: if Debug.quit: logDebug('no ping since {} seconds but we still have users:{}'. format(elapsedSince(self.lastPing), self.srvUsers)) if not self.srvUsers and since > 30: # no user at all since 30 seconds, but we did already have a user self.__stopAfterLastDisconnect() for user in self.srvUsers: if elapsedSince(user.lastPing) > 60: logInfo( 'No messages from %s since 60 seconds, clearing connection now' % user.name) user.mind = None self.logout(user) reactor.callLater(10, self.checkPings)
def speak(what): """this is what the user of this module will call.""" # pylint: disable=too-many-branches if not Internal.Preferences.useSounds: return game = Internal.scene.game reactor = Internal.reactor if game and not game.autoPlay and Sound.playProcesses: # in normal play, wait a moment between two speaks. Otherwise # sometimes too many simultaneous speaks make them ununderstandable lastSpeakStart = max(x.startTime for x in Sound.playProcesses) if elapsedSince(lastSpeakStart) < 0.3: reactor.callLater(1, Sound.speak, what) return if os.path.exists(what): oggBinary = Sound.findOggBinary() if oggBinary: if os.name == 'nt': # convert to .wav, store .wav in cacheDir name, ext = os.path.splitext(what) assert ext == '.ogg', 'what: {} name: {} ext: {}'.format( what, name, ext) if 'bell' in name: nameParts = ['bell'] else: nameParts = os.path.normpath(name).split(os.sep) nameParts = nameParts[nameParts.index('voices') + 1:] wavName = os.path.normpath('{}/{}.wav'.format( cacheDir(), '_'.join(nameParts))) if not os.path.exists(wavName): args = [ oggBinary, '-a', '-w', wavName, os.path.normpath(what) ] startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW subprocess.call(args, startupinfo=startupinfo) if Debug.sound: logDebug('converted {} to wav {}'.format( what, wavName)) try: winsound.PlaySound( wavName, winsound.SND_FILENAME | winsound.SND_NODEFAULT) except RuntimeError: pass else: args = [oggBinary, '-q', what] if Debug.sound: game.debug(' '.join(args)) process = subprocess.Popen(args) process.startTime = datetime.datetime.now() process.name = what Sound.playProcesses.append(process) reactor.callLater(3, Sound.cleanProcesses) reactor.callLater(6, Sound.cleanProcesses)
def __enrichMessage(msg, withGamePrefix=True): """ Add some optional prefixes to msg: S/C, process id, time, git commit. @param msg: The original message. @type msg: C{unicode} @param withGamePrefix: If set, prepend the game prefix. @type withGamePrefix: C{Boolean} @rtype: C{unicode} """ result = msg # set the default if withGamePrefix and Internal.logPrefix: result = u'{prefix}{process}: {msg}'.format( prefix=Internal.logPrefix, process=os.getpid() if Debug.process else '', msg=msg) if Debug.time: result = u'{:08.4f} {}'.format(elapsedSince(Debug.time), result) if Debug.git: head = gitHead() if head not in ('current', None): result = u'git:{}/p{} {}'.format(head, '3' if isPython3 else '2', result) return result