diff --git a/Core/Session/ExperienceProvider.cs b/Core/Session/ExperienceProvider.cs index 94e9cde9..59673320 100644 --- a/Core/Session/ExperienceProvider.cs +++ b/Core/Session/ExperienceProvider.cs @@ -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(json)!; + + int[] array = DeserializeObject(json) ?? []; + + MaxLevel = array.Length + 1; + + return array; } } diff --git a/Core/Session/GrindSession.cs b/Core/Session/GrindSession.cs index 3e5e01b4..2454df5b 100644 --- a/Core/Session/GrindSession.cs +++ b/Core/Session/GrindSession.cs @@ -1,4 +1,5 @@ using System; + using Newtonsoft.Json; namespace Core.Session; @@ -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; } @@ -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; diff --git a/Core/Session/IGrindSessionDAO.cs b/Core/Session/IGrindSessionDAO.cs index f260c068..bb703eb1 100644 --- a/Core/Session/IGrindSessionDAO.cs +++ b/Core/Session/IGrindSessionDAO.cs @@ -1,10 +1,11 @@ using System.Collections.Generic; -using System.Linq; +using System.Threading.Tasks; namespace Core.Session; public interface IGrindSessionDAO { - IQueryable Load(); + Task> LoadAsync(); + void Save(GrindSession session); } \ No newline at end of file diff --git a/Core/Session/LocalGrindSessionDAO.cs b/Core/Session/LocalGrindSessionDAO.cs index e2723c6b..db8f8fea 100644 --- a/Core/Session/LocalGrindSessionDAO.cs +++ b/Core/Session/LocalGrindSessionDAO.cs @@ -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; @@ -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 Load() + public async Task> LoadAsync() { - var sessions = Directory.EnumerateFiles(dataConfig.ExpHistory, "*.json") - .Select(file => DeserializeObject(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(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() + .OrderByDescending(s => s.SessionStart); + + return !sessions.Any() + ? [] + : sessions; } public void Save(GrindSession session) diff --git a/Frontend/Pages/History.razor b/Frontend/Pages/History.razor index 85b041c3..db89745c 100644 --- a/Frontend/Pages/History.razor +++ b/Frontend/Pages/History.razor @@ -4,64 +4,77 @@ @inject IGrindSessionDAO grindSessionDAO -@if (Sessions.Any()) -{ -

- Show: - -

- - - - @if (showPath) - { - - - - - - } - +

+ Show: + +

+ + + + + @if (showPath) + { + - - - - - - - - - - -} -else -{ -
No previous sessions!
-} + } + + + + + + + +

+ Min: @minLevelFilter + +

+
+
+ + +

+ Max: @maxLevelFilter + +

+
+
+ + + + + +
+ + @code { PaginationState pagination = new PaginationState { ItemsPerPage = 15 }; - private IQueryable Sessions = null!; + private IQueryable Sessions = Enumerable.Empty().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 Filtered { get { - var result = Sessions; + var result = Sessions.Where(s => s.LevelFrom <= maxLevelFilter); if (!string.IsNullOrEmpty(pathFilter)) { @@ -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(); } }