def argumentParser(self,bot,trigger): """split argument parser and show info/error""" parser = argparse.ArgumentParser(".trivial start",formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-t','--theme',nargs='*',help='execute trivial only with theme selected',default=[]) parser.add_argument('-n','--number-question',nargs='?',type=int,const='15', default='15',help='number question of game.',choices=xrange(1, 1000),metavar='choose from 1..1000') parser.add_argument('-p','--points-to-win',nargs='?',type=int,const='0', default='0',help='number points to win, if you reach this punctuation before finishing the game, this ends immediately. 0 to no reach this punctuation',choices=xrange(0, 1000),metavar='choose from 0...1000') parser.add_argument("-team",nargs ='*',action='append', default=[], help='trivial with teams, example -team user1 user2 -team user3') #disable stdout f = open(os.devnull, 'w') stderr_aux = sys.stderr stdout_aux = sys.stdout sys.stdout = f sys.stderr = f try: args = parser.parse_args(trigger.bytes.lower().split()[2:]) except: if trigger.bytes.lower().split()[2:] ==['-h']: for line in parser.format_help().split('\n'): bot.notice(line,recipient=trigger.nick) else: for line in parser.format_usage().split('\n'): bot.say(line) #enable stderr sys.stderr = stderr_aux raise #enable stderr sys.stderr = stderr_aux sys.stdout = stdout_aux return args
def check_answerd(self,bot,trigger): self.lock.acquire() if not self.running_game: print "vamos" self.lock.release() return if unidecode(trigger.bytes.lower()) == unidecode(self.answerd.answerd.lower()): self.t.cancel() check=False for team in self.teams: if team.search_score(trigger.nick): bot.say("minipunto para el equipo" + team.team()) check=True if not check: # user does not belong to any team bot.say("minipunto para " + trigger.nick) self.score[str(trigger.nick)]= self.score.get(str(trigger.nick),0)+1 for name, score in self.score.iteritems(): if score >= self.points_to_win and self.points_to_win!=0: self.endgame(bot) for team in self.teams: if team.score>=self.points_to_win and self.points_to_win!=0: self.endgame(bot) if self.running_game: # game not finished self.send_question(bot) self.lock.release()
def send_pista(self,bot): self.lock.acquire() if self.answerd.stop(): bot.say("Respuesta no acertada. La respuesta era: " + self.answerd.answerd) self.send_question(bot) else: bot.say(self.answerd.show_more_letters()) self.t = threading.Timer(int (bot.config.trivia_game.interval), self.send_pista,(bot,)) self.t.start() self.lock.release()
def send_question(self,bot): if self.i_question <= self.number_question: question = random.choice(self.questions) self.answerd = Answerd(question.answerd) bot.say("{0}/{1}|{2}".format(self.i_question,self.number_question,question.question)) self.i_question += 1 self.t = threading.Timer(int (bot.config.trivia_game.interval), self.send_pista,(bot,)) self.t.start() else: #finish game self.endgame(bot)
def endgame(self,bot): """stop game and reset score""" self.running_game=False bot.say("Endgame, score:") bot.say(self._score()) # check if score is not empty if self.score: # check if exist eol_manager if bot.memory.has_key('eol_manager'): # check if method exist if "post" in dir(bot.memory['eol_manager']): bot.memory['eol_manager'].post(self._score_eol())
def select_questions(self,bot,themes): """Select questions by theme""" myset = set(themes) if len(myset) !=0: themes = self._themes() for i in myset: if i not in themes: bot.say("Theme {0} not found".format(i)) raise Exception l = [i for i in self.ddbb_questions if i.theme in myset] self.questions = l else: self.questions=self.ddbb_questions
def select_teams(self,bot,teams): self.teams = [] list_users = [] for channel in bot.channels: for i in bot.privileges[channel]: list_users.append(i) for team in teams: for user in team: if user not in list_users: bot.say("User {0} not found".format(user)) raise Exception ("error in team") for team in teams: # create list of teams if len(team)>0: self.teams.append(Team(team))
def _trivial_start(self,bot,trigger): """Start trivia game. Usage: .trivial start to see more options .trivial start -h""" if self.running_game == False: if len(self.ddbb_questions)==0: bot.say("no hay ninguna pregunta cargada") else: self.score={} self.teams=[] Team.reset() try: args = self.argumentParser(bot,trigger) self.select_questions(bot,args.theme) self.select_teams(bot,args.team) except Exception as e: return self.number_question = args.number_question # gnumber of questions in the game self.points_to_win = args.points_to_win self.i_question = 1 # number ot question self.send_question(bot) self.running_game = True else : bot.say("juego ya ha comenzado")
def _trivial_themes(self,bot, trigger): """Show topics available in the questions. Usage: .trivial themes""" # cache decorator or even easier, upload theme ready to start bot.say(str(self._themes()))
def _trivial_score(self,bot, trigger): """Show a score. Usage: .trivial score""" bot.say(self._score())
def _trivial_pista(self,bot, trigger): """Show a help more. Usage: .trivial pista""" if self.running_game: bot.say(self.answerd.show_more_letters())