X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/be8072b500f64089b2bc540e81ce90f115231853..72e5bb4ecc41cbe5ca3083f9ec0e0d1679ac9fca:/modules/trivia.py diff --git a/modules/trivia.py b/modules/trivia.py index 0343418..ae9a74e 100644 --- a/modules/trivia.py +++ b/modules/trivia.py @@ -39,9 +39,23 @@ def findnth(haystack, needle, n): #http://stackoverflow.com/a/1884151 return -1 return len(haystack)-len(parts[-1])-len(needle) -def person(num): return state.db['users'][state.db['ranks'][num]]['realnick'] -def pts(num): return str(state.db['users'][state.db['ranks'][num]]['points']) -def country(num, default="??"): return lib.mod('userinfo')._get(person(num), 'country', default=default) +def person(num, throwindexerror=False): + try: + return state.db['users'][state.db['ranks'][num]]['realnick'] + except IndexError: + if throwindexerror: + raise + else: + return '' + +def pts(num): + try: + return str(state.db['users'][state.db['ranks'][num]]['points']) + except IndexError: + return 0 + +def country(num, default="??"): + return lib.mod('userinfo')._get(person(num), 'country', default=default).upper() class MyTimer(threading._Timer): def __init__(self, *args, **kwargs): @@ -71,8 +85,8 @@ class TriviaState(object): self.curqid = None self.lastqid = None - if 'starttime' not in self.db or self.db['starttime'] is None: - self.db['starttime'] = time.time() + if 'lastwon' not in self.db or self.db['lastwon'] is None: + self.db['lastwon'] = time.time() if pointvote: self.getchan().msg("Vote for the next round target points! Options: %s. Vote using !vote " % (', '.join([str(x) for x in self.db['targetoptions']]))) @@ -104,16 +118,19 @@ class TriviaState(object): answer = self.hintanswer if self.hintstr is None or self.revealpossibilities is None or self.reveal is None: + oldhintstr = "" 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)) + self.reveal = int(round(''.join(self.hintstr).count('*') * (7/24.0))) + else: + oldhintstr = ''.join(self.hintstr) for i in range(self.reveal): revealcount = random.choice(self.revealpossibilities) revealloc = findnth(''.join(self.hintstr), '*', revealcount) self.revealpossibilities.remove(revealcount) self.hintstr[revealloc] = answer[revealloc] - self.parent.channel(self.chan).bot.fastmsg(self.chan, "\00304,01Here's a hint: %s" % (''.join(self.hintstr))) + if oldhintstr != ''.join(self.hintstr): self.getchan().fastmsg("\00304,01Here's a hint: %s" % (''.join(self.hintstr))) self.hintsgiven += 1 @@ -129,10 +146,10 @@ class TriviaState(object): winner = person(0) try: msg("\00312THE GAME IS OVER!!!") - msg("THE WINNER IS: %s (%s)" % (person(0), pts(0))) - msg("2ND PLACE: %s (%s)" % (person(1), pts(1))) - msg("3RD PLACE: %s (%s)" % (person(2), pts(2))) - [msg("%dth place: %s (%s)" % (i+1, person(i), pts(i))) for i in range(3,10)] + msg("THE WINNER IS: %s (%s)" % (person(0, True), pts(0))) + msg("2ND PLACE: %s (%s)" % (person(1, True), pts(1))) + msg("3RD PLACE: %s (%s)" % (person(2, True), pts(2))) + [msg("%dth place: %s (%s)" % (i+1, person(i, True), pts(i))) for i in range(3,10)] except IndexError: pass except Exception as e: msg("DERP! %r" % (e)) @@ -174,7 +191,7 @@ class TriviaState(object): f.seek(insertpos) f.write((self.db['hofformat']+"\n") % { 'date': time.strftime("%F", time.gmtime()), - 'duration': str(datetime.timedelta(seconds=time.time()-self.db['starttime'])), + 'duration': str(datetime.timedelta(seconds=time.time()-self.db['lastwon'])), 'targetscore': self.db['target'], 'firstperson': person(0), 'firstscore': pts(0), @@ -260,9 +277,12 @@ class TriviaState(object): else: nextq.append(time.time()) - nextq[1] = nextq[1].lower() + if isinstance(nextq[1], basestring): + nextq[1] = nextq[1].lower() + else: + nextq[1] = [s.lower() for s in nextq[1]] - qtext = "\00304,01Next up: " + qtext = "\00312,01Next up: " qtext += "(%5d)" % (random.randint(0,99999)) qary = nextq[0].split(None) qtext += " " @@ -352,7 +372,10 @@ def trivia_checkanswer(bot, user, chan, *args): line = ' '.join([str(arg) for arg in args]) if state.checkanswer(line): state.curq = None - bot.fastmsg(chan, "\00312%s\003 has it! The answer was \00312%s\003. New score: %d. Rank: %d. Target: %s (%s)." % (user, line, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user))) + if state.hintanswer.lower() == line.lower(): + bot.fastmsg(chan, "\00312%s\003 has it! The answer was \00312%s\003. New score: %d. Rank: %d. Target: %s (%s)." % (user, line, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user))) + else: + bot.fastmsg(chan, "\00312%s\003 has it! The answer was \00312%s\003 (hinted answer: %s). New score: %d. Rank: %d. Target: %s (%s)." % (user, line, state.hintanswer, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user))) 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))) state.nextquestion() @@ -422,9 +445,9 @@ def start(bot, user, chan, realtarget, *args): bot.msg(state.db['chan'], "%s has started the game!" % (user)) state.nextquestion(skipwait=True) elif state.pointvote is not None: - bot.msg(replyto, "There's a vote in progress!") + bot.msg(user, "There's a vote in progress!") else: - bot.msg(replyto, "Game is already started!") + bot.msg(user, "Game is already started!") @lib.hook('stop', glevel=1, needchan=False) def cmd_stop(bot, user, chan, realtarget, *args): @@ -438,24 +461,15 @@ def cmd_exception(*args, **kwargs): raise Exception() def stop(): - try: - if state.curq is not None or state.nextq is not None: - state.curq = None - state.nextq = None - try: - state.steptimer.cancel() - except Exception as e: - print "!!! steptimer.cancel(): %s %r" % (e,e) - try: - state.nextquestiontimer.cancel() - state.nextquestiontimer = None - except Exception as e: - print "!!! nextquestiontimer.cancel(): %s %r" % (e,e) - return True - else: - return False - except NameError: - pass + state.curq = None + state.nextq = None + try: state.steptimer.cancel() + except Exception as e: print "!!! steptimer.cancel(): %s %r" % (e,e) + state.steptimer = None + try: state.nextquestiontimer.cancel() + except Exception as e: print "!!! nextquestiontimer.cancel(): %s %r" % (e,e) + state.nextquestiontimer = None + return True @lib.hook(needchan=False) @lib.argsGE(1) @@ -515,8 +529,8 @@ def top10(bot, user, chan, realtarget, *args): max = len(state.db['ranks']) if max > 10: max = 10 - replylist = ', '.join(["%s (%s) %s" % (person(x), country(x, "unknown"), pts(x)) for x in range(max)]) - bot.msg(state.db['chan'], "Top %d: %s" % (max, replylist)) + replylist = ', '.join(["%s (%s) %s" % (person(x), country(x), pts(x)) for x in range(max)]) + bot.msg(state.db['chan'], "Top 10: %s" % (replylist)) @lib.hook(glevel=lib.ADMIN, needchan=False) def settarget(bot, user, chan, realtarget, *args): @@ -616,33 +630,33 @@ def addq(bot, user, chan, realtarget, *args): @lib.hook(needchan=False) def triviahelp(bot, user, chan, realtarget, *args): - bot.msg(user, "START") - bot.msg(user, "TOP10") - bot.msg(user, "POINTS []") - bot.msg(user, "RANK []") - bot.msg(user, "BADQ (include info to identify question)") + bot.slowmsg(user, "START") + bot.slowmsg(user, "TOP10") + bot.slowmsg(user, "POINTS []") + bot.slowmsg(user, "RANK []") + bot.slowmsg(user, "BADQ (include info to identify question)") if user.glevel >= 1: - bot.msg(user, "SKIP (>=KNOWN)") - bot.msg(user, "STOP (>=KNOWN)") - bot.msg(user, "FINDQ (>=KNOWN)") + bot.slowmsg(user, "SKIP (>=KNOWN)") + bot.slowmsg(user, "STOP (>=KNOWN)") + bot.slowmsg(user, "FINDQ (>=KNOWN)") if user.glevel >= lib.STAFF: - bot.msg(user, "GIVE [] (>=STAFF)") - bot.msg(user, "SETNEXT * (>=STAFF)") - bot.msg(user, "ADDQ * (>=STAFF)") - bot.msg(user, "DELETEQ * (>=STAFF) [aka DELQ]") - bot.msg(user, "BADQS (>=STAFF)") - bot.msg(user, "CLEARBADQS (>=STAFF)") - bot.msg(user, "DELBADQ (>=STAFF)") + bot.slowmsg(user, "GIVE [] (>=STAFF)") + bot.slowmsg(user, "SETNEXT * (>=STAFF)") + bot.slowmsg(user, "ADDQ * (>=STAFF)") + bot.slowmsg(user, "DELETEQ * (>=STAFF) [aka DELQ]") + bot.slowmsg(user, "BADQS (>=STAFF)") + bot.slowmsg(user, "CLEARBADQS (>=STAFF)") + bot.slowmsg(user, "DELBADQ (>=STAFF)") if user.glevel >= lib.ADMIN: - bot.msg(user, "SETTARGET (>=ADMIN)") - bot.msg(user, "MAXMISSED (>=ADMIN)") - bot.msg(user, "HINTTIMER (>=ADMIN)") - bot.msg(user, "HINTNUM (>=ADMIN)") - bot.msg(user, "QUESTIONPAUSE (>=ADMIN)") + bot.slowmsg(user, "SETTARGET (>=ADMIN)") + bot.slowmsg(user, "MAXMISSED (>=ADMIN)") + bot.slowmsg(user, "HINTTIMER (>=ADMIN)") + bot.slowmsg(user, "HINTNUM (>=ADMIN)") + bot.slowmsg(user, "QUESTIONPAUSE (>=ADMIN)") @lib.hooknum(417) def num_417(bot, textline): - bot.msg(state.db['chan'], "Whoops, it looks like that question didn't quite go through! (E:417). Let's try another...") +# bot.fastmsg(state.db['chan'], "Whoops, it looks like that question didn't quite go through! (E:417). Let's try another...") state.nextquestion(qskipped=False, skipwait=True) @lib.hooknum(332) @@ -696,7 +710,7 @@ def specialQuestion(oldq): def spellout(num): return [ - "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty" ][num]