Exemplo n.º 1
0
    def _tell_number(self) -> None:
        """To evaluate user input and send result"""
        bases = {'0b': 2, '0o': 8, '0x': 16}
        base = self.user_input[:2]
        short = base == '0s'
        if short:
            self.user_input = self.user_input[2:]
            base = self.user_input[:2]
        message = random.choice(msg.errors)

        if self.user_input.isdecimal():
            message = translate_num(int(self.user_input), short)
        elif base in bases:
            try:
                dec = int(self.user_input, bases[base])
                message = msg.decimal.format(dec) +\
                    translate_num(dec, short)
            except ValueError:
                pass
        else:
            ten, _, power = self.user_input.rpartition('*')
            try:
                if ten == '10*':
                    message = translate_10power(int(power))
                elif ten == '10':
                    message = msg.power10_err
            except ValueError:
                pass
        tg.send_message(self.user_id, message)
Exemplo n.º 2
0
 def _stop_game(self) -> None:
     """Stops current game without saving stats"""
     if self.user.game_started:
         tg.send_message(self.user.id, msg.break_game)
         self.user.game_started = False
         self.user.save()
     else:
         tg.send_message(self.user.id, msg.no_game_started)
Exemplo n.º 3
0
 def _start_game(self) -> None:
     """Inits variables for starting game and logs a start game info"""
     self.user.game_started = True
     self.user.hard_mode = self.user_input in commands.hardmode
     self.user.guess_number = random.randint(1, config.MAX_NUMBER)
     self.user.start_time = self.update_time
     self.user.tries_count = 0
     print(self.user)
     message = msg.start_game.format(config.MAX_NUMBER)
     tg.send_message(self.user.id, message)
     self.user.save()
Exemplo n.º 4
0
    def _make_move(self) -> None:
        """Analizes ingame input from user and acts"""
        self.user.tries_count += 1
        self.user.save()
        try:
            user_guess = int(self.user_input)
        except ValueError:
            tg.send_message(self.user.id, random.choice(msg.errors))
            return

        if user_guess != self.user.guess_number:
            message = self._distanse(user_guess, self.user.guess_number,
                                     self.user.hard_mode)
        else:
            message = self._finish_game()
        tg.send_message(self.user.id, message)
Exemplo n.º 5
0
 def _stats(self) -> None:
     """Shows current user stats"""
     if self.user.games_played or self.user.hard_games_played:
         message = msg.stats_title
         if self.user.games_played:
             message += msg.stats_normal.format(
                 self._plural(self.user.games_played, msg.games),
                 self._plural_time(self.user.best_time),
                 self._plural(self.user.best_steps, msg.tries))
         if self.user.hard_games_played:
             message += msg.stats_hard.format(
                 self._plural(self.user.hard_games_played, msg.games),
                 self._plural_time(self.user.hard_best_time),
                 self._plural(self.user.hard_best_steps, msg.tries))
     else:
         message = msg.stats_nogames
     tg.send_message(self.user.id, message)
Exemplo n.º 6
0
 def _proceed(self, text: str) -> None:
     """Analizes text command from user and acts"""
     self.user_input = text.lower().strip('/')
     print(self._now(), self.user.name, '-->', self.user_input)
     if self.user_input in commands.helping:
         self._help()
     elif self.user_input in commands.leaders:
         self._leaders()
     elif self.user_input in commands.stats:
         self._stats()
     elif self.user_input in commands.stop:
         self._stop_game()
     elif self.user.game_started:
         self._make_move()
     elif self.user_input in commands.start:
         self._start_game()
     else:
         tg.send_message(self.user.id, random.choice(msg.relpies))
Exemplo n.º 7
0
    def listen(self) -> None:
        """Receives updates from the bot and acts"""
        data = tg.get_updates(self.last_update_id + 1)
        if not data:
            return

        for update in data['result']:
            if not self._get_update_info(update):
                return

            if not self.user.read():
                self.user.create()
                message = msg.hello.format(self.user.name)
                tg.send_message(self.user.id, message)

            if 'text' in update['message']:
                self._proceed(update['message']['text'])
            else:
                self._idle()
Exemplo n.º 8
0
    def _leaders(self) -> None:
        """Shows TOP-3 leaderboard"""
        def _leaders_list(rows: list, func, plurals=()) -> str:
            result = ''
            for i, (nick, name, value) in enumerate(rows):
                user = f'@{nick}' if nick else name
                result += f'{msg.medals[i]} {user} {func(value, plurals)}\n'
            return result

        norm_steps = self.user.get_top3_steps()
        norm_times = self.user.get_top3_time()
        hard_steps = self.user.get_top3_hard_steps()
        hard_times = self.user.get_top3_hard_time()
        message = msg.leaders_title.format(
            _leaders_list(norm_steps, self._plural, msg.tries),
            _leaders_list(norm_times, self._plural_time),
            _leaders_list(hard_steps, self._plural, msg.tries),
            _leaders_list(hard_times, self._plural_time))
        tg.send_message(self.user.id, message, parse_mode='html')
Exemplo n.º 9
0
 def _idle(self) -> None:
     """Reacts to non text event"""
     tg.send_message(self.user.id, msg.sticker_reply)
Exemplo n.º 10
0
 def _help(self) -> None:
     """Sends appropriate help message"""
     if self.user.game_started:
         tg.send_message(self.user.id, msg.ingame_help)
     else:
         tg.send_message(self.user.id, msg.command_help)
Exemplo n.º 11
0
 def _easter(self) -> None:
     """Sends easter egg message"""
     tg.send_message(self.user_id, msg.command_easter)
Exemplo n.º 12
0
 def _interesting(self) -> None:
     """Sends interesting message"""
     tg.send_message(self.user_id, msg.command_interesting)
Exemplo n.º 13
0
 def _help(self) -> None:
     """Sends help message"""
     tg.send_message(self.user_id, msg.command_help)
Exemplo n.º 14
0
 def _idle(self) -> None:
     """To react to the non text event"""
     tg.send_message(self.user_id, msg.sticker_reply)