diff --git a/src/Modules/LiveRankingModule/Interfaces/ILiveRankingService.cs b/src/Modules/LiveRankingModule/Interfaces/ILiveRankingService.cs index e94909d85..c5615e3ad 100644 --- a/src/Modules/LiveRankingModule/Interfaces/ILiveRankingService.cs +++ b/src/Modules/LiveRankingModule/Interfaces/ILiveRankingService.cs @@ -39,22 +39,22 @@ public interface ILiveRankingService public Task HideWidgetAsync(); /// - /// Determines whether a score should be displayed in the widget. + /// Returns whether the current mode is points based. /// - /// /// - public Task ScoreShouldBeDisplayedAsync(PlayerScore score); + public Task CurrentModeIsPointsBasedAsync(); /// - /// Returns whether the current mode is points based. + /// Determines whether a score should be displayed in the widget. /// + /// /// - public Task CurrentModeIsPointsBasedAsync(); + public bool ScoreShouldBeDisplayed(PlayerScore score); /// /// Converts a PlayerScore to a LiveRankingPosition object. /// /// /// - public Task PlayerScoreToLiveRankingPositionAsync(PlayerScore score); + public LiveRankingPosition PlayerScoreToLiveRankingPosition(PlayerScore score); } diff --git a/src/Modules/LiveRankingModule/Services/LiveRankingService.cs b/src/Modules/LiveRankingModule/Services/LiveRankingService.cs index 442261477..131063c7d 100644 --- a/src/Modules/LiveRankingModule/Services/LiveRankingService.cs +++ b/src/Modules/LiveRankingModule/Services/LiveRankingService.cs @@ -45,23 +45,30 @@ public Task> MapScoresAsync(ScoresEventArgs sco scores.Players.Take(settings.MaxWidgetRows) .Where(score => score != null) .OfType() - .Where(score => ScoreShouldBeDisplayedAsync(score).Result) - .Select(score => PlayerScoreToLiveRankingPositionAsync(score).Result) + .Where(ScoreShouldBeDisplayed) + .Select(PlayerScoreToLiveRankingPosition) ); } public Task HideWidgetAsync() => manialinkManager.HideManialinkAsync(WidgetTemplate); - public Task ScoreShouldBeDisplayedAsync(PlayerScore score) => - Task.FromResult((_isPointsBased ? score.MatchPoints : score.BestRaceTime) > 0); - public Task CurrentModeIsPointsBasedAsync() => Task.FromResult(_isPointsBased); - public async Task PlayerScoreToLiveRankingPositionAsync(PlayerScore score) + public bool ScoreShouldBeDisplayed(PlayerScore score) + { + if (_isPointsBased) + { + return score.MatchPoints > 0; + } + + return score.BestRaceTime > 0; + } + + public LiveRankingPosition PlayerScoreToLiveRankingPosition(PlayerScore score) { - var player = await playerManager.GetPlayerAsync(score.AccountId); + var player = playerManager.GetPlayerAsync(score.AccountId).Result; var nickname = score.Name; if (player != null) diff --git a/tests/Modules/LiveRankingModule.Tests/Services/LiveRankingServiceTests.cs b/tests/Modules/LiveRankingModule.Tests/Services/LiveRankingServiceTests.cs index 3158f3f01..105177865 100644 --- a/tests/Modules/LiveRankingModule.Tests/Services/LiveRankingServiceTests.cs +++ b/tests/Modules/LiveRankingModule.Tests/Services/LiveRankingServiceTests.cs @@ -113,11 +113,11 @@ public async Task Maps_Scores_To_LiveRankingPositions(int bestRaceTime, int matc if (expectedLiveRankingPositionCount > 0) { - Assert.True(await liveRankingService.ScoreShouldBeDisplayedAsync(scoresEventArgs.Players.First())); + Assert.True(liveRankingService.ScoreShouldBeDisplayed(scoresEventArgs.Players.First())); } else { - Assert.False(await liveRankingService.ScoreShouldBeDisplayedAsync(scoresEventArgs.Players.First())); + Assert.False(liveRankingService.ScoreShouldBeDisplayed(scoresEventArgs.Players.First())); } var mappedScores = await liveRankingService.MapScoresAsync(scoresEventArgs); @@ -146,4 +146,85 @@ public async Task Maps_Scores_And_Displays_Widget() Times.Once ); } + + [Fact] + public Task Maps_Player_Score_To_Live_Ranking_Position() + { + var liveRankingService = LiveRankingServiceMock(); + var player = new Player { AccountId = "UnitTest", NickName = "unit_test", UbisoftName = "unittest"}; + + _playerManagerService.Setup(s => s.GetPlayerAsync(player.AccountId)) + .Returns(Task.FromResult((IPlayer?)player)); + + var liveRankingPosition = liveRankingService.PlayerScoreToLiveRankingPosition(new() + { + AccountId = player.AccountId, + Name = player.UbisoftName, + BestRaceTime = 1234, + MatchPoints = 3, + Rank = 7 + }); + + Assert.Equal("UnitTest", liveRankingPosition.AccountId); + Assert.Equal("unit_test", liveRankingPosition.Name); + Assert.Equal(1234, liveRankingPosition.Time); + Assert.Equal(3, liveRankingPosition.Points); + Assert.Equal(7, liveRankingPosition.Position); + + return Task.CompletedTask; + } + + [Fact] + public Task Maps_Player_Score_To_Live_Ranking_Position_Where_Player_Is_Unknown() + { + var liveRankingService = LiveRankingServiceMock(); + var player = new Player { AccountId = "UnitTest", NickName = "unit_test", UbisoftName = "unittest"}; + + _playerManagerService.Setup(s => s.GetPlayerAsync(player.AccountId)) + .Returns(Task.FromResult((IPlayer?)null)); + + var liveRankingPosition = liveRankingService.PlayerScoreToLiveRankingPosition(new() + { + AccountId = player.AccountId, + Name = player.UbisoftName, + BestRaceTime = 1234, + MatchPoints = 3, + Rank = 7 + }); + + Assert.Equal("UnitTest", liveRankingPosition.AccountId); + Assert.Equal("unittest", liveRankingPosition.Name); + Assert.Equal(1234, liveRankingPosition.Time); + Assert.Equal(3, liveRankingPosition.Points); + Assert.Equal(7, liveRankingPosition.Position); + + return Task.CompletedTask; + } + + [Theory] + [InlineData(DefaultModeScriptName.Rounds, 0, 0, false)] + [InlineData(DefaultModeScriptName.Rounds, 0, 1, true)] + [InlineData(DefaultModeScriptName.Rounds, 1, 0, false)] + [InlineData(DefaultModeScriptName.TimeAttack, 0, 0, false)] + [InlineData(DefaultModeScriptName.TimeAttack, 0, 1, false)] + [InlineData(DefaultModeScriptName.TimeAttack, 1, 0, true)] + public Task Determines_Whether_Score_Should_Be_Send_To_Widget(DefaultModeScriptName modeScriptName, int time, int points, bool expected) + { + var liveRankingService = LiveRankingServiceMock(); + + _matchSettingsService.Setup(s => s.GetCurrentModeAsync()) + .Returns(Task.FromResult(modeScriptName)); + + liveRankingService.DetectModeAndRequestScoreAsync(); + + var shouldBeDisplayed = liveRankingService.ScoreShouldBeDisplayed(new PlayerScore + { + BestRaceTime = time, + MatchPoints = points + }); + + Assert.Equal(expected, shouldBeDisplayed); + + return Task.CompletedTask; + } }