From d6e27fa75c33f37a66c5a69130b63e3049a32ac1 Mon Sep 17 00:00:00 2001 From: asesidaa <1061472754@qq.com> Date: Wed, 14 Sep 2022 20:14:21 +0800 Subject: [PATCH] Add costume setting to user settings and page prototype --- SharedProject/Models/UserSetting.cs | 31 ++++++++++- TaikoLocalServer.sln.DotSettings | 2 + TaikoLocalServer/Common/Utils/JsonHelper.cs | 52 +++++++++++++++++++ TaikoLocalServer/Common/Utils/PathHelper.cs | 9 +++- TaikoLocalServer/Context/TaikoDbContext.cs | 2 +- .../Controllers/Api/UserSettingsController.cs | 35 ++++++++++++- .../Controllers/Game/BaidController.cs | 33 ++---------- .../Game/InitialDataCheckController.cs | 9 +--- TaikoLocalServer/Entities/UserDatum.cs | 10 ++-- TaikoLocalServer/Program.cs | 2 +- .../Services/Interfaces/IUserDatumService.cs | 2 + TaikoLocalServer/TaikoLocalServer.csproj | 4 +- .../wwwroot/{ => data}/dan_data.json | 0 TaikoWebUI/Pages/Profile.razor | 25 +++++++++ 14 files changed, 165 insertions(+), 51 deletions(-) create mode 100644 TaikoLocalServer/Common/Utils/JsonHelper.cs rename TaikoLocalServer/wwwroot/{ => data}/dan_data.json (100%) diff --git a/SharedProject/Models/UserSetting.cs b/SharedProject/Models/UserSetting.cs index 6020490..50808a4 100644 --- a/SharedProject/Models/UserSetting.cs +++ b/SharedProject/Models/UserSetting.cs @@ -20,9 +20,36 @@ public class UserSetting public int NotesPosition { get; set; } - public string MyDonName { get; set; } = String.Empty; + public string MyDonName { get; set; } = string.Empty; - public string Title { get; set; } = String.Empty; + public string Title { get; set; } = string.Empty; public uint TitlePlateId { get; set; } + + public uint Kigurumi { get; set; } + + public uint Head { get; set; } + + public uint Body { get; set; } + + public uint Face { get; set; } + + public uint Puchi { get; set; } + + public List UnlockedKigurumi { get; set; } = new(); + + public List UnlockedHead { get; set; } = new(); + + public List UnlockedBody { get; set; } = new(); + + public List UnlockedFace { get; set; } = new(); + + public List UnlockedPuchi { get; set; } = new(); + + public uint FaceColor { get; set; } + + public uint BodyColor { get; set; } + + public uint LimbColor { get; set; } + } \ No newline at end of file diff --git a/TaikoLocalServer.sln.DotSettings b/TaikoLocalServer.sln.DotSettings index eea663e..abd12a3 100644 --- a/TaikoLocalServer.sln.DotSettings +++ b/TaikoLocalServer.sln.DotSettings @@ -1,4 +1,6 @@  + True True True + True True \ No newline at end of file diff --git a/TaikoLocalServer/Common/Utils/JsonHelper.cs b/TaikoLocalServer/Common/Utils/JsonHelper.cs new file mode 100644 index 0000000..d409045 --- /dev/null +++ b/TaikoLocalServer/Common/Utils/JsonHelper.cs @@ -0,0 +1,52 @@ +using System.Text.Json; + +namespace TaikoLocalServer.Common.Utils; + +public static class JsonHelper +{ + public static List GetCostumeDataFromUserData(UserDatum userData, ILogger logger) + { + var costumeData = new List { 0, 0, 0, 0, 0 }; + try + { + costumeData = JsonSerializer.Deserialize>(userData.CostumeData); + } + catch (JsonException e) + { + logger.LogError(e, "Parsing costume json data failed"); + } + + if (costumeData != null && costumeData.Count >= 5) + { + return costumeData; + } + + logger.LogWarning("Costume data is null or count less than 5!"); + costumeData = new List { 0, 0, 0, 0, 0 }; + + return costumeData; + } + + public static List> GetCostumeUnlockDataFromUserData(UserDatum userData, ILogger logger) + { + var costumeUnlockData = new List> { new(), new(), new(), new(), new() }; + try + { + costumeUnlockData = JsonSerializer.Deserialize>>(userData.CostumeFlgArray); + } + catch (JsonException e) + { + logger.LogError(e, "Parsing costume json data failed"); + } + + if (costumeUnlockData != null && costumeUnlockData.Count >= 5) + { + return costumeUnlockData; + } + + logger.LogWarning("Costume unlock data is null or count less than 5!"); + costumeUnlockData = new List> { new(), new(), new(), new(), new() }; + + return costumeUnlockData; + } +} \ No newline at end of file diff --git a/TaikoLocalServer/Common/Utils/PathHelper.cs b/TaikoLocalServer/Common/Utils/PathHelper.cs index 152d29f..9073fdc 100644 --- a/TaikoLocalServer/Common/Utils/PathHelper.cs +++ b/TaikoLocalServer/Common/Utils/PathHelper.cs @@ -2,7 +2,7 @@ public static class PathHelper { - public static string GetDataPath() + public static string GetRootPath() { var path = Environment.ProcessPath; if (path is null) @@ -14,6 +14,11 @@ public static class PathHelper { throw new ApplicationException(); } - return Path.Combine(parentPath.ToString(), "wwwroot", "data"); + return Path.Combine(parentPath.ToString(), "wwwroot"); + } + + public static string GetDataPath() + { + return Path.Combine(GetRootPath(), "data"); } } \ No newline at end of file diff --git a/TaikoLocalServer/Context/TaikoDbContext.cs b/TaikoLocalServer/Context/TaikoDbContext.cs index f6c80cb..69d41ae 100644 --- a/TaikoLocalServer/Context/TaikoDbContext.cs +++ b/TaikoLocalServer/Context/TaikoDbContext.cs @@ -22,7 +22,7 @@ { return; } - var path = Path.Combine(PathHelper.GetDataPath(), Constants.DEFAULT_DB_NAME); + var path = Path.Combine(PathHelper.GetRootPath(), Constants.DEFAULT_DB_NAME); optionsBuilder.UseSqlite($"Data Source={path}"); } diff --git a/TaikoLocalServer/Controllers/Api/UserSettingsController.cs b/TaikoLocalServer/Controllers/Api/UserSettingsController.cs index 43ac82b..40feec2 100644 --- a/TaikoLocalServer/Controllers/Api/UserSettingsController.cs +++ b/TaikoLocalServer/Controllers/Api/UserSettingsController.cs @@ -1,9 +1,11 @@ using System.Buffers.Binary; +using System.Text.Json; using SharedProject.Models; using SharedProject.Models.Responses; using SharedProject.Utils; using TaikoLocalServer.Services; using TaikoLocalServer.Services.Interfaces; +using Throw; namespace TaikoLocalServer.Controllers.Api; @@ -28,6 +30,10 @@ public class UserSettingsController : BaseController return NotFound(); } + var costumeData = JsonHelper.GetCostumeDataFromUserData(user, Logger); + + var costumeUnlockData = JsonHelper.GetCostumeUnlockDataFromUserData(user, Logger); + var response = new UserSetting { AchievementDisplayDifficulty = user.AchievementDisplayDifficulty, @@ -40,7 +46,20 @@ public class UserSettingsController : BaseController ToneId = user.SelectedToneId, MyDonName = user.MyDonName, Title = user.Title, - TitlePlateId = user.TitlePlateId + TitlePlateId = user.TitlePlateId, + Kigurumi = costumeData[0], + Head = costumeData[1], + Body = costumeData[2], + Face = costumeData[3], + Puchi = costumeData[4], + UnlockedKigurumi = costumeUnlockData[0], + UnlockedHead = costumeUnlockData[1], + UnlockedBody = costumeUnlockData[2], + UnlockedFace = costumeUnlockData[3], + UnlockedPuchi = costumeUnlockData[4], + BodyColor = user.ColorBody, + FaceColor = user.ColorFace, + LimbColor = user.ColorLimb }; return Ok(response); } @@ -55,6 +74,15 @@ public class UserSettingsController : BaseController return NotFound(); } + var costumes = new List + { + userSetting.Kigurumi, + userSetting.Head, + userSetting.Body, + userSetting.Face, + userSetting.Puchi, + }; + user.IsSkipOn = userSetting.IsSkipOn; user.IsVoiceOn = userSetting.IsVoiceOn; user.DisplayAchievement = userSetting.IsDisplayAchievement; @@ -66,6 +94,11 @@ public class UserSettingsController : BaseController user.MyDonName = userSetting.MyDonName; user.Title = userSetting.Title; user.TitlePlateId = userSetting.TitlePlateId; + user.ColorBody = userSetting.BodyColor; + user.ColorFace = userSetting.FaceColor; + user.ColorLimb = userSetting.LimbColor; + user.CostumeData = JsonSerializer.Serialize(costumes); + await userDatumService.UpdateUserDatum(user); diff --git a/TaikoLocalServer/Controllers/Game/BaidController.cs b/TaikoLocalServer/Controllers/Game/BaidController.cs index 3ffd832..32b98bd 100644 --- a/TaikoLocalServer/Controllers/Game/BaidController.cs +++ b/TaikoLocalServer/Controllers/Game/BaidController.cs @@ -1,6 +1,4 @@ -using System.Text.Json; -using TaikoLocalServer.Services.Interfaces; -using Throw; +using TaikoLocalServer.Services.Interfaces; namespace TaikoLocalServer.Controllers.Game; @@ -79,34 +77,9 @@ public class BaidController : BaseController var scoreRankCount = CalculateScoreRankCount(songCountData); - var costumeData = new List{ 0, 0, 0, 0, 0 }; - try - { - costumeData = JsonSerializer.Deserialize>(userData.CostumeData); - } - catch (JsonException e) - { - Logger.LogError(e, "Parsing costume json data failed"); - } - if (costumeData == null || costumeData.Count < 5) - { - Logger.LogWarning("Costume data is null or count less than 5!"); - costumeData = new List { 0, 0, 0, 0, 0 }; - } + var costumeData = JsonHelper.GetCostumeDataFromUserData(userData, Logger); - var costumeArrays = Array.Empty(); - try - { - costumeArrays = JsonSerializer.Deserialize(userData.CostumeFlgArray); - } - catch (JsonException e) - { - Logger.LogError(e, "Parsing costume flg json data failed"); - } - - // The only way to get a null is provide string "null" as input, - // which means database content need to be fixed, so better throw - costumeArrays.ThrowIfNull("Costume flg should never be null!"); + var costumeArrays = JsonHelper.GetCostumeUnlockDataFromUserData(userData, Logger); var costumeFlagArrays = Constants.CostumeFlagArraySizes .Select((size, index) => FlagCalculator.GetBitArrayFromIds(costumeArrays[index], size, Logger)) diff --git a/TaikoLocalServer/Controllers/Game/InitialDataCheckController.cs b/TaikoLocalServer/Controllers/Game/InitialDataCheckController.cs index 1693e5c..3e1a129 100644 --- a/TaikoLocalServer/Controllers/Game/InitialDataCheckController.cs +++ b/TaikoLocalServer/Controllers/Game/InitialDataCheckController.cs @@ -14,13 +14,8 @@ public class InitialDataCheckController : BaseController(); for (var danId = Constants.MIN_DAN_ID; danId <= Constants.MAX_DAN_ID; danId++) diff --git a/TaikoLocalServer/Entities/UserDatum.cs b/TaikoLocalServer/Entities/UserDatum.cs index 89be015..cfd98b6 100644 --- a/TaikoLocalServer/Entities/UserDatum.cs +++ b/TaikoLocalServer/Entities/UserDatum.cs @@ -6,10 +6,10 @@ public string MyDonName { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; public uint TitlePlateId { get; set; } - public string FavoriteSongsArray { get; set; } = string.Empty; - public string ToneFlgArray { get; set; } = string.Empty; - public string TitleFlgArray { get; set; } = string.Empty; - public string CostumeFlgArray { get; set; } = string.Empty; + public string FavoriteSongsArray { get; set; } = "[]"; + public string ToneFlgArray { get; set; } = "[]"; + public string TitleFlgArray { get; set; } = "[]"; + public string CostumeFlgArray { get; set; } = "[]"; public short OptionSetting { get; set; } public int NotesPosition { get; set; } public bool IsVoiceOn { get; set; } @@ -20,7 +20,7 @@ public uint ColorBody { get; set; } public uint ColorFace { get; set; } public uint ColorLimb { get; set; } - public string CostumeData { get; set; } = string.Empty; + public string CostumeData { get; set; } = "[[],[],[],[],[]]"; public bool DisplayDan { get; set; } public bool DisplayAchievement { get; set; } public Difficulty AchievementDisplayDifficulty { get; set; } diff --git a/TaikoLocalServer/Program.cs b/TaikoLocalServer/Program.cs index a0cb9bc..e12a919 100644 --- a/TaikoLocalServer/Program.cs +++ b/TaikoLocalServer/Program.cs @@ -28,7 +28,7 @@ builder.Services.AddDbContext(option => { dbName = Constants.DEFAULT_DB_NAME; } - var path = Path.Combine(PathHelper.GetDataPath(), dbName); + var path = Path.Combine(PathHelper.GetRootPath(), dbName); option.UseSqlite($"Data Source={path}"); }); builder.Services.AddHttpLogging(options => diff --git a/TaikoLocalServer/Services/Interfaces/IUserDatumService.cs b/TaikoLocalServer/Services/Interfaces/IUserDatumService.cs index c78d592..25d811c 100644 --- a/TaikoLocalServer/Services/Interfaces/IUserDatumService.cs +++ b/TaikoLocalServer/Services/Interfaces/IUserDatumService.cs @@ -17,4 +17,6 @@ public interface IUserDatumService public Task> GetFavoriteSongIds(uint baid); public Task UpdateFavoriteSong(uint baid, uint songId, bool isFavorite); + + } \ No newline at end of file diff --git a/TaikoLocalServer/TaikoLocalServer.csproj b/TaikoLocalServer/TaikoLocalServer.csproj index fd477fb..a2bcdb8 100644 --- a/TaikoLocalServer/TaikoLocalServer.csproj +++ b/TaikoLocalServer/TaikoLocalServer.csproj @@ -35,10 +35,10 @@ - + PreserveNewest - + PreserveNewest diff --git a/TaikoLocalServer/wwwroot/dan_data.json b/TaikoLocalServer/wwwroot/data/dan_data.json similarity index 100% rename from TaikoLocalServer/wwwroot/dan_data.json rename to TaikoLocalServer/wwwroot/data/dan_data.json diff --git a/TaikoWebUI/Pages/Profile.razor b/TaikoWebUI/Pages/Profile.razor index a70d29e..f898932 100644 --- a/TaikoWebUI/Pages/Profile.razor +++ b/TaikoWebUI/Pages/Profile.razor @@ -90,6 +90,31 @@ + + + + Costume Options + + + + + + + + + + + + + + + + + + + + +