X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/a28e2ae95b1012933a01f3fa992d9f6954f82346..b68479d779c98ec06a1d66290dba8d98fb40f068:/modules/trivia.py diff --git a/modules/trivia.py b/modules/trivia.py index 9f7624f..48b4db6 100644 --- a/modules/trivia.py +++ b/modules/trivia.py @@ -1,4 +1,5 @@ # Erebus IRC bot - Author: Erebus Team +# vim: fileencoding=utf-8 # trivia module # This file is released into the public domain; see http://unlicense.org/ @@ -64,7 +65,7 @@ def pts(num): return 0 def country(num, default="??"): - return lib.mod('userinfo')._get(person(num), 'country', default=default).upper() + return lib.mod('userinfo').get(person(num), 'country', default=default).upper() class MyTimer(timerbase): def __init__(self, *args, **kwargs): @@ -100,8 +101,8 @@ class TriviaState(object): 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']]))) - self.getchan().msg("You have %s seconds." % (self.db['votetimer'])) + self.getchan().fastmsg("Vote for the next round target points! Options: %s. Vote using !vote " % (', '.join([str(x) for x in self.db['targetoptions']]))) + self.getchan().fastmsg("You have %s seconds." % (self.db['votetimer'])) self.voteamounts = dict([(x, 0) for x in self.db['targetoptions']]) # make a dict {pointsoptionA: 0, pointsoptionB: 0, ...} self.pointvote = MyTimer(self.db['votetimer'], self.endPointVote) self.pointvote.start() @@ -158,11 +159,13 @@ class TriviaState(object): 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] + try: + 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] + except IndexError: pass # if everything is revealed, random.choice will IndexError if oldhintstr != ''.join(self.hintstr): self.getchan().fastmsg("\00304,01Here's a hint: %s" % (''.join(self.hintstr))) self.hintsgiven += 1 @@ -195,6 +198,7 @@ class TriviaState(object): self.db['users'] = {} self.db['ranks'] = [] + self.savedb() stop() self.closeshop() @@ -217,6 +221,7 @@ class TriviaState(object): except: return 0 status = False + f = None try: f = open(self.db['hofpath'], 'rb+') for i in range(self.db['hoflines']): #skip this many lines @@ -240,7 +245,8 @@ class TriviaState(object): except Exception as e: status = False finally: - f.close() + if f is not None: + f.close() return status def endPointVote(self): @@ -333,9 +339,10 @@ class TriviaState(object): ) qtext += "\00304,01"+qword+"\00301,01"+chr(spacer) #a-z if not self.getbot().fastmsg(self.chan, qtext): #if message is too long: - if nextqid is None: nextqid = "manual" - self.getbot().slowmsg(self.chan, "(Unable to ask question #%s: line too long)" % (nextqid)) - return self._nextquestion(iteration) #retry; don't increment the iteration + if not self.getbot().fastmsg(self.chan, "\00312,01Next up: " + ("(%5d)" % (random.randint(0,99999))) + "\00304,01" + nextq[0]): + if nextqid is None: nextqid = "manual" + self.getbot().slowmsg(self.chan, "(Unable to ask question #%s: line too long)" % (nextqid)) + return self._nextquestion(iteration) #retry; don't increment the iteration self.curq = nextq self.curqid = nextqid @@ -605,7 +612,7 @@ def top10(bot, user, chan, realtarget, *args): if max > 10: max = 10 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)) + bot.msg(state.db['chan'], "Game is to %s! Top 10: %s" % (state.db['target'], replylist)) @lib.hook(glevel=lib.ADMIN, needchan=False) @lib.help("", "changes the target score for this round") @@ -851,7 +858,8 @@ def triviahelp(bot, user, chan, realtarget, *args): bot.slowmsg(user, "HINTNUM (ADMIN)") bot.slowmsg(user, "QUESTIONPAUSE (ADMIN)") -@lib.hooknum(332) +@lib.hooknum(332) # topic is... +@lib.hooknum(331) # no topic set def num_TOPIC(bot, textline): pieces = textline.split(None, 4) chan = pieces[3] @@ -884,7 +892,7 @@ def num_TOPIC(bot, textline): 'category': state.db['category'], } if gottopic != formatted: - state.getbot().conn.send("TOPIC %s :%s" % (state.db['chan'], formatted)) + state.getbot().conn.send(bot.parent.cfg.get('trivia', 'topiccommand', default="TOPIC %(chan)s :%(topic)s") % {'chan': state.db['chan'], 'topic': formatted}) def specialQuestion(oldq):