Skip to content

Commit 02f259a

Browse files
authored
Merge pull request #24 from zeroquinc:trophies-count-fix
fix: fix set completion count on trophy embeds
2 parents b0b488e + 7c3f1bf commit 02f259a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/trophies.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ def get_recent_titles(client, minutes=TROPHIES_INTERVAL):
2525
logger.info("Calling Sony API to get recently played games")
2626
now = get_current_time()
2727
titles = list(client.title_stats())
28-
title_ids = [title.title_id for title in titles if title.last_played_date_time > now - timedelta(minutes=minutes)]
28+
title_ids = [(title.title_id, 'PS5' if 'ps5' in title.category.value else 'PS4') for title in titles if title.last_played_date_time > now - timedelta(minutes=minutes)]
29+
logger.debug(f"API response: {titles}")
2930
for title in titles:
30-
if title.title_id in title_ids:
31+
if title.title_id in [id_ for id_, platform in title_ids]:
3132
logger.debug(f"Found a recently played game: {title.name}")
3233
return title_ids
3334

3435
def get_earned_trophies(client, title_ids):
3536
logger.debug("Calling Sony API to get earned trophies.")
3637
trophies = []
37-
for trophy_title in client.trophy_titles_for_title(title_ids=title_ids):
38-
earned_trophies = client.trophies(np_communication_id=trophy_title.np_communication_id, platform='all', trophy_group_id='all', include_metadata=True)
39-
# Add each trophy and its title to the list
40-
trophies.extend((trophy, trophy_title) for trophy in earned_trophies)
38+
for title_id, platform in title_ids:
39+
for trophy_title in client.trophy_titles_for_title(title_ids=[title_id]):
40+
try:
41+
earned_trophies = client.trophies(np_communication_id=trophy_title.np_communication_id, platform=platform, trophy_group_id='all', include_metadata=True)
42+
# Add each trophy and its title to the list
43+
trophies.extend((trophy, trophy_title) for trophy in earned_trophies)
44+
except Exception as e:
45+
logger.error(f"Failed to get trophies for platform {platform}: {e}")
4146
return trophies
4247

4348
def create_trophy_embed(trophy, trophy_title, client, current, total_trophies):
@@ -59,6 +64,7 @@ async def process_trophies_embeds(client, title_ids, TROPHIES_INTERVAL):
5964
all_trophies = get_earned_trophies(client, title_ids)
6065
# Filter out trophies with None earned date
6166
earned_trophies = [t for t in all_trophies if t[0].earned_date_time is not None]
67+
logger.debug(f"Found {len(earned_trophies)} earned trophies.")
6268
# Sort earned trophies by earned date
6369
earned_trophies.sort(key=lambda x: x[0].earned_date_time)
6470
# Calculate total trophies of the game (before filtering for earned_date_time)
@@ -67,14 +73,16 @@ async def process_trophies_embeds(client, title_ids, TROPHIES_INTERVAL):
6773
now = get_current_time()
6874
cutoff = now - timedelta(minutes=TROPHIES_INTERVAL)
6975
# Filter out trophies that were earned before the cutoff time
70-
earned_trophies = [t for t in earned_trophies if t[0].earned_date_time >= cutoff]
76+
recent_trophies = [t for t in earned_trophies if t[0].earned_date_time >= cutoff]
7177
# Calculate total trophies earned (after filtering)
7278
total_trophies_earned = len(earned_trophies)
73-
for i, (trophy, trophy_title) in enumerate(earned_trophies):
79+
# Calculate the starting count
80+
starting_count = total_trophies_earned - len(recent_trophies)
81+
for i, (trophy, trophy_title) in enumerate(recent_trophies):
7482
# Pass total_trophies to create_trophy_embed function
75-
embed = create_trophy_embed(trophy, trophy_title, client, i + 1, total_trophies)
83+
embed = create_trophy_embed(trophy, trophy_title, client, starting_count + i + 1, total_trophies)
7684
trophy_embeds.append((trophy.earned_date_time, embed))
77-
return trophy_embeds, total_trophies_earned
85+
return trophy_embeds, len(recent_trophies)
7886

7987
async def process_trophies(trophies_channel):
8088
client = get_client()

0 commit comments

Comments
 (0)