Skip to content

Commit

Permalink
Make utility classes non-async and add more tests for LiveRankingService
Browse files Browse the repository at this point in the history
  • Loading branch information
araszka committed Aug 10, 2024
1 parent 9deba0f commit d6ffc45
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/Modules/LiveRankingModule/Interfaces/ILiveRankingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ public interface ILiveRankingService
public Task HideWidgetAsync();

/// <summary>
/// Determines whether a score should be displayed in the widget.
/// Returns whether the current mode is points based.
/// </summary>
/// <param name="score"></param>
/// <returns></returns>
public Task<bool> ScoreShouldBeDisplayedAsync(PlayerScore score);
public Task<bool> CurrentModeIsPointsBasedAsync();

/// <summary>
/// Returns whether the current mode is points based.
/// Determines whether a score should be displayed in the widget.
/// </summary>
/// <param name="score"></param>
/// <returns></returns>
public Task<bool> CurrentModeIsPointsBasedAsync();
public bool ScoreShouldBeDisplayed(PlayerScore score);

/// <summary>
/// Converts a PlayerScore to a LiveRankingPosition object.
/// </summary>
/// <param name="score"></param>
/// <returns></returns>
public Task<LiveRankingPosition> PlayerScoreToLiveRankingPositionAsync(PlayerScore score);
public LiveRankingPosition PlayerScoreToLiveRankingPosition(PlayerScore score);
}
21 changes: 14 additions & 7 deletions src/Modules/LiveRankingModule/Services/LiveRankingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,30 @@ public Task<IEnumerable<LiveRankingPosition>> MapScoresAsync(ScoresEventArgs sco
scores.Players.Take(settings.MaxWidgetRows)
.Where(score => score != null)
.OfType<PlayerScore>()
.Where(score => ScoreShouldBeDisplayedAsync(score).Result)
.Select(score => PlayerScoreToLiveRankingPositionAsync(score).Result)
.Where(ScoreShouldBeDisplayed)
.Select(PlayerScoreToLiveRankingPosition)
);
}

public Task HideWidgetAsync()
=> manialinkManager.HideManialinkAsync(WidgetTemplate);

public Task<bool> ScoreShouldBeDisplayedAsync(PlayerScore score) =>
Task.FromResult((_isPointsBased ? score.MatchPoints : score.BestRaceTime) > 0);

public Task<bool> CurrentModeIsPointsBasedAsync()
=> Task.FromResult(_isPointsBased);

public async Task<LiveRankingPosition> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit d6ffc45

Please sign in to comment.