diff --git a/RetakesAllocatorCore/Config/Configs.cs b/RetakesAllocatorCore/Config/Configs.cs index 76df860..0f0c1c8 100644 --- a/RetakesAllocatorCore/Config/Configs.cs +++ b/RetakesAllocatorCore/Config/Configs.cs @@ -46,7 +46,7 @@ public static ConfigData Load(string modulePath, bool saveDefaults = true) } else { - _configData = GetDefaultData(); + _configData = GetDefaultConfigData(); if (saveDefaults) { SaveConfigData(_configData); @@ -57,7 +57,7 @@ public static ConfigData Load(string modulePath, bool saveDefaults = true) { throw new Exception("Failed to load configs."); } - + _configData.Validate(); return _configData; @@ -73,9 +73,10 @@ private static void SaveConfigData(ConfigData configData) File.WriteAllText(_configFilePath, JsonSerializer.Serialize(configData, SerializationOptions)); } - private static ConfigData GetDefaultData() + public static ConfigData GetDefaultConfigData() { - return new ConfigData { + return new ConfigData + { UsableWeapons = WeaponHelpers.GetAllWeapons(), AllowedWeaponSelectionTypes = Enum.GetValues().ToList(), RoundTypePercentages = new() @@ -87,6 +88,14 @@ private static ConfigData GetDefaultData() MigrateOnStartup = true, }; } + + public static ConfigData OverrideConfigDataForTests( + ConfigData configData + ) + { + _configData = configData; + return _configData; + } } public enum WeaponSelectionType @@ -98,10 +107,11 @@ public enum WeaponSelectionType public record ConfigData { - public required List UsableWeapons {get; set; } - public required List AllowedWeaponSelectionTypes {get; set; } - public required Dictionary RoundTypePercentages {get; set; } - public required bool MigrateOnStartup {get; set; } + public required List UsableWeapons { get; set; } + public required List AllowedWeaponSelectionTypes { get; set; } + public required Dictionary RoundTypePercentages { get; set; } + public required bool MigrateOnStartup { get; set; } + public void Validate() { if (RoundTypePercentages.Values.Sum() != 100) diff --git a/RetakesAllocatorTest/AllocationTests.cs b/RetakesAllocatorTest/AllocationTests.cs new file mode 100644 index 0000000..a90b534 --- /dev/null +++ b/RetakesAllocatorTest/AllocationTests.cs @@ -0,0 +1,35 @@ +using CounterStrikeSharp.API.Modules.Entities.Constants; +using CounterStrikeSharp.API.Modules.Utils; +using RetakesAllocatorCore; +using RetakesAllocatorCore.Config; +using RetakesAllocatorCore.Db; + +namespace RetakesAllocatorTest; + +public class AllocationTests +{ + [SetUp] + public void Setup() + { + Queries.Wipe(); + Configs.Load(".", false); + } + + [Test] + public void UseableWeaponsTest() + { + Queries.SetWeaponPreferenceForUser(1, CsTeam.Terrorist, RoundType.FullBuy, CsItem.Galil); + + var weapon = WeaponHelpers.GetWeaponForRoundType(RoundType.FullBuy, CsTeam.Terrorist, Queries.GetUserSettings(1)); + Assert.That(weapon, Is.EqualTo(CsItem.Galil)); + + var configData = Configs.GetDefaultConfigData(); + configData.UsableWeapons = configData.UsableWeapons + .Where(w => w != CsItem.Galil) + .ToList(); + Configs.OverrideConfigDataForTests(configData); + + weapon = WeaponHelpers.GetWeaponForRoundType(RoundType.FullBuy, CsTeam.Terrorist, Queries.GetUserSettings(1)); + Assert.That(weapon, Is.EqualTo(CsItem.AK47)); + } +} diff --git a/RetakesAllocatorTest/GlobalSetup.cs b/RetakesAllocatorTest/GlobalSetup.cs index 87db9a8..c2c49a1 100644 --- a/RetakesAllocatorTest/GlobalSetup.cs +++ b/RetakesAllocatorTest/GlobalSetup.cs @@ -10,7 +10,7 @@ public class GlobalSetup public void Setup() { Queries.Migrate(); - Configs.Load("."); + Configs.Load(".", false); } [OneTimeTearDown]