def restart_player(self, id): """ Рестарт игрока """ player_answers = session.query(models.PlayerAnswer).filter_by(player_id=id).all() for answer in player_answers: session.delete(answer) self.set_finish_status(id, False) session.query(models.Player).filter_by(tg_id=id).update({'current_q_id': 1}) session.commit()
def get_q_id(self, add=False): """ Получение айди вопроса, на котором остановился игрок """ # player_q_id = session.query(models.Player).with_entities(models.Player.current_q_id).filter_by(tg_id=self.player_id).all() player_q_id = session.query(models.Player).with_entities(models.Player.current_q_id).filter_by(tg_id=self.player_id).all() player_q_id = player_q_id[0][0] if add: player_q_id += 1 session.query(models.Player).filter_by(tg_id=self.player_id).update({'current_q_id': player_q_id}) session.commit() return player_q_id
def add_answer(self, answer): """ Добавить ответ игрока в таблицу ответов всех игроков""" player_q_id = self.get_q_id() answer_q_id = session.query(models.QuestionVariable).filter_by(variable=answer).with_entities(models.QuestionVariable.q_id).all() answer_q_id = answer_q_id[0][0] if player_q_id == 1 or player_q_id == answer_q_id: answer_choice_id = session.query(models.QuestionVariable).filter_by(variable=answer).first() answer_choice_id = answer_choice_id.id answer = models.PlayerAnswer(player_id=self.player_id, answer_id=answer_choice_id, q_id=player_q_id) session.add(answer) session.commit() return True return False
def set_new_quiz(): """ Функция обновления БД для новой викторины """ session.query(RightAnswer).delete() session.query(Question).delete() session.query(QuestionVariable).delete() session.commit() for id in range(1, len(cfg.QUESTIONS) + 1): question_id = id - 1 current_question = cfg.QUESTIONS[question_id]['question'] current_answer = cfg.QUESTIONS[question_id]['answer'] right_answer = RightAnswer(right_answer=current_answer) question = Question(question=current_question, answer_id=id) session.add(right_answer) session.add(question) session.commit() current_variables = cfg.QUESTIONS[question_id]['variables'] for var in current_variables: session.add(QuestionVariable(variable=var, q_id=id)) session.commit()
def get_right_answers(self): """ Получение всех правильных ответов викторины """ right_answers = session.query(models.RightAnswer).all() return [right_answer.right_answer for right_answer in right_answers]
def get_questions_count(self): """ Получение общего числа всех вопросов """ questions = session.query(models.Question).all() return len(questions)
def get_question_variables(self, q_id): """ Получение вариантов ответа для текущего вопроса """ variables = session.query(models.QuestionVariable).filter_by(q_id=q_id).all() variables = [variable.variable for variable in variables] return variables
def get_question(self, q_id): """ Получение текста текущего вопроса """ question = session.query(models.Question).filter_by(id=q_id).all() return question[0].question
def set_finish_status(self, id, is_finished=True): """ Установка игроку значение поля finished """ session.query(models.Player).filter_by(tg_id=id).update({'is_finished': is_finished}) session.commit()
def check_player(self, id): """ Проверка существования игрока """ player = session.query(models.Player).filter_by(tg_id=id).all() if not player: return False return True
def all_answers(self): """ Получение всех ответов игрока """ player_answers = session.query(models.PlayerAnswer).filter_by(player_id=self.player_id).all() player_answers = [player_answer.answer.variable for player_answer in player_answers] return player_answers