Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for valid mod combinations to give pp #9

Merged
merged 5 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// 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.

using osu.Framework.Extensions.TypeExtensions;
using osu.Game.Rulesets.Catch.Mods;
using osu.Game.Rulesets.Mania.Mods;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.Taiko.Mods;
using osu.Server.Queues.ScoreStatisticsProcessor.Processors;
using Xunit;

namespace osu.Server.Queues.ScoreStatisticsProcessor.Tests
{
public class PerformanceProcessorTests
{
[Fact]
public void LegacyModsThatGivePpAreAllowed()
{
var mods = new Mod[]
{
// Osu
new OsuModEasy(),
new OsuModNoFail(),
new OsuModHalfTime(),
new OsuModHardRock(),
new OsuModSuddenDeath(),
new OsuModPerfect(),
new OsuModDoubleTime(),
new OsuModNightcore(),
new OsuModHidden(),
new OsuModFlashlight(),
new OsuModSpunOut(),
// Taiko
new TaikoModEasy(),
new TaikoModNoFail(),
new TaikoModHalfTime(),
new TaikoModHardRock(),
new TaikoModSuddenDeath(),
new TaikoModPerfect(),
new TaikoModDoubleTime(),
new TaikoModNightcore(),
new TaikoModHidden(),
new TaikoModFlashlight(),
// Catch
new CatchModEasy(),
new CatchModNoFail(),
new CatchModHalfTime(),
new CatchModHardRock(),
new CatchModSuddenDeath(),
new CatchModPerfect(),
new CatchModDoubleTime(),
new CatchModNightcore(),
new CatchModHidden(),
new CatchModFlashlight(),
// Mania
new ManiaModEasy(),
new ManiaModNoFail(),
new ManiaModHalfTime(),
new ManiaModSuddenDeath(),
new ManiaModKey4(),
new ManiaModKey5(),
new ManiaModKey6(),
new ManiaModKey7(),
new ManiaModKey8(),
new ManiaModKey9(),
new ManiaModMirror(),
};

foreach (var mod in mods)
Assert.True(PerformanceProcessor.AllModsValidForPerformance(new[] { mod }), mod.GetType().ReadableName());
}

[Fact]
public void LegacyModsThatDoNotGivePpAreDisallowed()
{
var mods = new Mod[]
{
// Osu
new OsuModRelax(),
new OsuModAutopilot(),
new OsuModTarget(),
new OsuModAutoplay(),
new OsuModCinema(),
// Taiko
new TaikoModRelax(),
new TaikoModAutoplay(),
// Catch
new CatchModRelax(),
new CatchModAutoplay(),
// Mania
new ManiaModHardRock(),
new ManiaModKey1(),
new ManiaModKey2(),
new ManiaModKey3(),
new ManiaModKey10(),
new ManiaModDualStages(),
new ManiaModRandom(),
new ManiaModAutoplay(),
};

foreach (var mod in mods)
Assert.False(PerformanceProcessor.AllModsValidForPerformance(new[] { mod }), mod.GetType().ReadableName());
}

[Fact]
public void ModsThatDoNotGivePpAreDisallowed()
{
// Not an extensive list.
var mods = new Mod[]
{
new ModWindUp(),
new ModWindDown(),
// Osu
new OsuModDeflate(),
new OsuModApproachDifferent(),
new OsuModDifficultyAdjust(),
// Taiko
new TaikoModRandom(),
new TaikoModSwap(),
// Catch
new CatchModMirror(),
new CatchModFloatingFruits(),
new CatchModDifficultyAdjust(),
// Mania
new ManiaModInvert(),
new ManiaModConstantSpeed(),
};

foreach (var mod in mods)
Assert.False(PerformanceProcessor.AllModsValidForPerformance(new[] { mod }), mod.GetType().ReadableName());
}

[Fact]
public void ModsThatGivePpAreAllowed()
{
// Not an extensive list.
var mods = new Mod[]
{
// Osu
new OsuModMuted(),
new OsuModClassic(),
new OsuModDaycore(),
// Taiko
new TaikoModMuted(),
new TaikoModClassic(),
new TaikoModDaycore(),
// Catch
new CatchModMuted(),
new CatchModClassic(),
new CatchModDaycore(),
// Mania
new ManiaModMuted(),
new ManiaModClassic(),
new ManiaModDaycore(),
};

foreach (var mod in mods)
Assert.True(PerformanceProcessor.AllModsValidForPerformance(new[] { mod }), mod.GetType().ReadableName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
Expand Down Expand Up @@ -58,8 +59,9 @@ public class SoloScoreInfo // TODO: hopefully combine with client-side ScoreInfo

public ScoreInfo ToScoreInfo(Mod[] mods) => new ScoreInfo
{
OnlineScoreID = id,
UserID = user_id,
BeatmapInfoID = beatmap_id,
Beatmap = new BeatmapInfo { OnlineBeatmapID = beatmap_id },
RulesetID = ruleset_id,
Passed = passed,
TotalScore = total_score,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using MySqlConnector;
using osu.Game.Beatmaps.Legacy;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mania.Mods;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Scoring;
using osu.Server.Queues.ScoreStatisticsProcessor.Models;

Expand All @@ -30,35 +32,83 @@ public void ApplyToUserStats(SoloScoreInfo score, UserStats userStats, MySqlConn
Mod[] mods = score.mods.Select(m => m.ToMod(ruleset)).ToArray();
ScoreInfo scoreInfo = score.ToScoreInfo(mods);

double performance = computePerformance(ruleset, mods, scoreInfo, conn, transaction);

conn.Execute("INSERT INTO solo_scores_performance (`score_id`, `pp`) VALUES (@ScoreId, @PP) ON DUPLICATE KEY UPDATE `pp` = @PP", new
{
ScoreId = score.id,
PP = performance
}, transaction);
}

public void ApplyGlobal(SoloScoreInfo score, MySqlConnection conn)
{
}

private double computePerformance(Ruleset ruleset, Mod[] mods, ScoreInfo score, MySqlConnection conn, MySqlTransaction transaction)
{
if (!AllModsValidForPerformance(mods))
return 0;

var beatmap = conn.QuerySingle<Beatmap>("SELECT * FROM osu_beatmaps WHERE beatmap_id = @BeatmapId", new
{
BeatmapId = score.beatmap_id
BeatmapId = score.Beatmap.OnlineBeatmapID
}, transaction);

// Todo: We shouldn't be using legacy mods, but this requires difficulty calculation to be performed in-line.
LegacyMods legacyModValue = LegacyModsHelper.MaskRelevantMods(ruleset.ConvertToLegacyMods(mods), score.ruleset_id != beatmap.playmode);
LegacyMods legacyModValue = LegacyModsHelper.MaskRelevantMods(ruleset.ConvertToLegacyMods(mods), score.RulesetID != beatmap.playmode);

var rawDifficultyAttribs = conn.Query<BeatmapDifficultyAttribute>(
"SELECT * FROM osu_beatmap_difficulty_attribs WHERE beatmap_id = @BeatmapId AND mode = @RulesetId AND mods = @ModValue", new
{
BeatmapId = score.beatmap_id,
RulesetId = score.ruleset_id,
BeatmapId = score.Beatmap.OnlineBeatmapID,
RulesetId = score.RulesetID,
ModValue = (uint)legacyModValue
}, transaction).ToArray();

var difficultyAttributes = rawDifficultyAttribs.ToDictionary(a => (int)a.attrib_id).Map(score.ruleset_id, beatmap);
var performanceCalculator = ruleset.CreatePerformanceCalculator(difficultyAttributes, scoreInfo);
var performance = performanceCalculator.Calculate();

conn.Execute("INSERT INTO solo_scores_performance (`score_id`, `pp`) VALUES (@ScoreId, @PP) ON DUPLICATE KEY UPDATE `pp` = @PP", new
{
ScoreId = score.id,
PP = performance
}, transaction);
var difficultyAttributes = rawDifficultyAttribs.ToDictionary(a => (int)a.attrib_id).Map(score.RulesetID, beatmap);
var performanceCalculator = ruleset.CreatePerformanceCalculator(difficultyAttributes, score);
return performanceCalculator.Calculate();
}

public void ApplyGlobal(SoloScoreInfo score, MySqlConnection conn)
/// <summary>
/// Checks whether all mods in a given array are valid to give PP for.
/// </summary>
public static bool AllModsValidForPerformance(Mod[] mods)
{
foreach (var m in mods)
{
switch (m)
{
case ManiaModHardRock:
case ManiaModKey1:
case ManiaModKey2:
case ManiaModKey3:
case ManiaModKey10:
return false;

case ModEasy:
case ModNoFail:
case ModHalfTime:
case ModSuddenDeath:
case ModPerfect:
case ModHardRock:
case ModDoubleTime:
case ModHidden:
case ModFlashlight:
case ModMuted:
case ModClassic:
case OsuModSpunOut:
case ManiaKeyMod:
case ManiaModMirror:
continue;

default:
return false;
}
}

return true;
}

private static List<Ruleset> getRulesets()
Expand Down