Skip to content

Commit

Permalink
Return the total score count along with position
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Jun 7, 2024
1 parent 89d3526 commit 862c7c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
16 changes: 11 additions & 5 deletions GlobalRankLookupCache/Controllers/BeatmapRankCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public BeatmapRankCache(int beatmapId, string highScoresTable)

private readonly ManualResetEventSlim populated = new ManualResetEventSlim();

public async Task<int> Lookup(int score)
public async Task<(int position, int total)> Lookup(int score)
{
if (!waitForPopulation())
{
Expand All @@ -35,9 +35,15 @@ public async Task<int> Lookup(int score)
{
cmd.CommandTimeout = 10;
cmd.CommandText = $"select count(*) from {highScoresTable} where beatmap_id = {beatmapId} and score > {score} and hidden = 0";
int count = (int)(long)await cmd.ExecuteScalarAsync();
Console.WriteLine($"quick lookup for {beatmapId} = {count}");
return count;
int pos = (int)(long)(await cmd.ExecuteScalarAsync())!;

cmd.CommandTimeout = 10;
cmd.CommandText = $"select count(*) from {highScoresTable} where beatmap_id = {beatmapId} and hidden = 0";
int total = (int)(long)(await cmd.ExecuteScalarAsync())!;

Console.WriteLine($"quick lookup for {beatmapId} = {pos}/{total}");

return (pos, total);
}
}

Expand All @@ -49,7 +55,7 @@ public async Task<int> Lookup(int score)
queuePopulation();

int result = scores.BinarySearch(score + 1);
return scores.Count - (result < 0 ? ~result : result);
return (scores.Count - (result < 0 ? ~result : result), scores.Count);
}

private bool waitForPopulation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void Update(int beatmapId, in int oldScore, in int newScore)

public bool Clear(in int beatmapId) => beatmapScoresLookup.TryRemove(beatmapId, out var _);

public Task<int> Lookup(int beatmapId, in int score)
public Task<(int position, int total)> Lookup(int beatmapId, in int score)
{
return beatmapScoresLookup.GetOrAdd(beatmapId, new BeatmapRankCache(beatmapId, highScoresTable)).Lookup(score);
}
}
}
}
5 changes: 3 additions & 2 deletions GlobalRankLookupCache/Controllers/RankLookupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class RankLookupController : ControllerBase
};

[HttpGet]
public Task<int> Get(int rulesetId, int beatmapId, int score)
public async Task<IActionResult> Get(int rulesetId, int beatmapId, int score)
{
return beatmap_rank_cache[rulesetId].Lookup(beatmapId, score);
(int position, int total) = await beatmap_rank_cache[rulesetId].Lookup(beatmapId, score);
return Content($"{position},{total}");
}
}
}

0 comments on commit 862c7c0

Please sign in to comment.