+ self.closeshop()
+ def closeshop(self):
+ if threading is not None and threading._Timer is not None and isinstance(self.steptimer, threading._Timer):
+ self.steptimer.cancel()
+ if json is not None and json.dump is not None:
+ json.dump(self.db, open(self.questionfile, "w"), indent=4, separators=(',',': '))
+
+ def getchan(self):
+ return self.parent.channel(self.chan)
+ def getbot(self):
+ return self.getchan().bot
+
+ def nexthint(self, hintnum):
+ answer = self.hintanswer
+
+ 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))
+
+ 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.msg(self.chan, "Here's a hint: %s" % (''.join(self.hintstr)))
+
+ self.hintsgiven += 1
+
+ if hintnum < HINTNUM:
+ self.steptimer = threading.Timer(HINTTIMER, self.nexthint, args=[hintnum+1])
+ self.steptimer.start()
+ else:
+ self.steptimer = threading.Timer(HINTTIMER, self.nextquestion, args=[True])
+ self.steptimer.start()
+
+ def doGameOver(self):
+ def msg(line): self.getbot().msg(self.getchan(), line)
+ def person(num): return self.db['users'][self.db['ranks'][num]]['realnick']
+ def pts(num): return self.db['users'][self.db['ranks'][num]]['points']
+ 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)]
+ except IndexError: pass
+ except Exception as e: msg("DERP! %r" % (e))
+
+ self.db['users'] = {}
+ self.db['ranks'] = []
+ stop()
+ self.closeshop()
+
+ if hastwitter:
+ t = twitter.Twitter(auth=twitter.OAuth(self.getbot().parent.cfg.get('trivia', 'token'),
+ self.getbot().parent.cfg.get('trivia', 'token_sec'),
+ self.getbot().parent.cfg.get('trivia', 'con'),
+ self.getbot().parent.cfg.get('trivia', 'con_sec')))
+ t.statuses.update(status="Round is over! The winner was %s" % (winner))
+
+ self.__init__(self.questionfile, self.parent)
+
+ def nextquestion(self, qskipped=False, iteration=0):
+ if self.gameover == True:
+ return self.doGameOver()
+ if qskipped:
+ self.getbot().msg(self.getchan(), "Fail! The correct answer was: %s" % (self.hintanswer))
+ self.missedquestions += 1
+ else:
+ self.missedquestions = 0
+
+ if isinstance(self.steptimer, threading._Timer):
+ self.steptimer.cancel()
+
+ self.hintstr = None
+ self.hintsgiven = 0
+ self.revealpossibilities = None
+ self.reveal = None
+
+ if self.missedquestions > MAXMISSEDQUESTIONS:
+ stop()
+ self.getbot().msg(self.getchan(), "%d questions unanswered! Stopping the game.")
+
+ if state.nextq is not None:
+ nextq = state.nextq
+ state.nextq = None
+ else:
+ nextq = random.choice(self.db['questions'])
+
+ 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)
+ for qword in qary:
+ qtext += "\00300,01"+qword+"\00301,01"+chr(random.randrange(32,126))
+ self.getbot().msg(self.chan, qtext)