From e6eb83af2ce25d9928489921255bca3ac22bf07d Mon Sep 17 00:00:00 2001 From: asesidaa <1061472754@qq.com> Date: Thu, 15 Sep 2022 15:27:27 +0800 Subject: [PATCH] Clean up --- TaikoWebUI/Pages/Profile.razor | 14 +- TaikoWebUI/Pages/Profile.razor.cs | 10 +- TaikoWebUI/Services/GameDataService.cs | 162 ++++++++++++------------ TaikoWebUI/Services/IGameDataService.cs | 4 +- TaikoWebUI/TaikoWebUI.csproj | 1 + 5 files changed, 89 insertions(+), 102 deletions(-) diff --git a/TaikoWebUI/Pages/Profile.razor b/TaikoWebUI/Pages/Profile.razor index 8e93555..8ec99a5 100644 --- a/TaikoWebUI/Pages/Profile.razor +++ b/TaikoWebUI/Pages/Profile.razor @@ -21,18 +21,8 @@ - @if (enterTextDirectly) - { - - } - else - { - - } - Enter Text Directly + + diff --git a/TaikoWebUI/Pages/Profile.razor.cs b/TaikoWebUI/Pages/Profile.razor.cs index f3e1fda..05998a9 100644 --- a/TaikoWebUI/Pages/Profile.razor.cs +++ b/TaikoWebUI/Pages/Profile.razor.cs @@ -1,7 +1,4 @@ -using MudBlazor.Utilities; -using static MudBlazor.Colors; - -namespace TaikoWebUI.Pages; +namespace TaikoWebUI.Pages; public partial class Profile { @@ -74,11 +71,10 @@ public partial class Profile await Client.PostAsJsonAsync($"api/UserSettings/{Baid}", response); isSavingOptions = false; } - -#pragma warning disable CS1998 + private async Task> SearchForTitle(string value) -#pragma warning restore CS1998 { + await Task.Delay(1); var titles = GameDataService.GetTitles(); return string.IsNullOrWhiteSpace(value) ? titles : titles.Where(x => x.Contains(value, StringComparison.OrdinalIgnoreCase)); diff --git a/TaikoWebUI/Services/GameDataService.cs b/TaikoWebUI/Services/GameDataService.cs index c1a3527..eb9278b 100644 --- a/TaikoWebUI/Services/GameDataService.cs +++ b/TaikoWebUI/Services/GameDataService.cs @@ -1,5 +1,4 @@ using System.Collections.Immutable; -using MudBlazor.Utilities; using Swan.Mapping; using TaikoWebUI.Shared.Models; @@ -7,20 +6,20 @@ namespace TaikoWebUI.Services; public class GameDataService : IGameDataService { + private readonly string[] bodyTitles = new string[Constants.COSTUME_BODY_MAX]; private readonly HttpClient client; - - private readonly Dictionary musicMap = new(); - - private ImmutableDictionary danMap = ImmutableDictionary.Empty; - - private ImmutableHashSet titles = ImmutableHashSet.Empty; + private readonly string[] faceTitles = new string[Constants.COSTUME_FACE_MAX]; private readonly string[] headTitles = new string[Constants.COSTUME_HEAD_MAX]; - private readonly string[] faceTitles = new string[Constants.COSTUME_FACE_MAX]; - private readonly string[] bodyTitles = new string[Constants.COSTUME_BODY_MAX]; private readonly string[] kigurumiMTitles = new string[Constants.COSTUME_KIGURUMI_MAX]; + + private readonly Dictionary musicMap = new(); private readonly string[] puchiTitles = new string[Constants.COSTUME_PUCHI_MAX]; + private ImmutableDictionary danMap = ImmutableDictionary.Empty; + + private ImmutableHashSet titles = ImmutableHashSet.Empty; + public GameDataService(HttpClient client) { this.client = client; @@ -39,11 +38,11 @@ public class GameDataService : IGameDataService danData.ThrowIfNull(); danMap = danData.ToImmutableDictionary(data => data.DanId); - + // To prevent duplicate entries in wordlist var dict = wordList.WordListEntries.GroupBy(entry => entry.Key) .ToImmutableDictionary(group => group.Key, group => group.First()); - await Task.Run(()=>InitializeMusicMap(musicInfo, dict, musicOrder)); + await Task.Run(() => InitializeMusicMap(musicInfo, dict, musicOrder)); await Task.Run(() => InitializeHeadTitles(dict)); await Task.Run(() => InitializeFaceTitles(dict)); @@ -53,6 +52,76 @@ public class GameDataService : IGameDataService await Task.Run(() => InitializeTitles(dict)); } + public string GetMusicNameBySongId(uint songId) + { + return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.SongName : string.Empty; + } + + public string GetMusicArtistBySongId(uint songId) + { + return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.ArtistName : string.Empty; + } + + public SongGenre GetMusicGenreBySongId(uint songId) + { + return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.Genre : SongGenre.Variety; + } + + public int GetMusicIndexBySongId(uint songId) + { + return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.Index : int.MaxValue; + } + + public DanData GetDanDataById(uint danId) + { + return danMap.GetValueOrDefault(danId, new DanData()); + } + + public int GetMusicStarLevel(uint songId, Difficulty difficulty) + { + var success = musicMap.TryGetValue(songId, out var musicDetail); + return difficulty switch + { + Difficulty.None => throw new ArgumentException("Difficulty cannot be none"), + Difficulty.Easy => success ? musicDetail!.StarEasy : 0, + Difficulty.Normal => success ? musicDetail!.StarNormal : 0, + Difficulty.Hard => success ? musicDetail!.StarHard : 0, + Difficulty.Oni => success ? musicDetail!.StarOni : 0, + Difficulty.UraOni => success ? musicDetail!.StarUra : 0, + _ => throw new ArgumentOutOfRangeException(nameof(difficulty), difficulty, null) + }; + } + + public string GetHeadTitle(uint index) + { + return index < headTitles.Length ? headTitles[index] : string.Empty; + } + + public string GetKigurumiTitle(uint index) + { + return index < kigurumiMTitles.Length ? kigurumiMTitles[index] : string.Empty; + } + + public string GetBodyTitle(uint index) + { + return index < bodyTitles.Length ? bodyTitles[index] : string.Empty; + } + + public string GetFaceTitle(uint index) + { + return index < faceTitles.Length ? faceTitles[index] : string.Empty; + } + + public string GetPuchiTitle(uint index) + { + return index < puchiTitles.Length ? puchiTitles[index] : string.Empty; + } + + public IEnumerable GetTitles() + { + return titles.ToArray(); + } + private void InitializeTitles(ImmutableDictionary dict) { var set = ImmutableHashSet.CreateBuilder(); @@ -123,7 +192,8 @@ public class GameDataService : IGameDataService } } - private void InitializeMusicMap(MusicInfo musicInfo, ImmutableDictionary dict, MusicOrder musicOrder) + private void InitializeMusicMap(MusicInfo musicInfo, ImmutableDictionary dict, + MusicOrder musicOrder) { foreach (var music in musicInfo.Items) { @@ -151,72 +221,4 @@ public class GameDataService : IGameDataService } } } - - public string GetMusicNameBySongId(uint songId) - { - return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.SongName : string.Empty; - } - - public string GetMusicArtistBySongId(uint songId) - { - return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.ArtistName : string.Empty; - } - public SongGenre GetMusicGenreBySongId(uint songId) - { - return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.Genre : SongGenre.Variety; - } - - public int GetMusicIndexBySongId(uint songId) - { - return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.Index : int.MaxValue; - } - public DanData GetDanDataById(uint danId) - { - return danMap.GetValueOrDefault(danId, new DanData()); - } - - public int GetMusicStarLevel(uint songId, Difficulty difficulty) - { - var success = musicMap.TryGetValue(songId, out var musicDetail); - return difficulty switch - { - Difficulty.None => throw new ArgumentException("Difficulty cannot be none"), - Difficulty.Easy => success ? musicDetail!.StarEasy : 0, - Difficulty.Normal => success ? musicDetail!.StarNormal : 0, - Difficulty.Hard => success ? musicDetail!.StarHard : 0, - Difficulty.Oni => success ? musicDetail!.StarOni : 0, - Difficulty.UraOni => success ? musicDetail!.StarUra : 0, - _ => throw new ArgumentOutOfRangeException(nameof(difficulty), difficulty, null) - }; - } - - public string GetHeadTitle(uint index) - { - return index < headTitles.Length ? headTitles[index] : string.Empty; - } - - public string GetKigurumiTitle(uint index) - { - return index < kigurumiMTitles.Length ? kigurumiMTitles[index] : string.Empty; - } - - public string GetBodyTitle(uint index) - { - return index < bodyTitles.Length ? bodyTitles[index] : string.Empty; - } - - public string GetFaceTitle(uint index) - { - return index < faceTitles.Length ? faceTitles[index] : string.Empty; - } - - public string GetPuchiTitle(uint index) - { - return index < puchiTitles.Length ? puchiTitles[index] : string.Empty; - } - - public IEnumerable GetTitles() - { - return titles.ToArray(); - } } \ No newline at end of file diff --git a/TaikoWebUI/Services/IGameDataService.cs b/TaikoWebUI/Services/IGameDataService.cs index 95eb397..b21d2bc 100644 --- a/TaikoWebUI/Services/IGameDataService.cs +++ b/TaikoWebUI/Services/IGameDataService.cs @@ -1,6 +1,4 @@ -using System.Collections.Immutable; - -namespace TaikoWebUI.Services; +namespace TaikoWebUI.Services; public interface IGameDataService { diff --git a/TaikoWebUI/TaikoWebUI.csproj b/TaikoWebUI/TaikoWebUI.csproj index 02bf122..d6221e1 100644 --- a/TaikoWebUI/TaikoWebUI.csproj +++ b/TaikoWebUI/TaikoWebUI.csproj @@ -7,6 +7,7 @@ +