def gotAnswer(self, rawAnswer): """convert the wired answer into something more useful""" if isinstance(rawAnswer, tuple): answer = nativeString(rawAnswer[0]) if isinstance(rawAnswer[1], tuple): self.args = nativeStringArgs(rawAnswer[1]) else: self.args = nativeStringArgs([rawAnswer[1]]) else: answer = nativeString(rawAnswer) self.args = None if answer in Message.defined: self.answer = Message.defined[answer] else: if Debug.deferredBlock: logDebug(u'Request %s ignores %s' % (self, rawAnswer))
def pName(self): """ Return self.propertyName() as a python string. @return: C{str} """ return nativeString(self.propertyName())
def perspective_setClientProperties( self, dbIdent, voiceId, maxGameId, clientVersion=None): """perspective_* methods are to be called remotely""" self.dbIdent = dbIdent self.voiceId = voiceId self.maxGameId = maxGameId clientVersion = nativeString(clientVersion) serverVersion = Internal.defaultPort if clientVersion != serverVersion: # we assume that versions x.y.* are compatible if clientVersion is None: # client passed no version info return fail(srvError(pb.Error, m18nE( 'Your client has a version older than 4.9.0 but you need %1 for this server'), serverVersion)) else: commonDigits = len([x for x in zip( clientVersion.split(b'.'), serverVersion.split(b'.')) if x[0] == x[1]]) if commonDigits < 2: return fail(srvError(pb.Error, m18nE( 'Your client has version %1 but you need %2 for this server'), clientVersion or '<4.9.0', '.'.join(serverVersion.split('.')[:2]) + '.*')) if Debug.table: logDebug(u'client has dbIdent={} voiceId={} maxGameId={} clientVersion {}'.format( self.dbIdent, self.voiceId, self.maxGameId, clientVersion)) self.server.sendTables(self)
def computeLastMeld(self): """compile hand info into a string as needed by the scoring engine""" if self.scoringDialog: # is None while ScoringGame is created cbLastMeld = self.scoringDialog.cbLastMeld idx = cbLastMeld.currentIndex() if idx >= 0: return Meld(nativeString( variantValue(cbLastMeld.itemData(idx)))) return Meld()
def remote_abort(self, tableid, message, *args): """the server aborted this game""" message = nativeString(message) args = nativeStringArgs(args) if self.table and self.table.tableid == tableid: # translate Robot to Roboter: if self.game: args = self.game.players.translatePlayerNames(args) logWarning(m18n(message, *args)) if self.game: self.game.close()
def __init__(self, player, command, kwargs): if isinstance(command, Message): self.message = command else: self.message = Message.defined[nativeString(command)] self.table = None self.notifying = False self._player = weakref.ref(player) if player else None self.token = kwargs['token'] self.kwargs = kwargs.copy() del self.kwargs['token'] self.score = None self.lastMeld = None for key, value in kwargs.items(): assert value != 'None' if value is None: self.__setattr__(key, None) elif key.lower().endswith('tile'): self.__setattr__(key, Tile(nativeString(value))) elif key.lower().endswith('tiles'): self.__setattr__(key, TileList(nativeString(value))) elif key.lower().endswith('meld'): self.__setattr__(key, Meld(nativeString(value))) elif key.lower().endswith('melds'): self.__setattr__(key, MeldList(nativeString(value))) elif key in ('wantedGame', 'score'): self.__setattr__(key, nativeString(value)) elif key == 'playerNames': self.__setattr__(key, self.convertWinds(value)) else: self.__setattr__(key, value)
def _initRuleset(self): """load ruleset headers but not the rules""" if isinstance(self.name, int): query = Query( "select id,hash,name,description from ruleset where id=?", (self.name,)) elif isinstance(self.name, list): # we got the rules over the wire self.rawRules = (nativeStringArgs(x) for x in self.name[1:]) (self.rulesetId, self.__hash, self.name, self.description) = self.name[0] self.__hash = nativeString(self.__hash) self.load() # load raw rules at once, rules from db only when needed return else: query = Query( "select id,hash,name,description from ruleset where hash=?", (nativeString(self.name), )) if len(query.records): (self.rulesetId, self.__hash, self.name, self.description) = query.records[0] else: raise Exception('ruleset %s not found' % self.name)
def data(self, index, role=None): """get score table from view""" if role is None: role = Qt.DisplayRole if not (index.isValid() and role == Qt.DisplayRole): return toQVariant() if role == Qt.DisplayRole: unformatted = unicode( self._resultRows[index.row()][index.column()]) if index.column() == 2: # we do not yet use this for listing remote games but if we do # this translation is needed for robot players names = [m18n(name) for name in unformatted.split('///')] return toQVariant(', '.join(names)) elif index.column() == 1: dateVal = datetime.datetime.strptime( unformatted, '%Y-%m-%dT%H:%M:%S') return toQVariant(nativeString(dateVal.strftime('%c'))) elif index.column() == 0: return toQVariant(int(unformatted)) with RealQVariant(): return QAbstractTableModel.data(self, index, role)
def m18nc(context, englishText, *args): """ Wrapper around i18n. i18n only accepts and returns native strings. @param context: The context for this string. @type context: C{str} @param englishText : The text template. @type englishText: C{str} (utf-8) or C{unicode}. @param args : Arguments for the text template. @type args: A list or tuple of strings. @return: The translated text with args inserted. @rtype: C{unicode}. """ result = unicodeString( i18nc( context, nativeString(englishText), *unicodeStringArgs(args))) if not args: ENGLISHDICT[result] = unicodeString(englishText) return result
def m18n(englishText, *args): """ Wrapper around i18n. @param englishText : The text template. @type englishText: C{str} (utf-8) or C{unicode}. @param args : Arguments for the text template. @type args: A list or tuple of strings. @return: The translated text with args inserted. @rtype: C{unicode}. Since when do args have to be unicode? utf-8 encoded fails. """ result = unicodeString( i18n( nativeString( englishText), *unicodeStringArgs(args))) if not args: ENGLISHDICT[result] = unicodeString(englishText) return result
def __new__(cls, value=None): if value is None: return unicode.__new__(cls) else: return unicode.__new__(cls, nativeString(value))
def logException(exception, withGamePrefix=True): """logs error message and re-raises exception""" logError(exception, withGamePrefix=withGamePrefix) raise Exception(nativeString(exception))
def perspective_newTable( self, ruleset, playOpen, autoPlay, wantedGame, tableId=None): """perspective_* methods are to be called remotely""" wantedGame = nativeString(wantedGame) return self.server.newTable(self, ruleset, playOpen, autoPlay, wantedGame, tableId)