def lastgames(self, call, args):
        """!lastgames [game [game ..]]

        List last games played in given modes."""
        games_ = self.pickup.get_games(call, args)
        games = [game.nick for game in games_.games]
        params = dict(zip([str(i) for i in range(len(games))], games)) 

        d = db.runQuery("""
            SELECT game, time, id
            FROM pickup_games
            WHERE """+ ' OR '.join(['game=:%d' % i for i in range(len(games))]) +"""
            ORDER BY time DESC
            LIMIT 10
        """, params)
        def _printResult(r):
            o = []
            for nick, ts, id in r:
                date = datetime.fromtimestamp(ts)
                o.append(config.get('Pickup player tracking', 'lastgames game').decode('string-escape') % {
                    'year': date.year,
                    'month': date.month,
                    'day': date.day,
                    'hour': date.hour,
                    'minutes': date.minute,
                    'nick': nick,
                    'id': id,
                })
            o.reverse()
            call.reply(config.get('Pickup player tracking', 'lastgames').decode('string-escape')%{
                'games': ', '.join(games),
                'lastgames': config.get('Pickup player tracking', 'lastgames separator').decode('string-escape').join(o)
                }, config.get('Pickup player tracking', 'lastgames separator').decode('string-escape'))
        d.addCallback(_printResult)
Example #2
0
 def joinedHomeChannel(self):
     """when home channel joined, add motd to topic"""
     d = db.runQuery("""
         SELECT val
         FROM meta
         WHERE key="motd" """)
     self.current_topic = None
     self.motd = []
     self.motd_str = []
     def _setTopic(r):
         if len(r):
             self.motd_from_str(r[0][0])
             self.setmotd()
         else:
             db.runOperation("""
                 INSERT INTO
                 meta(key, val)
                 VALUES("motd", "")
             """)
     d.addCallback(_setTopic)
    def lastgame(self, call, args):
        """!lastgame [#id|game [game ..]]
        
        Shows when the last game or game given by id started and which players were in it"""
        if len(args) == 1 and args[0].startswith('#'):
            try:
                id = int(args[0][1:])
            except ValueError:
                raise InputError("Game id must be an integer")
            d = db.runQuery("""
                SELECT game, time, players, captains, id
                FROM pickup_games
                WHERE id=?
                LIMIT 1""", (id,))
        else:
            games_ = self.pickup.get_games(call, args)
            games = [game.nick for game in games_.games]
            params = dict(zip([str(i) for i in range(len(games))], games)) 
            d = db.runQuery("""
                SELECT game, time, players, captains, id
                FROM pickup_games
                WHERE """+ ' OR '.join(['game=:%d' % i for i in range(len(games))]) +"""
                ORDER BY time DESC
                LIMIT 1""", params)
        def _printResult(r):
            if len(r) < 1:
                if args and args[0].startswith('#'):
                    call.reply(_("No record for game #{0}").format(id))
                else:
                    call.reply(_("No game played yet in mode(s): {0}").format(' '.join(games)))
                return
            gamenick, gtime, players_, captains_, id_ = r[0]
            if ',' in players_:
                players = json.loads(players_)
                captains = json.loads(captains_)
            else:
                players = players_.split()
                captains = captains_.split()
            try:
                game = self.pickup.get_game(call, [gamenick])
                gamename = game.name
                teamnameFactory = game.teamname
            except InputError:
                gamename = _("Unknown(%s)") % gamenick
                teamnameFactory = lambda i: _("Team {0}").format(i + 1)

            timestr = str_from_timediff(itime()-gtime)

            if captains:
                call.reply(config.get('Pickup player tracking', 'lastgame').decode('string-escape') % \
                    {
                        'name': gamename,
                        'nick': gamenick,
                        'id': id_,
                        'when': timestr,
                        'playerlist': ', '.join(players),
                        'captainlist': ', '.join(captains)
                    })
            elif not isinstance(players[0], StringTypes):
                call.reply(config.get('Pickup player tracking', 'lastgame autopick').decode('string-escape') % \
                    {
                        'name': gamename,
                        'nick': gamenick,
                        'id': id_,
                        'when': timestr,
                        'teamslist': ', '.join([
                            config.get('Pickup messages', 'game ready autopick team').decode('string-escape')%
                            {
                                'name': teamnameFactory(i),
                                'players': ', '.join(team)
                            }
                            for i, team in enumerate(players)])
                    })
            else:
                call.reply(config.get('Pickup player tracking', 'lastgame nocaptains').decode('string-escape') % \
                    {
                        'name': gamename,
                        'nick': gamenick,
                        'id': id_,
                        'when': timestr,
                        'playerlist': ', '.join(players),
                    })
        d.addCallback(_printResult)