예제 #1
0
    def test_get(self):
        leaderboard_bot = Leaderboard(auto_close_session=True)

        result = run(leaderboard_bot.get())

        self.assertEqual(len(result), 2)
        self.assertIn("dataUser", result[0])
        self.assertGreaterEqual(len(result[1]), 5)
예제 #2
0
    def test_complete_leaderboard_flow(self):
        async def complete_flow(leaderboard_bot):
            try:
                leaderboard_data = await leaderboard_bot.get()
                leaderboard_result = leaderboard_bot.build_leaderboard(
                    *leaderboard_data)
                return await leaderboard_bot.draw_leaderboard(
                    leaderboard_result, 1)
            finally:
                await leaderboard_bot.main_session.close()

        leaderboard_bot = Leaderboard()

        actual = run(complete_flow(leaderboard_bot))

        self.assertIsNotNone(Image.open(actual))
예제 #3
0
    def test_draw_leaderboard_second_page(self):
        leaderboard_bot = Leaderboard(auto_close_session=True)
        leaderboard = [
            ("User2", {
                'points': 500,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("User1LongoNameUser1LongoNameUser1LongoName", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    },
                    'Medal2': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    },
                    'Medal5': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    },
                    'Medal3': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    }
                }
            }),
            ("User3", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    },
                    'Medal2': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    }
                }
            }),
            ("User5", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("User6", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("User7", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("User8", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("User9", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserA", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserB", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserC", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserD", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserE", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserF", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserG", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserH", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserI", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserJ", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserK", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("UserL", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
        ]

        actual = run(leaderboard_bot.draw_leaderboard(leaderboard, 2))

        with open(
                os.path.join('tests', 'support',
                             'draw_leaderboard_second_page.png'), 'rb') as f:
            self.assertEqual(actual.getvalue(), f.read())
예제 #4
0
    def test_build_leaderboard_success(self):
        leaderboard_bot = Leaderboard()

        medals = {
            "dataUser": {
                "User1": ["Medal1:1", "Medal2:3"],
                "User2": ["Medal4:1"],
                "User3": ["Medal1:2", "Medal3:4"],
                "User4": ["Medal1:2", "Medal3:1", "Medal2:1"]
            },
            "dataMedal": {
                "Medal1": {
                    "title": "Medal description",
                    "image_url": "some_url"
                },
                "Medal2": {
                    "title": "Medal description",
                    "image_url": "some_url"
                },
                "Medal3": {
                    "title": "Medal description",
                    "image_url": "some_url"
                },
                "Medal4": {
                    "title": "Medal description",
                    "image_url": "some_url"
                },
            }
        }
        medals_points = {
            "Medal1": 50,
            "Medal2": 100,
            "Medal3": 25,
            "Medal4": 500,
            "DescontoInativo": {
                "usuários": ["User3"],
                "desconto": 0.8
            },
            "DescontoAdmin": {
                "usuários": ["User4"],
                "desconto": 0.8
            },
        }
        expected = [('User2', {
            'points': 500,
            'medals': {
                'Medal4': {
                    'quantity': 1,
                    'text': 'Medal description',
                    'image_url': 'some_url'
                }
            }
        }),
                    ('User1', {
                        'points': 350,
                        'medals': {
                            'Medal1': {
                                'quantity': 1,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            },
                            'Medal2': {
                                'quantity': 3,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            }
                        }
                    }),
                    ('User4', {
                        'points': 180,
                        'medals': {
                            'Medal1': {
                                'quantity': 2,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            },
                            'Medal3': {
                                'quantity': 1,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            },
                            'Medal2': {
                                'quantity': 1,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            }
                        }
                    }),
                    ('User3', {
                        'points': 160,
                        'medals': {
                            'Medal1': {
                                'quantity': 2,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            },
                            'Medal3': {
                                'quantity': 4,
                                'text': 'Medal description',
                                'image_url': 'some_url'
                            }
                        }
                    })]

        actual = leaderboard_bot.build_leaderboard(medals, medals_points)

        self.assertEqual(actual, expected)
예제 #5
0
    def test_draw_leaderboard_one_page(self):
        leaderboard_bot = Leaderboard(auto_close_session=True)
        leaderboard = [
            ("User2", {
                'points': 500,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    }
                }
            }),
            ("User1LongoNameUser1LongoNameUser1LongoName", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    },
                    'Medal2': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    },
                    'Medal5': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    },
                    'Medal3': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    }
                }
            }),
            ("User3", {
                'points': 350,
                'medals': {
                    'Medal1': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/b/b7/Medalha_destaque.png'
                    },
                    'Medal2': {
                        'quantity':
                        1,
                        'image_url':
                        'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
                    }
                }
            })
        ]

        actual = run(leaderboard_bot.draw_leaderboard(leaderboard, 1))

        with open('tests/support/draw_leaderboard.png', 'rb') as f:
            self.assertEqual(actual.getvalue(), f.read())
예제 #6
0
    def test_build_medals_info(self):
        leaderboard_bot = Leaderboard()
        medals = {
            "dataUser": {
                "User1": ["Medal1:1", "Medal2:3"],
                "User2": ["Medal4:1"],
                "User3": ["Medal1:2", "Medal3:4"],
                "User4": ["Medal1:2", "Medal3:1", "Medal2:1"]
            },
            "dataMedal": {
                "Medal1": {
                    "title":
                    "Medal description",
                    "image_url":
                    "https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png"
                },
                "Medal2": {
                    "title":
                    "Medal description",
                    "image_url":
                    "https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png"
                },
                "Medal3": {
                    "title":
                    "Medal description",
                    "image_url":
                    "https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png"
                },
                "Medal4": {
                    "title":
                    "Medal description",
                    "image_url":
                    "https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png"
                },
            }
        }
        medals_points = {
            "Medal1": 50,
            "Medal2": 100,
            "Medal3": 25,
            "Medal4": 500,
            "DescontoInativo": {
                "usuários": ["User3"],
                "desconto": 0.8
            },
            "DescontoAdmin": {
                "usuários": ["User4"],
                "desconto": 0.8
            },
        }

        actual = run(leaderboard_bot.build_medals_info(medals, medals_points))
        image_bytes = run(
            leaderboard_bot._get_image(
                'Medal1',
                'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png'
            ))

        self.assertEqual(actual, [
            {
                'name': 'Medal1',
                'text': 'Medal description',
                'image_url':
                'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png',
                'image': image_bytes,
                'points': 50
            },
            {
                'name': 'Medal2',
                'text': 'Medal description',
                'image_url':
                'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png',
                'image': image_bytes,
                'points': 100
            },
            {
                'name': 'Medal3',
                'text': 'Medal description',
                'image_url':
                'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png',
                'image': image_bytes,
                'points': 25
            },
            {
                'name': 'Medal4',
                'text': 'Medal description',
                'image_url':
                'https://vignette.wikia.nocookie.net/pt.starwars/images/2/2e/Medalha_bom.png',
                'image': image_bytes,
                'points': 500
            },
        ])
예제 #7
0
    def test_build_leaderboard_invalid_parameters(self):
        leaderboard_bot = Leaderboard()

        with self.assertRaises(Exception):
            actual = leaderboard_bot.build_leaderboard("invalid", {})
예제 #8
0
class StarWarsWikiCog(commands.Cog):
    """
    Comandos da Star Wars Wiki em Português
    """

    def __init__(self, client):
        self.client = client
        self.leaderboard_bot = Leaderboard()
        self.medals_paginated_embed_manager = PaginatedEmbedManager(
            client, self._build_medals_embed)

    @commands.command(aliases=['lb'])
    async def leaderboard(self, ctx, page: int=1):
        """
        Exibe o leaderboard de medalhas da Star Wars Wiki
        """
        await ctx.trigger_typing()
        try:
            leaderboard_data = await self.leaderboard_bot.get()
            leaderboard_result = self.leaderboard_bot.build_leaderboard(*leaderboard_data)
            leaderboard_img = await self.leaderboard_bot.draw_leaderboard(leaderboard_result, page)

            await ctx.send(file=discord.File(leaderboard_img, 'leaderboard.png'))
        except Exception as e:
            logging.warning(e, exc_info=True)
            return await ctx.send(i(ctx, "Something went wrong when trying to fetch Star Wars Wiki's leaderboard"))

    @commands.command(aliases=['medalha'])
    async def medal(self, ctx, *, medal_name):
        """
        Exibe detalhes de uma medalha da Star Wars Wiki
        """
        await ctx.trigger_typing()
        try:
            leaderboard_data = await self.leaderboard_bot.get()
            medals = await self.leaderboard_bot.build_medals_info(*leaderboard_data)
            
            medal_info = [medal for medal in medals if medal['name'] == medal_name]
            if not medal_info:
                return await ctx.send(i(ctx, "Medal not found"))
            
            medal_info = medal_info[0]
            embed = discord.Embed(
                title=i(ctx, "Star Wars Wiki's medals"),
                description=medal_info['name'],
                colour=discord.Color.blurple(),
                timestamp=ctx.message.created_at
            )
            embed.set_thumbnail(url=medal_info['image_url'])
            embed.add_field(name=i(ctx, 'Description'), value=medal_info['text'])
            embed.add_field(name=i(ctx, 'Points'), value=medal_info['points'])
            await ctx.send(embed=embed)
        except Exception as e:
            logging.warning(e, exc_info=True)
            return await ctx.send(i(ctx, "Something went wrong when trying to fetch Star Wars Wiki's leaderboard"))

    @commands.command(aliases=['medalhas'])
    async def medals(self, ctx, page: int=1):
        """
        Exibe as medalhas disponíveis da Star Wars Wiki
        """
        await ctx.trigger_typing()
        try:
            return await self.medals_paginated_embed_manager.send_embed(
                await self._build_medals_embed(page, ctx), page, ctx)
        except Exception as e:
            logging.warning(e, exc_info=True)
            return await ctx.send(i(ctx, "Something went wrong when trying to fetch Star Wars Wiki's leaderboard"))

    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
예제 #9
0
 def __init__(self, client):
     self.client = client
     self.leaderboard_bot = Leaderboard()
     self.medals_paginated_embed_manager = PaginatedEmbedManager(
         client, self._build_medals_embed)