From 57170501cdadfa786d8d59bd67efa4f45a9ce81a Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Sat, 4 Nov 2023 17:25:09 +0200 Subject: [PATCH] Improve code quality --- osu.Game.Rulesets.Catch/CatchRuleset.cs | 31 +++------------- osu.Game.Rulesets.Mania/ManiaRuleset.cs | 30 +++------------- osu.Game.Rulesets.Osu/OsuRuleset.cs | 33 +++-------------- osu.Game.Rulesets.Taiko/TaikoRuleset.cs | 30 +++------------- .../Overlays/Mods/BeatmapAttributesDisplay.cs | 20 ++++++----- .../Overlays/Mods/VerticalAttributeDisplay.cs | 33 ++++++++++++----- osu.Game/Rulesets/Ruleset.cs | 17 +-------- .../Screens/Select/Details/AdvancedStats.cs | 35 ++++++++++++------- 8 files changed, 76 insertions(+), 153 deletions(-) diff --git a/osu.Game.Rulesets.Catch/CatchRuleset.cs b/osu.Game.Rulesets.Catch/CatchRuleset.cs index f0b20c68c4..2d0e34431d 100644 --- a/osu.Game.Rulesets.Catch/CatchRuleset.cs +++ b/osu.Game.Rulesets.Catch/CatchRuleset.cs @@ -238,37 +238,14 @@ namespace osu.Game.Rulesets.Catch public float ArFromPreempt(double preempt) => (float)(preempt > 1200 ? ((1800 - preempt) / 120) : ((1200 - preempt) / 150)) + 5; public float ChangeArFromRate(float AR, double rate) => ArFromPreempt(PreemptFromAr(AR) / rate); - public override BeatmapDifficulty GetEffectiveDifficulty(IBeatmapDifficultyInfo baseDifficulty, IReadOnlyList mods, ref (RateAdjustType AR, RateAdjustType OD) rateAdjustedInfo) + public override BeatmapDifficulty GetRateAdjustedDifficulty(IBeatmapDifficultyInfo baseDifficulty, double rate) { - BeatmapDifficulty? adjustedDifficulty = null; - rateAdjustedInfo = (RateAdjustType.NotChanged, RateAdjustType.NotChanged); + BeatmapDifficulty adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - if (mods.Any(m => m is IApplicableToDifficulty)) - { - adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - mod.ApplyToDifficulty(adjustedDifficulty); - } - - if (mods.Any(m => m is ModRateAdjust)) - { - adjustedDifficulty ??= new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - { - double speedChange = (float)mod.SpeedChange.Value; - - float ar = adjustedDifficulty.ApproachRate; - float od = adjustedDifficulty.OverallDifficulty; - - adjustedDifficulty.ApproachRate = ChangeArFromRate(ar, speedChange); - - rateAdjustedInfo.AR = GetRateAdjustType(ar, adjustedDifficulty.ApproachRate); - } - } + adjustedDifficulty.ApproachRate = ChangeArFromRate(adjustedDifficulty.ApproachRate, rate); return adjustedDifficulty ?? (BeatmapDifficulty)baseDifficulty; } + } } diff --git a/osu.Game.Rulesets.Mania/ManiaRuleset.cs b/osu.Game.Rulesets.Mania/ManiaRuleset.cs index a8a54ebf05..cfdfcbadee 100644 --- a/osu.Game.Rulesets.Mania/ManiaRuleset.cs +++ b/osu.Game.Rulesets.Mania/ManiaRuleset.cs @@ -431,34 +431,12 @@ namespace osu.Game.Rulesets.Mania public double HitwindowFromOd(float OD) => 64.0 - 3 * OD; public float OdFromHitwindow(double hitwindow300) => (float)(64.0 - hitwindow300) / 3; public float ChangeOdFromRate(float OD, double rate) => OdFromHitwindow(HitwindowFromOd(OD) / rate); - public override BeatmapDifficulty GetEffectiveDifficulty(IBeatmapDifficultyInfo baseDifficulty, IReadOnlyList mods, ref (RateAdjustType AR, RateAdjustType OD) rateAdjustedInfo) + + public override BeatmapDifficulty GetRateAdjustedDifficulty(IBeatmapDifficultyInfo baseDifficulty, double rate) { - BeatmapDifficulty? adjustedDifficulty = null; - rateAdjustedInfo = (RateAdjustType.NotChanged, RateAdjustType.NotChanged); + BeatmapDifficulty adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - if (mods.Any(m => m is IApplicableToDifficulty)) - { - adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - mod.ApplyToDifficulty(adjustedDifficulty); - } - - if (mods.Any(m => m is ModRateAdjust)) - { - adjustedDifficulty ??= new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - { - double speedChange = (float)mod.SpeedChange.Value; - - float od = adjustedDifficulty.OverallDifficulty; - - adjustedDifficulty.OverallDifficulty = ChangeOdFromRate(od, speedChange); - - rateAdjustedInfo.OD = GetRateAdjustType(od, adjustedDifficulty.OverallDifficulty); - } - } + adjustedDifficulty.OverallDifficulty = ChangeOdFromRate(adjustedDifficulty.OverallDifficulty, rate); return adjustedDifficulty ?? (BeatmapDifficulty)baseDifficulty; } diff --git a/osu.Game.Rulesets.Osu/OsuRuleset.cs b/osu.Game.Rulesets.Osu/OsuRuleset.cs index 4810be5db2..3a8f589c9f 100644 --- a/osu.Game.Rulesets.Osu/OsuRuleset.cs +++ b/osu.Game.Rulesets.Osu/OsuRuleset.cs @@ -335,37 +335,12 @@ namespace osu.Game.Rulesets.Osu public float OdFromHitwindow(double hitwindow300) => (float)(80.0 - hitwindow300) / 6; public float ChangeArFromRate(float AR, double rate) => ArFromPreempt(PreemptFromAr(AR) / rate); public float ChangeOdFromRate(float OD, double rate) => OdFromHitwindow(HitwindowFromOd(OD) / rate); - public override BeatmapDifficulty GetEffectiveDifficulty(IBeatmapDifficultyInfo baseDifficulty, IReadOnlyList mods, ref (RateAdjustType AR, RateAdjustType OD) rateAdjustedInfo) + public override BeatmapDifficulty GetRateAdjustedDifficulty(IBeatmapDifficultyInfo baseDifficulty, double rate) { - BeatmapDifficulty? adjustedDifficulty = null; - rateAdjustedInfo = (RateAdjustType.NotChanged, RateAdjustType.NotChanged); + BeatmapDifficulty adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - if (mods.Any(m => m is IApplicableToDifficulty)) - { - adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - mod.ApplyToDifficulty(adjustedDifficulty); - } - - if (mods.Any(m => m is ModRateAdjust)) - { - adjustedDifficulty ??= new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - { - double speedChange = (float)mod.SpeedChange.Value; - - float ar = adjustedDifficulty.ApproachRate; - float od = adjustedDifficulty.OverallDifficulty; - - adjustedDifficulty.ApproachRate = ChangeArFromRate(ar, speedChange); - adjustedDifficulty.OverallDifficulty = ChangeOdFromRate(od, speedChange); - - rateAdjustedInfo.AR = GetRateAdjustType(ar, adjustedDifficulty.ApproachRate); - rateAdjustedInfo.OD = GetRateAdjustType(od, adjustedDifficulty.OverallDifficulty); - } - } + adjustedDifficulty.ApproachRate = ChangeArFromRate(adjustedDifficulty.ApproachRate, rate); + adjustedDifficulty.OverallDifficulty = ChangeOdFromRate(adjustedDifficulty.OverallDifficulty, rate); return adjustedDifficulty ?? (BeatmapDifficulty)baseDifficulty; } diff --git a/osu.Game.Rulesets.Taiko/TaikoRuleset.cs b/osu.Game.Rulesets.Taiko/TaikoRuleset.cs index a9abbea251..026ac4f42e 100644 --- a/osu.Game.Rulesets.Taiko/TaikoRuleset.cs +++ b/osu.Game.Rulesets.Taiko/TaikoRuleset.cs @@ -266,34 +266,12 @@ namespace osu.Game.Rulesets.Taiko public double HitwindowFromOd(float OD) => 35.0 - 15.0 * (OD - 5) / 5; public float OdFromHitwindow(double hitwindow300) => (float)(5 * (35 - hitwindow300) / 15 + 5); public float ChangeOdFromRate(float OD, double rate) => OdFromHitwindow(HitwindowFromOd(OD) / rate); - public override BeatmapDifficulty GetEffectiveDifficulty(IBeatmapDifficultyInfo baseDifficulty, IReadOnlyList mods, ref (RateAdjustType AR, RateAdjustType OD) rateAdjustedInfo) + + public override BeatmapDifficulty GetRateAdjustedDifficulty(IBeatmapDifficultyInfo baseDifficulty, double rate) { - BeatmapDifficulty? adjustedDifficulty = null; - rateAdjustedInfo = (RateAdjustType.NotChanged, RateAdjustType.NotChanged); + BeatmapDifficulty adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - if (mods.Any(m => m is IApplicableToDifficulty)) - { - adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - mod.ApplyToDifficulty(adjustedDifficulty); - } - - if (mods.Any(m => m is ModRateAdjust)) - { - adjustedDifficulty ??= new BeatmapDifficulty(baseDifficulty); - - foreach (var mod in mods.OfType()) - { - double speedChange = (float)mod.SpeedChange.Value; - - float od = adjustedDifficulty.OverallDifficulty; - - adjustedDifficulty.OverallDifficulty = ChangeOdFromRate(od, speedChange); - - rateAdjustedInfo.OD = GetRateAdjustType(od, adjustedDifficulty.OverallDifficulty); - } - } + adjustedDifficulty.OverallDifficulty = ChangeOdFromRate(adjustedDifficulty.OverallDifficulty, rate); return adjustedDifficulty ?? (BeatmapDifficulty)baseDifficulty; } diff --git a/osu.Game/Overlays/Mods/BeatmapAttributesDisplay.cs b/osu.Game/Overlays/Mods/BeatmapAttributesDisplay.cs index d0ee9750b2..fd9ce8f5d9 100644 --- a/osu.Game/Overlays/Mods/BeatmapAttributesDisplay.cs +++ b/osu.Game/Overlays/Mods/BeatmapAttributesDisplay.cs @@ -251,17 +251,21 @@ namespace osu.Game.Overlays.Mods bpmDisplay.Current.Value = BeatmapInfo.Value.BPM * rate; - (Ruleset.RateAdjustType AR, Ruleset.RateAdjustType OD) rateAdjustType = (Ruleset.RateAdjustType.NotChanged, Ruleset.RateAdjustType.NotChanged); + var moddedDifficulty = new BeatmapDifficulty(BeatmapInfo.Value.Difficulty); + + foreach (var mod in mods.Value.OfType()) + mod.ApplyToDifficulty(moddedDifficulty); Ruleset ruleset = gameRuleset.Value.CreateInstance(); - BeatmapDifficulty adjustedDifficulty = ruleset.GetEffectiveDifficulty(BeatmapInfo.Value.Difficulty, mods.Value, ref rateAdjustType); - approachRateDisplay.RateChangeType.Value = rateAdjustType.AR; - overallDifficultyDisplay.RateChangeType.Value = rateAdjustType.OD; + var rateAdjustedDifficulty = ruleset.GetRateAdjustedDifficulty(moddedDifficulty, rate); - circleSizeDisplay.Current.Value = adjustedDifficulty.CircleSize; - drainRateDisplay.Current.Value = adjustedDifficulty.DrainRate; - approachRateDisplay.Current.Value = adjustedDifficulty.ApproachRate; - overallDifficultyDisplay.Current.Value = adjustedDifficulty.OverallDifficulty; + approachRateDisplay.AdjustType.Value = VerticalAttributeDisplay.CalculateEffect(moddedDifficulty.ApproachRate, rateAdjustedDifficulty.ApproachRate); + overallDifficultyDisplay.AdjustType.Value = VerticalAttributeDisplay.CalculateEffect(moddedDifficulty.OverallDifficulty, rateAdjustedDifficulty.OverallDifficulty); + + circleSizeDisplay.Current.Value = rateAdjustedDifficulty.CircleSize; + drainRateDisplay.Current.Value = rateAdjustedDifficulty.DrainRate; + approachRateDisplay.Current.Value = rateAdjustedDifficulty.ApproachRate; + overallDifficultyDisplay.Current.Value = rateAdjustedDifficulty.OverallDifficulty; }); private void updateCollapsedState() diff --git a/osu.Game/Overlays/Mods/VerticalAttributeDisplay.cs b/osu.Game/Overlays/Mods/VerticalAttributeDisplay.cs index 16d75cd448..0b0c49bc1b 100644 --- a/osu.Game/Overlays/Mods/VerticalAttributeDisplay.cs +++ b/osu.Game/Overlays/Mods/VerticalAttributeDisplay.cs @@ -12,7 +12,6 @@ using osu.Framework.Localisation; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; -using osu.Game.Rulesets; using osu.Game.Rulesets.Mods; using osuTK.Graphics; @@ -28,7 +27,7 @@ namespace osu.Game.Overlays.Mods private readonly BindableWithCurrent current = new BindableWithCurrent(); - public Bindable RateChangeType = new Bindable(Ruleset.RateAdjustType.NotChanged); + public Bindable AdjustType = new Bindable(); /// /// Text to display in the top area of the display. @@ -43,22 +42,22 @@ namespace osu.Game.Overlays.Mods private void updateTextColor() { Color4 newColor; - switch (RateChangeType.Value) + switch (AdjustType.Value) { - case Ruleset.RateAdjustType.NotChanged: + case ModEffect.NotChanged: newColor = Color4.White; break; - case Ruleset.RateAdjustType.DifficultyReduction: + case ModEffect.DifficultyReduction: newColor = colours.ForModType(ModType.DifficultyReduction); break; - case Ruleset.RateAdjustType.DifficultyIncrease: + case ModEffect.DifficultyIncrease: newColor = colours.ForModType(ModType.DifficultyIncrease); break; default: - throw new ArgumentOutOfRangeException(nameof(RateChangeType.Value)); + throw new ArgumentOutOfRangeException(nameof(AdjustType.Value)); } text.Colour = newColor; @@ -74,7 +73,7 @@ namespace osu.Game.Overlays.Mods Origin = Anchor.CentreLeft; Anchor = Anchor.CentreLeft; - RateChangeType.BindValueChanged(_ => updateTextColor()); + AdjustType.BindValueChanged(_ => updateTextColor()); InternalChild = new FillFlowContainer { @@ -101,6 +100,24 @@ namespace osu.Game.Overlays.Mods } }; } + + public static ModEffect CalculateEffect(double oldValue, double newValue) + { + if (newValue == oldValue) + return ModEffect.NotChanged; + if (newValue < oldValue) + return ModEffect.DifficultyReduction; + + return ModEffect.DifficultyIncrease; + } + + public enum ModEffect + { + NotChanged, + DifficultyReduction, + DifficultyIncrease, + } + private partial class EffectCounter : RollingCounter { protected override double RollingDuration => 500; diff --git a/osu.Game/Rulesets/Ruleset.cs b/osu.Game/Rulesets/Ruleset.cs index 7a09bff4aa..fbeec5ff4b 100644 --- a/osu.Game/Rulesets/Ruleset.cs +++ b/osu.Game/Rulesets/Ruleset.cs @@ -389,21 +389,6 @@ namespace osu.Game.Rulesets /// Can be overridden to alter the difficulty section to the editor beatmap setup screen. /// public virtual DifficultySection? CreateEditorDifficultySection() => null; - public virtual BeatmapDifficulty GetEffectiveDifficulty(IBeatmapDifficultyInfo baseDifficulty, IReadOnlyList mods, ref (RateAdjustType AR, RateAdjustType OD) rateAdjustedInfo) => (BeatmapDifficulty)baseDifficulty; - public enum RateAdjustType - { - NotChanged, - DifficultyReduction, - DifficultyIncrease - } - - protected RateAdjustType GetRateAdjustType(float baseValue, float adjustedValue) - { - if (adjustedValue > baseValue) - return RateAdjustType.DifficultyIncrease; - if (adjustedValue < baseValue) - return RateAdjustType.DifficultyReduction; - return RateAdjustType.NotChanged; - } + public virtual BeatmapDifficulty GetRateAdjustedDifficulty(IBeatmapDifficultyInfo baseDifficulty, double rate) => new BeatmapDifficulty(baseDifficulty); } } diff --git a/osu.Game/Screens/Select/Details/AdvancedStats.cs b/osu.Game/Screens/Select/Details/AdvancedStats.cs index 867d86a69b..d143e12667 100644 --- a/osu.Game/Screens/Select/Details/AdvancedStats.cs +++ b/osu.Game/Screens/Select/Details/AdvancedStats.cs @@ -24,6 +24,7 @@ using osu.Framework.Utils; using osu.Game.Configuration; using osu.Game.Resources.Localisation.Web; using osu.Game.Rulesets; +using System.Linq; namespace osu.Game.Screens.Select.Details { @@ -116,12 +117,21 @@ namespace osu.Game.Screens.Select.Details { IBeatmapDifficultyInfo baseDifficulty = BeatmapInfo?.Difficulty; BeatmapDifficulty adjustedDifficulty = null; - (Ruleset.RateAdjustType AR, Ruleset.RateAdjustType OD) rateAdjustInfo = (Ruleset.RateAdjustType.NotChanged, Ruleset.RateAdjustType.NotChanged); if (baseDifficulty != null) { - Ruleset ruleset = gameRuleset.Value.CreateInstance(); - adjustedDifficulty = ruleset.GetEffectiveDifficulty(baseDifficulty, mods.Value, ref rateAdjustInfo); + adjustedDifficulty = new BeatmapDifficulty(baseDifficulty); + + foreach (var mod in mods.Value.OfType()) + mod.ApplyToDifficulty(adjustedDifficulty); + + // For now we not using rate adjusted difficulty here + + //Ruleset ruleset = gameRuleset.Value.CreateInstance(); + //double rate = 1; + //foreach (var mod in mods.Value.OfType()) + // rate = mod.ApplyToRate(0, rate); + //adjustedDifficulty = ruleset.GetRateAdjustedDifficulty(adjustedDifficulty, rate); } switch (BeatmapInfo?.Ruleset.OnlineID) @@ -130,18 +140,18 @@ namespace osu.Game.Screens.Select.Details // Account for mania differences locally for now // Eventually this should be handled in a more modular way, allowing rulesets to return arbitrary difficulty attributes FirstValue.Title = BeatmapsetsStrings.ShowStatsCsMania; - FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, null, false); + FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, null); break; default: FirstValue.Title = BeatmapsetsStrings.ShowStatsCs; - FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, adjustedDifficulty?.CircleSize, false); + FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, adjustedDifficulty?.CircleSize); break; } - HpDrain.Value = (baseDifficulty?.DrainRate ?? 0, adjustedDifficulty?.DrainRate, false); - Accuracy.Value = (baseDifficulty?.OverallDifficulty ?? 0, adjustedDifficulty?.OverallDifficulty, rateAdjustInfo.OD != Ruleset.RateAdjustType.NotChanged); - ApproachRate.Value = (baseDifficulty?.ApproachRate ?? 0, adjustedDifficulty?.ApproachRate, rateAdjustInfo.OD != Ruleset.RateAdjustType.NotChanged); + HpDrain.Value = (baseDifficulty?.DrainRate ?? 0, adjustedDifficulty?.DrainRate); + Accuracy.Value = (baseDifficulty?.OverallDifficulty ?? 0, adjustedDifficulty?.OverallDifficulty); + ApproachRate.Value = (baseDifficulty?.ApproachRate ?? 0, adjustedDifficulty?.ApproachRate); updateStarDifficulty(); } @@ -175,7 +185,7 @@ namespace osu.Game.Screens.Select.Details if (normalDifficulty == null || moddedDifficulty == null) return; - starDifficulty.Value = ((float)normalDifficulty.Value.Stars, (float)moddedDifficulty.Value.Stars, false); + starDifficulty.Value = ((float)normalDifficulty.Value.Stars, (float)moddedDifficulty.Value.Stars); }), starDifficultyCancellationSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current); }); @@ -206,11 +216,11 @@ namespace osu.Game.Screens.Select.Details set => name.Text = value; } - private (float baseValue, float? adjustedValue, bool isRateAdjusted)? value; + private (float baseValue, float? adjustedValue)? value; - public (float baseValue, float? adjustedValue, bool isRateAdjusted) Value + public (float baseValue, float? adjustedValue) Value { - get => value ?? (0, null, false); + get => value ?? (0, null); set { if (value == this.value) @@ -221,7 +231,6 @@ namespace osu.Game.Screens.Select.Details bar.Length = value.baseValue / maxValue; valueText.Text = (value.adjustedValue ?? value.baseValue).ToString(forceDecimalPlaces ? "0.00" : "0.##"); - if (value.isRateAdjusted) valueText.Text += "*"; ModBar.Length = (value.adjustedValue ?? 0) / maxValue; if (Precision.AlmostEquals(value.baseValue, value.adjustedValue ?? value.baseValue, 0.05f))