Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.18.2 #419

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Gameboard.Api.Data.Abstractions;
using Gameboard.Api.Features.Games;
using Gameboard.Api.Features.Practice;
using Gameboard.Api.Features.Scores;
using Gameboard.Api.Features.Teams;
using Gameboard.Api.Services;
using MediatR;
Expand All @@ -29,6 +30,7 @@ public static PlayerService GetTestableSut
INowService? now = null,
IPlayerStore? playerStore = null,
IPracticeService? practiceService = null,
IScoringService? scoringService = null,
IStore? store = null,
ISyncStartGameService? syncStartGameService = null,
ITeamService? teamService = null
Expand All @@ -48,6 +50,7 @@ public static PlayerService GetTestableSut
now ?? A.Fake<INowService>(),
playerStore ?? A.Fake<IPlayerStore>(),
practiceService ?? A.Fake<IPracticeService>(),
scoringService ?? A.Fake<IScoringService>(),
store ?? A.Fake<IStore>(),
syncStartGameService ?? A.Fake<ISyncStartGameService>(),
teamService ?? A.Fake<ITeamService>()
Expand Down
5 changes: 2 additions & 3 deletions src/Gameboard.Api/Features/Game/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@ public async Task<ActionResult<UploadedFile>> 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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using MediatR;

namespace Gameboard.Api.Features.Games;

public record GameCacheInvalidateCommand(string GameId) : INotification;
17 changes: 10 additions & 7 deletions src/Gameboard.Api/Features/Hubs/GameHub/GameHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Gameboard.Api.Features.Games;

public interface IGameHubService : INotificationHandler<GameEnrolledPlayersChangeNotification>, INotificationHandler<AppStartupNotification>
public interface IGameHubService : INotificationHandler<GameEnrolledPlayersChangeNotification>, INotificationHandler<AppStartupNotification>, INotificationHandler<GameCacheInvalidateCommand>
{
// invoke functions on clients
Task SendExternalGameChallengesDeployStart(GameStartUpdate state);
Expand Down Expand Up @@ -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<string> GetGameUserIds(string gameId)
{
Expand All @@ -245,21 +248,21 @@ private IEnumerable<string> GetGameUserIds(string gameId)
return userIds;
}

private async Task UpdateGameIdUserIdsMap(GameEnrolledPlayersChangeNotification notification)
private async Task UpdateGameIdUserIdsMap(string gameId)
{
var gameUsers = await _store
.WithNoTracking<Data.Player>()
.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);
}
}
}
11 changes: 9 additions & 2 deletions src/Gameboard.Api/Features/Player/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Gameboard.Api.Features.Games;
using Gameboard.Api.Features.Player;
using Gameboard.Api.Features.Practice;
using Gameboard.Api.Features.Scores;
using Gameboard.Api.Features.Sponsors;
using Gameboard.Api.Features.Teams;
using MediatR;
Expand All @@ -32,6 +33,7 @@ public class PlayerService
private readonly IMediator _mediator;
private readonly INowService _now;
private readonly IPracticeService _practiceService;
private readonly IScoringService _scores;
private readonly IStore _store;
private readonly ISyncStartGameService _syncStartGameService;
private readonly ITeamService _teamService;
Expand All @@ -56,6 +58,7 @@ public PlayerService(
INowService now,
IPlayerStore playerStore,
IPracticeService practiceService,
IScoringService scores,
IStore store,
ISyncStartGameService syncStartGameService,
ITeamService teamService
Expand All @@ -75,6 +78,7 @@ ITeamService teamService
LocalCache = memCache;
_mapper = mapper;
PlayerStore = playerStore;
_scores = scores;
_store = store;
_syncStartGameService = syncStartGameService;
_teamService = teamService;
Expand Down Expand Up @@ -679,10 +683,13 @@ public async Task AdvanceTeams(TeamAdvancement model)
foreach (var team in teams)
{
string newId = _guids.GetGuid();
// compute complete score, including bonuses
var teamScore = await _scores.GetTeamScore(team.Key, CancellationToken.None);

foreach (var player in team)
{
player.Advanced = true;

var newPlayer = new Data.Player
{
TeamId = newId,
Expand All @@ -691,12 +698,12 @@ public async Task AdvanceTeams(TeamAdvancement model)
AdvancedFromGameId = player.GameId,
AdvancedFromPlayerId = player.Id,
AdvancedFromTeamId = player.TeamId,
AdvancedWithScore = model.WithScores ? player.Score : null,
AdvancedWithScore = model.WithScores ? teamScore.OverallScore.TotalScore : null,
ApprovedName = player.ApprovedName,
Name = player.Name,
SponsorId = player.SponsorId,
Role = player.Role,
Score = model.WithScores ? player.Score : 0
Score = model.WithScores ? (int)Math.Floor(teamScore.OverallScore.TotalScore) : 0
};

enrollments.Add(newPlayer);
Expand Down
Loading