the power of linq

This commit is contained in:
Jorolf
2017-04-11 18:43:48 +02:00
parent 9026880495
commit d4e5f55091
4 changed files with 28 additions and 50 deletions

View File

@ -42,9 +42,9 @@ namespace osu.Desktop.VisualTests.Tests
StarDifficulty = 5.3f, StarDifficulty = 5.3f,
Metric = new BeatmapMetric Metric = new BeatmapMetric
{ {
Ratings = Enumerable.Range(0,10).ToList(), Ratings = Enumerable.Range(0,10),
Fails = Enumerable.Range(lastRange, 100).Select(i => i % 12 - 6).ToList(), Fails = Enumerable.Range(lastRange, 100).Select(i => i % 12 - 6),
Retries = Enumerable.Range(lastRange - 3, 100).Select(i => i % 12 - 6).ToList(), Retries = Enumerable.Range(lastRange - 3, 100).Select(i => i % 12 - 6),
}, },
}, },
}); });
@ -56,29 +56,9 @@ namespace osu.Desktop.VisualTests.Tests
private void newRetryAndFailValues() private void newRetryAndFailValues()
{ {
details.Beatmap = new BeatmapInfo details.Beatmap.Metric.Fails = Enumerable.Range(lastRange, 100).Select(i => i % 12 - 6);
{ details.Beatmap.Metric.Retries = Enumerable.Range(lastRange - 3, 100).Select(i => i % 12 - 6);
Version = "VisualTest", details.Beatmap = details.Beatmap;
Metadata = new BeatmapMetadata
{
Source = "Some guy",
Tags = "beatmap metadata example with a very very long list of tags and not much creativity",
},
Difficulty = new BeatmapDifficulty
{
CircleSize = 7,
ApproachRate = 3.5f,
OverallDifficulty = 5.7f,
DrainRate = 1,
},
StarDifficulty = 5.3f,
Metric = new BeatmapMetric
{
Ratings = Enumerable.Range(0, 10).ToList(),
Fails = Enumerable.Range(lastRange, 100).Select(i => i % 12 - 6).ToList(),
Retries = Enumerable.Range(lastRange - 3, 100).Select(i => i % 12 - 6).ToList(),
},
};
lastRange += 100; lastRange += 100;
} }
} }

View File

@ -10,16 +10,16 @@ namespace osu.Game.Database
/// <summary> /// <summary>
/// Ratings for a beatmap, length should be 10 /// Ratings for a beatmap, length should be 10
/// </summary> /// </summary>
public List<int> Ratings { get; set; } public IEnumerable<int> Ratings { get; set; }
/// <summary> /// <summary>
/// Fails for a beatmap, length should be 100 /// Fails for a beatmap, length should be 100
/// </summary> /// </summary>
public List<int> Fails { get; set; } public IEnumerable<int> Fails { get; set; }
/// <summary> /// <summary>
/// Retries for a beatmap, length should be 100 /// Retries for a beatmap, length should be 100
/// </summary> /// </summary>
public List<int> Retries { get; set; } public IEnumerable<int> Retries { get; set; }
} }
} }

View File

@ -42,23 +42,22 @@ namespace osu.Game.Graphics.UserInterface
{ {
set set
{ {
List<float> values = value?.ToList() ?? new List<float>(); List<Bar> bars = Children.ToList();
List<Bar> graphBars = Children.ToList(); foreach (var bar in value.Select((length, index) => new { Value = length, Bar = bars.Count > index ? bars[index] : null }))
for (int i = 0; i < values.Count; i++) if (bar.Bar != null)
if (graphBars.Count > i)
{ {
graphBars[i].Length = values[i] / (MaxValue ?? values.Max()); bar.Bar.Length = bar.Value / (MaxValue ?? value.Max());
graphBars[i].Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / values.Count) : new Vector2(1.0f / values.Count, 1); bar.Bar.Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / value.Count()) : new Vector2(1.0f / value.Count(), 1);
} }
else else
Add(new Bar Add(new Bar
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / values.Count) : new Vector2(1.0f / values.Count, 1), Size = (direction & BarDirection.Horizontal) > 0 ? new Vector2(1, 1.0f / value.Count()) : new Vector2(1.0f / value.Count(), 1),
Length = values[i] / (MaxValue ?? values.Max()), Length = bar.Value / (MaxValue ?? value.Max()),
Direction = Direction, Direction = Direction,
}); });
Remove(Children.Where((bar, index) => index >= values.Count).ToList()); Remove(Children.Where((bar, index) => index >= value.Count()).ToList());
} }
} }
} }

View File

@ -49,7 +49,6 @@ namespace osu.Game.Screens.Select
} }
set set
{ {
if (beatmap == value) return;
beatmap = value; beatmap = value;
description.Text = beatmap.Version; description.Text = beatmap.Version;
@ -62,11 +61,9 @@ namespace osu.Game.Screens.Select
approachRate.Value = beatmap.Difficulty.ApproachRate; approachRate.Value = beatmap.Difficulty.ApproachRate;
stars.Value = (float)beatmap.StarDifficulty; stars.Value = (float)beatmap.StarDifficulty;
if (beatmap.Metric?.Ratings == null) if (beatmap.Metric?.Ratings.Count() > 0)
ratingsContainer.Hide();
else
{ {
List<int> ratings = beatmap.Metric.Ratings; List<int> ratings = beatmap.Metric.Ratings.ToList();
ratingsContainer.Show(); ratingsContainer.Show();
negativeRatings.Text = ratings.GetRange(0, 5).Sum().ToString(); negativeRatings.Text = ratings.GetRange(0, 5).Sum().ToString();
@ -75,22 +72,24 @@ namespace osu.Game.Screens.Select
ratingsGraph.Values = ratings.Select(rating => (float)rating); ratingsGraph.Values = ratings.Select(rating => (float)rating);
} }
if (beatmap.Metric?.Retries == null || beatmap.Metric?.Fails == null)
retryFailContainer.Hide();
else else
ratingsContainer.Hide();
if (beatmap.Metric?.Retries.Count() > 0 && beatmap.Metric?.Retries.Count() > 0)
{ {
List<int> retries = beatmap.Metric.Retries; IEnumerable<int> retries = beatmap.Metric.Retries;
List<int> fails = beatmap.Metric.Fails; IEnumerable<int> fails = beatmap.Metric.Fails;
retryFailContainer.Show(); retryFailContainer.Show();
float maxValue = fails.Select((fail, index) => fail + retries[index]).Max(); float maxValue = fails.Zip(retries, (fail, retry) => fail + retry).Max();
failGraph.MaxValue = maxValue; failGraph.MaxValue = maxValue;
retryGraph.MaxValue = maxValue; retryGraph.MaxValue = maxValue;
failGraph.Values = fails.Select(fail => (float)fail); failGraph.Values = fails.Select(fail => (float)fail);
retryGraph.Values = retries.Select((retry, index) => retry + MathHelper.Clamp(fails[index], 0, maxValue)); retryGraph.Values = retries.Zip(fails, (retry, fail) => retry + MathHelper.Clamp(fail, 0, maxValue));
} }
else
retryFailContainer.Hide();
} }
} }