Ejemplo n.º 1
0
    def __init__(self, bot):
        if self.name == NotImplemented:
            raise NotImplementedError(__class__)

        self.pre_init(bot)

        self.last_check = 0
        self.listed = []

        def _itrxn(txn):
            txn.execute("""
                CREATE TABLE IF NOT EXISTS
                tracker_%ss
                (
                    id          INTEGER PRIMARY KEY AUTOINCREMENT,
                    meta        TEXT,
                    start       INT,
                    length      INT,
                    deleted     INT
                )            
            """ % self.name )
            txn.execute("""
                SELECT
                id, meta, start, length, deleted
                FROM tracker_%ss
            """% self.name)
            return txn.fetchall()
        d = db.runInteraction(_itrxn)

        def _fillItems(items):
            self.listed.extend(items)
            return True

        self.dbready = d.addCallback(self.ItemClass.from_results, self).addCallback(_fillItems)
Ejemplo n.º 2
0
    def pickup_game_started(self, game, players, captains):
        def _insertGame(txn):
            txn.execute("""
                INSERT INTO
                pickup_games(game, time, players, captains)
                VALUES(:game, :time, :players, :captains)
            """,
            {
                'game': game.nick,
                'time': itime(),
                'players': json.dumps(players, separators=(',',':')),
                'captains': json.dumps(captains, separators=(',',':')),
            })

            txn.execute("SELECT last_insert_rowid() AS id")
            result = txn.fetchall()

            id_ = result[0][0]

            def _insertPlayers(playerlist):
                for player in playerlist:
                    if isinstance(player, StringTypes):
                        db.runOperation("""INSERT INTO
                            pickup_players_games(game_id, name, game, time)
                            VALUES(?, ?, ?, ?)
                        """, (id_, player, game.nick, itime()))
                    else:
                        _insertPlayers(player)
            _insertPlayers(players)
            return id_
        def _gotId(id_):
            self.pypickupbot.cmsg("Lastgame id: {0}".format(id_))
            self.pypickupbot.fire('pickup_lastgame_id', id_, game, players, captains)
        return db.runInteraction(_insertGame).addCallback(_gotId)
Ejemplo n.º 3
0
    def top10(self, call, args):
        """!top10 [game [game ..]]
        
        Shows who participated most in said game(s)."""
        games_ = self.pickup.get_games(call, args)
        games = [game.nick for game in games_.games]

        def _doTransaction(txn):
            params = dict(zip([str(i) for i in range(len(games))], games))
            params.update({'time': itime()-config.getduration("Pickup player tracking", "top10 spread")})
            txn.execute("""
                SELECT name FROM pickup_players_games
                WHERE
                    (""" + \
                    ' OR '.join(['game=:%d' % i for i in range(len(games))]) \
                    + """)
                    AND time>:time""", params )

            players = {}

            for (player,) in txn.fetchall():
                try:
                    players[player] += 1
                except KeyError:
                    players[player] = 1

            ordered = players.items()
            ordered.sort(None, lambda x: x[1], True)

            return ordered[:10]

        d = db.runInteraction(_doTransaction)

        def _cback(playerlist):
            o = [config.get('Pickup player tracking', 'top10 player').decode('string-escape') % {
                    'player': player,
                    'count': count,
                    }
                for player, count in playerlist]
            call.reply(
                config.get('Pickup player tracking', 'top10').decode('string-escape') % {
                    'playerlist': ', '.join(o),
                    'games': ' '.join(games)
                }, ', ')
        d.addCallback(_cback)
Ejemplo n.º 4
0
    def update_db(self):
        """Updates this item's DB entry or creates it if
        self.id == None"""

        if self.id == None:
            q = """
                INSERT INTO
                tracker_%ss(meta, start, length, deleted)
                VALUES(:meta, :start, :length, :deleted)
            """
        else:
            q = """
                UPDATE tracker_%ss
                SET
                    meta = :meta,
                    start = :start,
                    length = :length,
                    deleted = :deleted
                WHERE id = :id
            """

        def _itrxn(txn):
            txn.execute(q % self.tracker.name, {
                    'meta': json.dumps(self.meta),
                    'start': self.start,
                    'length': self.length,
                    'deleted': self.deleted,
                    'id': self.id
                })
            if self.id == None:
                txn.execute("SELECT last_insert_rowid() AS id")
                return txn.fetchall()[0][0]

        def _knowId(id=None):
            if self.id == None:
                self.id = id
            return self

        return db.runInteraction(_itrxn).addCallback(_knowId)