From af6d3057f881fd4f8dc99e1401fbdb5a17b9d997 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 19:22:24 -0800 Subject: [PATCH 1/9] adding .latency (ping) --- plugin.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index fd8a77d..b4b24fa 100644 --- a/plugin.py +++ b/plugin.py @@ -34,7 +34,8 @@ class TriviaTime(callbacks.Plugin): """ games info """ self.games = {} # separate game for each channel - self.skips = {} # separate game for each channel + self.skips = {} + self.pings = {} """ connections """ dbLocation = self.registryValue('sqlitedb') @@ -115,6 +116,13 @@ class TriviaTime(callbacks.Plugin): irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]))) irc.queueMsg(ircmsgs.voice(channel, username)) + def doPong(self, irc, msg): + username = str.lower(msg.args[1]) + if username in self.pings: + pingTime = float(time.time() - self.pings[username][1]) + irc.sendMsg(ircmsgs.privmsg(self.pings[username][0], """%s: Pong: response %0.2f seconds""" % (username, pingTime))) + del self.pings[username] + def deletequestion(self, irc, msg, arg, id): """ Deletes a question from the database. @@ -248,6 +256,21 @@ class TriviaTime(callbacks.Plugin): alltime = wrap(alltime) """ + def latency(self, irc, msg, arg): + username = str.lower(msg.nick) + channel = ircutils.toLower(msg.args[0]) + expiredPings = [] + for ping in self.pings: + if time.time() - self.pings[ping] > 60: + expiredPings.append(ping) + for ping in expiredPings: + del expiredPings[ping] + if username in self.pings: + return + self.pings[username] = (channel, time.time()) + irc.sendMsg(ircmsgs.ping(username)) + latency = wrap(latency) + def edit(self, irc, msg, arg, num, question): """ Correct a question by providing the question number and the corrected text. From 85b2df327720ea8bbddaafa31d69cccfd4b08c9b Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 19:27:34 -0800 Subject: [PATCH 2/9] removing bad latency command --- plugin.py | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/plugin.py b/plugin.py index b4b24fa..f0c41e4 100644 --- a/plugin.py +++ b/plugin.py @@ -116,13 +116,6 @@ class TriviaTime(callbacks.Plugin): irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]))) irc.queueMsg(ircmsgs.voice(channel, username)) - def doPong(self, irc, msg): - username = str.lower(msg.args[1]) - if username in self.pings: - pingTime = float(time.time() - self.pings[username][1]) - irc.sendMsg(ircmsgs.privmsg(self.pings[username][0], """%s: Pong: response %0.2f seconds""" % (username, pingTime))) - del self.pings[username] - def deletequestion(self, irc, msg, arg, id): """ Deletes a question from the database. @@ -256,21 +249,6 @@ class TriviaTime(callbacks.Plugin): alltime = wrap(alltime) """ - def latency(self, irc, msg, arg): - username = str.lower(msg.nick) - channel = ircutils.toLower(msg.args[0]) - expiredPings = [] - for ping in self.pings: - if time.time() - self.pings[ping] > 60: - expiredPings.append(ping) - for ping in expiredPings: - del expiredPings[ping] - if username in self.pings: - return - self.pings[username] = (channel, time.time()) - irc.sendMsg(ircmsgs.ping(username)) - latency = wrap(latency) - def edit(self, irc, msg, arg, num, question): """ Correct a question by providing the question number and the corrected text. From 6af63c42c7ebb1a6d96902b8eda903268973af75 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 20:18:48 -0800 Subject: [PATCH 3/9] adding fixed latency command --- plugin.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/plugin.py b/plugin.py index f0c41e4..de47f38 100644 --- a/plugin.py +++ b/plugin.py @@ -116,6 +116,13 @@ class TriviaTime(callbacks.Plugin): irc.sendMsg(ircmsgs.privmsg(channel, 'Giving MVP to %s for being top #%d this WEEK' % (username, user[15]))) irc.queueMsg(ircmsgs.voice(channel, username)) + def doNotice(self,irc,msg): + username = str.lower(msg.nick) + if username in self.pings: + pingTime = float(time.time() - self.pings[username][0]) + irc.sendMsg(ircmsgs.privmsg(self.pings[username][1], """ %s Pong: response %0.2f seconds""" % (username, pingTime))) + del self.pings[username] + def deletequestion(self, irc, msg, arg, id): """ Deletes a question from the database. @@ -397,6 +404,22 @@ class TriviaTime(callbacks.Plugin): irc.error('Sorry, round %d could not be found in the database') report = wrap(report, ['int', 'text']) + def latency(self, irc, msg, arg): + channel = ircutils.toLower(msg.args[0]) + username = str.lower(msg.nick) + expiredPings = [] + + for ping in self.pings: + if time.time() - self.pings[ping][0] > 60: + expiredPings.append(ping) + for ping in expiredPings: + del expiredPings[ping] + if username in self.pings: + return + self.pings[username] = (time.time(), channel) + irc.sendMsg(ircmsgs.privmsg('rootcoma', """\x01PING ping\x01""")) + latency = wrap(latency) + def skip(self, irc, msg, arg): """ Skip a question @@ -461,12 +484,7 @@ class TriviaTime(callbacks.Plugin): pass irc.sendMsg(ircmsgs.privmsg(channel, 'Skipped question! (%d of %d voted)' % (len(self.games[channel].skipVoteCount), totalActive))) - timeout = self.registryValue('timeout', channel) - if timeout < 2: - timout = 2 - log.error('timeout was set too low(<2 seconds). setting to 2 seconds') - timeout += time.time() - self.games[channel].queueEvent(timeout, self.games[channel].nextQuestion) + self.games[channel].nextQuestion() irc.noReply() skip = wrap(skip) From 31e3ad9a2e3129546706e6f971351f98fb75aada Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 20:28:00 -0800 Subject: [PATCH 4/9] removing hard coded username from latency adding checks for notices --- plugin.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin.py b/plugin.py index de47f38..51f23c2 100644 --- a/plugin.py +++ b/plugin.py @@ -118,10 +118,11 @@ class TriviaTime(callbacks.Plugin): def doNotice(self,irc,msg): username = str.lower(msg.nick) - if username in self.pings: - pingTime = float(time.time() - self.pings[username][0]) - irc.sendMsg(ircmsgs.privmsg(self.pings[username][1], """ %s Pong: response %0.2f seconds""" % (username, pingTime))) - del self.pings[username] + if msg.args[1][1:5] == "PING": + if username in self.pings: + pingTime = float(time.time() - self.pings[username][0]) + irc.sendMsg(ircmsgs.privmsg(self.pings[username][1], """ %s Pong: response %0.2f seconds""" % (username, pingTime))) + del self.pings[username] def deletequestion(self, irc, msg, arg, id): """ @@ -417,7 +418,7 @@ class TriviaTime(callbacks.Plugin): if username in self.pings: return self.pings[username] = (time.time(), channel) - irc.sendMsg(ircmsgs.privmsg('rootcoma', """\x01PING ping\x01""")) + irc.sendMsg(ircmsgs.privmsg(username, """\x01PING ping\x01""")) latency = wrap(latency) def skip(self, irc, msg, arg): From 9cfe2f5927179194029594c5b2c271d6f273e1a8 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 20:48:35 -0800 Subject: [PATCH 5/9] small bugfix for hints, spaces shouldnt count as characters --- plugin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index 51f23c2..13a139e 100644 --- a/plugin.py +++ b/plugin.py @@ -905,7 +905,9 @@ class TriviaTime(callbacks.Plugin): unmasked = 0 for i in range(len(ans)-divider): masked = ansend[i] - if maskedInARow > 2 and unmasked < (len(ans)-divider): + if masked == ' ': + hintsend = ' ' + elif maskedInARow > 2 and unmasked < (len(ans)-divider): lettersInARow += 1 hintsend += ansend[i] unmasked += 1 From d304d203cf59c8afbe461219b2ab35cf7514fc65 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 20:55:55 -0800 Subject: [PATCH 6/9] Adding change to small hint fix --- plugin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin.py b/plugin.py index 13a139e..7c43bbe 100644 --- a/plugin.py +++ b/plugin.py @@ -907,6 +907,7 @@ class TriviaTime(callbacks.Plugin): masked = ansend[i] if masked == ' ': hintsend = ' ' + unmasked += 1 elif maskedInARow > 2 and unmasked < (len(ans)-divider): lettersInARow += 1 hintsend += ansend[i] From 0e97593f3bcd767659b870efcfc610aab31adbd6 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Wed, 6 Nov 2013 22:48:30 -0800 Subject: [PATCH 7/9] Adding active this week to .info --- plugin.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index 7c43bbe..f302948 100644 --- a/plugin.py +++ b/plugin.py @@ -171,9 +171,12 @@ class TriviaTime(callbacks.Plugin): """ Get TriviaTime information, how many questions/users in database, time, etc """ + numActiveThisWeek = self.storage.getNumActiveThisWeek() infoText = ''' TriviaTime by #trivialand on Freenode''' irc.sendMsg(ircmsgs.privmsg(msg.args[0], infoText)) - infoText = '''\x02 %d Users\x02 on scoreboard Time is %s ''' % (self.storage.getNumUser(),time.asctime(time.localtime())) + infoText = ''' Time is %s ''' % (time.asctime(time.localtime(),)) + irc.sendMsg(ircmsgs.privmsg(msg.args[0], infoText)) + infoText = '''\x02 %d Users\x02 on scoreboard \x02%d Active This Week\x02''' % (self.storage.getNumUser(), numActiveThisWeek) irc.sendMsg(ircmsgs.privmsg(msg.args[0], infoText)) numKaos = self.storage.getNumKAOS() numQuestionTotal = self.storage.getNumQuestions() @@ -2120,6 +2123,28 @@ class TriviaTime(callbacks.Plugin): c.close() return data + def getNumActiveThisWeek(self): + d = datetime.date.today() + weekday=d.weekday() + d -= datetime.timedelta(weekday) + weekSqlString = '' + for i in range(7): + if i > 0: + weekSqlString += ' or ' + weekSqlString += ''' + (tl.year=%d + and tl.month=%d + and tl.day=%d)''' % (d.year, d.month, d.day) + d += datetime.timedelta(1) + c = self.conn.cursor() + weekSql = '''select count(distinct(tl.username)) + from triviauserlog tl + where ''' + weekSql += weekSqlString + result = c.execute(weekSql) + rows = result.fetchone()[0] + return rows + def deleteQuestion(self, questionId): c = self.conn.cursor() test = c.execute('''update triviaquestion set From 6b93cc0ba8c8c5dd8313e73a4fcf9c3791dfa327 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Thu, 7 Nov 2013 04:47:59 -0800 Subject: [PATCH 8/9] Adding .transferpoints --- plugin.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/plugin.py b/plugin.py index f302948..0a7b075 100644 --- a/plugin.py +++ b/plugin.py @@ -648,18 +648,17 @@ class TriviaTime(callbacks.Plugin): irc.noReply() time = wrap(time) - """ def transferpoints(self, irc, msg, arg, userfrom, userto): - ''' + """ Transfers all points and records from one user to another - ''' + """ userfrom = str.lower(userfrom) userto = str.lower(userto) self.storage.transferUserLogs(userfrom, userto) irc.reply('Done! Transfered records from %s to %s' % (userfrom, userto)) transferpoints = wrap(transferpoints, ['nick', 'nick']) - """ + """ Game instance @@ -2153,6 +2152,66 @@ class TriviaTime(callbacks.Plugin): self.conn.commit() c.close() + def transferUserLogs(self, userFrom, userTo): + userFrom = str.lower(userFrom) + userTo = str.lower(userTo) + c = self.conn.cursor() + c.execute(''' + update triviauserlog + set num_answered=num_answered + +ifnull( + ( + select t3.num_answered + from triviauserlog t3 + where t3.day=triviauserlog.day + and t3.month=triviauserlog.month + and t3.year=triviauserlog.year + and t3.username=? + ) + ,0), + points_made=points_made + +ifnull( + ( + select t2.points_made + from triviauserlog t2 + where t2.day=triviauserlog.day + and t2.month=triviauserlog.month + and t2.year=triviauserlog.year + and t2.username=? + ) + ,0) + where id in ( + select id + from triviauserlog tl + where username=? + and exists ( + select id + from triviauserlog tl2 + where tl2.day=tl.day + and tl2.month=tl.month + and tl2.year=tl.year + and username=? + ) + ) + ''', (userFrom,userFrom,userTo,userFrom)) + + c.execute(''' + update triviauserlog + set username=? + where username=? + and not exists ( + select 1 + from triviauserlog tl + where tl.day=triviauserlog.day + and tl.month=triviauserlog.month + and tl.year=triviauserlog.year + and tl.username=? + ) + ''',(userTo, userFrom, userTo)) + + self.removeUserLogs(userFrom) + + """ def transferUserLogs(self, userFrom, userTo): userFrom = str.lower(userFrom) @@ -2161,7 +2220,7 @@ class TriviaTime(callbacks.Plugin): c.execute('''update triviauserlog set username=?, - points_made=(select + points_made=points_made+(select tl2.points_made from triviauserlog tl2 where @@ -2170,7 +2229,7 @@ class TriviaTime(callbacks.Plugin): and tl2.month=month and tl2.year=year) , - num_answered=(select + num_answered=points_made+(select tl2.num_answered from triviauserlog tl2 where From a74a137725314bde493504d87ed86bd4b39acbd5 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Thu, 7 Nov 2013 04:58:21 -0800 Subject: [PATCH 9/9] cleaning up code --- plugin.py | 116 ++++++++++++------------------------------------------ 1 file changed, 25 insertions(+), 91 deletions(-) diff --git a/plugin.py b/plugin.py index 0a7b075..3337284 100644 --- a/plugin.py +++ b/plugin.py @@ -245,21 +245,6 @@ class TriviaTime(callbacks.Plugin): irc.noReply() year = wrap(year) - """ - def alltime(self, irc, msg, arg): - ''' - Gives the top10 scores from all time - ''' - channel = ircutils.toLower(msg.args[0]) - tops = self.storage.viewAllTimeTop10() - topsText = '\x0301,08 ALL TIME Top 10 Players - ' - for i in range(len(tops)): - topsText += '\x02\x0301,08 #%d:\x02 \x0300,04 %s %d ' % ((i+1) , tops[i][1], tops[i][2]) - irc.sendMsg(ircmsgs.privmsg(channel, topsText)) - irc.noReply() - alltime = wrap(alltime) - """ - def edit(self, irc, msg, arg, num, question): """ Correct a question by providing the question number and the corrected text. @@ -659,10 +644,7 @@ class TriviaTime(callbacks.Plugin): irc.reply('Done! Transfered records from %s to %s' % (userfrom, userto)) transferpoints = wrap(transferpoints, ['nick', 'nick']) - - """ - Game instance - """ + #Game instance class Game: """ Main game logic, single game instance for each channel. @@ -1130,18 +1112,6 @@ class TriviaTime(callbacks.Plugin): question = question[0] return (question[0], question[2]) - """ - def retrieveRandomQuestionFromFile(self): - ''' - Helper function to grab a random line from a file - ''' - filename = self.registryValue('quizfile') - filesLines = open(filename).readlines() - randomNumber = random.randint(1,len(filesLines)) - randomLine = filesLines[randomNumber] - return (randomNumber, randomLine) - """ - def sendMessage(self, msg, color=None, bgcolor=None): """ , [], [] @@ -1165,11 +1135,8 @@ class TriviaTime(callbacks.Plugin): if self.channel in self.games: del self.games[self.channel] self.sendMessage(self.registryValue('stopped'), 2) - # end Game - """ - Storage for users and points using sqlite3 - """ + #Storage for users and points using sqlite3 class Storage: """ Storage class @@ -2169,31 +2136,31 @@ class TriviaTime(callbacks.Plugin): and t3.username=? ) ,0), - points_made=points_made - +ifnull( - ( - select t2.points_made - from triviauserlog t2 - where t2.day=triviauserlog.day - and t2.month=triviauserlog.month - and t2.year=triviauserlog.year - and t2.username=? - ) - ,0) - where id in ( - select id - from triviauserlog tl - where username=? - and exists ( - select id - from triviauserlog tl2 - where tl2.day=tl.day - and tl2.month=tl.month - and tl2.year=tl.year - and username=? + points_made=points_made + +ifnull( + ( + select t2.points_made + from triviauserlog t2 + where t2.day=triviauserlog.day + and t2.month=triviauserlog.month + and t2.year=triviauserlog.year + and t2.username=? ) + ,0) + where id in ( + select id + from triviauserlog tl + where username=? + and exists ( + select id + from triviauserlog tl2 + where tl2.day=tl.day + and tl2.month=tl.month + and tl2.year=tl.year + and username=? ) - ''', (userFrom,userFrom,userTo,userFrom)) + ) + ''', (userFrom,userFrom,userTo,userFrom)) c.execute(''' update triviauserlog @@ -2211,38 +2178,6 @@ class TriviaTime(callbacks.Plugin): self.removeUserLogs(userFrom) - - """ - def transferUserLogs(self, userFrom, userTo): - userFrom = str.lower(userFrom) - userTo = str.lower(userTo) - c = self.conn.cursor() - c.execute('''update triviauserlog - set - username=?, - points_made=points_made+(select - tl2.points_made - from triviauserlog tl2 - where - tl2.username=? - and tl2.day=day - and tl2.month=month - and tl2.year=year) - , - num_answered=points_made+(select - tl2.num_answered - from triviauserlog tl2 - where - tl2.username=? - and tl2.day=day - and tl2.month=month - and tl2.year=year) - where - username=?''', (userTo,userTo,userTo,userFrom)) - self.conn.commit() - c.close() - """ - def removeEdit(self, editId): c = self.conn.cursor() c.execute('''delete from triviaedit @@ -2265,6 +2200,5 @@ class TriviaTime(callbacks.Plugin): self.conn.commit() c.close() - Class = TriviaTime # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: