示例#1
0
    def start_battle_push(self, battle_poll):
        if self.task_exist(battle_poll.user.id):
            return

        # Update poll to database
        store.update_battle_poll(battle_poll)

        self._battle_pools.append(battle_poll)

        job_params = (battle_poll, Splatoon2(battle_poll.user.iksm_session))
        job = self.job_queue.run_repeating(self._battle_push_task,
                                           interval=10,
                                           first=0,
                                           context=job_params,
                                           name=str(battle_poll.user.id))
        self._jobs.append(job)
示例#2
0
    def _battle_push_task(self, context: CallbackContext):
        (battle_poll, splatoon2) = context.job.context

        last_message_id = battle_poll.last_message_id
        last_battle_number = battle_poll.last_battle_number
        bot = context.bot

        # Get last battle detail
        try:
            battle_overview = splatoon2.get_battle_overview()
        except Splatoon2SessionInvalid:
            # Stop
            self.stop_push(battle_poll.user.id)
            return
        except:
            self.stop_push(battle_poll.user.id)
            return

        if len(battle_overview.results) == 0:
            return
        last_battle = battle_overview.results[0]

        if configs.DEBUG:
            print(f'Load battle: {last_battle.battle_number}')

        if last_battle_number and \
                last_battle_number != last_battle.battle_number:

            print(f'Found new battle: {last_battle.battle_number}')

            battle = splatoon2.get_battle(last_battle.battle_number)

            rank = battle.player_result.player.udemae

            if battle_poll.last_battle_udemae and \
                    battle_poll.last_battle_rule == battle.rule.key and \
                    battle_poll.last_battle_udemae.name != rank.name and \
                    battle_poll.last_battle_udemae.s_plus_number != rank.s_plus_number:
                content = Message.rank_changed(
                    battle.player_result.player.nickname,
                    battle_poll.last_battle_udemae,
                    rank
                )
                bot.send_message(
                        battle_poll.chat.id,
                        content
                    )

            # Update poll
            battle_poll.game_count += 1
            battle_poll.game_victory_count += int(battle.victory)
            battle_poll.last_battle_number = battle.battle_number
            battle_poll.last_battle_udemae = rank
            battle_poll.last_battle_rule = battle.rule.key
            # Save updated to context
            context.job.context = (battle_poll, splatoon2)

            # Menus
            buttons = [[
                InlineKeyboardButton('👍',
                                     callback_data=f'battle_like/{battle_poll.user.id}'),
                InlineKeyboardButton('🖼',
                                     callback_data=f'battle_detail/{battle_poll.user.id}/{last_battle.battle_number}')
            ]]
            reply_markup = InlineKeyboardMarkup(buttons)

            # Send push message
            (content, message_type) = Message.push_battle(battle, battle_poll)
            parse_mode = message_type if message_type else None

            try:
                sent_message = bot.send_message(battle_poll.chat.id,
                                                content,
                                                parse_mode=parse_mode,
                                                reply_markup=reply_markup)
            except BadRequest as e:
                # Resend
                sent_message = bot.send_message(battle_poll.chat.id,
                                                content,
                                                reply_markup=reply_markup)

            # Update value
            battle_poll.last_message_id = sent_message.message_id
            # Save updated to context
            context.job.context = (battle_poll, splatoon2)

            # Delete
            if last_message_id and battle_poll.chat.type != 'private':
                bot.delete_message(battle_poll.chat.id, last_message_id)

        elif not last_battle_number:
            battle_poll.last_battle_number = last_battle.battle_number
            # Save updated to context
            context.job.context = (battle_poll, splatoon2)

        store.update_battle_poll(battle_poll)