1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 09:32:58 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneScoring.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

499 lines
17 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-10-17 22:41:07 +08:00
using System;
using System.Collections.Generic;
2022-10-18 18:01:43 +08:00
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2022-10-18 18:01:43 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
2022-10-17 22:41:07 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2022-10-17 22:41:07 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Beatmaps;
using osu.Game.Rulesets.Osu.Judgements;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Scoring;
2022-10-18 18:01:43 +08:00
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Gameplay
{
public partial class TestSceneScoring : OsuTestScene
{
private GraphContainer graphs = null!;
private SettingsSlider<int> sliderMaxCombo = null!;
2022-10-17 22:41:07 +08:00
private FillFlowContainer legend = null!;
[Test]
public void TestBasic()
{
AddStep("setup tests", () =>
{
Children = new Drawable[]
{
new GridContainer
{
RelativeSizeAxes = Axes.Both,
2022-10-18 15:50:30 +08:00
RowDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.AutoSize),
},
Content = new[]
{
new Drawable[]
{
graphs = new GraphContainer
{
2022-10-18 15:50:30 +08:00
RelativeSizeAxes = Axes.Both,
},
},
new Drawable[]
2022-10-17 22:41:07 +08:00
{
legend = new FillFlowContainer
{
2022-10-18 15:50:30 +08:00
Padding = new MarginPadding(20),
Direction = FillDirection.Full,
2022-10-17 22:41:07 +08:00
RelativeSizeAxes = Axes.X,
2022-10-18 15:50:30 +08:00
AutoSizeAxes = Axes.Y,
2022-10-17 22:41:07 +08:00
},
},
new Drawable[]
{
new FillFlowContainer
{
2022-10-18 15:50:30 +08:00
Padding = new MarginPadding(20),
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Full,
Children = new Drawable[]
{
sliderMaxCombo = new SettingsSlider<int>
{
Width = 0.5f,
2022-10-17 22:41:07 +08:00
TransferValueOnCommit = true,
Current = new BindableInt(1024)
{
MinValue = 96,
MaxValue = 8192,
},
LabelText = "max combo",
},
new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
Width = 0.5f,
AutoSizeAxes = Axes.Y,
Text = $"Left click to add miss\nRight click to add OK/{base_ok}"
}
}
},
},
}
}
};
2022-10-17 22:41:07 +08:00
sliderMaxCombo.Current.BindValueChanged(_ => rerun());
graphs.MissLocations.BindCollectionChanged((_, __) => rerun());
graphs.NonPerfectLocations.BindCollectionChanged((_, __) => rerun());
graphs.MaxCombo.BindTo(sliderMaxCombo.Current);
rerun();
});
}
private const int base_great = 300;
private const int base_ok = 100;
2022-10-17 22:41:07 +08:00
private void rerun()
{
graphs.Clear();
2022-10-17 22:41:07 +08:00
legend.Clear();
runForProcessor("lazer-standardised", Color4.YellowGreen, new ScoreProcessor(new OsuRuleset()) { Mode = { Value = ScoringMode.Standardised } });
2022-10-18 16:50:51 +08:00
runForProcessor("lazer-classic", Color4.MediumPurple, new ScoreProcessor(new OsuRuleset()) { Mode = { Value = ScoringMode.Classic } });
2022-10-18 16:23:23 +08:00
runScoreV1();
runScoreV2();
}
private void runScoreV1()
{
2022-10-18 16:23:23 +08:00
int totalScore = 0;
int currentCombo = 0;
void applyHitV1(int baseScore)
2022-10-18 16:23:23 +08:00
{
if (baseScore == 0)
{
currentCombo = 0;
return;
}
2022-10-18 16:23:23 +08:00
const float score_multiplier = 1;
totalScore += baseScore;
2022-10-18 16:23:23 +08:00
// combo multiplier
// ReSharper disable once PossibleLossOfFraction
totalScore += (int)(Math.Max(0, currentCombo - 1) * (baseScore / 25 * score_multiplier));
2022-10-18 16:23:23 +08:00
currentCombo++;
}
runForAlgorithm("ScoreV1 (classic)", Color4.Purple,
() => applyHitV1(base_great),
() => applyHitV1(base_ok),
() => applyHitV1(0),
() =>
{
// Arbitrary value chosen towards the upper range.
const double score_multiplier = 4;
return (int)(totalScore * score_multiplier);
});
}
2022-10-18 16:50:51 +08:00
private void runScoreV2()
{
2022-10-18 16:50:51 +08:00
int maxCombo = sliderMaxCombo.Current.Value;
2022-10-18 17:30:18 +08:00
int currentCombo = 0;
double comboPortion = 0;
2022-10-18 16:50:51 +08:00
double currentBaseScore = 0;
double maxBaseScore = 0;
int currentHits = 0;
for (int i = 0; i < maxCombo; i++)
applyHitV2(base_great);
2022-10-18 16:50:51 +08:00
double comboPortionMax = comboPortion;
2022-10-18 16:50:51 +08:00
2022-10-18 17:30:18 +08:00
currentCombo = 0;
comboPortion = 0;
currentBaseScore = 0;
2022-10-18 17:30:18 +08:00
maxBaseScore = 0;
currentHits = 0;
void applyHitV2(int baseScore)
2022-10-18 16:50:51 +08:00
{
maxBaseScore += base_great;
currentBaseScore += baseScore;
comboPortion += baseScore * (1 + ++currentCombo / 10.0);
2022-10-18 16:50:51 +08:00
currentHits++;
}
2022-10-18 16:50:51 +08:00
runForAlgorithm("ScoreV2", Color4.OrangeRed,
() => applyHitV2(base_great),
() => applyHitV2(base_ok),
() =>
{
currentHits++;
maxBaseScore += base_great;
currentCombo = 0;
}, () =>
{
double accuracy = currentBaseScore / maxBaseScore;
2022-10-18 16:50:51 +08:00
return (int)Math.Round
(
700000 * comboPortion / comboPortionMax +
300000 * Math.Pow(accuracy, 10) * ((double)currentHits / maxCombo)
);
});
}
2022-10-18 16:28:13 +08:00
private void runForProcessor(string name, Color4 colour, ScoreProcessor processor)
{
int maxCombo = sliderMaxCombo.Current.Value;
var beatmap = new OsuBeatmap();
for (int i = 0; i < maxCombo; i++)
beatmap.HitObjects.Add(new HitCircle());
processor.ApplyBeatmap(beatmap);
2022-10-18 16:28:13 +08:00
runForAlgorithm(name, colour,
2022-10-18 16:23:23 +08:00
() => processor.ApplyResult(new OsuJudgementResult(new HitCircle(), new OsuJudgement()) { Type = HitResult.Great }),
() => processor.ApplyResult(new OsuJudgementResult(new HitCircle(), new OsuJudgement()) { Type = HitResult.Ok }),
2022-10-18 16:23:23 +08:00
() => processor.ApplyResult(new OsuJudgementResult(new HitCircle(), new OsuJudgement()) { Type = HitResult.Miss }),
() => (int)processor.TotalScore.Value);
}
private void runForAlgorithm(string name, Color4 colour, Action applyHit, Action applyNonPerfect, Action applyMiss, Func<int> getTotalScore)
2022-10-18 16:23:23 +08:00
{
int maxCombo = sliderMaxCombo.Current.Value;
List<float> results = new List<float>();
for (int i = 0; i < maxCombo; i++)
{
if (graphs.MissLocations.Contains(i))
2022-10-18 16:23:23 +08:00
applyMiss();
else if (graphs.NonPerfectLocations.Contains(i))
applyNonPerfect();
else
2022-10-18 16:23:23 +08:00
applyHit();
2022-10-18 16:23:23 +08:00
results.Add(getTotalScore());
}
graphs.Add(new LineGraph
{
2022-10-18 18:01:43 +08:00
Name = name,
RelativeSizeAxes = Axes.Both,
2022-10-17 22:41:07 +08:00
LineColour = colour,
Values = results
});
2022-10-17 22:41:07 +08:00
legend.Add(new OsuSpriteText
{
Colour = colour,
RelativeSizeAxes = Axes.X,
Width = 0.5f,
2022-10-17 22:41:07 +08:00
Text = $"{FontAwesome.Solid.Circle.Icon} {name}"
});
legend.Add(new OsuSpriteText
{
Colour = colour,
RelativeSizeAxes = Axes.X,
Width = 0.5f,
Text = $"final score {getTotalScore():#,0}"
});
}
}
2022-10-18 18:01:43 +08:00
public partial class GraphContainer : Container, IHasCustomTooltip<IEnumerable<LineGraph>>
{
public readonly BindableList<double> MissLocations = new BindableList<double>();
public readonly BindableList<double> NonPerfectLocations = new BindableList<double>();
public Bindable<int> MaxCombo = new Bindable<int>();
protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };
private readonly Box hoverLine;
private readonly Container missLines;
2022-10-17 22:49:15 +08:00
private readonly Container verticalGridLines;
2022-10-18 18:01:43 +08:00
public int CurrentHoverCombo { get; private set; }
public GraphContainer()
{
InternalChild = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
Colour = OsuColour.Gray(0.1f),
RelativeSizeAxes = Axes.Both,
},
2022-10-17 22:49:15 +08:00
verticalGridLines = new Container
{
RelativeSizeAxes = Axes.Both,
},
hoverLine = new Box
{
Colour = Color4.Yellow,
RelativeSizeAxes = Axes.Y,
Origin = Anchor.TopCentre,
Alpha = 0,
Width = 1,
},
missLines = new Container
{
Alpha = 0.6f,
RelativeSizeAxes = Axes.Both,
},
Content,
}
};
2022-10-17 22:49:15 +08:00
MissLocations.BindCollectionChanged((_, _) => updateMissLocations());
NonPerfectLocations.BindCollectionChanged((_, _) => updateMissLocations());
2022-10-17 22:49:15 +08:00
MaxCombo.BindValueChanged(_ =>
{
updateMissLocations();
updateVerticalGridLines();
}, true);
}
private void updateVerticalGridLines()
{
verticalGridLines.Clear();
2022-10-17 22:49:15 +08:00
for (int i = 0; i < MaxCombo.Value; i++)
{
if (i % 100 == 0)
{
2022-10-18 15:50:30 +08:00
verticalGridLines.AddRange(new Drawable[]
2022-10-17 22:49:15 +08:00
{
2022-10-18 15:50:30 +08:00
new Box
{
Colour = OsuColour.Gray(0.2f),
Origin = Anchor.TopCentre,
2022-10-18 15:50:30 +08:00
Width = 1,
RelativeSizeAxes = Axes.Y,
RelativePositionAxes = Axes.X,
X = (float)i / MaxCombo.Value,
},
new OsuSpriteText
{
RelativePositionAxes = Axes.X,
X = (float)i / MaxCombo.Value,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Text = $"{i:#,0}",
Rotation = -30,
Y = -20,
}
2022-10-17 22:49:15 +08:00
});
}
}
}
private void updateMissLocations()
{
missLines.Clear();
foreach (int miss in MissLocations)
{
missLines.Add(new Box
{
Colour = Color4.Red,
Origin = Anchor.TopCentre,
Width = 1,
RelativeSizeAxes = Axes.Y,
RelativePositionAxes = Axes.X,
X = (float)miss / MaxCombo.Value,
});
}
foreach (int miss in NonPerfectLocations)
{
missLines.Add(new Box
{
Colour = Color4.Orange,
Origin = Anchor.TopCentre,
Width = 1,
RelativeSizeAxes = Axes.Y,
RelativePositionAxes = Axes.X,
X = (float)miss / MaxCombo.Value,
});
}
}
protected override bool OnHover(HoverEvent e)
{
hoverLine.Show();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
hoverLine.Hide();
base.OnHoverLost(e);
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
2022-10-18 18:01:43 +08:00
CurrentHoverCombo = (int)(e.MousePosition.X / DrawWidth * MaxCombo.Value);
hoverLine.X = e.MousePosition.X;
return base.OnMouseMove(e);
}
protected override bool OnMouseDown(MouseDownEvent e)
{
if (e.Button == MouseButton.Left)
2022-10-18 18:01:43 +08:00
MissLocations.Add(CurrentHoverCombo);
else
2022-10-18 18:01:43 +08:00
NonPerfectLocations.Add(CurrentHoverCombo);
return true;
}
2022-10-18 18:01:43 +08:00
private GraphTooltip? tooltip;
public ITooltip<IEnumerable<LineGraph>> GetCustomTooltip() => tooltip ??= new GraphTooltip(this);
public IEnumerable<LineGraph> TooltipContent => Content.OfType<LineGraph>();
public partial class GraphTooltip : CompositeDrawable, ITooltip<IEnumerable<LineGraph>>
{
private readonly GraphContainer graphContainer;
private readonly OsuTextFlowContainer textFlow;
public GraphTooltip(GraphContainer graphContainer)
{
this.graphContainer = graphContainer;
AutoSizeAxes = Axes.Both;
Masking = true;
CornerRadius = 10;
InternalChildren = new Drawable[]
{
new Box
{
Colour = OsuColour.Gray(0.15f),
RelativeSizeAxes = Axes.Both,
},
textFlow = new OsuTextFlowContainer
{
Colour = Color4.White,
AutoSizeAxes = Axes.Both,
Padding = new MarginPadding(10),
}
};
}
private int? lastContentCombo;
public void SetContent(IEnumerable<LineGraph> content)
{
int relevantCombo = graphContainer.CurrentHoverCombo;
if (lastContentCombo == relevantCombo)
return;
lastContentCombo = relevantCombo;
textFlow.Clear();
textFlow.AddParagraph($"At combo {relevantCombo}:");
foreach (var graph in content)
{
float valueAtHover = graph.Values.ElementAt(relevantCombo);
float ofTotal = valueAtHover / graph.Values.Last();
textFlow.AddParagraph($"{graph.Name}: {valueAtHover:#,0} ({ofTotal * 100:N0}% of final)\n", st => st.Colour = graph.LineColour);
}
}
public void Move(Vector2 pos) => this.MoveTo(pos);
}
}
}