def start(self):
try:
self.dbp.execute("CREATE TABLE IF NOT EXISTS trivia_chans (id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(200) NOT NULL, theme VARCHAR(64), UNIQUE KEY(name)) ENGINE=MyISAM")
- self.dbp.execute("CREATE TABLE IF NOT EXISTS trivia_scores (nick VARCHAR(30), points INT(10) DEFAULT '0', fastest_time FLOAT, highest_streak INT(10), channel INT(10)) ENGINE=MyISAM")
+ self.dbp.execute("CREATE TABLE IF NOT EXISTS trivia_scores (nick VARCHAR(30) DEFAULT NULL, points INT(10) DEFAULT '0', fastest_time FLOAT DEFAULT NULL, highest_streak INT(10) DEFAULT NULL, channel INT(10) DEFAULT NULL, KEY `idx` (`channel`,`points`) USING BTREE) ENGINE=MyISAM DEFAULT CHARSET=latin1;")
except Exception, err:
self.log.exception("Error creating table for trivia module (%s)" % err)
raise
self.notice(sender, '; '.join(out))
elif command == 'rank':
- self.dbp.execute("SELECT nick, points FROM trivia_scores WHERE channel = %s ORDER BY points DESC", (self.get_cid(channel),))
+ if arg != '':
+ querynick = arg.lower()
+ else:
+ querynick = sender.lower()
+ # Select user and user above this user in scoring list.
+ querystring = "SELECT x.nick, x.position, x.points, x.channel FROM (SELECT t.nick, t.points, t.channel, @rownum := @rownum + 1 AS position FROM trivia_scores AS t JOIN (SELECT @rownum := 0) r ON t.channel = %s ORDER BY t.points DESC) x WHERE x.nick >= %s ORDER BY x.position DESC LIMIT 2;"
+
+ self.dbp.execute(querystring, (self.get_cid(channel), querynick))
rows = self.dbp.fetchall()
- out = ""
- # XXX: This is inefficient
- for i, row in enumerate(rows):
- if row[0].lower() == sender.lower():
- out = "You are currently ranked #%d with %d points" % (i+1, row[1])
- if i > 0:
- out += ", %d points behind %s" % (rows[i-1][1] - row[1], rows[i-1][0])
- out += "."
-
- if out != "":
+ if len(rows) >= 1 and rows[0][0].lower() == querynick:
+ userdata = rows[0]
+ out = "%s is currently ranked #%d with %d %s" % (userdata[0], userdata[1], userdata[2], "point" if userdata[2] == 1 else "points")
+ if len(rows) == 2:
+ otherdata = rows[1]
+ diff = otherdata[2] - userdata[2]
+ out += ", %d %s behind %s" % (diff, "point" if diff == 1 else "points", otherdata[0])
+ out += "."
self.notice(sender, out)
+ else:
+ self.notice(sender, "Nickname not found.")
#elif command == 'next': # Defunct in orig trivia
#pass
elif command == 'themes':