Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
snixtho committed Jun 30, 2024
1 parent e22f0d0 commit 1ceb147
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,72 @@ namespace EvoSC.Modules.Official.LocalRecordsModule.Interfaces.Database;

public interface ILocalRecordRepository
{
/// <summary>
/// Get all local records from a map by its ID.
/// </summary>
/// <param name="mapId">ID of the map to get local records from.</param>
/// <returns></returns>
public Task<IEnumerable<DbLocalRecord>> GetLocalRecordsOfMapByIdAsync(long mapId);

/// <summary>
/// Add a new local record, or update it if its better than existing one.
/// </summary>
/// <param name="map">Map to add local record to.</param>
/// <param name="record">The record to add.</param>
/// <returns></returns>
public Task<DbLocalRecord?> AddOrUpdateRecordAsync(IMap map, IPlayerRecord record);

/// <summary>
/// Add multiple local records to a map.
/// </summary>
/// <param name="map">Map to add local records to.</param>
/// <param name="records">Records to add.</param>
/// <returns></returns>
public Task AddRecordsAsync(IMap map, IEnumerable<IPlayerRecord> records);

/// <summary>
/// Recalculate positions of all local records in a map. It will
/// also remove local records that does not fit within the configured
/// top N local records.
/// </summary>
/// <param name="map">Map to re-calculate local records for.</param>
/// <returns></returns>
public Task<DbLocalRecord[]> RecalculatePositionsOfMapAsync(IMap map);

/// <summary>
/// Get local records from a player.
/// </summary>
/// <param name="player">Player to get local records from.</param>
/// <returns></returns>
public Task<IEnumerable<DbLocalRecord>> GetRecordsByPlayerAsync(IPlayer player);

/// <summary>
/// Get the local record of a player for a map.
/// </summary>
/// <param name="player">The player to get the local record of.</param>
/// <param name="map">The map to get the local record for.</param>
/// <returns></returns>
public Task<DbLocalRecord?> GetRecordOfPlayerInMapAsync(IPlayer player, IMap map);

/// <summary>
/// Delete a local record for a player in a map.
/// </summary>
/// <param name="player">The player that has the local record.</param>
/// <param name="map">The map that contains the local record.</param>
/// <returns></returns>
public Task DeleteRecordAsync(IPlayer player, IMap map);

/// <summary>
/// Delete a local record.
/// </summary>
/// <param name="localRecord">The local record to remove.</param>
/// <returns></returns>
public Task DeleteRecordAsync(ILocalRecord localRecord);

/// <summary>
/// Delete all local records from a map.
/// </summary>
/// <param name="map">The map to clear all local records from.</param>
/// <returns></returns>
public Task DeleteRecordsAsync(IMap map);
}
15 changes: 15 additions & 0 deletions src/Modules/LocalRecordsModule/Interfaces/ILocalRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@ namespace EvoSC.Modules.Official.LocalRecordsModule.Interfaces;

public interface ILocalRecord
{
/// <summary>
/// The ID of the record.
/// </summary>
public long Id { get; }

/// <summary>
/// The position of the record in the leaderboard.
/// </summary>
public int Position { get; }

/// <summary>
/// The map the record is set on.
/// </summary>
public IMap Map { get; }

/// <summary>
/// The player record.
/// </summary>
public IPlayerRecord Record { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@ namespace EvoSC.Modules.Official.LocalRecordsModule.Interfaces.Services;

public interface ILocalRecordsService
{
/// <summary>
/// Get all local records of the current map on the server.
/// </summary>
/// <returns></returns>
public Task<ILocalRecord[]> GetLocalsOfCurrentMapAsync();

/// <summary>
/// Show/Update the local records widget to a player.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public Task ShowWidgetAsync(IPlayer player);

/// <summary>
/// Show/Update the local records widget to all players.
/// </summary>
/// <returns></returns>
public Task ShowWidgetToAllAsync();

/// <summary>
/// Update a player's local record for the current map.
/// </summary>
/// <param name="record"></param>
/// <returns></returns>
public Task UpdatePbAsync(IPlayerRecord record);

/// <summary>
/// Remove all local records on all maps, and add them back based on the
/// registered PBs of players.
/// </summary>
/// <returns></returns>
public Task ResetLocalRecordsAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ public async Task Reset_Records_Cancel_Does_Not_Resets_All_Records()
AuditEventBuilder.Verify(m => m.Success(), Times.Never);
AuditEventBuilder.Verify(m => m.Error(), Times.Never);
}

[Fact]
public async Task Reset_Records_Error_Is_Reported()
{
_localRecordsService.Setup(m => m.ResetLocalRecordsAsync()).Throws<Exception>();

await Assert.ThrowsAsync<Exception>(() => Controller.ConfirmResetAsync(true));
_server.Server.Verify(m => m.ErrorMessageAsync(It.IsAny<string>()));
AuditEventBuilder.Verify(m => m.Error());
}
}
32 changes: 32 additions & 0 deletions tests/Modules/LocalRecordsModule.Tests/LocalRecordsModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using EvoSC.Manialinks.Interfaces;
using EvoSC.Modules.Official.LocalRecordsModule.Interfaces.Services;
using Moq;

namespace LocalRecordsModule.Tests;

public class LocalRecordsModuleTests
{
[Fact]
public async Task Widget_Is_Shown_To_All_On_Enable()
{
var manialinks = new Mock<IManialinkManager>();
var localRecords = new Mock<ILocalRecordsService>();
var module = new EvoSC.Modules.Official.LocalRecordsModule.LocalRecordsModule(manialinks.Object, localRecords.Object);

await module.EnableAsync();

localRecords.Verify(m => m.ShowWidgetToAllAsync());
}

[Fact]
public async Task Widget_Is_Hidden_On_Disable()
{
var manialinks = new Mock<IManialinkManager>();
var localRecords = new Mock<ILocalRecordsService>();
var module = new EvoSC.Modules.Official.LocalRecordsModule.LocalRecordsModule(manialinks.Object, localRecords.Object);

await module.DisableAsync();

manialinks.Verify(m => m.HideManialinkAsync("LocalRecordsModule.LocalRecordsWidget"));
}
}

0 comments on commit 1ceb147

Please sign in to comment.