Skip to content

Commit

Permalink
update osu api service to process lazer score ids instead
Browse files Browse the repository at this point in the history
  • Loading branch information
minisbett committed Jul 27, 2024
1 parent 683f358 commit 3ea33f9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion huisbot/Modules/Alias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task HandleAddAsync(
}

// Get the score.
OsuScore? score = await GetScoreAsync(0 /* TODO: Support for other rulsets */, scoreId.ToString());
OsuScore? score = await GetScoreAsync(scoreId.ToString());
if (score is null)
return;

Expand Down
2 changes: 1 addition & 1 deletion huisbot/Modules/Huis/Simulate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task HandleAsync(
// If a score was specified, get the score and fill the unset parameters with it's attributes.
if (scoreId is not null)
{
OsuScore? score = await GetScoreAsync(rework.RulesetId, scoreId);
OsuScore? score = await GetScoreAsync(scoreId);
if (score is null)
return;

Expand Down
4 changes: 2 additions & 2 deletions huisbot/Modules/ModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public async Task<bool> QueuePlayerAsync(OsuUser player, int reworkId, ulong dis
/// </summary>
/// <param name="scoreId">An identifier for the score. (Score ID or alias)</param>
/// <returns>The score.</returns>
public async Task<OsuScore?> GetScoreAsync(int rulesetId, string scoreId)
public async Task<OsuScore?> GetScoreAsync(string scoreId)
{
// If the identifier is not a number, try to find a score alias.
if (!scoreId.All(char.IsDigit))
Expand All @@ -315,7 +315,7 @@ public async Task<bool> QueuePlayerAsync(OsuUser player, int reworkId, ulong dis
scoreId = alias.ScoreId.ToString();
}
// Get the score from the osu! API. If it failed or the score was not found, notify the user.
NotFoundOr<OsuScore>? score = await _osu.GetScoreAsync(rulesetId, long.Parse(scoreId));
NotFoundOr<OsuScore>? score = await _osu.GetScoreAsync(long.Parse(scoreId));
if (score is null)
await ModifyOriginalResponseAsync(x => x.Embed = Embeds.InternalError("Failed to get the score from the osu! API."));
else if (!score.Found)
Expand Down
20 changes: 5 additions & 15 deletions huisbot/Services/OsuApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,20 @@ public async Task<bool> EnsureAccessTokenAsync()
}

/// <summary>
/// Returns the score with the specified ID in the specified ruleset.
/// Returns the score with the specified ID.
/// </summary>
/// <param name="rulesetId">The ruleset ID.</param>
/// <param name="scoreId">The score ID.</param>
/// <returns>The score with the specified ID./returns>
public async Task<NotFoundOr<OsuScore>?> GetScoreAsync(int rulesetId, long scoreId)
public async Task<NotFoundOr<OsuScore>?> GetScoreAsync(long scoreId)
{
// Make sure a valid access token exists. If not, return null.
if (!await EnsureAccessTokenAsync())
return null;

// Get the string version of the ruleset ID.
string ruleset = rulesetId switch
{
1 => "taiko",
2 => "fruits",
3 => "mania",
_ => "osu"
};

try
{
// Get the score from the API and check whether a 404 was returned. If so, the score was not found.
HttpResponseMessage response = await _http.GetAsync($"api/v2/scores/{ruleset}/{scoreId}");
HttpResponseMessage response = await _http.GetAsync($"api/v2/scores/{scoreId}");
if (response.StatusCode == HttpStatusCode.NotFound)
return NotFoundOr<OsuScore>.NotFound;

Expand All @@ -231,8 +221,8 @@ public async Task<bool> EnsureAccessTokenAsync()
}
catch (Exception ex)
{
_logger.LogError("Failed to get the score with ID {Id} in ruleset {Ruleset} from the osu! API: {Message}",
scoreId, rulesetId, ex.Message);
_logger.LogError("Failed to get the score with ID {Id} from the osu! API: {Message}",
scoreId, ex.Message);
return null;
}
}
Expand Down

0 comments on commit 3ea33f9

Please sign in to comment.