def __init__(self, name, start_level=1): self.current_streak = 0 self.max_streak = 0 self.total_points = 0 self.total_time = 0 self.name = name self.level = Level(start_level) self.question = None io.send("welcome", name=self.name)
def ask(self): io.send("question", question=self.question, time=self.level.time) answered = False while not answered: self.user_answer, self.time = Question.get_answer() if self.user_answer is None: Game.help() else: answered = True self._calculate_points()
def help(): io.send("help")
def try_exit(self): if self.question.user_answer in EXIT_REQUEST: io.send("quit", streak=self.max_streak, points=self.total_points) self.log() sys.exit()
def _calculate_level(self): if not (self.question.correct and self.question.in_time): self.level = Level(max(1, self.level.level - 1)) elif self.current_streak % 5 == 0 and self.current_streak and self.level.level < NUMBER_OF_LEVELS: self.level = Level(self.level.level + 1) io.send("level up", level=self.level.level)
def run(self): self.ask_question() self.try_exit() self.question.log() self.total_points += self.question.points self.total_time += self.question.time self._calculate_streak() if self.question.correct and self.question.in_time: io.send("success") elif not self.question.in_time: io.send("time failure", self.question.correct_answer) elif not self.question.correct: io.send("failure", self.question.correct_answer) if self.current_streak > 3: io.send("streak", streak=self.current_streak) io.send("points", points=self.total_points, end=' ') io.send("time", time=self.question.time) io.send("question end") self._calculate_level() self.run()