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 --mod-option parameter to simulate command #224

Merged
merged 9 commits into from
Oct 23, 2024
32 changes: 6 additions & 26 deletions PerformanceCalculator/Difficulty/DifficultyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
using Newtonsoft.Json;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Difficulty;
using osu.Game.Rulesets.Mods;

namespace PerformanceCalculator.Difficulty
{
Expand All @@ -36,6 +34,11 @@ public class DifficultyCommand : ProcessorCommand
+ "Values: hr, dt, hd, fl, ez, 4k, 5k, etc...")]
public string[] Mods { get; }

[UsedImplicitly]
[Option(CommandOptionType.MultipleValue, Template = "-o|--mod-option <option>",
Description = "The options of mods, with one for each setting. Specified as acryonym_settingkey=value. Example: DT_speed_change=1.35")]
public string[] ModOptions { get; set; } = [];

public override void Execute()
{
var resultSet = new ResultSet();
Expand Down Expand Up @@ -63,9 +66,6 @@ public override void Execute()
string json = JsonConvert.SerializeObject(resultSet);

Console.WriteLine(json);

if (OutputFile != null)
File.WriteAllText(OutputFile, json);
}
else
{
Expand Down Expand Up @@ -120,7 +120,7 @@ private Result processBeatmap(WorkingBeatmap beatmap)
{
// Get the ruleset
var ruleset = LegacyHelper.GetRulesetFromLegacyID(Ruleset ?? beatmap.BeatmapInfo.Ruleset.OnlineID);
var mods = getMods(ruleset);
var mods = ParseMods(ruleset, Mods, ModOptions);
var attributes = ruleset.CreateDifficultyCalculator(beatmap).Calculate(mods);

return new Result
Expand All @@ -133,26 +133,6 @@ private Result processBeatmap(WorkingBeatmap beatmap)
};
}

private Mod[] getMods(Ruleset ruleset)
{
var mods = new List<Mod>();
if (Mods == null)
return Array.Empty<Mod>();

var availableMods = ruleset.CreateAllMods().ToList();

foreach (var modString in Mods)
{
Mod newMod = availableMods.FirstOrDefault(m => string.Equals(m.Acronym, modString, StringComparison.CurrentCultureIgnoreCase));
if (newMod == null)
throw new ArgumentException($"Invalid mod provided: {modString}");

mods.Add(newMod);
}

return mods.ToArray();
}

private class ResultSet
{
[JsonProperty("errors")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public override void Execute()
string json = JsonConvert.SerializeObject(resultSet);

Console.WriteLine(json);

if (OutputFile != null)
File.WriteAllText(OutputFile, json);
}
else
{
Expand Down
4 changes: 0 additions & 4 deletions PerformanceCalculator/Leaderboard/LeaderboardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Alba.CsConsoleFormat;
using JetBrains.Annotations;
Expand Down Expand Up @@ -98,9 +97,6 @@ public override void Execute()
var json = JsonConvert.SerializeObject(calculatedPlayers);

Console.Write(json);

if (OutputFile != null)
File.WriteAllText(OutputFile, json);
}
else
{
Expand Down
42 changes: 33 additions & 9 deletions PerformanceCalculator/ProcessorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Difficulty;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;

Expand All @@ -26,10 +28,6 @@ public abstract class ProcessorCommand
/// </summary>
public IConsole Console { get; private set; }

[UsedImplicitly]
[Option(Template = "-o|--output <file.txt>", Description = "Output results to text file.")]
public string OutputFile { get; }

[UsedImplicitly]
[Option(Template = "-j|--json", Description = "Output results as JSON.")]
public bool OutputJson { get; }
Expand Down Expand Up @@ -65,9 +63,6 @@ public void OutputPerformance(ScoreInfo score, PerformanceAttributes performance
string json = JsonConvert.SerializeObject(result, Formatting.Indented);

Console.WriteLine(json);

if (OutputFile != null)
File.WriteAllText(OutputFile, json);
}
else
{
Expand Down Expand Up @@ -131,15 +126,44 @@ public void OutputDocument(Document document)
str = string.Join('\n', lines);

Console.Write(str);
if (OutputFile != null)
File.WriteAllText(OutputFile, str);
}
}

public virtual void Execute()
{
}

public static Mod[] ParseMods(Ruleset ruleset, string[] acronyms, string[] options)
{
acronyms ??= [];
options ??= [];

if (acronyms.Length == 0)
return [];

var mods = new List<Mod>();

foreach (var acronym in acronyms)
{
APIMod mod = new APIMod { Acronym = acronym };

foreach (string optionString in options.Where(x => x.StartsWith($"{acronym}_", StringComparison.CurrentCultureIgnoreCase)))
{
string optionTuple = optionString[(acronym.Length + 1)..];

string[] split = optionTuple.Split('=');
if (split.Length != 2)
throw new ArgumentException($"Invalid mod-option format (key=value): {optionTuple}");

mod.Settings[split[0]] = split[1];
}

mods.Add(mod.ToMod(ruleset));
}

return mods.ToArray();
}

private class Result
{
[JsonProperty("score")]
Expand Down
4 changes: 0 additions & 4 deletions PerformanceCalculator/Profile/ProfileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Alba.CsConsoleFormat;
using JetBrains.Annotations;
Expand Down Expand Up @@ -109,9 +108,6 @@ public override void Execute()
});

Console.Write(json);

if (OutputFile != null)
File.WriteAllText(OutputFile, json);
}
else
{
Expand Down
29 changes: 6 additions & 23 deletions PerformanceCalculator/Simulate/SimulateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using JetBrains.Annotations;
using McMaster.Extensions.CommandLineUtils;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;

Expand All @@ -32,6 +30,11 @@ public abstract class SimulateCommand : ProcessorCommand
[Option(CommandOptionType.MultipleValue, Template = "-m|--mod <mod>", Description = "One for each mod. The mods to compute the performance with. Values: hr, dt, hd, fl, etc...")]
public string[] Mods { get; }

[UsedImplicitly]
[Option(CommandOptionType.MultipleValue, Template = "-o|--mod-option <option>",
Description = "The options of mods, with one for each setting. Specified as acryonym_settingkey=value. Example: DT_speed_change=1.35")]
public string[] ModOptions { get; set; } = [];

[UsedImplicitly]
[Option(Template = "-X|--misses <misses>", Description = "Number of misses. Defaults to 0.")]
public int Misses { get; }
Expand Down Expand Up @@ -59,7 +62,7 @@ public override void Execute()
var ruleset = Ruleset;

var workingBeatmap = ProcessorWorkingBeatmap.FromFileOrId(Beatmap);
var mods = GetMods(ruleset);
var mods = ParseMods(ruleset, Mods, ModOptions);
var beatmap = workingBeatmap.GetPlayableBeatmap(ruleset.RulesetInfo, mods);

var beatmapMaxCombo = GetMaxCombo(beatmap);
Expand All @@ -80,26 +83,6 @@ public override void Execute()
OutputPerformance(scoreInfo, performanceAttributes, difficultyAttributes);
}

protected Mod[] GetMods(Ruleset ruleset)
{
if (Mods == null)
return Array.Empty<Mod>();

var availableMods = ruleset.CreateAllMods().ToList();
var mods = new List<Mod>();

foreach (var modString in Mods)
{
Mod newMod = availableMods.FirstOrDefault(m => string.Equals(m.Acronym, modString, StringComparison.CurrentCultureIgnoreCase));
if (newMod == null)
throw new ArgumentException($"Invalid mod provided: {modString}");

mods.Add(newMod);
}

return mods.ToArray();
}

protected abstract int GetMaxCombo(IBeatmap beatmap);

protected abstract Dictionary<HitResult, int> GenerateHitResults(double accuracy, IBeatmap beatmap, int countMiss, int? countMeh, int? countGood);
Expand Down
Loading