// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Game.Beatmaps.Timing; using osu.Game.Rulesets.Objects; using System.Collections.Generic; using System.Linq; using osu.Game.Beatmaps.ControlPoints; using Newtonsoft.Json; using osu.Game.IO.Serialization.Converters; namespace osu.Game.Beatmaps { /// /// A Beatmap containing converted HitObjects. /// public class Beatmap : IBeatmap where T : HitObject { private BeatmapDifficulty difficulty = new BeatmapDifficulty(); public BeatmapDifficulty Difficulty { get => difficulty; set { difficulty = value; if (beatmapInfo != null) beatmapInfo.BaseDifficulty = difficulty.Clone(); } } private BeatmapInfo beatmapInfo; public BeatmapInfo BeatmapInfo { get => beatmapInfo; set { beatmapInfo = value; if (beatmapInfo?.BaseDifficulty != null) Difficulty = beatmapInfo.BaseDifficulty.Clone(); } } public Beatmap() { beatmapInfo = new BeatmapInfo { Metadata = new BeatmapMetadata { Artist = @"Unknown", Title = @"Unknown", AuthorString = @"Unknown Creator", }, DifficultyName = @"Normal", BaseDifficulty = Difficulty, }; } [JsonIgnore] public BeatmapMetadata Metadata => BeatmapInfo.Metadata; public ControlPointInfo ControlPointInfo { get; set; } = new ControlPointInfo(); public List Breaks { get; set; } = new List(); [JsonIgnore] public double TotalBreakTime => Breaks.Sum(b => b.Duration); [JsonConverter(typeof(TypedListConverter))] public List HitObjects { get; set; } = new List(); IReadOnlyList IBeatmap.HitObjects => HitObjects; IReadOnlyList IBeatmap.HitObjects => HitObjects; public virtual IEnumerable GetStatistics() => Enumerable.Empty(); public double GetMostCommonBeatLength() { // The last playable time in the beatmap - the last timing point extends to this time. // Note: This is more accurate and may present different results because osu-stable didn't have the ability to calculate slider durations in this context. double lastTime = HitObjects.LastOrDefault()?.GetEndTime() ?? ControlPointInfo.TimingPoints.LastOrDefault()?.Time ?? 0; var mostCommon = // Construct a set of (beatLength, duration) tuples for each individual timing point. ControlPointInfo.TimingPoints.Select((t, i) => { if (t.Time > lastTime) return (beatLength: t.BeatLength, 0); // osu-stable forced the first control point to start at 0. // This is reproduced here to maintain compatibility around osu!mania scroll speed and song select display. double currentTime = i == 0 ? 0 : t.Time; double nextTime = i == ControlPointInfo.TimingPoints.Count - 1 ? lastTime : ControlPointInfo.TimingPoints[i + 1].Time; return (beatLength: t.BeatLength, duration: nextTime - currentTime); }) // Aggregate durations into a set of (beatLength, duration) tuples for each beat length .GroupBy(t => Math.Round(t.beatLength * 1000) / 1000) .Select(g => (beatLength: g.Key, duration: g.Sum(t => t.duration))) // Get the most common one, or 0 as a suitable default .OrderByDescending(i => i.duration).FirstOrDefault(); return mostCommon.beatLength; } IBeatmap IBeatmap.Clone() => Clone(); public Beatmap Clone() => (Beatmap)MemberwiseClone(); } public class Beatmap : Beatmap { public new Beatmap Clone() => (Beatmap)base.Clone(); public override string ToString() => BeatmapInfo?.ToString() ?? base.ToString(); } }