Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions RetakesAllocatorCore/Config/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static ConfigData Load(string modulePath, bool saveDefaults = true)
}
else
{
_configData = GetDefaultData();
_configData = GetDefaultConfigData();
if (saveDefaults)
{
SaveConfigData(_configData);
Expand All @@ -57,7 +57,7 @@ public static ConfigData Load(string modulePath, bool saveDefaults = true)
{
throw new Exception("Failed to load configs.");
}

_configData.Validate();

return _configData;
Expand All @@ -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<WeaponSelectionType>().ToList(),
RoundTypePercentages = new()
Expand All @@ -87,6 +88,14 @@ private static ConfigData GetDefaultData()
MigrateOnStartup = true,
};
}

public static ConfigData OverrideConfigDataForTests(
ConfigData configData
)
{
_configData = configData;
return _configData;
}
}

public enum WeaponSelectionType
Expand All @@ -98,10 +107,11 @@ public enum WeaponSelectionType

public record ConfigData
{
public required List<CsItem> UsableWeapons {get; set; }
public required List<WeaponSelectionType> AllowedWeaponSelectionTypes {get; set; }
public required Dictionary<RoundType, int> RoundTypePercentages {get; set; }
public required bool MigrateOnStartup {get; set; }
public required List<CsItem> UsableWeapons { get; set; }
public required List<WeaponSelectionType> AllowedWeaponSelectionTypes { get; set; }
public required Dictionary<RoundType, int> RoundTypePercentages { get; set; }
public required bool MigrateOnStartup { get; set; }

public void Validate()
{
if (RoundTypePercentages.Values.Sum() != 100)
Expand Down
35 changes: 35 additions & 0 deletions RetakesAllocatorTest/AllocationTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
2 changes: 1 addition & 1 deletion RetakesAllocatorTest/GlobalSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class GlobalSetup
public void Setup()
{
Queries.Migrate();
Configs.Load(".");
Configs.Load(".", false);
}

[OneTimeTearDown]
Expand Down