Changed image creation to use tuples, to allow order preservation
This commit is contained in:
parent
5e1976dcb8
commit
79e9e66d95
4 changed files with 21 additions and 22 deletions
|
@ -55,7 +55,6 @@ class Osu:
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@commands.group(pass_context=True, invoke_without_command=True)
|
@commands.group(pass_context=True, invoke_without_command=True)
|
||||||
@checks.custom_perms(send_messages=True)
|
@checks.custom_perms(send_messages=True)
|
||||||
async def osu(self, ctx):
|
async def osu(self, ctx):
|
||||||
|
@ -122,7 +121,7 @@ class Osu:
|
||||||
# If the key is in our wanted_info list
|
# 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 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
|
# 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
|
# 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
|
# 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)
|
banner = await images.create_banner(ctx.message.author, "Osu User Stats", fmt)
|
||||||
await self.bot.upload(banner)
|
await self.bot.upload(banner)
|
||||||
except (FileNotFoundError, discord.Forbidden):
|
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))
|
await self.bot.say("```\n{}```".format(_fmt))
|
||||||
|
|
||||||
@osu.command(name='user', pass_context=True)
|
@osu.command(name='user', pass_context=True)
|
||||||
|
|
|
@ -77,20 +77,18 @@ class Overwatch:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Same list comprehension as before
|
# Same list comprehension as before
|
||||||
output_data = {k.title().replace("_", " "): r for k, r in data['general_stats'].items() if
|
output_data = [(k.title().replace("_", " "), r) for k, r in data['general_stats'].items() if
|
||||||
k in check_g_stats}
|
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
|
# 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'):
|
#if data['general_stats'].get('eliminations') and data['general_stats'].get('deaths'):
|
||||||
output_data["Kill Death Ratio"] = "{0:.2f}".format(
|
#output_data["Kill Death Ratio"] = "{0:.2f}".format(
|
||||||
data['general_stats'].get('eliminations') / data['general_stats'].get('deaths'))
|
#data['general_stats'].get('eliminations') / data['general_stats'].get('deaths'))
|
||||||
try:
|
try:
|
||||||
banner = await images.create_banner(user, "Overwatch", output_data)
|
banner = await images.create_banner(user, "Overwatch", output_data)
|
||||||
await self.bot.upload(banner)
|
await self.bot.upload(banner)
|
||||||
except (FileNotFoundError, discord.Forbidden):
|
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))
|
await self.bot.say("Overwatch stats for {}: ```py\n{}```".format(user.name, fmt))
|
||||||
|
|
||||||
@ow.command(pass_context=True, name="add")
|
@ow.command(pass_context=True, name="add")
|
||||||
|
|
|
@ -55,10 +55,10 @@ class Stats:
|
||||||
server_usage = command_stats['server_usage'].get(ctx.message.server.id, 0)
|
server_usage = command_stats['server_usage'].get(ctx.message.server.id, 0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = {"Command Name": cmd.qualified_name,
|
data = [("Command Name", cmd.qualified_name),
|
||||||
"Total Usage": total_usage,
|
("Total Usage", total_usage),
|
||||||
"Your Usage": member_usage,
|
("Your Usage", member_usage),
|
||||||
"This Server's Usage": server_usage}
|
("This Server's Usage": server_usage)}
|
||||||
banner = await images.create_banner(ctx.message.author, "Command Stats", data)
|
banner = await images.create_banner(ctx.message.author, "Command Stats", data)
|
||||||
await self.bot.upload(banner)
|
await self.bot.upload(banner)
|
||||||
except (FileNotFoundError, discord.Forbidden):
|
except (FileNotFoundError, discord.Forbidden):
|
||||||
|
@ -90,6 +90,7 @@ class Stats:
|
||||||
# I'm letting it use the length of the sorted_stats[:5]
|
# 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
|
# As this can include, for example, all 3 if there are only 3 entries
|
||||||
try:
|
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]}
|
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)
|
banner = await images.create_banner(ctx.message.author, "Your command usage", top_5)
|
||||||
await self.bot.upload(banner)
|
await self.bot.upload(banner)
|
||||||
|
@ -159,8 +160,8 @@ class Stats:
|
||||||
sorted_booped_members = sorted_booped_members[:10]
|
sorted_booped_members = sorted_booped_members[:10]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
output = {"{0.display_name}".format(ctx.message.server.get_member(m_id)): amt
|
output = [("{0.display_name}".format(ctx.message.server.get_member(m_id)), amt)
|
||||||
for m_id, amt in sorted_booped_members}
|
for m_id, amt in sorted_booped_members]
|
||||||
banner = await images.create_banner(ctx.message.author, "Your booped victims", output)
|
banner = await images.create_banner(ctx.message.author, "Your booped victims", output)
|
||||||
await self.bot.upload(banner)
|
await self.bot.upload(banner)
|
||||||
except (FileNotFoundError, discord.Forbidden):
|
except (FileNotFoundError, discord.Forbidden):
|
||||||
|
@ -181,13 +182,13 @@ class Stats:
|
||||||
# Sort the members based on their rating
|
# Sort the members based on their rating
|
||||||
sorted_members = sorted(battles, key=lambda k: k['rating'], reverse=True)
|
sorted_members = sorted(battles, key=lambda k: k['rating'], reverse=True)
|
||||||
|
|
||||||
output = {}
|
output = []
|
||||||
count = 1
|
count = 1
|
||||||
for x in sorted_members:
|
for x in sorted_members:
|
||||||
member_id = x['member_id']
|
member_id = x['member_id']
|
||||||
rating = x['rating']
|
rating = x['rating']
|
||||||
member = ctx.message.server.get_member(member_id)
|
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
|
count += 1
|
||||||
if count >= 11:
|
if count >= 11:
|
||||||
break
|
break
|
||||||
|
@ -196,7 +197,7 @@ class Stats:
|
||||||
banner = await images.create_banner(ctx.message.author, "Battling Leaderboard", output)
|
banner = await images.create_banner(ctx.message.author, "Battling Leaderboard", output)
|
||||||
await self.bot.upload(banner)
|
await self.bot.upload(banner)
|
||||||
except (FileNotFoundError, discord.Forbidden):
|
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))
|
await self.bot.say("Battling leaderboard for this server:```\n{}```".format(fmt))
|
||||||
|
|
||||||
@commands.command(pass_context=True, no_pm=True)
|
@commands.command(pass_context=True, no_pm=True)
|
||||||
|
|
|
@ -43,8 +43,9 @@ async def create_banner(member, image_title, data):
|
||||||
|
|
||||||
# Parse the data we need to create our image
|
# Parse the data we need to create our image
|
||||||
username = (member.display_name[:23] + '...') if len(member.display_name) > 23 else member.display_name
|
username = (member.display_name[:23] + '...') if len(member.display_name) > 23 else member.display_name
|
||||||
result_keys = list(data.keys())
|
# Our data will be a list of tuples, so this is how we can get the keys and values we want
|
||||||
result_values = list(data.values())
|
result_keys = [k for k, v in data]
|
||||||
|
result_values = [v for k, v in data]
|
||||||
lines_of_text = len(result_keys)
|
lines_of_text = len(result_keys)
|
||||||
output_file = "{}/banner_{}_{}.jpg".format(tmp_path, member.id, int(datetime.datetime.utcnow().timestamp()))
|
output_file = "{}/banner_{}_{}.jpg".format(tmp_path, member.id, int(datetime.datetime.utcnow().timestamp()))
|
||||||
base_height = canvas_height + (lines_of_text * 20)
|
base_height = canvas_height + (lines_of_text * 20)
|
||||||
|
|
Loading…
Reference in a new issue