This commit is contained in:
Jeremy Fincher 2003-12-03 02:17:10 +00:00
parent d4442a04ff
commit f8ea57b05f
3 changed files with 46 additions and 29 deletions

View File

@ -1,3 +1,5 @@
* Added a ranking to ChannelDB.wordstats.
* Added 'roulette' command to Fun plugin. * Added 'roulette' command to Fun plugin.
* Added a Channel.lobotomies command to list the channels in which * Added a Channel.lobotomies command to list the channels in which

View File

@ -536,6 +536,7 @@ class ChannelDB(plugins.ChannelDBHandler,
cursor.execute("""SELECT word FROM words cursor.execute("""SELECT word FROM words
WHERE word=%s""", arg1) WHERE word=%s""", arg1)
if cursor.rowcount == 0: if cursor.rowcount == 0:
# It was a user.
try: try:
id = ircdb.users.getUserId(arg1) id = ircdb.users.getUserId(arg1)
except KeyError: # Maybe it was a nick. Check the hostmask. except KeyError: # Maybe it was a nick. Check the hostmask.
@ -556,31 +557,45 @@ class ChannelDB(plugins.ChannelDBHandler,
username = ircdb.users.getUser(id).name username = ircdb.users.getUser(id).name
irc.error(msg, '%r has no wordstats' % username) irc.error(msg, '%r has no wordstats' % username)
return return
l = [('%r: %s' % (word, count)) for \ L = [('%r: %s' % (word, count)) for \
(word, count) in cursor.fetchall()] (word, count) in cursor.fetchall()]
irc.reply(msg, utils.commaAndify(l)) irc.reply(msg, utils.commaAndify(L))
return return
# It's a word, not a user else:
word = arg1 # It's a word, not a user
cursor.execute("""SELECT word_stats.count, word = arg1
word_stats.user_id cursor.execute("""SELECT word_stats.count,
FROM words, word_stats word_stats.user_id
WHERE words.word=%s FROM words, word_stats
AND words.id=word_stats.word_id WHERE words.word=%s AND
ORDER BY word_stats.count DESC words.id=word_stats.word_id
LIMIT 3""", word) ORDER BY word_stats.count DESC""", word)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'No one has said %r' % word) irc.error(msg, 'No one has said %r' % word)
return return
results = cursor.fetchall() results = cursor.fetchall()
s = 'Top %s: ' % utils.nItems(cursor.rowcount, ers = '%rer' % word
'%rer' % word) ret = 'Top %s: ' % utils.nItems(cursor.rowcount, ers)
l = [] L = []
for (count, id) in results: for (count, id) in results[:3]:
username = ircdb.users.getUser(id).name username = ircdb.users.getUser(id).name
l.append('%s: %s' % (username, count)) L.append('%s: %s' % (username, count))
s += utils.commaAndify(l) try:
irc.reply(msg, s) id = ircdb.users.getUserId(msg.prefix)
rank = 1
for (_, userId) in results:
if userId == id:
s = 'You are ranked %s out of %s %rers.' % \
(rank, len(L), utils.nItems(rank, ers))
break
else:
rank += 1
else:
s = 'You have not said %r' % word
ret = '%s %s. %s' % (ret, utils.commaAndify(L), s)
except KeyError:
ret = '%s %s.' % utils.commaAndify(L)
irc.reply(msg, ret)
Class = ChannelDB Class = ChannelDB

View File

@ -118,18 +118,18 @@ if sqlite is not None:
_ = self.irc.takeMsg() _ = self.irc.takeMsg()
_ = self.irc.takeMsg() _ = self.irc.takeMsg()
self.assertNotError('addword lol') self.assertNotError('addword lol')
self.assertResponse('wordstats lol', 'Top 1 \'lol\'er: foo: 1') self.assertRegexp('wordstats lol', 'foo: 1')
for i in range(5): for i in range(5):
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=userPrefix1)) prefix=userPrefix1))
self.assertResponse('wordstats lol', 'Top 2 \'lol\'ers: %s: 5 ' self.assertRegexp('wordstats lol',
'and foo: 2' % userNick1) '2.*%s: 5.*foo: 2' % userNick1)
for i in range(10): for i in range(10):
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'lol',
prefix=userPrefix2)) prefix=userPrefix2))
self.assertResponse('wordstats lol', 'Top 3 \'lol\'ers: %s: 10, ' self.assertRegexp('wordstats lol',
'%s: 5, and foo: 3' % (\ '3.*%s: 10.*%s: 5.*foo: 3' %
userNick2, userNick1)) (userNick2, userNick1))
def testWordStatsUserWord(self): def testWordStatsUserWord(self):
self.assertNotError('addword lol') self.assertNotError('addword lol')