Skip to content

Commit

Permalink
Add rank times to history API
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStolk committed Mar 3, 2024
1 parent c7088a0 commit 1593ffd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public record GetPlayerHistory
public required List<GetPlayerHistoryActivityEntry> ActivityHistory { get; init; }

public required List<GetPlayerHistoryRankEntry> RankHistory { get; init; }

public required Dictionary<int, TimeSpan> RankTimes { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public record PlayerHistory
public required List<PlayerHistoryActivityEntry> ActivityHistory { get; init; }

public required List<PlayerHistoryRankEntry> RankHistory { get; init; }

public required Dictionary<int, TimeSpan> RankTimes { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ public PlayerHistory GetPlayerHistoryById(int id)
DateTime? datePreviousForActivityHistory = null;
List<PlayerHistoryActivityEntry> activityHistory = new();

Dictionary<int, TimeSpan> rankTimes = new();
DateTime? datePrevious = null;

foreach (string leaderboardHistoryPath in _fileSystemService.TryGetFiles(DataSubDirectory.LeaderboardHistory).Where(p => p.EndsWith(".bin")))
{
LeaderboardHistory leaderboard = _leaderboardHistoryCache.GetLeaderboardHistoryByFilePath(leaderboardHistoryPath);
EntryHistory? entry = leaderboard.Entries.Find(e => e.Id == id);
if (entry == null)
{
datePrevious = leaderboard.DateTime;
continue;
}

int illegitimateScoresAbove = leaderboard.Entries.Count(e => e.Rank < entry.Rank && bannedPlayerIds.Contains(e.Id));
int correctedRank = entry.Rank - illegitimateScoresAbove;
Expand Down Expand Up @@ -103,6 +109,15 @@ public PlayerHistory GetPlayerHistoryById(int id)
rankPreviousForRankHistory = correctedRank;
}

if (datePrevious.HasValue)
{
// TODO: The TODO at the top of this method needs to be resolved before this can be accurate.
if (rankTimes.ContainsKey(correctedRank))
rankTimes[correctedRank] += leaderboard.DateTime - datePrevious.Value;
else
rankTimes.Add(correctedRank, leaderboard.DateTime - datePrevious.Value);
}

if (entry.DeathsTotal > 0)
{
TimeSpan? timeSpan = datePreviousForActivityHistory == null ? null : leaderboard.DateTime - datePreviousForActivityHistory.Value;
Expand All @@ -118,6 +133,8 @@ public PlayerHistory GetPlayerHistoryById(int id)
totalTimeForActivityHistory = entry.TimeTotal;
datePreviousForActivityHistory = leaderboard.DateTime;
}

datePrevious = leaderboard.DateTime;
}

return new()
Expand All @@ -128,6 +145,7 @@ public PlayerHistory GetPlayerHistoryById(int id)
RankHistory = rankHistory,
ScoreHistory = scoreHistory,
Usernames = usernamesHistory.OrderByDescending(kvp => kvp.Value).Select(kvp => kvp.Key).ToList(),
RankTimes = rankTimes,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ public static class PlayerConverters
DeathType = sh.DeathType,
}),
HidePastUsernames = playerHistory.HidePastUsernames,
RankTimes = playerHistory.RankTimes,
};
}

0 comments on commit 1593ffd

Please sign in to comment.