예제 #1
0
  def update(self):
    clients = self.console.clients.getList()  
    
    scoreList = self.console.getPlayerScores() 
         
    self.verbose('Building XML status')
    xml = '<B3Status Time="%s">\n<Clients Total="%s">\n' % (time.asctime(), len(clients))
        
    for c in clients:
      if not c.name:
        c.name = "@"+str(c.id)
      if c.exactName == "^7":
        c.exactName = "@"+str(c.id)+"^7"

      if not c.maskedLevel:
        _level = c.maxLevel
      else:
        _level = c.maskedLevel

      try:          
        xml += '<Client Name="%s" ColorName="%s" DBID="%s" Connections="%s" CID="%s" Level="%s" GUID="%s" PBID="%s" IP="%s" Team="%s" Joined="%s" Updated="%s" Score="%s" State="%s">\n' % (escape("%s"%sanitizeMe(c.name)), escape("%s"%sanitizeMe(c.exactName)), c.id, c.connections, c.cid, _level, c.guid, c.pbid, c.ip, escape("%s"%c.team), time.ctime(c.timeAdd), time.ctime(c.timeEdit) , scoreList[c.cid], c.state )
        for k,v in c.data.iteritems():
          xml += '<Data Name="%s" Value="%s"/>' % (escape("%s"%k), escape("%s"%sanitizeMe(v))) 
            
        if self._tkPlugin:
          if hasattr(c, 'tkplugin_points'):       
            xml += '<TkPlugin Points="%s">\n' % c.var(self, 'points').toInt()
            if hasattr(c, 'tkplugin_attackers'):
              for acid,points in c.var(self, 'attackers').value.items():
                try:
                  xml += '<Attacker Name="%s" CID="%s" Points="%s"/>\n' % (self.console.clients[acid].name, acid, points)
                except:
                  pass
                
            xml += '</TkPlugin>\n'
        
        xml += '</Client>\n'
      except:
        pass

    c = self.console.game
    xml += '</Clients>\n<Game Name="%s" Type="%s" Map="%s" TimeLimit="%s" FragLimit="%s" CaptureLimit="%s" Rounds="%s">\n' % (escape("%s"%c.gameName), escape("%s"%c.gameType), escape("%s"%c.mapName), c.timeLimit, c.fragLimit, c.captureLimit, c.rounds)
    for k,v in self.console.game.__dict__.items():
      xml += '<Data Name="%s" Value="%s"/>\n' % (escape("%s"%k), escape("%s"%v)) 
    xml += '</Game>\n</B3Status>'
        

    self.writeXML(xml)
예제 #2
0
    def update(self):
        clients = self.console.clients.getList()
        scoreList = self.console.getPlayerScores() 
                 
        self.verbose('Building XML status')
        xml = Document()
        # --- Main section
        b3status = xml.createElement("B3Status")
        b3status.setAttribute("Time", time.asctime())
        xml.appendChild(b3status)

        # --- Game section
        c = self.console.game
        gamename = ''
        gametype = ''
        mapname = ''
        timelimit = ''
        fraglimit = ''
        capturelimit = ''
        rounds = ''
        roundTime = ''
        mapTime = ''
        if c.gameName:
            gamename = c.gameName
        if c.gameType:
            gametype = c.gameType
        if c.mapName:
            mapname = c.mapName
        if c.timelimit:
            timelimit = c.timelimit
        if c.fraglimit:
            fraglimit = c.fraglimit
        if c.captureLimit:
            capturelimit = c.captureLimit
        if c.rounds:
            rounds = c.rounds
        if c.roundTime:
            roundTime = c.roundTime()
        if c.mapTime:
            mapTime = c.mapTime()
        game = xml.createElement("Game")
        game.setAttribute("Name", str(gamename))
        game.setAttribute("Type", str(gametype))
        game.setAttribute("Map", str(mapname))
        game.setAttribute("TimeLimit", str(timelimit))
        game.setAttribute("FragLimit", str(fraglimit))
        game.setAttribute("CaptureLimit", str(capturelimit))
        game.setAttribute("Rounds", str(rounds))
        game.setAttribute("RoundTime", str(roundTime))
        game.setAttribute("MapTime", str(mapTime))
        b3status.appendChild(game)

        for k,v in self.console.game.__dict__.items():
            data = xml.createElement("Data")
            data.setAttribute("Name", str(k))
            data.setAttribute("Value", str(v))
            game.appendChild(data)
        # --- End Game section        

        # --- Clients section
        b3clients = xml.createElement("Clients")
        b3clients.setAttribute("Total", str(len(clients)))
        b3status.appendChild(b3clients)

        for c in clients:
            if not c.name:
                c.name = "@"+str(c.id)
            if c.exactName == "^7":
                c.exactName = "@"+str(c.id)+"^7"

            if not c.maskedLevel:
                _level = c.maxLevel
            else:
                _level = c.maskedLevel

            try:
                client = xml.createElement("Client")
                client.setAttribute("Name", str(sanitizeMe(c.name)))
                client.setAttribute("ColorName", str(sanitizeMe(c.exactName)))
                client.setAttribute("DBID", str(c.id))
                client.setAttribute("Connections", str(c.connections))
                client.setAttribute("CID", str(c.cid))
                client.setAttribute("Level", str(_level))
                if c.guid:
                    client.setAttribute("GUID", c.guid)
                else:
                    client.setAttribute("GUID", '')
                if c.pbid:
                    client.setAttribute("PBID", c.pbid)
                else:
                    client.setAttribute("PBID", '')
                client.setAttribute("IP", c.ip)
                client.setAttribute("Team", str(c.team))
                client.setAttribute("Joined", str(time.ctime(c.timeAdd)))
                client.setAttribute("Updated", str(time.ctime(c.timeEdit)))
                if scoreList and c.cid in scoreList:
                    client.setAttribute("Score", str(scoreList[c.cid]))
                client.setAttribute("State", str(c.state))
                b3clients.appendChild(client)

                for k,v in c.data.iteritems():
                    data = xml.createElement("Data")
                    data.setAttribute("Name", str(k))
                    data.setAttribute("Value", str(sanitizeMe(v)))
                    client.appendChild(data)
                        
                if self._tkPlugin:
                    if hasattr(c, 'tkplugin_points'):
                        tkplugin = xml.createElement("TkPlugin")
                        tkplugin.setAttribute("Points", str(c.var(self, 'points')))
                        client.appendChild(tkplugin)            
                        if hasattr(c, 'tkplugin_attackers'):
                            for acid,points in c.var(self, 'attackers').value.items():
                                try:
                                    attacker = xml.createElement("Attacker")
                                    attacker.setAttribute("Name", sanitizeMe(self.console.clients[acid].name))
                                    attacker.setAttribute("CID", str(acid))
                                    attacker.setAttribute("Points", str(points))
                                    tkplugin.appendChild(attacker)
                                except:
                                    pass
                                
            except Exception, err:
                self.debug('XML Failed: %r' % err)
                pass
예제 #3
0
    def update(self):
        clients = self.console.clients.getList()
        scoreList = self.console.getPlayerScores()

        self.verbose('Building XML status')
        xml = Document()
        # --- Main section
        b3status = xml.createElement("B3Status")
        b3status.setAttribute("Time", time.asctime())
        xml.appendChild(b3status)

        # --- Game section
        c = self.console.game
        gamename = ''
        gametype = ''
        mapname = ''
        timelimit = ''
        fraglimit = ''
        capturelimit = ''
        rounds = ''
        roundTime = ''
        mapTime = ''
        if c.gameName:
            gamename = c.gameName
        if c.gameType:
            gametype = c.gameType
        if c.mapName:
            mapname = c.mapName
        if c.timelimit:
            timelimit = c.timelimit
        if c.fraglimit:
            fraglimit = c.fraglimit
        if c.captureLimit:
            capturelimit = c.captureLimit
        if c.rounds:
            rounds = c.rounds
        if c.roundTime:
            roundTime = c.roundTime()
        if c.mapTime:
            mapTime = c.mapTime()
        game = xml.createElement("Game")
        game.setAttribute("Name", str(gamename))
        game.setAttribute("Type", str(gametype))
        game.setAttribute("Map", str(mapname))
        game.setAttribute("TimeLimit", str(timelimit))
        game.setAttribute("FragLimit", str(fraglimit))
        game.setAttribute("CaptureLimit", str(capturelimit))
        game.setAttribute("Rounds", str(rounds))
        game.setAttribute("RoundTime", str(roundTime))
        game.setAttribute("MapTime", str(mapTime))
        b3status.appendChild(game)

        for k, v in self.console.game.__dict__.items():
            data = xml.createElement("Data")
            data.setAttribute("Name", str(k))
            data.setAttribute("Value", str(v))
            game.appendChild(data)
            if self._enableDBsvarSaving:
                sql = "INSERT INTO current_svars (name, value) VALUES ('%s','%s') ON DUPLICATE KEY UPDATE value = VALUES(value);" % (
                    str(k), str(v))
                try:
                    self.console.storage.query(sql)
                except:
                    self.error('Error: inserting svars. sqlqry=%s' % (sql))
        if self._enableDBsvarSaving:
            sql = "INSERT INTO current_svars (name, value) VALUES ('lastupdate',UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE value = VALUES(value);"
            try:
                self.console.storage.query(sql)
            except:
                self.error('Error: inserting svars. sqlqry=%s' % (sql))
        # --- End Game section

        # --- Clients section
        b3clients = xml.createElement("Clients")
        b3clients.setAttribute("Total", str(len(clients)))
        b3status.appendChild(b3clients)

        if self._enableDBclientSaving:
            # empty table current_clients
            sql = "TRUNCATE TABLE `current_clients`;"
            self.console.storage.query(sql)

        for c in clients:
            if not c.name:
                c.name = "@" + str(c.id)
            if c.exactName == "^7":
                c.exactName = "@" + str(c.id) + "^7"

            if not c.maskedLevel:
                _level = c.maxLevel
            else:
                _level = c.maskedLevel

            try:
                client = xml.createElement("Client")
                client.setAttribute("Name", str(sanitizeMe(c.name)))
                client.setAttribute("ColorName", str(sanitizeMe(c.exactName)))
                client.setAttribute("DBID", str(c.id))
                client.setAttribute("Connections", str(c.connections))
                client.setAttribute("CID", str(c.cid))
                client.setAttribute("Level", str(_level))
                if c.guid:
                    client.setAttribute("GUID", c.guid)
                else:
                    client.setAttribute("GUID", '')
                if c.pbid:
                    client.setAttribute("PBID", c.pbid)
                else:
                    client.setAttribute("PBID", '')
                client.setAttribute("IP", c.ip)
                client.setAttribute("Team", str(c.team))
                client.setAttribute("Joined", str(time.ctime(c.timeAdd)))
                client.setAttribute("Updated", str(time.ctime(c.timeEdit)))
                if scoreList and c.cid in scoreList:
                    client.setAttribute("Score", str(scoreList[c.cid]))
                client.setAttribute("State", str(c.state))
                if self._enableDBclientSaving:
                    qryBuilderKey = ""
                    qryBuilderValue = ""
                    # get our attributes
                    for k, v in client.attributes.items():
                        # build the qrystring
                        qryBuilderKey = "%s%s," % (qryBuilderKey, k)
                        qryBuilderValue = "%s'%s'," % (qryBuilderValue, v)
                    # cut last ,
                    qryBuilderKey = qryBuilderKey[:-1]
                    qryBuilderValue = qryBuilderValue[:-1]
                    # and insert
                    try:
                        sql = "INSERT INTO current_clients (%s) VALUES (%s); " % (
                            qryBuilderKey, qryBuilderValue)
                        self.console.storage.query(sql)
                    except:
                        self.error('Error: inserting clients. sqlqry=%s' %
                                   (sql))

                b3clients.appendChild(client)
                for k, v in c.data.iteritems():
                    data = xml.createElement("Data")
                    data.setAttribute("Name", str(k))
                    data.setAttribute("Value", str(sanitizeMe(v)))
                    client.appendChild(data)

                if self._tkPlugin:
                    if hasattr(c, 'tkplugin_points'):
                        tkplugin = xml.createElement("TkPlugin")
                        tkplugin.setAttribute("Points",
                                              str(c.var(self, 'points')))
                        client.appendChild(tkplugin)
                        if hasattr(c, 'tkplugin_attackers'):
                            for acid, points in c.var(
                                    self, 'attackers').value.items():
                                try:
                                    attacker = xml.createElement("Attacker")
                                    attacker.setAttribute(
                                        "Name",
                                        sanitizeMe(
                                            self.console.clients[acid].name))
                                    attacker.setAttribute("CID", str(acid))
                                    attacker.setAttribute(
                                        "Points", str(points))
                                    tkplugin.appendChild(attacker)
                                except:
                                    pass

            except Exception, err:
                self.debug('XML Failed: %r' % err)
                pass
예제 #4
0
    def update(self):
        clients = self.console.clients.getList()
        scoreList = self.console.getPlayerScores() 
                 
        self.verbose('Building XML status')
        xml = Document()
        # --- Main section
        b3status = xml.createElement("B3Status")
        b3status.setAttribute("Time", time.asctime())
        xml.appendChild(b3status)

        # --- Game section
        c = self.console.game
        gamename = ''
        gametype = ''
        mapname = ''
        timelimit = ''
        fraglimit = ''
        capturelimit = ''
        rounds = ''
        roundTime = ''
        mapTime = ''
        if c.gameName:
            gamename = c.gameName
        if c.gameType:
            gametype = c.gameType
        if c.mapName:
            mapname = c.mapName
        if c.timelimit:
            timelimit = c.timelimit
        if c.fraglimit:
            fraglimit = c.fraglimit
        if c.captureLimit:
            capturelimit = c.captureLimit
        if c.rounds:
            rounds = c.rounds
        if c.roundTime:
            roundTime = c.roundTime()
        if c.mapTime:
            mapTime = c.mapTime()
        game = xml.createElement("Game")
        game.setAttribute("Name", str(gamename))
        game.setAttribute("Type", str(gametype))
        game.setAttribute("Map", str(mapname))
        game.setAttribute("TimeLimit", str(timelimit))
        game.setAttribute("FragLimit", str(fraglimit))
        game.setAttribute("CaptureLimit", str(capturelimit))
        game.setAttribute("Rounds", str(rounds))
        game.setAttribute("RoundTime", str(roundTime))
        game.setAttribute("MapTime", str(mapTime))
        b3status.appendChild(game)

        for k,v in self.console.game.__dict__.items():
            data = xml.createElement("Data")
            data.setAttribute("Name", str(k))
            data.setAttribute("Value", str(v))
            game.appendChild(data)
            if self._enableDBsvarSaving:
                sql = "INSERT INTO current_svars (name, value) VALUES ('%s','%s') ON DUPLICATE KEY UPDATE value = VALUES(value);" % (str(k),str(v))
                try:
                    self.console.storage.query(sql)
                except:
                    self.error('Error: inserting svars. sqlqry=%s' % (sql))
        if self._enableDBsvarSaving:
            sql = "INSERT INTO current_svars (name, value) VALUES ('lastupdate',UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE value = VALUES(value);")
            try:
                self.console.storage.query(sql)
            except:
                self.error('Error: inserting svars. sqlqry=%s' % (sql))
        # --- End Game section        

        # --- Clients section
        b3clients = xml.createElement("Clients")
        b3clients.setAttribute("Total", str(len(clients)))
        b3status.appendChild(b3clients)

        if self._enableDBclientSaving:
            # empty table current_clients
            sql = "TRUNCATE TABLE `current_clients`;"
            self.console.storage.query(sql)

        for c in clients:
            if not c.name:
                c.name = "@"+str(c.id)
            if c.exactName == "^7":
                c.exactName = "@"+str(c.id)+"^7"

            if not c.maskedLevel:
                _level = c.maxLevel
            else:
                _level = c.maskedLevel

            try:
                client = xml.createElement("Client")
                client.setAttribute("Name", str(sanitizeMe(c.name)))
                client.setAttribute("ColorName", str(sanitizeMe(c.exactName)))
                client.setAttribute("DBID", str(c.id))
                client.setAttribute("Connections", str(c.connections))
                client.setAttribute("CID", str(c.cid))
                client.setAttribute("Level", str(_level))
                if c.guid:
                    client.setAttribute("GUID", c.guid)
                else:
                    client.setAttribute("GUID", '')
                if c.pbid:
                    client.setAttribute("PBID", c.pbid)
                else:
                    client.setAttribute("PBID", '')
                client.setAttribute("IP", c.ip)
                client.setAttribute("Team", str(c.team))
                client.setAttribute("Joined", str(time.ctime(c.timeAdd)))
                client.setAttribute("Updated", str(time.ctime(c.timeEdit)))
                if scoreList and c.cid in scoreList:
                    client.setAttribute("Score", str(scoreList[c.cid]))
                client.setAttribute("State", str(c.state))
                if self._enableDBclientSaving:
                    qryBuilderKey = ""
                    qryBuilderValue = ""
                    # get our attributes
                    for k, v in client.attributes.items():
                        # build the qrystring
                        qryBuilderKey = "%s%s," % (qryBuilderKey, k)
                        qryBuilderValue = "%s'%s'," % (qryBuilderValue, v)
                    # cut last ,
                    qryBuilderKey = qryBuilderKey[:-1]
                    qryBuilderValue = qryBuilderValue[:-1]
                    # and insert
                    try:
                        sql = "INSERT INTO current_clients (%s) VALUES (%s); " % (qryBuilderKey,qryBuilderValue)
                        self.console.storage.query(sql)
                    except:
                        self.error('Error: inserting clients. sqlqry=%s' % (sql))

                b3clients.appendChild(client)
                for k,v in c.data.iteritems():
                    data = xml.createElement("Data")
                    data.setAttribute("Name", str(k))
                    data.setAttribute("Value", str(sanitizeMe(v)))
                    client.appendChild(data)
                        
                if self._tkPlugin:
                    if hasattr(c, 'tkplugin_points'):
                        tkplugin = xml.createElement("TkPlugin")
                        tkplugin.setAttribute("Points", str(c.var(self, 'points')))
                        client.appendChild(tkplugin)            
                        if hasattr(c, 'tkplugin_attackers'):
                            for acid,points in c.var(self, 'attackers').value.items():
                                try:
                                    attacker = xml.createElement("Attacker")
                                    attacker.setAttribute("Name", sanitizeMe(self.console.clients[acid].name))
                                    attacker.setAttribute("CID", str(acid))
                                    attacker.setAttribute("Points", str(points))
                                    tkplugin.appendChild(attacker)
                                except:
                                    pass
                                
            except Exception, err:
                self.debug('XML Failed: %r' % err)
                pass
예제 #5
0
    def update(self):
        clients = self.console.clients.getList()
        scoreList = self.console.getPlayerScores() 
                 
        self.verbose('Building XML status')
        xml = Document()
        b3status = xml.createElement("B3Status")
        b3status.setAttribute("Time", time.asctime())
        xml.appendChild(b3status)
        b3clients = xml.createElement("Clients")
        b3clients.setAttribute("Total", str(len(clients)))
        b3status.appendChild(b3clients)

        for c in clients:
            if not c.name:
                c.name = "@"+str(c.id)
            if c.exactName == "^7":
                c.exactName = "@"+str(c.id)+"^7"

            if not c.maskedLevel:
                _level = c.maxLevel
            else:
                _level = c.maskedLevel

            try:
                client = xml.createElement("Client")
                client.setAttribute("Name", str(sanitizeMe(c.name)))
                client.setAttribute("ColorName", str(sanitizeMe(c.exactName)))
                client.setAttribute("DBID", str(c.id))
                client.setAttribute("Connections", str(c.connections))
                client.setAttribute("CID", str(c.cid))
                client.setAttribute("Level", str(_level))
                if c.guid:
                    client.setAttribute("GUID", c.guid)
                else:
                    client.setAttribute("GUID", '')
                if c.pbid:
                    client.setAttribute("PBID", c.pbid)
                else:
                    client.setAttribute("PBID", '')
                client.setAttribute("IP", c.ip)
                client.setAttribute("Team", str(c.team))
                client.setAttribute("Joined", str(time.ctime(c.timeAdd)))
                client.setAttribute("Updated", str(time.ctime(c.timeEdit)))
                if c.cid in scoreList:
                    client.setAttribute("Score", str(scoreList[c.cid]))
                client.setAttribute("State", str(c.state))
                b3clients.appendChild(client)

                for k,v in c.data.iteritems():
                    data = xml.createElement("Data")
                    data.setAttribute("Name", str(k))
                    data.setAttribute("Value", str(sanitizeMe(v)))
                    client.appendChild(data)
                        
                if self._tkPlugin:
                    if hasattr(c, 'tkplugin_points'):
                        tkplugin = xml.createElement("TkPlugin")
                        tkplugin.setAttribute("Points", str(c.var(self, 'points')))
                        client.appendChild(tkplugin)            
                        if hasattr(c, 'tkplugin_attackers'):
                            for acid,points in c.var(self, 'attackers').value.items():
                                try:
                                    attacker = xml.createElement("Attacker")
                                    attacker.setAttribute("Name", sanitizeMe(self.console.clients[acid].name))
                                    attacker.setAttribute("CID", str(acid))
                                    attacker.setAttribute("Points", str(points))
                                    tkplugin.appendChild(attacker)
                                except:
                                    pass
                                
            except Exception, err:
                self.debug('XML Failed: %r' % err)
                pass
예제 #6
0
    def update(self):
        """
        Update XML/DB status.
        """
        clients = self.console.clients.getList()
        score_list = self.console.getPlayerScores()
                 
        self.verbose('building XML status')
        xml = Document()

        # --- Main section
        b3status = xml.createElement("B3Status")
        b3status.setAttribute("Time", time.asctime())
        xml.appendChild(b3status)

        # --- Game section
        c = self.console.game
        gamename = ''
        gametype = ''
        mapname = ''
        timelimit = ''
        fraglimit = ''
        capturelimit = ''
        rounds = ''
        round_time = ''
        map_time = ''

        if c.gameName:
            gamename = c.gameName
        if c.gameType:
            gametype = c.gameType
        if c.mapName:
            mapname = c.mapName
        if c.timeLimit:
            timelimit = c.timeLimit
        if c.fragLimit:
            fraglimit = c.fragLimit
        if c.captureLimit:
            capturelimit = c.captureLimit
        if c.rounds:
            rounds = c.rounds
        if c.roundTime:
            round_time = c.roundTime()
        if c.mapTime():
            map_time = c.mapTime()

        # For XML:
        game = xml.createElement("Game")
        game.setAttribute("Ip", str(self.console._publicIp))
        game.setAttribute("Port", str(self.console._port))
        game.setAttribute("Name", str(gamename))
        game.setAttribute("Type", str(gametype))
        game.setAttribute("Map", str(mapname))
        game.setAttribute("TimeLimit", str(timelimit))
        game.setAttribute("FragLimit", str(fraglimit))
        game.setAttribute("CaptureLimit", str(capturelimit))
        game.setAttribute("Rounds", str(rounds))
        game.setAttribute("RoundTime", str(round_time))
        game.setAttribute("MapTime", str(map_time))
        game.setAttribute("OnlinePlayers", str(len(clients)))
        b3status.appendChild(game)

        # For DB:
        if self._enableDBsvarSaving:
            # EMPTY DB SVARS TABLE
            self.verbose('cleaning database table: %s...' % self._tables['svars'])
            self.console.storage.truncateTable(self._tables['svars'])
            # ADD NEW DATA
            self.storeServerinfo("Ip", str(self.console._publicIp))
            self.storeServerinfo("Port", str(self.console._port))
            self.storeServerinfo("Name", str(gamename))
            self.storeServerinfo("Type", str(gametype))
            self.storeServerinfo("Map", str(mapname))
            self.storeServerinfo("TimeLimit", str(timelimit))
            self.storeServerinfo("FragLimit", str(fraglimit))
            self.storeServerinfo("CaptureLimit",str(capturelimit) )
            self.storeServerinfo("Rounds", str(rounds))
            self.storeServerinfo("RoundTime", str(round_time))
            self.storeServerinfo("MapTime", str(map_time))
            self.storeServerinfo("OnlinePlayers", str(len(clients)))
            self.storeServerinfo("lastupdate", str(int(time.time())))

        for k, v in self.console.game.__dict__.items():
            data = xml.createElement("Data")
            data.setAttribute("Name", str(k))
            data.setAttribute("Value", str(v))
            game.appendChild(data)
            self.storeServerinfo(k, v)

        # --- End Game section        

        # --- Clients section
        b3clients = xml.createElement("Clients")
        b3clients.setAttribute("Total", str(len(clients)))
        b3status.appendChild(b3clients)

        if self._enableDBclientSaving:
            # EMPTY DB CVARS TABLE
            self.verbose('cleaning database table: %s...' % self._tables['cvars'])
            self.console.storage.truncateTable(self._tables['cvars'])

        for c in clients:

            if not c.name:
                c.name = "@" + str(c.id)

            if c.exactName == "^7":
                c.exactName = "@" + str(c.id) + "^7"

            if not c.maskedLevel:
                level = c.maxLevel
            else:
                level = c.maskedLevel
            try:
                client = xml.createElement("Client")
                client.setAttribute("Name", sanitizeMe(c.name))
                client.setAttribute("ColorName", sanitizeMe(c.exactName))
                client.setAttribute("DBID", str(c.id))
                client.setAttribute("Connections", str(c.connections))
                client.setAttribute("CID", c.cid)
                client.setAttribute("Level", str(level))

                if c.guid:
                    client.setAttribute("GUID", c.guid)
                else:
                    client.setAttribute("GUID", '')
                if c.pbid:
                    client.setAttribute("PBID", c.pbid)
                else:
                    client.setAttribute("PBID", '')

                client.setAttribute("IP", c.ip)
                client.setAttribute("Team", str(c.team))
                client.setAttribute("Joined", str(time.ctime(c.timeAdd)))
                client.setAttribute("Updated", str(time.ctime(c.timeEdit)))

                if score_list and c.cid in score_list:
                    client.setAttribute("Score", str(score_list[c.cid]))
                else:
                    client.setAttribute("Score", 0)

                client.setAttribute("State", str(c.state))

                if self._enableDBclientSaving:
                    builder_key = ""
                    builder_value = ""
                    # get our attributes
                    for k, v in client.attributes.items():
                        # build the qrystring
                        builder_key = "%s%s," % (builder_key, k)
                        if isinstance(v, basestring):
                            if "'" in v:
                                v = "%s" % v.replace("'", "\\'")
                        builder_value = "%s'%s'," % (builder_value, v)

                    # cut last ,
                    builder_key = builder_key[:-1]
                    builder_value = builder_value[:-1]

                    try:
                        self.console.storage.query("""INSERT INTO %s (%s) VALUES (%s);""" % (self._tables['cvars'], builder_key, builder_value))
                    except Exception:
                        # exception is already logged, just don't raise it again
                        pass

                b3clients.appendChild(client)

                for k, v in c.data.iteritems():
                    data = xml.createElement("Data")
                    data.setAttribute("Name", "%s" % k)

                    try:
                        clean_data = sanitizeMe(str(v))
                    except Exception, err:
                        self.error("could not sanitize %r" % v, exc_info=err)
                        data.setAttribute("Value", "")
                    else:
                        data.setAttribute("Value", clean_data)

                    client.appendChild(data)
                        
                if self._tkPlugin:
                    if hasattr(c, 'tkplugin_points'):
                        tkplugin = xml.createElement("TkPlugin")
                        tkplugin.setAttribute("Points", str(c.var(self, 'points')))
                        client.appendChild(tkplugin)            
                        if hasattr(c, 'tkplugin_attackers'):
                            for acid, points in c.var(self, 'attackers').value.items():
                                try:
                                    attacker = xml.createElement("Attacker")
                                    attacker.setAttribute("Name", sanitizeMe(self.console.clients[acid].name))
                                    attacker.setAttribute("CID", str(acid))
                                    attacker.setAttribute("Points", str(points))
                                    tkplugin.appendChild(attacker)
                                except Exception, e:
                                    self.warning('could not append child node in XML tree: %s' % e)
                                    pass
예제 #7
0
    def update(self):
        """
        Update XML/DB status.
        """
        clients = self.console.clients.getList()
        score_list = self.console.getPlayerScores()

        self.verbose('building XML status')
        xml = Document()

        # --- Main section
        b3status = xml.createElement("B3Status")
        b3status.setAttribute("Time", time.asctime())
        xml.appendChild(b3status)

        # --- Game section
        c = self.console.game
        gamename = ''
        gametype = ''
        mapname = ''
        timelimit = ''
        fraglimit = ''
        capturelimit = ''
        rounds = ''
        round_time = ''
        map_time = ''

        if c.gameName:
            gamename = c.gameName
        if c.gameType:
            gametype = c.gameType
        if c.mapName:
            mapname = c.mapName
        if c.timeLimit:
            timelimit = c.timeLimit
        if c.fragLimit:
            fraglimit = c.fragLimit
        if c.captureLimit:
            capturelimit = c.captureLimit
        if c.rounds:
            rounds = c.rounds
        if c.roundTime:
            round_time = c.roundTime()
        if c.mapTime():
            map_time = c.mapTime()

        # For XML:
        game = xml.createElement("Game")
        game.setAttribute("Ip", str(self.console._publicIp))
        game.setAttribute("Port", str(self.console._port))
        game.setAttribute("Name", str(gamename))
        game.setAttribute("Type", str(gametype))
        game.setAttribute("Map", str(mapname))
        game.setAttribute("TimeLimit", str(timelimit))
        game.setAttribute("FragLimit", str(fraglimit))
        game.setAttribute("CaptureLimit", str(capturelimit))
        game.setAttribute("Rounds", str(rounds))
        game.setAttribute("RoundTime", str(round_time))
        game.setAttribute("MapTime", str(map_time))
        game.setAttribute("OnlinePlayers", str(len(clients)))
        b3status.appendChild(game)

        # For DB:
        if self._enableDBsvarSaving:
            # EMPTY DB SVARS TABLE
            self.verbose('cleaning database table: %s...' %
                         self._tables['svars'])
            self.console.storage.truncateTable(self._tables['svars'])
            # ADD NEW DATA
            self.storeServerinfo("Ip", str(self.console._publicIp))
            self.storeServerinfo("Port", str(self.console._port))
            self.storeServerinfo("Name", str(gamename))
            self.storeServerinfo("Type", str(gametype))
            self.storeServerinfo("Map", str(mapname))
            self.storeServerinfo("TimeLimit", str(timelimit))
            self.storeServerinfo("FragLimit", str(fraglimit))
            self.storeServerinfo("CaptureLimit", str(capturelimit))
            self.storeServerinfo("Rounds", str(rounds))
            self.storeServerinfo("RoundTime", str(round_time))
            self.storeServerinfo("MapTime", str(map_time))
            self.storeServerinfo("OnlinePlayers", str(len(clients)))
            self.storeServerinfo("lastupdate", str(int(time.time())))

        for k, v in list(self.console.game.__dict__.items()):
            data = xml.createElement("Data")
            data.setAttribute("Name", str(k))
            data.setAttribute("Value", str(v))
            game.appendChild(data)
            self.storeServerinfo(k, v)

        # --- End Game section

        # --- Clients section
        b3clients = xml.createElement("Clients")
        b3clients.setAttribute("Total", str(len(clients)))
        b3status.appendChild(b3clients)

        if self._enableDBclientSaving:
            # EMPTY DB CVARS TABLE
            self.verbose('cleaning database table: %s...' %
                         self._tables['cvars'])
            self.console.storage.truncateTable(self._tables['cvars'])

        for c in clients:

            if not c.name:
                c.name = "@" + str(c.id)

            if c.exactName == "^7":
                c.exactName = "@" + str(c.id) + "^7"

            if not c.maskedLevel:
                level = c.maxLevel
            else:
                level = c.maskedLevel
            try:
                client = xml.createElement("Client")
                client.setAttribute("Name", sanitizeMe(c.name))
                client.setAttribute("ColorName", sanitizeMe(c.exactName))
                client.setAttribute("DBID", str(c.id))
                client.setAttribute("Connections", str(c.connections))
                client.setAttribute("CID", c.cid)
                client.setAttribute("Level", str(level))

                if c.guid:
                    client.setAttribute("GUID", c.guid)
                else:
                    client.setAttribute("GUID", '')
                if c.pbid:
                    client.setAttribute("PBID", c.pbid)
                else:
                    client.setAttribute("PBID", '')

                client.setAttribute("IP", c.ip)
                client.setAttribute("Team", str(c.team))
                client.setAttribute("Joined", str(time.ctime(c.timeAdd)))
                client.setAttribute("Updated", str(time.ctime(c.timeEdit)))

                if score_list and c.cid in score_list:
                    client.setAttribute("Score", str(score_list[c.cid]))
                else:
                    client.setAttribute("Score", 0)

                client.setAttribute("State", str(c.state))

                if self._enableDBclientSaving:
                    builder_key = ""
                    builder_value = ""
                    # get our attributes
                    for k, v in list(client.attributes.items()):
                        # build the qrystring
                        builder_key = "%s%s," % (builder_key, k)
                        if isinstance(v, str):
                            if "'" in v:
                                v = "%s" % v.replace("'", "\\'")
                        builder_value = "%s'%s'," % (builder_value, v)

                    # cut last ,
                    builder_key = builder_key[:-1]
                    builder_value = builder_value[:-1]

                    try:
                        self.console.storage.query(
                            """INSERT INTO %s (%s) VALUES (%s);""" %
                            (self._tables['cvars'], builder_key,
                             builder_value))
                    except Exception:
                        # exception is already logged, just don't raise it again
                        pass

                b3clients.appendChild(client)

                for k, v in c.data.items():
                    data = xml.createElement("Data")
                    data.setAttribute("Name", "%s" % k)

                    try:
                        clean_data = sanitizeMe(str(v))
                    except Exception as err:
                        self.error("could not sanitize %r" % v, exc_info=err)
                        data.setAttribute("Value", "")
                    else:
                        data.setAttribute("Value", clean_data)

                    client.appendChild(data)

                if self._tkPlugin:
                    if hasattr(c, 'tkplugin_points'):
                        tkplugin = xml.createElement("TkPlugin")
                        tkplugin.setAttribute("Points",
                                              str(c.var(self, 'points')))
                        client.appendChild(tkplugin)
                        if hasattr(c, 'tkplugin_attackers'):
                            for acid, points in list(
                                    c.var(self, 'attackers').value.items()):
                                try:
                                    attacker = xml.createElement("Attacker")
                                    attacker.setAttribute(
                                        "Name",
                                        sanitizeMe(
                                            self.console.clients[acid].name))
                                    attacker.setAttribute("CID", str(acid))
                                    attacker.setAttribute(
                                        "Points", str(points))
                                    tkplugin.appendChild(attacker)
                                except Exception as e:
                                    self.warning(
                                        'could not append child node in XML tree: %s'
                                        % e)
                                    pass

            except Exception as err:
                self.error('XML Failed: %r' % err, exc_info=err)
                pass

        # --- End Clients section

        self.writeXML(xml.toprettyxml(encoding="UTF-8", indent="        "))