Skip to content

Commit

Permalink
Merge pull request #31 from zeroquinc:platinum-total-time
Browse files Browse the repository at this point in the history
feature: support for total time for platinum
  • Loading branch information
zeroquinc authored May 12, 2024
2 parents 8a2992a + 7a2b37d commit 275c943
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/trophies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from psnawp_api import PSNAWP

from utils.image_utils import get_discord_color
from utils.trophy_utils import format_title
from utils.trophy_utils import format_title, calculate_total_time
from utils.date_utils import format_date
from config.config import PSNTOKEN, DISCORD_IMAGE, TROPHIES_INTERVAL

Expand Down Expand Up @@ -63,16 +63,16 @@ def create_trophy_embed(trophy, trophy_title_info, client, current, total_trophi
embed.set_author(name="A Trophy Unlocked", icon_url=trophy_title.title_icon_url)
return embed

def create_platinum_embed(trophy, trophy_title_info, client):
def create_platinum_embed(trophy, trophy_title_info, client, formatted_time_diff):
trophy_title = trophy_title_info['trophy_title']
game_url = format_title(trophy_title.title_name) # format the title name into a URL
platform = trophy_title_info['platform']
most_common_color = get_discord_color(trophy_title.title_icon_url)
embed = discord.Embed(description=f"**[{trophy_title.title_name}]({game_url}) ({platform})**\n\n {client.online_id} has achieved the Platinum trophy of the game! \n\n{trophy_title.title_name} has {trophy_title.defined_trophies['bronze']} Bronze, {trophy_title.defined_trophies['silver']} Silver, {trophy_title.defined_trophies['gold']} Gold, and {trophy_title.defined_trophies['platinum']} Platinum trophy.\n\n The Platinum has been achieved by {trophy.trophy_earn_rate}% of players", color=most_common_color)
embed = discord.Embed(description=f"**[{trophy_title.title_name}]({game_url}) ({platform})**\n\n Achieved in {formatted_time_diff} \n\n{trophy_title.title_name} has {trophy_title.defined_trophies['bronze']} Bronze, {trophy_title.defined_trophies['silver']} Silver, {trophy_title.defined_trophies['gold']} Gold, and {trophy_title.defined_trophies['platinum']} Platinum trophy\n\n The Platinum has been achieved by {trophy.trophy_earn_rate}% of players", color=most_common_color)
embed.add_field(name="Trophy", value=f"[{trophy.trophy_name}]({trophy.trophy_icon_url})", inline=True)
embed.set_image(url=DISCORD_IMAGE)
embed.set_thumbnail(url=trophy_title.title_icon_url)
embed.set_footer(text=f"{client.online_id}Earned on {format_date(trophy.earned_date_time)}", icon_url=client.profile_picture_url)
embed.set_footer(text=f"{client.online_id}Platinum achieved on {format_date(trophy.earned_date_time)}", icon_url=client.profile_picture_url)
embed.set_author(name="A New Platinum", icon_url=trophy_title.title_icon_url)
return embed

Expand Down Expand Up @@ -107,7 +107,15 @@ async def process_trophies_embeds(client, title_ids, TROPHIES_INTERVAL):
embed = create_trophy_embed(trophy, trophy_title, client, starting_count + i + 1, total_trophies)
trophy_embeds.append((trophy.earned_date_time, embed))
if trophy.trophy_type.name.lower() == 'platinum':
embed = create_platinum_embed(trophy, trophy_title, client)
# Get the oldest and newest trophy
oldest_trophy = earned_trophies[0]
newest_trophy = earned_trophies[-1]
# Calculate the time difference
time_diff = newest_trophy[0].earned_date_time - oldest_trophy[0].earned_date_time
# Format the time difference
formatted_time_diff = calculate_total_time(time_diff)
# Pass formatted_time_diff to create_platinum_embed function
embed = create_platinum_embed(trophy, trophy_title, client, formatted_time_diff)
platinum_embeds.append((trophy.earned_date_time, embed))
return trophy_embeds, len(recent_trophies), platinum_embeds

Expand Down
29 changes: 28 additions & 1 deletion utils/trophy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,31 @@ def format_title(title):
title = title.lower() # convert to lowercase
title = re.sub(r"[^\w\s]", '', title) # remove special characters except whitespace and underscores
title = title.replace(' ', '-') # replace spaces with hyphens
return f"https://www.playstation.com/games/{title}/" # prepend base URL
return f"https://www.playstation.com/games/{title}/" # prepend base URL

def calculate_total_time(td):
minutes, _ = divmod(td.seconds + td.days * 86400, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
weeks, days = divmod(days, 7)
years, weeks = divmod(weeks, 52)

result = []
if years:
result.append(f"{years} year{'s' if years > 1 else ''}")
if weeks:
result.append(f"{weeks} week{'s' if weeks > 1 else ''}")
if days:
result.append(f"{days} day{'s' if days > 1 else ''}")
if hours:
result.append(f"{hours} hour{'s' if hours > 1 else ''}")
if minutes:
result.append(f"{minutes} minute{'s' if minutes > 1 else ''}")

if len(result) > 1:
last = result.pop()
return ', '.join(result) + ' and ' + last
elif result:
return result[0]
else:
return ''

0 comments on commit 275c943

Please sign in to comment.