From 883287c2615df41898fe8177c7b494d0a983fc7a Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sun, 31 Aug 2003 08:42:07 +0000 Subject: [PATCH] Added bestuptime command. --- plugins/FunDB.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/plugins/FunDB.py b/plugins/FunDB.py index aff9cc48f..05d6cf478 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -36,6 +36,8 @@ Provides fun commands that require a database to operate. from baseplugin import * import sets +import time +import atexit import string import random import os.path @@ -100,6 +102,10 @@ def makeDb(dbfilename, replace=False): city TEXT, state CHAR(2) )""") + cursor.execute("""CREATE TABLE uptime ( + started INTEGER UNIQUE ON CONFLICT IGNORE, + ended INTEGER + )""") db.commit() return db @@ -123,10 +129,17 @@ class FunDB(callbacks.Privmsg): database-backed commands for crossword puzzle solving, anagram searching, larting, excusing, and insulting. """ + _tables = sets.Set(['lart', 'insult', 'excuse', 'praise']) def __init__(self): callbacks.Privmsg.__init__(self) - self._tables = sets.Set(['lart', 'insult', 'excuse', 'praise']) self.db = makeDb(dbFilename) + cursor = db.cursor() + started = int(world.startedAt) + cursor.execute("""INSERT INTO uptime VALUES (%s, NULL)""", started) + def f(): + cursor.execute("""UPDATE uptime SET ended=%s WHERE started=%s""", + int(time.time()), started) + atexit.register(f) def die(self): self.db.commit() @@ -145,6 +158,32 @@ class FunDB(callbacks.Privmsg): else: return ('are', '%ss' % string) + def bestuptime(self, irc, msg, args): + """takes no arguments. + + Returns the highest uptimes attained by the bot. + """ + cursor = self.db.cursor() + cursor.execute("""SELECT started, ended FROM uptime + WHERE ended NOTNULL + ORDER BY ended-started DESC""") + L = [] + lenSoFar = 0 + counter = cursor.rowcount + if cursor.rowcount == 0: + irc.reply(msg, 'I don\'t have enough data to answer that.') + return + while counter and lenSoFar < 400: + (started, ended) = map(int, cursor.fetchone()) + s = '%s, up for %s' % \ + (time.strftime(conf.humanTimestampFormat, + time.localtime(ended)), + utils.timeElapsed(ended-started)) + lenSoFar += len(s) + counter -= 1 + L.append(s) + irc.reply(msg, ircutils.privmsgPayload(L, '; ')) + def insult(self, irc, msg, args): """ @@ -241,7 +280,7 @@ class FunDB(callbacks.Privmsg): cursor = self.db.cursor() sql = """INSERT INTO %ss VALUES (NULL, %%s, %%s, 'nobody', 0)""" % table - cursor.execute(sql, s, msg.prefix) + cursor.execute(sql, s, name) self.db.commit() sql = """SELECT id FROM %ss WHERE %s=%%s""" % (table, table) cursor.execute(sql, s)