Skip to content

Commit

Permalink
Add leaderboard info
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed May 29, 2024
1 parent 7b4e978 commit 1fc6b5c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
16 changes: 15 additions & 1 deletion MyApp.ServiceInterface/LeaderboardServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LeaderboardServices : Service
public async Task<object> Any(CalculateLeaderBoard request)
{
var statTotals = await Db.SelectAsync<StatTotals>();
var modelsToExclude = request.ModelsToExclude?.Split(",").ToList() ?? new List<string>();
var modelsToExclude = request.ModelsToExclude?.Split(",").ToList() ?? [];
// filter to answers only
var answers = statTotals.Where(x => FilterSpecificModels(x, modelsToExclude)).ToList();
// Sum up votes by model, first group by UserName
Expand Down Expand Up @@ -52,6 +52,14 @@ public async Task<object> Any(CalculateLeaderBoard request)
var modelsToExcludeSlug = request.ModelsToExclude?.GenerateSlug();
var combinedSuffix = modelsToExcludeSlug.IsNullOrEmpty() ? "" : $"-{modelsToExcludeSlug}";
await File.WriteAllTextAsync($"App_Data/leaderboard{combinedSuffix}.json", json);

var count = Db.Count<StatTotals>(x => x.Id != x.PostId.ToString());
var info = new LeaderboardInfo
{
AnswerCount = count,
GeneratedDate = DateTime.UtcNow,
};
await File.WriteAllTextAsync("App_Data/leaderboard-info.json", info.ToJson());

return leaderBoard;
}
Expand Down Expand Up @@ -318,6 +326,12 @@ public record LeaderboardStat
public double Value { get; set; }
}

public class LeaderboardInfo
{
public long AnswerCount { get; set; }
public DateTime GeneratedDate { get; set; }
}

public class CalculateLeaderBoard : IReturn<CalculateLeaderboardResponse>, IGet
{
public string? ModelsToExclude { get; set; }
Expand Down
18 changes: 17 additions & 1 deletion MyApp/Components/Pages/Leaderboard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

<PageTitle>Leaderboard</PageTitle>

@if (LeaderboardInfo != null)
{
<div class="absolute right-4 -mt-6">
<p class="text-sm text-gray-500">
<span>Leaderboard</span>
<a class="text-indigo-600 dark:text-indigo-300" href="/posts/leaderboard-intro">generated</a>
<span>
from <b>@LeaderboardInfo.AnswerCount.ToString("N0")</b> answers on
<b>@LeaderboardInfo.GeneratedDate.ToString("MMM dd, yyyy")</b>
</span>
</p>
</div>
}

<div class="mt-8 mb-20 mx-auto max-w-screen-lg">

@if (TotalVotes.Count > 0 && WinRates.Count > 0)
Expand Down Expand Up @@ -70,6 +84,7 @@
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

LeaderboardInfo? LeaderboardInfo;
CalculateLeaderboardResponse? allData;
CalculateLeaderboardResponse? top1kData;

Expand All @@ -84,7 +99,7 @@
{
// Only generate if missing, otherwise background task will update it.
var gateway = GatewayFactory.GetServiceGateway(HttpContext.ToRequest());
if (!File.Exists("App_Data/leaderboard.json"))
if (!File.Exists("App_Data/leaderboard.json") || !File.Exists("App_Data/leaderboard-info.json"))
{
await gateway.ApiAsync(new CalculateLeaderBoard());
}
Expand All @@ -93,6 +108,7 @@
await gateway.ApiAsync(new CalculateTop1KLeaderboard());
}

LeaderboardInfo = (await File.ReadAllTextAsync("App_Data/leaderboard-info.json")).FromJson<LeaderboardInfo>();
var allVotesData = await File.ReadAllTextAsync("App_Data/leaderboard.json");
allData = allVotesData.FromJson<CalculateLeaderboardResponse>();

Expand Down
6 changes: 6 additions & 0 deletions MyApp/wwwroot/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -3923,6 +3923,12 @@ select{
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);
}

.hover\:shadow-lg:hover {
--tw-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
--tw-shadow-colored: 0 10px 15px -3px var(--tw-shadow-color), 0 4px 6px -4px var(--tw-shadow-color);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);
}

.hover\:ring-2:hover {
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);
Expand Down

0 comments on commit 1fc6b5c

Please sign in to comment.