]> jfr.im git - erebus.git/commitdiff
trivia - also track time of streaks
authorJohn Runyon <redacted>
Sun, 15 Oct 2023 13:46:43 +0000 (07:46 -0600)
committerJohn Runyon <redacted>
Sun, 15 Oct 2023 13:46:43 +0000 (07:46 -0600)
modules/trivia.py

index ff106fab2f6d5f2aab9499ecd4fb3e6d375b7367..e7cf0d59d49bfbb1d0a820294f8d90e453a53cbf 100644 (file)
@@ -424,7 +424,7 @@ class TriviaState(object):
                if user in self.db['streaks']:
                        return self.db['streaks'][user]
                else:
-                       return 0
+                       return [0,0]
 
        def targetuser(self, user):
                if len(self.db['ranks']) == 0: return "no one is ranked!"
@@ -474,13 +474,15 @@ def trivia_checkanswer(bot, user, chan, *args):
                bot.fastmsg(chan, response)
 
                user_lower = str(user).lower()
+               new_max_streak = ""
                if user_lower not in state.db['streaks']:
-                       state.db['streaks'][user_lower] = 0
-               if state.streak > state.db['streaks'][user_lower]:
-                       state.db['streaks'][user_lower] = state.streak
+                       state.db['streaks'][user_lower] = [0, 0]
+               if state.streak > state.db['streaks'][user_lower][0]:
+                       state.db['streaks'][user_lower] = [state.streak, time.time()]
+                       new_max_streak = " That's a new record for them!"
 
                if state.streak >= 3:
-                       bot.msg(chan, "\00312%s\003 is on a streak! \00307%d\003 answers correct in a row!" % (user, state.streak))
+                       bot.msg(chan, "\00312%s\003 is on a streak! \00307%d\003 answers correct in a row!%s" % (user, state.streak, new_max_streak))
 
                if state.hintsgiven == 0:
                        bot.msg(chan, "\00312%s\003 got an extra point for getting it before the hints! New score: %d." % (user, state.addpoint(user)))
@@ -896,34 +898,41 @@ def streak(bot, user, chan, realtarget, *args):
        if len(args) != 0: who = args[0]
        else: who = user
 
-       bot.msg(replyto, "%s's highest streak is %d" % (who, state.get_streak(who)))
+       streak = state.get_streak(who)
+       bot.msg(replyto, "%s's highest streak is %d, set %s" % (who, streak[0], time.strftime("%d %b %Y %H:%M:%S %Z", time.gmtime(streak[1]))))
 
 @lib.hook(needchan=False)
 @lib.help(None, "shows top streaks of all time")
 def topstreaks(bot, user, chan, realtarget, *args):
        db = state.db['streaks']
-       streaks = [(k, db[k]) for k in db.keys()]
+       streaks = [(nick, db[nick][0], db[nick][1]) for nick in db.keys()]
        streaks.sort(key=lambda v: v[1], reverse=True)
        return "Top streaks of all time: %s (%d), %s (%d), %s (%d)." % (streaks[0][0], streaks[0][1], streaks[1][0], streaks[1][1], streaks[2][0], streaks[2][1])
 
 @lib.hook(glevel=lib.MANAGER, needchan=False)
-@lib.help("<nick> <new streak>", "set a user's max streak")
-@lib.argsEQ(2)
+@lib.help("<nick> <new streak> [<timestamp>]", "set a user's max streak")
+@lib.argsGE(2)
 def setstreak(bot, user, chan, realtarget, *args):
        temp = 0
        target = args[0].lower()
        try:
                newstreak = int(args[1])
        except ValueError:
-               return "Syntax: SETSTREAK <nick> <new streak>"
+               return "Error: <new streak> must be integer"
+       timestamp = time.time()
+       if len(args) > 2:
+               try:
+                       timestamp = int(args[2])
+               except ValueError:
+                       return "Error: <timestamp> must be integer (unix timestamp)"
 
        if target in state.db['streaks']:
                temp = state.db['streaks'][target]
                if newstreak == 0:
                        del state.db['streaks'][target]
        if newstreak > 0:
-               state.db['streaks'][target] = newstreak
-       return "Done. Streak used to be %d" % (temp)
+               state.db['streaks'][target] = [newstreak, timestamp]
+       return "Done. Streak used to be %d set %d" % (temp[0], temp[1])
 
 @lib.hook(needchan=False)
 def triviahelp(bot, user, chan, realtarget, *args):