def test_paginate_first_page(self): elems = [1, 2, 3, 4, 5, 6] max_itens = 5 page = 1 result = paginate(elems, page, max_itens) self.assertEqual(result[0], elems[:5]) self.assertEqual(result[1], 2)
def test_paginate_invalid_page_high(self): elems = [1, 2, 3, 4, 5, 6] max_itens = 5 page = 10 result = paginate(elems, page, max_itens) self.assertEqual(result[0], elems[5:]) self.assertEqual(result[1], 2)
async def _build_medals_embed(self, page_number, original_message): max_medals_per_page = 6 leaderboard_data = await self.leaderboard_bot.get() medals = await self.leaderboard_bot.build_medals_info(*leaderboard_data) paginated_medals, last_page = paginate(medals, page_number, max_medals_per_page) embed = discord.Embed( title=i(original_message, "Star Wars Wiki's medals"), description=f'{i(original_message, "Page")} {max(page_number, 1)}/{last_page}', colour=discord.Color.blurple() ) for medal_info in paginated_medals: embed.add_field(name=medal_info['name'], value=medal_info['text']) self.medals_paginated_embed_manager.last_page = last_page return embed
def get_all_boards_png(self, page: int = 0): """ Gets an image showing all ongoing games' current position. If there are more than 9 games being played at once, this command is paginated. :param page: Page index (defaults to 0) :type page: int :return: Image's bytesIO :rtype: BytesIO """ full_width = 1200 max_number_of_board_per_page = 9 if not self.games: return None final_image = Image.new('RGB', (full_width, full_width)) next_perfect_sqr = lambda n: int(pow(floor(sqrt(n)) + 1, 2) ) if n % n**0.5 != 0 else n number_of_boards_sqrt = sqrt( min(next_perfect_sqr(len(self.games)), max_number_of_board_per_page)) board_width = int(full_width / number_of_boards_sqrt) start_page_position = max_number_of_board_per_page * page paginated_games, _ = paginate(self.games, page, max_number_of_board_per_page) for index, game in enumerate(paginated_games): board_png = self.build_png_board(game) board_image = Image.open(board_png) board_position = (board_width * int(index % number_of_boards_sqrt), board_width * int(floor(index / number_of_boards_sqrt))) final_image.paste(board_image.resize((board_width, board_width)), board_position) bytesio = BytesIO() final_image.save(bytesio, format="png") bytesio.seek(0) return bytesio
async def _create_paginated_help_embed(self, page_number, original_message): max_itens_per_page = 9 bot_prefix = os.environ.get("BOT_PREFIX", 'cp!') bot_commands = sorted(self.client.commands, key=lambda x: x.name) paginated_commands, last_page = paginate(bot_commands, page_number, max_itens_per_page) help_embed = discord.Embed( title=i(original_message, 'Help'), description= f'{i(original_message, "Commands")} ({min(max(page_number, 1), last_page)}/{last_page}):', colour=discord.Color.blurple()) for cmd in paginated_commands: cmd_short_description = i(original_message, f'cmd_{cmd.name}').split("\n")[0] if cmd_short_description == f'cmd_{cmd.name}': cmd_short_description = 'No description available' help_embed.add_field(name=f'{bot_prefix}{cmd.name}', value=cmd_short_description) self.help_cmd_manager.last_page = last_page return help_embed
async def draw_leaderboard(self, leaderboard: list, page: int): rectangle_height = 50 image_width = 500 text_spacing = 10 font_size = 18 medal_size = 30 max_users_per_page = 10 paginated_leaderboard, _ = paginate(leaderboard, page, max_users_per_page) medal_positions = [5, int(medal_size * 0.5), int(medal_size * 0.833)] unique_medals = set([ (medal, user_info[1]['medals'][medal]['image_url']) for user_info in leaderboard for medal in user_info[1]['medals'] ]) await self._prepare_medals_images(unique_medals) final_image = Image.new('RGB', (image_width, rectangle_height * min(len(leaderboard), max_users_per_page))) draw_image = ImageDraw.Draw(final_image) last_rectangle_pos = 0 alternate_row_control = True for user_name, user_info in paginated_leaderboard: draw_image.rectangle( ((0, last_rectangle_pos), (image_width, last_rectangle_pos + rectangle_height)), fill="#D3D3D3" if alternate_row_control else "#CCC") if last_rectangle_pos: draw_image.line(((0, last_rectangle_pos), (image_width, last_rectangle_pos)), fill='#A9A9A9') draw_image.text((medal_size * 2 + text_spacing, last_rectangle_pos + text_spacing), '{:.20}'.format(user_name), fill='#2E2E2E', font=ImageFont.truetype( os.environ.get("TRUETYPE_FONT_FOR_USERS_PATH"), size=font_size)) draw_image.text( (image_width - int(5 * font_size * 0.7), last_rectangle_pos + text_spacing), '{:5}'.format(user_info["points"]), fill='#2E2E2E', font=ImageFont.truetype( os.environ.get("TRUETYPE_FONT_FOR_POINTS_PATH"), size=font_size + 2)) last_medal_pos = medal_positions[min( max(len(user_info['medals']) - 1, 0), 2)] for medal_name, medal_info in user_info['medals'].items(): medal_image = Image.open( BytesIO(await self._get_image( medal_name, medal_info['image_url']))).convert('RGBA').resize( (medal_size, medal_size)) final_image.paste( medal_image, (last_medal_pos, last_rectangle_pos + text_spacing), mask=medal_image) if not medal_positions.index(last_medal_pos): break last_medal_pos = medal_positions[ medal_positions.index(last_medal_pos) - 1] last_rectangle_pos += rectangle_height alternate_row_control = not (alternate_row_control) bytesio = BytesIO() final_image.save(bytesio, format="png") bytesio.seek(0) return bytesio