Skip to content

Commit

Permalink
Fix: Frontend: History page properly loads EXP and EXP/Min values.
Browse files Browse the repository at this point in the history
Frontend: History page added more way to filter
  • Loading branch information
Xian55 committed Dec 23, 2024
1 parent fe63281 commit 459ea3f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 58 deletions.
9 changes: 8 additions & 1 deletion Core/Session/ExperienceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ namespace Core.Session;

public static class ExperienceProvider
{
public static int MaxLevel = 60;

public static int[] Get(DataConfig dataConfig)
{
string json = ReadAllText(Join(dataConfig.ExpExperience, "exp.json"));
return DeserializeObject<int[]>(json)!;

int[] array = DeserializeObject<int[]>(json) ?? [];

MaxLevel = array.Length + 1;

return array;
}
}
22 changes: 19 additions & 3 deletions Core/Session/GrindSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

using Newtonsoft.Json;

namespace Core.Session;
Expand All @@ -12,7 +13,15 @@ public sealed class GrindSession
public string PathName { get; set; } = string.Empty;
public UnitClass PlayerClass { get; set; }
public DateTime SessionStart { get; set; }

[JsonIgnore]
public DateTime SessionStartToLocalTime => SessionStart.ToLocalTime();

public DateTime SessionEnd { get; set; }

[JsonIgnore]
public DateTime SessionEndToLocalTime => SessionStart.ToLocalTime();

[JsonIgnore]
public int TotalTimeInMinutes => (int)(SessionEnd - SessionStart).TotalMinutes;
public int LevelFrom { get; set; }
Expand Down Expand Up @@ -51,10 +60,17 @@ public float ExpGetInBotSession

for (int i = 0; i < LevelTo - LevelFrom; i++)
{
expSoFar += ExpList[LevelFrom - 1 + i] - XpFrom;
XpFrom = 0;
if (LevelTo > maxLevel)
int index = LevelFrom - 1 + i;
if (index < 0)
{
expSoFar -= XpFrom;
continue;
}

if (index >= ExpList.Length)
break;

expSoFar += ExpList[index];
}

return expSoFar;
Expand Down
5 changes: 3 additions & 2 deletions Core/Session/IGrindSessionDAO.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.Session;

public interface IGrindSessionDAO
{
IQueryable<GrindSession> Load();
Task<IEnumerable<GrindSession>> LoadAsync();

void Save(GrindSession session);
}
35 changes: 23 additions & 12 deletions Core/Session/LocalGrindSessionDAO.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using static Newtonsoft.Json.JsonConvert;

Expand All @@ -12,31 +14,40 @@ namespace Core.Session;
public sealed class LocalGrindSessionDAO : IGrindSessionDAO
{
private readonly DataConfig dataConfig;
private readonly int[] expList;

public LocalGrindSessionDAO(DataConfig dataConfig)
{
this.dataConfig = dataConfig;

expList = ExperienceProvider.Get(dataConfig);

if (!Directory.Exists(dataConfig.ExpHistory))
Directory.CreateDirectory(dataConfig.ExpHistory);
}

public IQueryable<GrindSession> Load()
public async Task<IEnumerable<GrindSession>> LoadAsync()
{
var sessions = Directory.EnumerateFiles(dataConfig.ExpHistory, "*.json")
.Select(file => DeserializeObject<GrindSession>(File.ReadAllText(file))!)
.OrderByDescending(s => s.SessionStart);
var filePaths = Directory.EnumerateFiles(dataConfig.ExpHistory, "*.json");

if (sessions.Any())
var sessions = (await Task.WhenAll(filePaths.Select(async fileName =>
{
int[] expList = ExperienceProvider.Get(dataConfig);
foreach (GrindSession? s in sessions)
var fileContent = await File.ReadAllTextAsync(fileName);
var session = DeserializeObject<GrindSession>(fileContent);
if (session != null)
{
s.ExpList = expList;
session.ExpList = expList;
session.PathName = Path.GetFileNameWithoutExtension(session.PathName) ?? string.Empty;
}
}

return sessions.AsQueryable();
return session;
})))
.Where(s => s != null)
.Cast<GrindSession>()
.OrderByDescending(s => s.SessionStart);

return !sessions.Any()
? []
: sessions;
}

public void Save(GrindSession session)
Expand Down
99 changes: 59 additions & 40 deletions Frontend/Pages/History.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,77 @@

@inject IGrindSessionDAO grindSessionDAO

@if (Sessions.Any())
{
<p>
Show:
<label><input type="checkbox" @bind="showPath" /> path</label>
</p>

<QuickGrid class="table table-dark table-striped table-hover" Items="Filtered" Pagination="@pagination">
<PropertyColumn Title="Start Date" Property="@(x => x.SessionStart)" Sortable="true" IsDefaultSortColumn="true"></PropertyColumn>
@if (showPath)
{
<PropertyColumn Title="Path" Property="@(x => x.PathName)" Sortable="true">
<ColumnOptions>
<div class="search-box">
<input type="search" autofocus @bind="pathFilter" @bind:event="oninput" placeholder="Path name..." />
</div>
</ColumnOptions>
</PropertyColumn>
}
<PropertyColumn Title="Class" Property="@(x => x.PlayerClass)" Sortable="true">
<p>
Show:
<label><input type="checkbox" @bind="showPath" /> path</label>
</p>

<QuickGrid class="table table-dark table-striped table-hover" Items="Filtered" Pagination="@pagination">
<PropertyColumn Title="Start Date" Property="@(x => x.SessionStartToLocalTime)" Sortable="true" IsDefaultSortColumn="true"></PropertyColumn>
<PropertyColumn Title="Duration Min" Property="@(x => x.TotalTimeInMinutes)" Sortable="true" Align="Align.Right"></PropertyColumn>
@if (showPath)
{
<PropertyColumn Title="Path" Property="@(x => x.PathName)" Sortable="true">
<ColumnOptions>
<div class="search-box">
<input type="search" autofocus @bind="classFilter" @bind:event="oninput" placeholder="Class name..." />
<input type="search" autofocus @bind="pathFilter" @bind:event="oninput" placeholder="Path name..." />
</div>
</ColumnOptions>
</PropertyColumn>
<PropertyColumn Title="Duration(min)" Property="@(x => x.TotalTimeInMinutes)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Level" Property="@(x => x.LevelFrom)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Exp" Property="@(x => x.ExpGetInBotSession)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Exp/Hour" Property="@(x => x.ExperiencePerHour)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Mobs" Property="@(x => x.MobsKilled)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Mobs/Min" Property="@(x => x.MobsPerMinute)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Death" Property="@(x => x.Death)" Sortable="true" Align="Align.Right"></PropertyColumn>
</QuickGrid>

<Paginator State="@pagination" />
}
else
{
<div>No previous sessions!</div>
}
}
<PropertyColumn Title="Class" Property="@(x => x.PlayerClass)" Sortable="true">
<ColumnOptions>
<div class="search-box">
<input type="search" autofocus @bind="classFilter" @bind:event="oninput" placeholder="Class name..." />
</div>
</ColumnOptions>
</PropertyColumn>
<PropertyColumn Title="Start Level" Property="@(x => x.LevelFrom)" Sortable="true" Align="Align.Right">
<ColumnOptions>
<p>
Min: <span class="inline-block w-10"><b>@minLevelFilter</b></span>
<input type="range" @bind="minLevelFilter" @bind:event="oninput" min="1" max="@MaxLevel" />
</p>
</ColumnOptions>
</PropertyColumn>
<PropertyColumn Title="End Level" Property="@(x => x.LevelTo)" Sortable="true" Align="Align.Right">
<ColumnOptions>
<p>
Max: <span class="inline-block w-10"><b>@maxLevelFilter</b></span>
<input type="range" @bind="maxLevelFilter" @bind:event="oninput" min="1" max="@MaxLevel" />
</p>
</ColumnOptions>
</PropertyColumn>
<PropertyColumn Title="Total Exp" Property="@(x => x.ExpGetInBotSession)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Exp/Hour" Property="@(x => x.ExperiencePerHour)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Mobs" Property="@(x => x.MobsKilled)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Mobs/Min" Property="@(x => x.MobsPerMinute)" Sortable="true" Align="Align.Right"></PropertyColumn>
<PropertyColumn Title="Death" Property="@(x => x.Death)" Sortable="true" Align="Align.Right"></PropertyColumn>
</QuickGrid>

<Paginator State="@pagination" />

@code {

PaginationState pagination = new PaginationState { ItemsPerPage = 15 };

private IQueryable<GrindSession> Sessions = null!;
private IQueryable<GrindSession> Sessions = Enumerable.Empty<GrindSession>().AsQueryable();

private bool showPath = true;

private string pathFilter = string.Empty;
private string classFilter = string.Empty;

private int MaxLevel;

private int minLevelFilter = 1;
private int maxLevelFilter = 60;

private IQueryable<GrindSession> Filtered
{
get
{
var result = Sessions;
var result = Sessions.Where(s => s.LevelFrom <= maxLevelFilter);

if (!string.IsNullOrEmpty(pathFilter))
{
Expand All @@ -73,12 +86,18 @@ else
result = result.Where(c => c.PlayerClass.ToStringF().Contains(classFilter, StringComparison.CurrentCultureIgnoreCase));
}

if (minLevelFilter > 0)
{
result = result.Where(c => c.LevelTo >= minLevelFilter);
}

return result;
}
}

protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
Sessions = grindSessionDAO.Load();
maxLevelFilter = MaxLevel = ExperienceProvider.MaxLevel;
Sessions = (await grindSessionDAO.LoadAsync()).AsQueryable();
}
}

0 comments on commit 459ea3f

Please sign in to comment.