2020-01-17 04:27:47 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-11-04 07:11:23 +00:00
|
|
|
using System;
|
2020-01-17 04:27:47 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Online.API;
|
2022-01-04 06:59:41 +00:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-10-29 05:32:31 +00:00
|
|
|
using osu.Game.Online.Solo;
|
2020-01-17 04:27:47 +00:00
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Rulesets.Difficulty;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2022-01-04 06:59:41 +00:00
|
|
|
using osu.Game.Rulesets.Osu;
|
2021-04-12 10:50:24 +00:00
|
|
|
using osu.Game.Rulesets.Osu.Mods;
|
2020-01-17 04:27:47 +00:00
|
|
|
using osu.Game.Rulesets.UI;
|
2021-04-12 10:50:24 +00:00
|
|
|
using osu.Game.Scoring;
|
2020-01-17 04:27:47 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.Online
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2021-02-03 10:44:39 +00:00
|
|
|
public class TestAPIModJsonSerialization
|
2020-01-17 04:27:47 +00:00
|
|
|
{
|
2022-03-09 08:13:51 +00:00
|
|
|
[Test]
|
|
|
|
public void TestUnknownMod()
|
|
|
|
{
|
|
|
|
var apiMod = new APIMod { Acronym = "WNG" };
|
|
|
|
|
|
|
|
var deserialized = JsonConvert.DeserializeObject<APIMod>(JsonConvert.SerializeObject(apiMod));
|
|
|
|
|
|
|
|
var converted = deserialized?.ToMod(new TestRuleset());
|
|
|
|
|
|
|
|
Assert.That(converted, Is.TypeOf(typeof(UnknownMod)));
|
|
|
|
Assert.That(converted?.Type, Is.EqualTo(ModType.System));
|
|
|
|
Assert.That(converted?.Acronym, Is.EqualTo("WNG??"));
|
|
|
|
}
|
|
|
|
|
2020-01-17 04:27:47 +00:00
|
|
|
[Test]
|
|
|
|
public void TestAcronymIsPreserved()
|
|
|
|
{
|
|
|
|
var apiMod = new APIMod(new TestMod());
|
|
|
|
|
|
|
|
var deserialized = JsonConvert.DeserializeObject<APIMod>(JsonConvert.SerializeObject(apiMod));
|
|
|
|
|
2021-03-30 00:00:09 +00:00
|
|
|
Assert.That(deserialized?.Acronym, Is.EqualTo(apiMod.Acronym));
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestRawSettingIsPreserved()
|
|
|
|
{
|
|
|
|
var apiMod = new APIMod(new TestMod { TestSetting = { Value = 2 } });
|
|
|
|
|
|
|
|
var deserialized = JsonConvert.DeserializeObject<APIMod>(JsonConvert.SerializeObject(apiMod));
|
|
|
|
|
2021-03-30 00:00:09 +00:00
|
|
|
Assert.That(deserialized?.Settings, Contains.Key("test_setting").With.ContainValue(2.0));
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestConvertedModHasCorrectSetting()
|
|
|
|
{
|
|
|
|
var apiMod = new APIMod(new TestMod { TestSetting = { Value = 2 } });
|
|
|
|
|
|
|
|
var deserialized = JsonConvert.DeserializeObject<APIMod>(JsonConvert.SerializeObject(apiMod));
|
2021-03-30 00:00:09 +00:00
|
|
|
var converted = (TestMod)deserialized?.ToMod(new TestRuleset());
|
2020-01-17 04:27:47 +00:00
|
|
|
|
2021-03-30 00:00:09 +00:00
|
|
|
Assert.That(converted?.TestSetting.Value, Is.EqualTo(2));
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 14:21:44 +00:00
|
|
|
[Test]
|
|
|
|
public void TestDeserialiseTimeRampMod()
|
|
|
|
{
|
|
|
|
// Create the mod with values different from default.
|
|
|
|
var apiMod = new APIMod(new TestModTimeRamp
|
|
|
|
{
|
|
|
|
AdjustPitch = { Value = false },
|
|
|
|
InitialRate = { Value = 1.25 },
|
|
|
|
FinalRate = { Value = 0.25 }
|
|
|
|
});
|
|
|
|
|
|
|
|
var deserialised = JsonConvert.DeserializeObject<APIMod>(JsonConvert.SerializeObject(apiMod));
|
2021-03-30 00:00:09 +00:00
|
|
|
var converted = (TestModTimeRamp)deserialised?.ToMod(new TestRuleset());
|
2020-06-26 14:21:44 +00:00
|
|
|
|
2021-11-04 07:11:23 +00:00
|
|
|
Assert.That(converted, Is.Not.Null);
|
|
|
|
|
|
|
|
Assert.That(converted.AdjustPitch.Value, Is.EqualTo(false));
|
|
|
|
Assert.That(converted.InitialRate.Value, Is.EqualTo(1.25));
|
|
|
|
Assert.That(converted.FinalRate.Value, Is.EqualTo(0.25));
|
2020-06-26 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 15:32:31 +00:00
|
|
|
[Test]
|
|
|
|
public void TestDeserialiseDifficultyAdjustModWithExtendedLimits()
|
|
|
|
{
|
|
|
|
var apiMod = new APIMod(new TestModDifficultyAdjust
|
|
|
|
{
|
|
|
|
OverallDifficulty = { Value = 11 },
|
|
|
|
ExtendedLimits = { Value = true }
|
|
|
|
});
|
|
|
|
|
|
|
|
var deserialised = JsonConvert.DeserializeObject<APIMod>(JsonConvert.SerializeObject(apiMod));
|
2021-03-30 00:00:09 +00:00
|
|
|
var converted = (TestModDifficultyAdjust)deserialised?.ToMod(new TestRuleset());
|
2020-12-09 15:32:31 +00:00
|
|
|
|
2021-03-30 00:00:09 +00:00
|
|
|
Assert.That(converted?.ExtendedLimits.Value, Is.True);
|
|
|
|
Assert.That(converted?.OverallDifficulty.Value, Is.EqualTo(11));
|
2020-12-09 15:32:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 10:50:24 +00:00
|
|
|
[Test]
|
2021-10-29 05:32:31 +00:00
|
|
|
public void TestDeserialiseSubmittableScoreWithEmptyMods()
|
2021-04-12 10:50:24 +00:00
|
|
|
{
|
2022-01-04 06:59:41 +00:00
|
|
|
var score = new SubmittableScore(new ScoreInfo
|
|
|
|
{
|
|
|
|
User = new APIUser(),
|
|
|
|
Ruleset = new OsuRuleset().RulesetInfo,
|
|
|
|
});
|
2021-04-12 10:50:24 +00:00
|
|
|
|
2021-10-29 05:32:31 +00:00
|
|
|
var deserialised = JsonConvert.DeserializeObject<SubmittableScore>(JsonConvert.SerializeObject(score));
|
2021-04-12 10:50:24 +00:00
|
|
|
|
|
|
|
Assert.That(deserialised?.Mods.Length, Is.Zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-10-29 05:32:31 +00:00
|
|
|
public void TestDeserialiseSubmittableScoreWithCustomModSetting()
|
2021-04-12 10:50:24 +00:00
|
|
|
{
|
2021-10-29 05:32:31 +00:00
|
|
|
var score = new SubmittableScore(new ScoreInfo
|
2021-04-12 10:50:24 +00:00
|
|
|
{
|
2022-01-04 06:59:41 +00:00
|
|
|
Mods = new Mod[] { new OsuModDoubleTime { SpeedChange = { Value = 2 } } },
|
|
|
|
User = new APIUser(),
|
|
|
|
Ruleset = new OsuRuleset().RulesetInfo,
|
2021-10-29 05:32:31 +00:00
|
|
|
});
|
2021-04-12 10:50:24 +00:00
|
|
|
|
2021-10-29 05:32:31 +00:00
|
|
|
var deserialised = JsonConvert.DeserializeObject<SubmittableScore>(JsonConvert.SerializeObject(score));
|
2021-04-12 10:50:24 +00:00
|
|
|
|
2021-10-29 05:32:31 +00:00
|
|
|
Assert.That((deserialised?.Mods[0])?.Settings["speed_change"], Is.EqualTo(2));
|
2021-04-12 10:50:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 08:01:04 +00:00
|
|
|
[Test]
|
|
|
|
public void TestAPIModDetachedFromSource()
|
|
|
|
{
|
|
|
|
var mod = new OsuModDoubleTime { SpeedChange = { Value = 1.01 } };
|
|
|
|
var apiMod = new APIMod(mod);
|
|
|
|
|
|
|
|
mod.SpeedChange.Value = 1.5;
|
|
|
|
|
|
|
|
Assert.That(apiMod.Settings["speed_change"], Is.EqualTo(1.01d));
|
|
|
|
}
|
|
|
|
|
2020-01-17 04:27:47 +00:00
|
|
|
private class TestRuleset : Ruleset
|
|
|
|
{
|
2020-06-26 14:21:44 +00:00
|
|
|
public override IEnumerable<Mod> GetModsFor(ModType type) => new Mod[]
|
|
|
|
{
|
|
|
|
new TestMod(),
|
|
|
|
new TestModTimeRamp(),
|
2020-12-09 15:32:31 +00:00
|
|
|
new TestModDifficultyAdjust()
|
2020-06-26 14:21:44 +00:00
|
|
|
};
|
2020-01-17 04:27:47 +00:00
|
|
|
|
2021-11-04 07:11:23 +00:00
|
|
|
public override DrawableRuleset CreateDrawableRulesetWith(IBeatmap beatmap, IReadOnlyList<Mod> mods = null) => throw new NotImplementedException();
|
2020-01-17 04:27:47 +00:00
|
|
|
|
2021-11-04 07:11:23 +00:00
|
|
|
public override IBeatmapConverter CreateBeatmapConverter(IBeatmap beatmap) => throw new NotImplementedException();
|
2020-01-17 04:27:47 +00:00
|
|
|
|
2021-11-15 09:19:23 +00:00
|
|
|
public override DifficultyCalculator CreateDifficultyCalculator(IWorkingBeatmap beatmap) => throw new NotImplementedException();
|
2020-01-17 04:27:47 +00:00
|
|
|
|
2020-01-17 09:52:13 +00:00
|
|
|
public override string Description { get; } = string.Empty;
|
|
|
|
public override string ShortName { get; } = string.Empty;
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class TestMod : Mod
|
|
|
|
{
|
|
|
|
public override string Name => "Test Mod";
|
|
|
|
public override string Acronym => "TM";
|
2021-04-15 05:32:01 +00:00
|
|
|
public override string Description => "This is a test mod.";
|
2020-01-17 04:27:47 +00:00
|
|
|
public override double ScoreMultiplier => 1;
|
|
|
|
|
|
|
|
[SettingSource("Test")]
|
|
|
|
public BindableNumber<double> TestSetting { get; } = new BindableDouble
|
|
|
|
{
|
|
|
|
MinValue = 0,
|
|
|
|
MaxValue = 10,
|
|
|
|
Default = 5,
|
|
|
|
Precision = 0.01,
|
|
|
|
};
|
|
|
|
}
|
2020-06-26 14:21:44 +00:00
|
|
|
|
|
|
|
private class TestModTimeRamp : ModTimeRamp
|
|
|
|
{
|
|
|
|
public override string Name => "Test Mod";
|
|
|
|
public override string Acronym => "TMTR";
|
2021-04-15 05:32:01 +00:00
|
|
|
public override string Description => "This is a test mod.";
|
2020-06-26 14:21:44 +00:00
|
|
|
public override double ScoreMultiplier => 1;
|
|
|
|
|
|
|
|
[SettingSource("Initial rate", "The starting speed of the track")]
|
|
|
|
public override BindableNumber<double> InitialRate { get; } = new BindableDouble
|
|
|
|
{
|
|
|
|
MinValue = 1,
|
|
|
|
MaxValue = 2,
|
|
|
|
Default = 1.5,
|
|
|
|
Value = 1.5,
|
|
|
|
Precision = 0.01,
|
|
|
|
};
|
|
|
|
|
|
|
|
[SettingSource("Final rate", "The speed increase to ramp towards")]
|
|
|
|
public override BindableNumber<double> FinalRate { get; } = new BindableDouble
|
|
|
|
{
|
|
|
|
MinValue = 0,
|
|
|
|
MaxValue = 1,
|
|
|
|
Default = 0.5,
|
|
|
|
Value = 0.5,
|
|
|
|
Precision = 0.01,
|
|
|
|
};
|
|
|
|
|
|
|
|
[SettingSource("Adjust pitch", "Should pitch be adjusted with speed")]
|
|
|
|
public override BindableBool AdjustPitch { get; } = new BindableBool
|
|
|
|
{
|
|
|
|
Default = true,
|
|
|
|
Value = true
|
|
|
|
};
|
|
|
|
}
|
2020-12-09 15:32:31 +00:00
|
|
|
|
|
|
|
private class TestModDifficultyAdjust : ModDifficultyAdjust
|
|
|
|
{
|
|
|
|
}
|
2020-01-17 04:27:47 +00:00
|
|
|
}
|
|
|
|
}
|