X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/77c617753d5a9ca57db00f5633a6340a888ebf77..4a8b16575ddf1f2818bae4928dad5bda3cd802f5:/modules/trivia.py diff --git a/modules/trivia.py b/modules/trivia.py index bd0889a..1836ed7 100644 --- a/modules/trivia.py +++ b/modules/trivia.py @@ -34,7 +34,7 @@ def modstop(*args, **kwargs): return lib.modstop(*args, **kwargs) # module code -import json, random, threading, re +import json, random, threading, re, time def findnth(haystack, needle, n): #http://stackoverflow.com/a/1884151 parts = haystack.split(needle, n+1) @@ -76,12 +76,12 @@ class TriviaState(object): else: self.hintanswer = random.choice(self.curq['answer']) answer = self.hintanswer - if self.hintstr is None or self.revealpossibilities is None: + if self.hintstr is None or self.revealpossibilities is None or self.reveal is None: self.hintstr = list(re.sub(r'[a-zA-Z0-9]', '*', answer)) self.revealpossibilities = range(''.join(self.hintstr).count('*')) + self.reveal = int(''.join(self.hintstr).count('*') * (7/24.0)) - reveal = int(len(self.hintstr) * (7/24.0)) - for i in range(reveal): + for i in range(self.reveal): revealcount = random.choice(self.revealpossibilities) revealloc = findnth(''.join(self.hintstr), '*', revealcount) self.revealpossibilities.remove(revealcount) @@ -116,7 +116,7 @@ class TriviaState(object): self.closeshop() self.__init__(self.questionfile, self.parent) - def nextquestion(self, qskipped=False): + def nextquestion(self, qskipped=False, iteration=0): if self.gameover == True: return self.doGameOver() if qskipped: @@ -128,15 +128,23 @@ class TriviaState(object): self.hintanswer = None self.hintsgiven = 0 self.revealpossibilities = None + self.reveal = None if state.nextq is not None: nextq = state.nextq - self.curq = nextq state.nextq = None else: nextq = random.choice(self.db['questions']) - self.curq = nextq + + if nextq['question'][0] == "!": + nextq = specialQuestion(nextq) + + if iteration < 10 and 'lastasked' in nextq and nextq['lastasked'] - time.time() < 24*60*60: + return self.nextquestion(iteration=iteration+1) #short-circuit to pick another question + nextq['lastasked'] = time.time() + + nextq['answer'] = nextq['answer'].lower() qtext = "\00300,01Next up: " qary = nextq['question'].split(None) @@ -144,6 +152,8 @@ class TriviaState(object): qtext += "\00300,01"+qword+"\00301,01"+chr(random.randrange(32,126)) self.getbot().msg(self.chan, qtext) + self.curq = nextq + self.steptimer = threading.Timer(HINTTIMER, self.nexthint, args=[1]) self.steptimer.start() @@ -221,7 +231,7 @@ def trivia_checkanswer(bot, user, chan, *args): bot.msg(chan, "\00308%s\003 got an extra point for getting it before the hints! New score: %d." % (user, state.addpoint(user))) state.nextquestion() -@lib.hook('points') +@lib.hook('points', needchan=False) def cmd_points(bot, user, chan, realtarget, *args): if chan == realtarget: replyto = chan else: replyto = user @@ -231,7 +241,7 @@ def cmd_points(bot, user, chan, realtarget, *args): bot.msg(replyto, "%s has %d points." % (who, state.points(who))) -@lib.hook('give', clevel=lib.OP) +@lib.hook('give', clevel=lib.OP, needchan=False) @lib.argsGE(1) def cmd_give(bot, user, chan, realtarget, *args): whoto = args[0] @@ -243,21 +253,24 @@ def cmd_give(bot, user, chan, realtarget, *args): bot.msg(chan, "%s gave %s %d points. New balance: %d" % (user, whoto, numpoints, balance)) -@lib.hook('setnext', clevel=lib.OP) +@lib.hook('setnext', clevel=lib.OP, needchan=False) @lib.argsGE(1) def cmd_setnext(bot, user, chan, realtarget, *args): line = ' '.join([str(arg) for arg in args]) linepieces = line.split('*') + if len(linepieces) < 2: + bot.msg(user, "Error: need *") + return question = linepieces[0].strip() answer = linepieces[1].strip() state.nextq = {'question':question,'answer':answer} bot.msg(user, "Done.") -@lib.hook('skip', clevel=lib.KNOWN) +@lib.hook('skip', clevel=lib.KNOWN, needchan=False) def cmd_skip(bot, user, chan, realtarget, *args): state.nextquestion() -@lib.hook('start') +@lib.hook('start', needchan=False) def cmd_start(bot, user, chan, realtarget, *args): if chan == realtarget: replyto = chan else: replyto = user @@ -267,7 +280,7 @@ def cmd_start(bot, user, chan, realtarget, *args): else: bot.msg(replyto, "Game is already started!") -@lib.hook('stop', clevel=lib.KNOWN) +@lib.hook('stop', clevel=lib.KNOWN, needchan=False) def cmd_stop(bot, user, chan, realtarget, *args): if stop(): bot.msg(state.chan, "Game stopped by %s" % (user)) @@ -283,7 +296,7 @@ def stop(): else: return False -@lib.hook('rank') +@lib.hook('rank', needchan=False) def cmd_rank(bot, user, chan, realtarget, *args): if chan == realtarget: replyto = chan else: replyto = user @@ -293,7 +306,7 @@ def cmd_rank(bot, user, chan, realtarget, *args): bot.msg(replyto, "%s is in %d place (%s points). Target is: %s (%s points)." % (who, state.rank(who), state.points(who), state.targetuser(who), state.targetpoints(who))) -@lib.hook('top10') +@lib.hook('top10', needchan=False) def cmd_top10(bot, user, chan, realtarget, *args): if len(state.db['ranks']) == 0: return bot.msg(state.db['chan'], "No one is ranked!") @@ -304,7 +317,7 @@ def cmd_top10(bot, user, chan, realtarget, *args): replylist.append("%s (%s)" % (user['realnick'], user['points'])) bot.msg(state.db['chan'], ', '.join(replylist)) -@lib.hook('settarget', clevel=lib.MASTER) +@lib.hook('settarget', clevel=lib.MASTER, needchan=False) def cmd_settarget(bot, user, chan, realtarget, *args): try: state.db['target'] = int(args[0]) @@ -312,7 +325,7 @@ def cmd_settarget(bot, user, chan, realtarget, *args): except: bot.msg(user, "Failed to set target.") -@lib.hook('triviahelp') +@lib.hook('triviahelp', needchan=False) def cmd_triviahelp(bot, user, chan, realtarget, *args): bot.msg(user, "POINTS []") bot.msg(user, "START") @@ -327,3 +340,25 @@ def cmd_triviahelp(bot, user, chan, realtarget, *args): bot.msg(user, "SETNEXT * (>=OP )") if bot.parent.channel(state.db['chan']).levelof(user.auth) >= lib.MASTER: bot.msg(user, "SETTARGET (>=MASTER)") + + +def specialQuestion(oldq): + newq = {'question': oldq['question'], 'answer': oldq['answer']} + qtype = oldq['question'].upper() + + if qtype == "!MONTH": + newq['question'] = "What month is it currently (in UTC)?" + newq['answer'] = time.strftime("%B").lower() + elif qtype == "!MATH+": + randnum1 = random.randrange(0, 11) + randnum2 = random.randrange(0, 11) + newq['question'] = "What is %d + %d?" % (randnum1, randnum2) + newq['answer'] = spellout(randnum1+randnum2) + return newq + +def spellout(num): + return [ + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", + "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", "seventeen", "eighteen", "nineteen", "twenty" + ][num]