From 2c21080e993ebcc1f7706f3855dd220acab1b8db Mon Sep 17 00:00:00 2001 From: asesidaa <1061472754@qq.com> Date: Sun, 28 Aug 2022 00:34:27 +0800 Subject: [PATCH] Try add new logic for recent song selection by add stage number Try refactor best data update Try refactor achievement default computation --- .../Controllers/BaidController.cs | 25 ++++++----- .../Controllers/PlayResultController.cs | 41 ++++++------------- .../Controllers/UserDataController.cs | 14 ++++++- .../Entities/SongBestDatumMethods.cs | 29 +++++++++++++ TaikoLocalServer/Entities/SongPlayDatum.cs | 3 ++ TaikoLocalServer/GlobalUsings.cs | 7 +++- 6 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 TaikoLocalServer/Entities/SongBestDatumMethods.cs diff --git a/TaikoLocalServer/Controllers/BaidController.cs b/TaikoLocalServer/Controllers/BaidController.cs index 3f36bb2..ccd96f9 100644 --- a/TaikoLocalServer/Controllers/BaidController.cs +++ b/TaikoLocalServer/Controllers/BaidController.cs @@ -59,23 +59,26 @@ public class BaidController:ControllerBase userData = context.UserData.First(datum => datum.Baid == baid); } - var achievementDisplayDifficulty = userData.AchievementDisplayDifficulty == Difficulty.None ? - context.SongPlayData.Where(datum => datum.Crown >= CrownType.Clear).Any() ? - context.SongPlayData.Where(datum => datum.Crown >= CrownType.Clear).Max(datum => datum.Difficulty) : - Difficulty.Easy : userData.AchievementDisplayDifficulty; + var songBestData = context.SongBestData.Where(datum => datum.Baid == baid).ToList(); + + var achievementDisplayDifficulty = userData.AchievementDisplayDifficulty; + if (userData.AchievementDisplayDifficulty == Difficulty.None) + { + achievementDisplayDifficulty = songBestData.Any(datum => datum.BestCrown >= CrownType.Clear) ? + songBestData.Where(datum => datum.BestCrown >= CrownType.Clear).Max(datum => datum.Difficulty) : + Difficulty.Easy; + } - var songBestData = context.SongBestData - .Where(datum => datum.Baid == baid && - achievementDisplayDifficulty != Difficulty.UraOni ? - datum.Difficulty == achievementDisplayDifficulty : - datum.Difficulty == Difficulty.Oni || datum.Difficulty == Difficulty.UraOni); + var songCountData = songBestData.Where(datum => achievementDisplayDifficulty != Difficulty.UraOni ? + datum.Difficulty == achievementDisplayDifficulty : + datum.Difficulty is Difficulty.Oni or Difficulty.UraOni).ToList(); var crownCount = new uint[3]; foreach (var crownType in Enum.GetValues()) { if (crownType != CrownType.None) { - crownCount[(int)crownType - 1] = (uint)songBestData.Count(datum => datum.BestCrown == crownType); + crownCount[(int)crownType - 1] = (uint)songCountData.Count(datum => datum.BestCrown == crownType); } } @@ -84,7 +87,7 @@ public class BaidController:ControllerBase { if (scoreRankType != ScoreRank.None) { - scoreRankCount[(int)scoreRankType - 2] = (uint)songBestData.Count(datum => datum.BestScoreRank == scoreRankType); + scoreRankCount[(int)scoreRankType - 2] = (uint)songCountData.Count(datum => datum.BestScoreRank == scoreRankType); } } diff --git a/TaikoLocalServer/Controllers/PlayResultController.cs b/TaikoLocalServer/Controllers/PlayResultController.cs index 5088d39..ec72a5b 100644 --- a/TaikoLocalServer/Controllers/PlayResultController.cs +++ b/TaikoLocalServer/Controllers/PlayResultController.cs @@ -60,25 +60,21 @@ public class PlayResultController : ControllerBase context.UserData.Update(userdata); var bestData = context.SongBestData.Where(datum => datum.Baid == request.BaidConf).ToList(); - foreach (var stageData in playResultData.AryStageInfoes) + for (var songNumber = 0; songNumber < playResultData.AryStageInfoes.Count; songNumber++) { + var stageData = playResultData.AryStageInfoes[songNumber]; var insert = false; var bestDatum = bestData .FirstOrDefault(datum => datum.SongId == stageData.SongNo && datum.Difficulty == (Difficulty)stageData.Level); - var crown = CrownType.None; - if (stageData.PlayResult > 0) + + // Determine whether it is dondaful crown as this is not reflected by play result + var crown = (CrownType)stageData.PlayResult; + if (crown == CrownType.Gold && stageData.OkCnt == 0) { - crown = CrownType.Clear; - if (stageData.NgCnt == 0) - { - crown = CrownType.Gold; - if (stageData.OkCnt == 0) - { - crown = CrownType.Dondaful; - } - } + crown = CrownType.Dondaful; } + if (bestDatum is null) { insert = true; @@ -89,22 +85,8 @@ public class PlayResultController : ControllerBase Difficulty = (Difficulty)stageData.Level }; } - if (bestDatum.BestCrown < crown) - { - bestDatum.BestCrown = crown; - } - if ((uint)bestDatum.BestScoreRank < stageData.ScoreRank) - { - bestDatum.BestScoreRank = (ScoreRank)stageData.ScoreRank; - } - if (bestDatum.BestScore < stageData.PlayScore) - { - bestDatum.BestScore = stageData.PlayScore; - } - if (bestDatum.BestRate < stageData.ScoreRate) - { - bestDatum.BestRate = stageData.ScoreRate; - } + + bestDatum.UpdateBestData(crown, stageData.ScoreRank, stageData.PlayScore, stageData.ScoreRate); if (insert) { @@ -114,10 +96,11 @@ public class PlayResultController : ControllerBase { context.SongBestData.Update(bestDatum); } - + var playData = new SongPlayDatum { Baid = request.BaidConf, + SongNumber = (uint)songNumber, GoodCount = stageData.GoodCnt, OkCount = stageData.OkCnt, MissCount = stageData.NgCnt, diff --git a/TaikoLocalServer/Controllers/UserDataController.cs b/TaikoLocalServer/Controllers/UserDataController.cs index a1920cf..2d28d62 100644 --- a/TaikoLocalServer/Controllers/UserDataController.cs +++ b/TaikoLocalServer/Controllers/UserDataController.cs @@ -19,6 +19,13 @@ public class UserDataController : ControllerBase this.context = context; } + public record PlayTimeOrderTuple(DateTime PlayTime, uint SongNumber) + { + public override string ToString() { + return $"{{ PlayTime = {PlayTime}, SongNumber = {SongNumber} }}"; + } + } + [HttpPost] [Produces("application/protobuf")] public IActionResult GetUserData([FromBody] UserDataRequest request) @@ -49,7 +56,12 @@ public class UserDataController : ControllerBase var recentSongs = context.SongPlayData .Where(datum => datum.Baid == request.Baid) .AsEnumerable() - .Reverse() + .OrderByDescending(datum => new PlayTimeOrderTuple(datum.PlayTime, datum.SongNumber), + Comparer.Create((tuple1, tuple2) => + { + var timeOrder = tuple1.PlayTime.CompareTo(tuple2.PlayTime); + return timeOrder != 0 ? timeOrder : tuple1.SongNumber.CompareTo(tuple2.SongNumber); + })) .DistinctBy(datum => datum.SongId) .Take(10) .Select(datum => datum.SongId) diff --git a/TaikoLocalServer/Entities/SongBestDatumMethods.cs b/TaikoLocalServer/Entities/SongBestDatumMethods.cs new file mode 100644 index 0000000..1b2d37c --- /dev/null +++ b/TaikoLocalServer/Entities/SongBestDatumMethods.cs @@ -0,0 +1,29 @@ +using TaikoLocalServer.Common.Enums; + +namespace TaikoLocalServer.Entities; + +public partial class SongBestDatum +{ + public void UpdateBestData(CrownType crown, uint scoreRank, uint playScore, uint scoreRate) + { + if (BestCrown < crown) + { + BestCrown = crown; + } + + if ((uint)BestScoreRank < scoreRank) + { + BestScoreRank = (ScoreRank)scoreRank; + } + + if (BestScore < playScore) + { + BestScore = playScore; + } + + if (BestRate < scoreRate) + { + BestRate = scoreRate; + } + } +} \ No newline at end of file diff --git a/TaikoLocalServer/Entities/SongPlayDatum.cs b/TaikoLocalServer/Entities/SongPlayDatum.cs index 9844b94..75175af 100644 --- a/TaikoLocalServer/Entities/SongPlayDatum.cs +++ b/TaikoLocalServer/Entities/SongPlayDatum.cs @@ -6,6 +6,9 @@ namespace TaikoLocalServer.Entities { public long Id { get; set; } public uint Baid { get; set; } + + public uint SongNumber { get; set; } + public uint SongId { get; set; } public Difficulty Difficulty { get; set; } public CrownType Crown { get; set; } diff --git a/TaikoLocalServer/GlobalUsings.cs b/TaikoLocalServer/GlobalUsings.cs index ce02804..bc31ea4 100644 --- a/TaikoLocalServer/GlobalUsings.cs +++ b/TaikoLocalServer/GlobalUsings.cs @@ -1,4 +1,9 @@ // Global using directives global using Microsoft.AspNetCore.Mvc; global using Swan.Formatters; -global using taiko.game; \ No newline at end of file +global using taiko.game; +global using TaikoLocalServer.Common; +global using TaikoLocalServer.Common.Enums; +global using TaikoLocalServer.Context; +global using TaikoLocalServer.Entities; +global using TaikoLocalServer.Utils; \ No newline at end of file