From 878d75f966e54961e32183a873e4776f4be369b6 Mon Sep 17 00:00:00 2001 From: JoKeRZH429 Date: Wed, 25 Mar 2026 22:43:09 +0500 Subject: [PATCH] feat(api): have matches api return matches from a timestamp onwards --- .../MatchUpdate/MatchUpdateController.cs | 8 ++++---- GenOnlineService/Database/Database.MatchHistory.cs | 13 +++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/GenOnlineService/Controllers/MatchUpdate/MatchUpdateController.cs b/GenOnlineService/Controllers/MatchUpdate/MatchUpdateController.cs index 8c946fb..2071a2c 100644 --- a/GenOnlineService/Controllers/MatchUpdate/MatchUpdateController.cs +++ b/GenOnlineService/Controllers/MatchUpdate/MatchUpdateController.cs @@ -123,9 +123,9 @@ public API_MatchHistoryController(IDbContextFactory dbFactory) _dbFactory = dbFactory; } - [HttpGet("{startingMatchID}")] + [HttpGet("{since}")] // TODO: Move to Authorize for this - public async Task GetHistorySince([FromHeader(Name = "X-Api-Key")] string apiKey, Int64 startingMatchID) + public async Task GetHistorySince([FromHeader(Name = "X-Api-Key")] string apiKey, DateTime since) { RouteHandler_Get_MatchHistory_Result result = new RouteHandler_Get_MatchHistory_Result(); @@ -141,10 +141,10 @@ public async Task GetHistorySince([FromHeader(Name = "X-Api-Key")] st return result; } - const Int64 maxLobbiesPerRequest = 99; // actually 100, but query is <= + const int maxLobbiesPerRequest = 500; await using var db = await _dbFactory.CreateDbContextAsync(); - result.matches = await Database.MatchHistory.GetMatchesInRange(db, startingMatchID, startingMatchID + maxLobbiesPerRequest); + result.matches = await Database.MatchHistory.GetMatchesSince(db, since, maxLobbiesPerRequest); return result; } diff --git a/GenOnlineService/Database/Database.MatchHistory.cs b/GenOnlineService/Database/Database.MatchHistory.cs index 26b8923..23ea958 100644 --- a/GenOnlineService/Database/Database.MatchHistory.cs +++ b/GenOnlineService/Database/Database.MatchHistory.cs @@ -659,16 +659,21 @@ await db.MatchHistory - public static async Task GetMatchesInRange( - AppDbContext db, long startID, long endID) + public static async Task GetMatchesSince( + AppDbContext db, DateTime since, int maxLobbiesPerRequest) { MatchHistoryCollection collection = new(); + since = DateTime.SpecifyKind(since, DateTimeKind.Utc); + try { // Single query fetches metadata + all slot columns — no concurrent reader issue. var rows = await db.MatchHistory - .Where(m => m.MatchId >= startID && m.MatchId <= endID && m.Finished) + .Where(m => m.Finished && m.TimeFinished >= since) + .OrderBy(m => m.TimeFinished) + .ThenBy(m => m.MatchId) + .Take(maxLobbiesPerRequest) .Select(m => new { m.MatchId, @@ -733,7 +738,7 @@ public static async Task GetMatchesInRange( } catch (Exception ex) { - Console.WriteLine($"[ERROR] GetMatchesInRange failed: {ex.Message}"); + Console.WriteLine($"[ERROR] GetMatchesSince failed: {ex.Message}"); SentrySdk.CaptureException(ex); }