Skip to content

Commit

Permalink
Add LeaderboardCount to CustomLeaderboardAllowedCategory model
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStolk committed Sep 9, 2024
1 parent a17cb63 commit c944470
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public record GetCustomLeaderboardAllowedCategory
public required GameMode GameMode { get; init; }

public required CustomLeaderboardRankSorting RankSorting { get; init; }

public required int LeaderboardCount { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public record GetCustomLeaderboardAllowedCategory
public required SpawnsetGameMode GameMode { get; init; }

public required CustomLeaderboardRankSorting RankSorting { get; init; }

public required int LeaderboardCount { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override async Task OnInitializedAsync()
GetTotalCustomLeaderboardData = await Http.GetTotalCustomLeaderboardData();

List<GetCustomLeaderboardAllowedCategory> allowedCategories = await Http.GetCustomLeaderboardAllowedCategories();
_categories = allowedCategories.ConvertAll(a => new CategoryDropdown(a.GameMode, a.RankSorting));
_categories = allowedCategories.ConvertAll(a => new CategoryDropdown(a.GameMode, a.RankSorting, a.LeaderboardCount));
}

protected override async Task OnParametersSetAsync()
Expand Down Expand Up @@ -135,18 +135,23 @@ private async Task Fetch()

private sealed class CategoryDropdown
{
public CategoryDropdown(GameMode gameMode, CustomLeaderboardRankSorting rankSorting)
private readonly int _leaderboardCount;

public CategoryDropdown(GameMode gameMode, CustomLeaderboardRankSorting rankSorting, int leaderboardCount)
{
GameMode = gameMode;
RankSorting = rankSorting;
_leaderboardCount = leaderboardCount;
}

public GameMode GameMode { get; }
public CustomLeaderboardRankSorting RankSorting { get; }

public override string ToString()
{
return $"{GameMode.ToCore().ToDisplayString()}: {RankSorting.ToDisplayString()}";
string gameMode = GameMode.ToCore().ToDisplayString();
string rankSorting = RankSorting.ToDisplayString();
return $"{gameMode}: {rankSorting} ({_leaderboardCount})";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@
Check out the global custom leaderboards <a class="link" href="/custom/leaderboards/global">here</a>.
</p>

@if (GetCustomLeaderboards == null)
@if (GetCustomLeaderboards == null || _categories == null)
{
<Loading />
}
else
{
CategoryDropdown defaultCategoryDropdown = _categories.Count > 0 ? _categories[0] : new CategoryDropdown(EnumConvert.GetGameMode(GameMode), EnumConvert.GetRankSorting(RankSorting), 0);

<div class="bg-gray-1 p-2 text-lg">
<div class="grid gap-2 grid-cols-2 max-w-md mb-2">
<span>Spawnset name</span>
<input class="bg-black border-gray-3 border-2 w-64" value="@SpawnsetFilter" @onchange="ChangeInputSpawnsetName" />
<input class="bg-black border-gray-3 border-2 w-80" value="@SpawnsetFilter" @onchange="ChangeInputSpawnsetName" />
<span>Author name</span>
<input class="bg-black border-gray-3 border-2 w-64" value="@AuthorFilter" @onchange="ChangeInputAuthorName" />
@if (_categories != null)
{
<span>Category</span>
<SelectList CssWidthClass="w-64" CssOpenerClass="pl-2" TValue="CategoryDropdown" Values="@(_categories)" DisplayFunction="@(c => c.ToString())" Default="new CategoryDropdown(EnumConvert.GetGameMode(GameMode), EnumConvert.GetRankSorting(RankSorting))" Selected="@SetCategory" />
}
<input class="bg-black border-gray-3 border-2 w-80" value="@AuthorFilter" @onchange="ChangeInputAuthorName" />
<span>Category</span>
<SelectList CssWidthClass="w-80" CssOpenerClass="pl-2" TValue="CategoryDropdown" Values="@(_categories)" DisplayFunction="@(c => c.ToString())" Default="defaultCategoryDropdown" Selected="@SetCategory" />
</div>

<CascadingValue Value="this">
Expand Down Expand Up @@ -71,7 +70,7 @@ else
CustomLeaderboardRankSorting.TimeAsc or CustomLeaderboardRankSorting.TimeDesc => StringFormats.TimeFormat,
_ => "0",
};

<div class="grid gap-3 grid-cols-custom-leaderboards-sm md:grid-cols-custom-leaderboards-md lg:grid-cols-custom-leaderboards-lg xl:grid-cols-custom-leaderboards-xl 2xl:grid-cols-custom-leaderboards-2xl h-6 @(i % 2 == 0 ? "bg-gray-1" : string.Empty)">
<div class="overflow-hidden"><a class="link" href="/custom/leaderboard/@customLeaderboard.Id">@customLeaderboard.SpawnsetName.ToNoBreakString()</a></div>
<div class="overflow-hidden hidden md:block">@customLeaderboard.SpawnsetAuthorName.ToNoBreakString()</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DevilDaggersInfo.Web.ApiSpec.Admin.CustomEntries;
using DevilDaggersInfo.Web.Server.Domain.Admin.Converters.ApiToDomain;
using DevilDaggersInfo.Web.Server.Domain.Admin.Exceptions;
using DevilDaggersInfo.Web.Server.Domain.Converters.CoreToDomain;
using DevilDaggersInfo.Web.Server.Domain.Entities;
using DevilDaggersInfo.Web.Server.Domain.Entities.Enums;
using DevilDaggersInfo.Web.Server.Domain.Entities.Values;
Expand Down Expand Up @@ -466,7 +467,7 @@ private async Task ValidateCustomLeaderboardAsync(
if (spawnsetBinary.GameMode == GameMode.TimeAttack && !spawnsetBinary.HasSpawns())
throw new CustomLeaderboardValidationException("Time Attack spawnset must have spawns.");

if (!CustomLeaderboardUtils.IsGameModeAndRankSortingCombinationAllowed(spawnsetBinary.GameMode, rankSorting))
if (!CustomLeaderboardUtils.IsGameModeAndRankSortingCombinationAllowed(spawnsetBinary.GameMode.ToDomain(), rankSorting))
throw new CustomLeaderboardValidationException($"Combining game mode '{spawnsetBinary.GameMode}' and rank sorting '{rankSorting}' is not allowed.");

CustomLeaderboardCriteriaOperator deathTypeOperator = deathTypeCriteria.Operator.ToDomain();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using DevilDaggersInfo.Web.Server.Domain.Entities.Enums;

namespace DevilDaggersInfo.Web.Server.Domain.Models.CustomLeaderboards;

public record CustomLeaderboardAllowedCategory
{
public required SpawnsetGameMode GameMode { get; init; }

public required CustomLeaderboardRankSorting RankSorting { get; init; }

public required int LeaderboardCount { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@ public async Task<int> GetCustomLeaderboardIdBySpawnsetHashAsync(byte[] hash)
return customLeaderboard.Id;
}

public async Task<List<CustomLeaderboardAllowedCategory>> GetCustomLeaderboardAllowedCategories()
{
List<(SpawnsetGameMode GameMode, CustomLeaderboardRankSorting RankSorting)> allowedCategories = CustomLeaderboardUtils.GetAllowedGameModeAndRankSortingCombinations();

// ! Navigation property.
var customLeaderboards = await _dbContext.CustomLeaderboards
.Select(cl => new { cl.Spawnset!.GameMode, cl.RankSorting })
.ToListAsync();

return allowedCategories.ConvertAll(ac => new CustomLeaderboardAllowedCategory
{
GameMode = ac.GameMode,
RankSorting = ac.RankSorting,
LeaderboardCount = customLeaderboards.Count(cl => cl.GameMode == ac.GameMode && cl.RankSorting == ac.RankSorting),
});
}

private static CustomLeaderboardOverview ToOverview(CustomLeaderboardData cl)
{
if (cl.CustomLeaderboard.Spawnset == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using DevilDaggersInfo.Core.Spawnset;
using DevilDaggersInfo.Web.Server.Domain.Entities.Enums;
using DevilDaggersInfo.Web.Server.Domain.Extensions;
using DevilDaggersInfo.Web.Server.Domain.Models.CustomLeaderboards;
Expand Down Expand Up @@ -53,24 +52,24 @@ public static CustomLeaderboardDagger GetDaggerFromStat(CustomLeaderboardRankSor
return CustomLeaderboardDagger.Default;
}

public static bool IsGameModeAndRankSortingCombinationAllowed(GameMode gameMode, CustomLeaderboardRankSorting rankSorting)
public static bool IsGameModeAndRankSortingCombinationAllowed(SpawnsetGameMode gameMode, CustomLeaderboardRankSorting rankSorting)
{
// Allow all rank sortings for Survival.
if (gameMode == GameMode.Survival)
if (gameMode == SpawnsetGameMode.Survival)
return true;

// Allow both time rank sortings for Race.
if (gameMode == GameMode.Race)
if (gameMode == SpawnsetGameMode.Race)
return rankSorting is CustomLeaderboardRankSorting.TimeAsc or CustomLeaderboardRankSorting.TimeDesc;

// Only allow ascending time sort for TimeAttack for now.
return rankSorting == CustomLeaderboardRankSorting.TimeAsc;
}

public static List<(GameMode GameMode, CustomLeaderboardRankSorting RankSorting)> GetAllowedGameModeAndRankSortingCombinations()
public static List<(SpawnsetGameMode GameMode, CustomLeaderboardRankSorting RankSorting)> GetAllowedGameModeAndRankSortingCombinations()
{
List<(GameMode GameMode, CustomLeaderboardRankSorting RankSorting)> allowedCombinations = [];
foreach (GameMode gameMode in Enum.GetValues<GameMode>())
List<(SpawnsetGameMode GameMode, CustomLeaderboardRankSorting RankSorting)> allowedCombinations = [];
foreach (SpawnsetGameMode gameMode in Enum.GetValues<SpawnsetGameMode>())
{
foreach (CustomLeaderboardRankSorting rankSorting in Enum.GetValues<CustomLeaderboardRankSorting>().Where(rs => IsGameModeAndRankSortingCombinationAllowed(gameMode, rs)))
allowedCombinations.Add((gameMode, rankSorting));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
using DevilDaggersInfo.Web.Client;
using DevilDaggersInfo.Web.Server.Converters.ApiToDomain.Main;
using DevilDaggersInfo.Web.Server.Converters.DomainToApi.Main;
using DevilDaggersInfo.Web.Server.Domain.Converters.CoreToDomain;
using DevilDaggersInfo.Web.Server.Domain.Main.Converters.DomainToApi;
using DevilDaggersInfo.Web.Server.Domain.Repositories;
using DevilDaggersInfo.Web.Server.Domain.Utils;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Model = DevilDaggersInfo.Web.Server.Domain.Models.CustomLeaderboards;
using MainApi = DevilDaggersInfo.Web.Server.Domain.Models.CustomLeaderboards;

namespace DevilDaggersInfo.Web.Server.Controllers.Main;

Expand Down Expand Up @@ -38,7 +37,7 @@ public async Task<ActionResult<Page<GetCustomLeaderboardOverview>>> GetCustomLea
CustomLeaderboardSorting? sortBy = null,
bool ascending = false)
{
Domain.Models.Page<Model.CustomLeaderboardOverview> cls = await _customLeaderboardRepository.GetCustomLeaderboardOverviewsAsync(
Domain.Models.Page<MainApi.CustomLeaderboardOverview> cls = await _customLeaderboardRepository.GetCustomLeaderboardOverviewsAsync(
rankSorting: rankSorting.ToDomain(),
gameMode: gameMode.ToDomain(),
spawnsetFilter: spawnsetFilter,
Expand All @@ -61,7 +60,7 @@ public async Task<ActionResult<Page<GetCustomLeaderboardOverview>>> GetCustomLea
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<GetGlobalCustomLeaderboard>> GetGlobalCustomLeaderboardForCategory([Required] GameMode gameMode, [Required] CustomLeaderboardRankSorting rankSorting)
{
Model.GlobalCustomLeaderboard globalCustomLeaderboard = await _customLeaderboardRepository.GetGlobalCustomLeaderboardAsync(gameMode.ToDomain(), rankSorting.ToDomain());
MainApi.GlobalCustomLeaderboard globalCustomLeaderboard = await _customLeaderboardRepository.GetGlobalCustomLeaderboardAsync(gameMode.ToDomain(), rankSorting.ToDomain());
return new GetGlobalCustomLeaderboard
{
Entries = globalCustomLeaderboard.Entries
Expand Down Expand Up @@ -97,7 +96,7 @@ public async Task<ActionResult<GetGlobalCustomLeaderboard>> GetGlobalCustomLeade
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<GetTotalCustomLeaderboardData>> GetTotalCustomLeaderboardData()
{
Model.CustomLeaderboardsTotalData totalData = await _customLeaderboardRepository.GetCustomLeaderboardsTotalDataAsync();
MainApi.CustomLeaderboardsTotalData totalData = await _customLeaderboardRepository.GetCustomLeaderboardsTotalDataAsync();
return new GetTotalCustomLeaderboardData
{
LeaderboardsPerGameMode = totalData.LeaderboardsPerGameMode.ToDictionary(kvp => kvp.Key.ToMainApi(), kvp => kvp.Value),
Expand All @@ -113,19 +112,15 @@ public async Task<ActionResult<GetTotalCustomLeaderboardData>> GetTotalCustomLea
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<GetCustomLeaderboard>> GetCustomLeaderboardById(int id)
{
Model.SortedCustomLeaderboard cl = await _customLeaderboardRepository.GetSortedCustomLeaderboardByIdAsync(id);
MainApi.SortedCustomLeaderboard cl = await _customLeaderboardRepository.GetSortedCustomLeaderboardByIdAsync(id);
return cl.ToMainApi();
}

[HttpGet("allowed-categories")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<List<GetCustomLeaderboardAllowedCategory>> GetCustomLeaderboardAllowedCategories()
public async Task<ActionResult<List<GetCustomLeaderboardAllowedCategory>>> GetCustomLeaderboardAllowedCategories()
{
List<(DevilDaggersInfo.Core.Spawnset.GameMode GameMode, Domain.Entities.Enums.CustomLeaderboardRankSorting RankSorting)> allowedCategories = CustomLeaderboardUtils.GetAllowedGameModeAndRankSortingCombinations();
return allowedCategories.ConvertAll(ac => new GetCustomLeaderboardAllowedCategory
{
GameMode = ac.GameMode.ToDomain().ToMainApi(),
RankSorting = ac.RankSorting.ToMainApi(),
});
List<MainApi.CustomLeaderboardAllowedCategory> allowedCategories = await _customLeaderboardRepository.GetCustomLeaderboardAllowedCategories();
return allowedCategories.ConvertAll(ac => ac.ToMainApi());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,9 @@ public async Task<ActionResult> CustomLeaderboardExistsBySpawnsetHash([FromQuery

[HttpGet("allowed-categories")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<List<GetCustomLeaderboardAllowedCategory>> GetCustomLeaderboardAllowedCategories()
public async Task<ActionResult<List<GetCustomLeaderboardAllowedCategory>>> GetCustomLeaderboardAllowedCategories()
{
List<(DevilDaggersInfo.Core.Spawnset.GameMode GameMode, Domain.Entities.Enums.CustomLeaderboardRankSorting RankSorting)> allowedCategories = CustomLeaderboardUtils.GetAllowedGameModeAndRankSortingCombinations();
return allowedCategories.ConvertAll(ac => new GetCustomLeaderboardAllowedCategory
{
GameMode = ac.GameMode.ToDomain().ToToolsApi(),
RankSorting = ac.RankSorting.ToToolsApi(),
});
List<CustomLeaderboardAllowedCategory> customLeaderboardAllowedCategories = await _customLeaderboardRepository.GetCustomLeaderboardAllowedCategories();
return customLeaderboardAllowedCategories.ConvertAll(ac => ac.ToToolsApi());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ private static MainApi.GetCustomEntry ToMainApi(this CustomEntry customEntry, Sp
};
}

public static MainApi.GetCustomLeaderboardAllowedCategory ToMainApi(this CustomLeaderboardAllowedCategory allowedCategory)
{
return new MainApi.GetCustomLeaderboardAllowedCategory
{
GameMode = allowedCategory.GameMode.ToMainApi(),
RankSorting = allowedCategory.RankSorting.ToMainApi(),
LeaderboardCount = allowedCategory.LeaderboardCount,
};
}

private static MainApi.GetCustomLeaderboardDaggers ToMainApi(this CustomLeaderboardDaggers customLeaderboardDaggers, bool isTime)
{
return new MainApi.GetCustomLeaderboardDaggers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public static ToolsApi.GetCustomLeaderboardForOverview ToToolsApi(this CustomLea
};
}

public static ToolsApi.GetCustomLeaderboardAllowedCategory ToToolsApi(this CustomLeaderboardAllowedCategory customLeaderboardAllowedCategory)
{
return new ToolsApi.GetCustomLeaderboardAllowedCategory
{
GameMode = customLeaderboardAllowedCategory.GameMode.ToToolsApi(),
RankSorting = customLeaderboardAllowedCategory.RankSorting.ToToolsApi(),
LeaderboardCount = customLeaderboardAllowedCategory.LeaderboardCount,
};
}

private static ToolsApi.GetCustomLeaderboardCriteria ToToolsApi(this CustomLeaderboardCriteria customLeaderboardCriteria)
{
return new ToolsApi.GetCustomLeaderboardCriteria
Expand Down

0 comments on commit c944470

Please sign in to comment.