diff --git a/src/Gameboard.Api/Features/Game/GameController.cs b/src/Gameboard.Api/Features/Game/GameController.cs index 3218b74a..802a7d8c 100644 --- a/src/Gameboard.Api/Features/Game/GameController.cs +++ b/src/Gameboard.Api/Features/Game/GameController.cs @@ -240,13 +240,12 @@ public async Task> DeleteImage([FromRoute] string id, [Authorize(AppConstants.AdminPolicy)] public async Task Rerank([FromRoute] string id, CancellationToken cancellationToken) { - AuthorizeAny( - () => Actor.IsDesigner - ); + AuthorizeAny(() => Actor.IsDesigner); await Validate(new Entity { Id = id }); await GameService.ReRank(id); await _scoreDenormalization.DenormalizeGame(id, cancellationToken); + await _mediator.Publish(new GameCacheInvalidateCommand(id)); } } } diff --git a/src/Gameboard.Api/Features/Game/Notifications/GameCacheInvalidateNotification.cs b/src/Gameboard.Api/Features/Game/Notifications/GameCacheInvalidateNotification.cs new file mode 100644 index 00000000..f4b2986b --- /dev/null +++ b/src/Gameboard.Api/Features/Game/Notifications/GameCacheInvalidateNotification.cs @@ -0,0 +1,5 @@ +using MediatR; + +namespace Gameboard.Api.Features.Games; + +public record GameCacheInvalidateCommand(string GameId) : INotification; diff --git a/src/Gameboard.Api/Features/Hubs/GameHub/GameHubService.cs b/src/Gameboard.Api/Features/Hubs/GameHub/GameHubService.cs index 8e558a91..c2a7ba7a 100644 --- a/src/Gameboard.Api/Features/Hubs/GameHub/GameHubService.cs +++ b/src/Gameboard.Api/Features/Hubs/GameHub/GameHubService.cs @@ -13,7 +13,7 @@ namespace Gameboard.Api.Features.Games; -public interface IGameHubService : INotificationHandler, INotificationHandler +public interface IGameHubService : INotificationHandler, INotificationHandler, INotificationHandler { // invoke functions on clients Task SendExternalGameChallengesDeployStart(GameStartUpdate state); @@ -230,11 +230,14 @@ public async Task Handle(AppStartupNotification appStartupNotification, Cancella .ToArrayAsync(cancellationToken); foreach (var game in games) - await UpdateGameIdUserIdsMap(new GameEnrolledPlayersChangeNotification(new GameEnrolledPlayersChangeContext(game.Id, game.RequireSynchronizedStart))); + await UpdateGameIdUserIdsMap(game.Id); } + public Task Handle(GameCacheInvalidateCommand notification, CancellationToken cancellationToken) + => UpdateGameIdUserIdsMap(notification.GameId); + public Task Handle(GameEnrolledPlayersChangeNotification notification, CancellationToken cancellationToken) - => UpdateGameIdUserIdsMap(notification); + => UpdateGameIdUserIdsMap(notification.Context.GameId); private IEnumerable GetGameUserIds(string gameId) { @@ -245,21 +248,21 @@ private IEnumerable GetGameUserIds(string gameId) return userIds; } - private async Task UpdateGameIdUserIdsMap(GameEnrolledPlayersChangeNotification notification) + private async Task UpdateGameIdUserIdsMap(string gameId) { var gameUsers = await _store .WithNoTracking() .Where(p => p.Game.GameEnd != DateTimeOffset.MinValue || p.Game.GameEnd > _now.Get()) .Where(p => p.Game.PlayerMode == PlayerMode.Competition && p.Mode == PlayerMode.Competition) - .Where(p => p.GameId == notification.Context.GameId) + .Where(p => p.GameId == gameId) .Select(p => p.UserId) .Distinct() .ToArrayAsync(); lock (_gameIdUserIdsMap) { - _gameIdUserIdsMap.TryRemove(notification.Context.GameId, out var existingValue); - _gameIdUserIdsMap.TryAdd(notification.Context.GameId, gameUsers); + _gameIdUserIdsMap.TryRemove(gameId, out var existingValue); + _gameIdUserIdsMap.TryAdd(gameId, gameUsers); } } }