From 79e9e66d959a8976e90e5f3ea2b667a3e234cda6 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sun, 30 Oct 2016 16:54:58 -0500 Subject: [PATCH] Changed image creation to use tuples, to allow order preservation --- cogs/osu.py | 5 ++--- cogs/overwatch.py | 14 ++++++-------- cogs/stats.py | 19 ++++++++++--------- cogs/utils/images.py | 5 +++-- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cogs/osu.py b/cogs/osu.py index 663fd57..f48e0e0 100644 --- a/cogs/osu.py +++ b/cogs/osu.py @@ -55,7 +55,6 @@ class Osu: except IndexError: return None - @commands.group(pass_context=True, invoke_without_command=True) @checks.custom_perms(send_messages=True) async def osu(self, ctx): @@ -122,7 +121,7 @@ class Osu: # If the key is in our wanted_info list # We also get the wanted value from the key_map if it exists, using the key itself if it doesn't # We then title it and replace _ with a space to ensure nice formatting - fmt = {key_map.get(k, k).title().replace('_', ' '): v for k, v in data.items() if k in wanted_info} + fmt = [(key_map.get(k, k).title().replace('_', ' '), v) for k, v in data.items() if k in wanted_info] # Attempt to create our banner and upload that # If we can't find the images needed, or don't have permissions, just send a message instead @@ -130,7 +129,7 @@ class Osu: banner = await images.create_banner(ctx.message.author, "Osu User Stats", fmt) await self.bot.upload(banner) except (FileNotFoundError, discord.Forbidden): - _fmt = "\n".join("{}: {}".format(k, r) for k, r in fmt.items()) + _fmt = "\n".join("{}: {}".format(k, r) for k, r in fmt) await self.bot.say("```\n{}```".format(_fmt)) @osu.command(name='user', pass_context=True) diff --git a/cogs/overwatch.py b/cogs/overwatch.py index 432ecc2..45a383c 100644 --- a/cogs/overwatch.py +++ b/cogs/overwatch.py @@ -77,20 +77,18 @@ class Overwatch: return # Same list comprehension as before - output_data = {k.title().replace("_", " "): r for k, r in data['general_stats'].items() if - k in check_g_stats} + output_data = [(k.title().replace("_", " "), r) for k, r in data['general_stats'].items() if + k in check_g_stats] - for k, r in data['hero_stats'].items(): - output_data[k.title().replace("_", " ")] = r # Someone was complaining there was no KDR provided, so I made one myself and added that to the list - if data['general_stats'].get('eliminations') and data['general_stats'].get('deaths'): - output_data["Kill Death Ratio"] = "{0:.2f}".format( - data['general_stats'].get('eliminations') / data['general_stats'].get('deaths')) + #if data['general_stats'].get('eliminations') and data['general_stats'].get('deaths'): + #output_data["Kill Death Ratio"] = "{0:.2f}".format( + #data['general_stats'].get('eliminations') / data['general_stats'].get('deaths')) try: banner = await images.create_banner(user, "Overwatch", output_data) await self.bot.upload(banner) except (FileNotFoundError, discord.Forbidden): - fmt = "\n".join("{}: {}".format(k, r) for k, r in output_data.items()) + fmt = "\n".join("{}: {}".format(k, r) for k, r in output_data) await self.bot.say("Overwatch stats for {}: ```py\n{}```".format(user.name, fmt)) @ow.command(pass_context=True, name="add") diff --git a/cogs/stats.py b/cogs/stats.py index f7c9bcb..2454a00 100644 --- a/cogs/stats.py +++ b/cogs/stats.py @@ -55,10 +55,10 @@ class Stats: server_usage = command_stats['server_usage'].get(ctx.message.server.id, 0) try: - data = {"Command Name": cmd.qualified_name, - "Total Usage": total_usage, - "Your Usage": member_usage, - "This Server's Usage": server_usage} + data = [("Command Name", cmd.qualified_name), + ("Total Usage", total_usage), + ("Your Usage", member_usage), + ("This Server's Usage": server_usage)} banner = await images.create_banner(ctx.message.author, "Command Stats", data) await self.bot.upload(banner) except (FileNotFoundError, discord.Forbidden): @@ -90,6 +90,7 @@ class Stats: # I'm letting it use the length of the sorted_stats[:5] # As this can include, for example, all 3 if there are only 3 entries try: + top_5 = [(data[0], data[1]) for data in sorted_stats[:5]] top_5 = {data[0]: data[1] for data in sorted_stats[:5]} banner = await images.create_banner(ctx.message.author, "Your command usage", top_5) await self.bot.upload(banner) @@ -159,8 +160,8 @@ class Stats: sorted_booped_members = sorted_booped_members[:10] try: - output = {"{0.display_name}".format(ctx.message.server.get_member(m_id)): amt - for m_id, amt in sorted_booped_members} + output = [("{0.display_name}".format(ctx.message.server.get_member(m_id)), amt) + for m_id, amt in sorted_booped_members] banner = await images.create_banner(ctx.message.author, "Your booped victims", output) await self.bot.upload(banner) except (FileNotFoundError, discord.Forbidden): @@ -181,13 +182,13 @@ class Stats: # Sort the members based on their rating sorted_members = sorted(battles, key=lambda k: k['rating'], reverse=True) - output = {} + output = [] count = 1 for x in sorted_members: member_id = x['member_id'] rating = x['rating'] member = ctx.message.server.get_member(member_id) - output[count] = "{} (Rating: {})".format(member.display_name, rating) + output.append((count, "{} (Rating: {})".format(member.display_name, rating))) count += 1 if count >= 11: break @@ -196,7 +197,7 @@ class Stats: banner = await images.create_banner(ctx.message.author, "Battling Leaderboard", output) await self.bot.upload(banner) except (FileNotFoundError, discord.Forbidden): - fmt = "\n".join("#{}) {}".format(key, value) for key, value in output.items()) + fmt = "\n".join("#{}) {}".format(key, value) for key, value in output await self.bot.say("Battling leaderboard for this server:```\n{}```".format(fmt)) @commands.command(pass_context=True, no_pm=True) diff --git a/cogs/utils/images.py b/cogs/utils/images.py index aa1fa82..1c3d5a0 100644 --- a/cogs/utils/images.py +++ b/cogs/utils/images.py @@ -43,8 +43,9 @@ async def create_banner(member, image_title, data): # Parse the data we need to create our image username = (member.display_name[:23] + '...') if len(member.display_name) > 23 else member.display_name - result_keys = list(data.keys()) - result_values = list(data.values()) + # Our data will be a list of tuples, so this is how we can get the keys and values we want + result_keys = [k for k, v in data] + result_values = [v for k, v in data] lines_of_text = len(result_keys) output_file = "{}/banner_{}_{}.jpg".format(tmp_path, member.id, int(datetime.datetime.utcnow().timestamp())) base_height = canvas_height + (lines_of_text * 20)