From fb41fe09b0cf70bb4557355bcd97d3ff7c2e6fa9 Mon Sep 17 00:00:00 2001 From: Yoni Lerner Date: Tue, 16 Jan 2024 02:31:08 -0500 Subject: [PATCH 1/3] Mocking Refactor --- RetakesAllocator/CounterStrikeSharpImpl.cs | 306 ++++++++++++++++++ RetakesAllocator/Helpers.cs | 8 - RetakesAllocator/RetakesAllocator.cs | 64 +--- .../ICCSPlayerControllerMock.cs | 18 ++ .../ICCSPlayerPawnMock.cs | 8 + .../ICCSPlayer_ItemServicesMock.cs | 7 + .../ICommandInfoMock.cs | 7 + .../ICounterStrikeSharpMock.cs | 30 ++ .../CounterStrikeSharpMock/INativeAPIMock.cs | 6 + .../IPlayerWeaponMock.cs | 14 + .../CounterStrikeSharpMock/IServerMock.cs | 6 + .../CounterStrikeSharpMock/IUtilitiesMock.cs | 6 + .../CounterStrikeSharpMock/IVector.cs | 7 + .../IWeaponServicesMock.cs | 6 + .../{ => Helpers}/OnRoundPostStartHelper.cs | 62 ++-- RetakesAllocatorCore/Utils.cs | 8 + 16 files changed, 473 insertions(+), 90 deletions(-) create mode 100644 RetakesAllocator/CounterStrikeSharpImpl.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs rename RetakesAllocatorCore/{ => Helpers}/OnRoundPostStartHelper.cs (63%) diff --git a/RetakesAllocator/CounterStrikeSharpImpl.cs b/RetakesAllocator/CounterStrikeSharpImpl.cs new file mode 100644 index 00000000..5661a3d9 --- /dev/null +++ b/RetakesAllocator/CounterStrikeSharpImpl.cs @@ -0,0 +1,306 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Entities.Constants; +using CounterStrikeSharp.API.Modules.Utils; +using RetakesAllocatorCore; +using RetakesAllocatorCore.CounterStrikeSharpMock; + +namespace RetakesAllocator; + +public class NativeApiImpl : INativeAPIMock +{ + public void IssueClientCommand(int clientIndex, string command) + { + NativeAPI.IssueClientCommand(clientIndex, command); + } +} + +public class ServerImpl : IServerMock +{ + public void PrintToChatAll(string message) + { + Server.PrintToChatAll(message); + } +} + +public class PlayerWeaponImpl : IPlayerWeaponMock +{ + private readonly CHandle _weapon; + + public PlayerWeaponImpl(CHandle weapon) + { + _weapon = weapon; + } + + public bool IsValid => _weapon.IsValid && (_weapon.Value?.IsValid ?? false); + public string DesignerName => _weapon.Value?.DesignerName ?? ""; + + public CsItem? Item => + _weapon.Value is not null + ? Utils.ToEnum(DesignerName) + : null; + + public nint? Handle => _weapon.Value?.Handle; + + public void Remove() + { + _weapon.Value?.Remove(); + } +} + +public class WeaponServicesImpl : IWeaponServicesMock +{ + private readonly CPlayer_WeaponServices _weaponServices; + + public WeaponServicesImpl(CPlayer_WeaponServices weaponServices) + { + _weaponServices = weaponServices; + } + + public ICollection MyWeapons => + _weaponServices + .MyWeapons + .Select(w => new PlayerWeaponImpl(w)) + .Cast() + .ToList(); +} + +public class CCSPlayerPawnImpl : ICCSPlayerPawnMock +{ + private readonly CHandle _pawn; + + public CCSPlayerPawnImpl(CHandle pawn) + { + _pawn = pawn; + } + + public IWeaponServicesMock? WeaponServices => + _pawn.Value?.WeaponServices is not null + ? new WeaponServicesImpl(_pawn.Value.WeaponServices) + : null; + + public void RemovePlayerItem(IPlayerWeaponMock weapon) + { + if (weapon.Handle is not null) + { + _pawn.Value?.RemovePlayerItem(new CBasePlayerWeapon(weapon.Handle.Value)); + } + } +} + +public class CCSPlayer_ItemServicesImpl : ICCSPlayer_ItemServicesMock +{ + private readonly CCSPlayer_ItemServices? _itemServices; + + public CCSPlayer_ItemServicesImpl(CPlayer_ItemServices? itemServices) + { + _itemServices = itemServices is not null + ? new CCSPlayer_ItemServices(itemServices.Handle) + : null; + } + + public bool HasDefuser + { + get => _itemServices?.HasDefuser ?? false; + set + { + if (_itemServices is not null) + { + _itemServices.HasDefuser = value; + } + } + } + + public bool IsValid => _itemServices?.Handle is not null; +} + +public class CCSPlayerControllerImpl : ICCSPlayerControllerMock +{ + private readonly CCSPlayerController? _player; + + public CCSPlayerControllerImpl(CCSPlayerController? player) + { + _player = player; + } + + public int? UserId => _player?.UserId; + public bool IsValid => _player?.IsValid ?? false; + + public ulong SteamId => _player is not null + ? _player.AuthorizedSteamID?.SteamId64 ?? 0 + : 0; + + public CsTeam Team => _player?.Team ?? CsTeam.None; + + public ICCSPlayerPawnMock? PlayerPawn => _player is not null + ? new CCSPlayerPawnImpl(_player.PlayerPawn) + : null; + + public ICCSPlayer_ItemServicesMock? ItemServices => + new CCSPlayer_ItemServicesImpl(_player?.PlayerPawn.Value?.ItemServices); + + public void GiveNamedItem(CsItem item) + { + _player?.GiveNamedItem(item); + } +} + +public class UtilitiesImpl : IUtilitiesMock +{ + public List GetPlayers() + { + return Utilities + .GetPlayers() + .Select(player => new CCSPlayerControllerImpl(player)) + .Cast().ToList(); + } +} + +public class CounterStrikeSharpImpl : ICounterStrikeSharpMock +{ + public INativeAPIMock NativeApi => new NativeApiImpl(); + public IServerMock Server => new ServerImpl(); + public IUtilitiesMock Utilities => new UtilitiesImpl(); + public string MessagePrefix => PluginInfo.MessagePrefix; + + private readonly RetakesAllocator _plugin; + + public CounterStrikeSharpImpl(RetakesAllocator plugin) + { + _plugin = plugin; + } + + public void AllocateItemsForPlayer(ICCSPlayerControllerMock player, ICollection items, string? slotToSelect) + { + // Log.Write($"Allocating items: {string.Join(",", items)}"); + AddTimer(0.1f, () => + { + if (player.IsValid) + { + // Log.Write($"Player is not valid when allocating item"); + return; + } + + foreach (var item in items) + { + player.GiveNamedItem(item); + } + + if (slotToSelect is not null) + { + AddTimer(0.1f, () => + { + if (player.IsValid && player.UserId is not null) + { + NativeApi.IssueClientCommand((int) player.UserId, slotToSelect); + } + }); + } + }); + } + + public void GiveDefuseKit(ICCSPlayerControllerMock player) + { + _plugin.AddTimer(0.1f, () => + { + var itemServices = player.ItemServices; + if ((itemServices?.IsValid ?? false) && player.IsValid) + { + itemServices.HasDefuser = true; + } + }); + } + + public void AddTimer(float interval, Action callback) + { + _plugin.AddTimer(interval, callback); + } + + public bool PlayerIsValid(ICCSPlayerControllerMock? player) + { + return (player?.IsValid ?? false) && player.SteamId != 0; + } + + public ICollection CommandInfoToArgList(ICommandInfoMock commandInfo, bool includeFirst = false) + { + var result = new List(); + + for (var i = includeFirst ? 0 : 1; i < commandInfo.ArgCount; i++) + { + result.Add(commandInfo.GetArg(i)); + } + + return result; + } + + public bool RemoveWeapons(ICCSPlayerControllerMock player, Func? where = null) + { + if (!PlayerIsValid(player) || player.PlayerPawn?.WeaponServices is null) + { + return false; + } + + var removed = false; + + foreach (var weapon in player.PlayerPawn.WeaponServices.MyWeapons) + { + // Log.Write($"want to remove wep {weapon.Value?.DesignerName} {weapon.IsValid}"); + if (!weapon.IsValid) + { + continue; + } + + // Log.Write($"item to remove: {item}"); + var item = weapon.Item; + if ( + where is not null && + (item is null || !where(item.Value)) + ) + { + continue; + } + + // Log.Write($"Removing weapon {weapon.Value.DesignerName} {weapon.IsValid}"); + + player.PlayerPawn.RemovePlayerItem(weapon); + weapon.Remove(); + + removed = true; + } + + return removed; + } + + private CCSGameRules GetGameRules() + { + var gameRulesEntities = CounterStrikeSharp.API.Utilities.FindAllEntitiesByDesignerName("cs_gamerules"); + var gameRules = gameRulesEntities.First().GameRules; + + if (gameRules is null) + { + const string message = "Game rules were null."; + Log.Write(message); + throw new Exception(message); + } + + return gameRules; + } + + public bool IsWarmup() + { + return GetGameRules().WarmupPeriod; + } + + public bool IsWeaponAllocationAllowed() + { + return WeaponHelpers.IsWeaponAllocationAllowed(GetGameRules().FreezePeriod); + } + + public double GetVectorDistance(IVector v1, IVector v2) + { + var dx = v1.X - v2.X; + var dy = v1.Y - v2.Y; + + return Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)); + } +} diff --git a/RetakesAllocator/Helpers.cs b/RetakesAllocator/Helpers.cs index 958461b4..318dd93b 100644 --- a/RetakesAllocator/Helpers.cs +++ b/RetakesAllocator/Helpers.cs @@ -14,14 +14,6 @@ public static bool PlayerIsValid(CCSPlayerController? player) return player is not null && player.IsValid && player.AuthorizedSteamID is not null; } - public static void WriteNewlineDelimited(string message, Action writer) - { - foreach (var line in message.Split("\n")) - { - writer($"{PluginInfo.MessagePrefix}{line}"); - } - } - public static ICollection CommandInfoToArgList(CommandInfo commandInfo, bool includeFirst = false) { var result = new List(); diff --git a/RetakesAllocator/RetakesAllocator.cs b/RetakesAllocator/RetakesAllocator.cs index db536072..7a30de73 100644 --- a/RetakesAllocator/RetakesAllocator.cs +++ b/RetakesAllocator/RetakesAllocator.cs @@ -11,6 +11,7 @@ using RetakesAllocatorCore.Config; using RetakesAllocatorCore.Db; using RetakesAllocator.Menus; +using RetakesAllocatorCore.Helpers; using SQLitePCL; using static RetakesAllocatorCore.PluginInfo; @@ -146,7 +147,7 @@ private void HandleWeaponCommand(CCSPlayerController? player, CommandInfo comman false, out var selectedWeapon ); - Helpers.WriteNewlineDelimited(result, l => commandInfo.ReplyToCommand(l)); + Utils.WriteNewlineDelimited(result, l => commandInfo.ReplyToCommand(l)); if (Helpers.IsWeaponAllocationAllowed() && selectedWeapon is not null) { @@ -198,7 +199,7 @@ public void OnAwpCommand(CCSPlayerController? player, CommandInfo commandInfo) currentPreferredSetting is not null, out _ ); - Helpers.WriteNewlineDelimited(result, l => commandInfo.ReplyToCommand(l)); + Utils.WriteNewlineDelimited(result, l => commandInfo.ReplyToCommand(l)); } [ConsoleCommand("css_removegun")] @@ -397,7 +398,7 @@ p.AbsOrigin is null false, out _ ); - Helpers.WriteNewlineDelimited(message, player.PrintToChat); + Utils.WriteNewlineDelimited(message, player.PrintToChat); } } @@ -407,35 +408,8 @@ out _ [GameEventHandler] public HookResult OnRoundPostStart(EventRoundPoststart @event, GameEventInfo info) { - if (Helpers.IsWarmup()) - { - return HookResult.Continue; - } - - var allPlayers = Utilities.GetPlayers() - .Where(Helpers.PlayerIsValid) - .ToList(); - - OnRoundPostStartHelper.Handle( - _nextRoundType, - allPlayers, - Helpers.GetSteamId, - Helpers.GetTeam, - GiveDefuseKit, - AllocateItemsForPlayer, - out var currentRoundType - ); - _currentRoundType = currentRoundType; - _nextRoundType = null; - - if (Configs.GetConfigData().EnableRoundTypeAnnouncement) - { - Server.PrintToChatAll( - $"{MessagePrefix}{Enum.GetName(_currentRoundType.Value)} Round" - ); - } - - return HookResult.Continue; + var css = new CounterStrikeSharpImpl(this); + return OnRoundPostStartHelper.Handle(_nextRoundType, css, out _currentRoundType); } #endregion @@ -444,31 +418,7 @@ out var currentRoundType private void AllocateItemsForPlayer(CCSPlayerController player, ICollection items, string? slotToSelect) { - // Log.Write($"Allocating items: {string.Join(",", items)}"); - AddTimer(0.1f, () => - { - if (!Helpers.PlayerIsValid(player)) - { - // Log.Write($"Player is not valid when allocating item"); - return; - } - - foreach (var item in items) - { - player.GiveNamedItem(item); - } - - if (slotToSelect is not null) - { - AddTimer(0.1f, () => - { - if (Helpers.PlayerIsValid(player) && player.UserId is not null) - { - NativeAPI.IssueClientCommand((int) player.UserId, slotToSelect); - } - }); - } - }); + // TODO refactor to CounterStrikeSharpImpl } private void GiveDefuseKit(CCSPlayerController player) diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs new file mode 100644 index 00000000..83b5c854 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs @@ -0,0 +1,18 @@ +using CounterStrikeSharp.API.Modules.Entities.Constants; +using CounterStrikeSharp.API.Modules.Utils; + +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface ICCSPlayerControllerMock +{ + public int? UserId { get; } + public bool IsValid { get; } + public ulong SteamId { get; } + public CsTeam Team { get; } + + public ICCSPlayerPawnMock? PlayerPawn { get; } + + public ICCSPlayer_ItemServicesMock? ItemServices { get; } + + public void GiveNamedItem(CsItem item); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs new file mode 100644 index 00000000..e6e4fc24 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs @@ -0,0 +1,8 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface ICCSPlayerPawnMock +{ + public IWeaponServicesMock? WeaponServices { get; } + + public void RemovePlayerItem(IPlayerWeaponMock weapon); +} \ No newline at end of file diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs new file mode 100644 index 00000000..bc7ed0eb --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs @@ -0,0 +1,7 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface ICCSPlayer_ItemServicesMock +{ + public bool HasDefuser { get; set; } + public bool IsValid { get; } +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs new file mode 100644 index 00000000..77686ed9 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs @@ -0,0 +1,7 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface ICommandInfoMock +{ + public string GetArg(int index); + public int ArgCount { get; } +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs new file mode 100644 index 00000000..9af9b91e --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs @@ -0,0 +1,30 @@ +using CounterStrikeSharp.API.Modules.Entities.Constants; + +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface ICounterStrikeSharpMock +{ + public INativeAPIMock NativeApi { get; } + public IServerMock Server { get; } + public IUtilitiesMock Utilities { get; } + + public string MessagePrefix { get; } + + void AllocateItemsForPlayer(ICCSPlayerControllerMock player, ICollection items, string? slotToSelect); + + public void GiveDefuseKit(ICCSPlayerControllerMock player); + + public void AddTimer(float interval, Action callback); + + public bool PlayerIsValid(ICCSPlayerControllerMock? player); + + public ICollection CommandInfoToArgList(ICommandInfoMock commandInfo, bool includeFirst = false); + + public bool RemoveWeapons(ICCSPlayerControllerMock playerController, Func? where = null); + + public bool IsWarmup(); + + public bool IsWeaponAllocationAllowed(); + + public double GetVectorDistance(IVector v1, IVector v2); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs new file mode 100644 index 00000000..d68cfbc3 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface INativeAPIMock +{ + public void IssueClientCommand(int clientIndex, string command); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs new file mode 100644 index 00000000..5e6fbc10 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs @@ -0,0 +1,14 @@ +using CounterStrikeSharp.API.Modules.Entities.Constants; + +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface IPlayerWeaponMock +{ + public bool IsValid { get; } + public string DesignerName { get; } + public CsItem? Item { get; } + + public nint? Handle { get; } + + public void Remove(); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs new file mode 100644 index 00000000..477b3b75 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface IServerMock +{ + public void PrintToChatAll(string message); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs new file mode 100644 index 00000000..586da842 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface IUtilitiesMock +{ + List GetPlayers(); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs new file mode 100644 index 00000000..292098f8 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs @@ -0,0 +1,7 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface IVector +{ + public float X { get; } + public float Y { get; } +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs new file mode 100644 index 00000000..562ddb4d --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpMock; + +public interface IWeaponServicesMock +{ + public ICollection MyWeapons { get; } +} \ No newline at end of file diff --git a/RetakesAllocatorCore/OnRoundPostStartHelper.cs b/RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs similarity index 63% rename from RetakesAllocatorCore/OnRoundPostStartHelper.cs rename to RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs index b2a21b4b..33e7066b 100644 --- a/RetakesAllocatorCore/OnRoundPostStartHelper.cs +++ b/RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs @@ -2,37 +2,40 @@ using CounterStrikeSharp.API.Modules.Entities.Constants; using CounterStrikeSharp.API.Modules.Utils; using RetakesAllocatorCore.Config; +using RetakesAllocatorCore.CounterStrikeSharpMock; using RetakesAllocatorCore.Db; -namespace RetakesAllocatorCore; +namespace RetakesAllocatorCore.Helpers; public class OnRoundPostStartHelper { - public static void Handle( + public static HookResult Handle( RoundType? nextRoundType, - ICollection allPlayers, - Func getSteamId, - Func getTeam, - Action giveDefuseKit, - Action, string?> allocateItemsForPlayer, - out RoundType currentRoundType + ICounterStrikeSharpMock counterStrikeSharp, + out RoundType? currentRoundType ) { var roundType = nextRoundType ?? RoundTypeHelpers.GetRandomRoundType(); currentRoundType = roundType; - var tPlayers = new List(); - var ctPlayers = new List(); + if (counterStrikeSharp.IsWarmup()) + { + return HookResult.Continue; + } + + var allPlayers = counterStrikeSharp.Utilities.GetPlayers(); + var tPlayers = new List(); + var ctPlayers = new List(); var playerIds = new List(); foreach (var player in allPlayers) { - var steamId = getSteamId(player); + var steamId = player.SteamId; if (steamId != 0) { playerIds.Add(steamId); } - var playerTeam = getTeam(player); + var playerTeam = player.Team; if (playerTeam == CsTeam.Terrorist) { tPlayers.Add(player); @@ -43,21 +46,21 @@ out RoundType currentRoundType } } - Log.Write($"#T Players: {string.Join(",", tPlayers.Select(getSteamId))}"); - Log.Write($"#CT Players: {string.Join(",", ctPlayers.Select(getSteamId))}"); + Log.Write($"#T Players: {string.Join(",", tPlayers.Select(p => p.SteamId))}"); + Log.Write($"#CT Players: {string.Join(",", ctPlayers.Select(p => p.SteamId))}"); var userSettingsByPlayerId = Queries.GetUsersSettings(playerIds); var defusingPlayer = Utils.Choice(ctPlayers); - HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => + HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => ps.Where(p => - userSettingsByPlayerId.TryGetValue(getSteamId(p), out var userSetting) && - userSetting.GetWeaponPreference(getTeam(p), WeaponAllocationType.Preferred) is not null) + userSettingsByPlayerId.TryGetValue(p.SteamId, out var userSetting) && + userSetting.GetWeaponPreference(p.Team, WeaponAllocationType.Preferred) is not null) .ToHashSet(); - ICollection tPreferredPlayers = new HashSet(); - ICollection ctPreferredPlayers = new HashSet(); + ICollection tPreferredPlayers = new HashSet(); + ICollection ctPreferredPlayers = new HashSet(); if (roundType == RoundType.FullBuy) { tPreferredPlayers = WeaponHelpers.SelectPreferredPlayers(FilterByPreferredWeaponPreference(tPlayers)); @@ -66,8 +69,8 @@ HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => foreach (var player in allPlayers) { - var team = getTeam(player); - var playerSteamId = getSteamId(player); + var team = player.Team; + var playerSteamId = player.SteamId; userSettingsByPlayerId.TryGetValue(playerSteamId, out var userSetting); var items = new List { @@ -96,15 +99,15 @@ HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => // On non-pistol rounds, everyone gets defuse kit and util if (roundType != RoundType.Pistol) { - giveDefuseKit(player); + counterStrikeSharp.GiveDefuseKit(player); items.AddRange(RoundTypeHelpers.GetRandomUtilForRoundType(roundType, team)); } else { // On pistol rounds, you get util *or* a defuse kit - if (getSteamId(defusingPlayer) == getSteamId(player)) + if (defusingPlayer?.SteamId == player.SteamId) { - giveDefuseKit(player); + counterStrikeSharp.GiveDefuseKit(player); } else { @@ -117,7 +120,16 @@ HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => items.AddRange(RoundTypeHelpers.GetRandomUtilForRoundType(roundType, team)); } - allocateItemsForPlayer(player, items, team == CsTeam.Terrorist ? "slot5" : "slot1"); + counterStrikeSharp.AllocateItemsForPlayer(player, items, team == CsTeam.Terrorist ? "slot5" : "slot1"); + } + + if (Configs.GetConfigData().EnableRoundTypeAnnouncement) + { + counterStrikeSharp.Server.PrintToChatAll( + $"{counterStrikeSharp.MessagePrefix}{Enum.GetName(roundType)} Round" + ); } + + return HookResult.Continue; } } diff --git a/RetakesAllocatorCore/Utils.cs b/RetakesAllocatorCore/Utils.cs index 184e1436..fbda08f0 100644 --- a/RetakesAllocatorCore/Utils.cs +++ b/RetakesAllocatorCore/Utils.cs @@ -72,4 +72,12 @@ public static void SetupSqlite(string connectionString, DbContextOptionsBuilder { optionsBuilder.UseSqlite(connectionString); } + + public static void WriteNewlineDelimited(string message, Action writer) + { + foreach (var line in message.Split("\n")) + { + writer($"{PluginInfo.MessagePrefix}{line}"); + } + } } From 3100fc22f6db7051355877faf9fe194713d04b0b Mon Sep 17 00:00:00 2001 From: Yoni Lerner Date: Tue, 16 Jan 2024 20:28:08 -0500 Subject: [PATCH 2/3] renames --- RetakesAllocator/CounterStrikeSharpImpl.cs | 52 +++++++++---------- .../ICCSPlayerControllerAdapter.cs} | 8 +-- .../ICCSPlayerPawnAdapter.cs | 8 +++ .../ICCSPlayer_ItemServicesAdapter.cs | 7 +++ .../ICommandInfoAdapter.cs | 7 +++ .../ICounterStrikeSharpAdapter.cs | 30 +++++++++++ .../INativeAPIAdapter.cs | 6 +++ .../IPlayerWeaponAdapter.cs} | 4 +- .../IServerAdapter.cs | 6 +++ .../IUtilitiesAdapter.cs | 6 +++ .../IVector.cs | 2 +- .../IWeaponServicesAdapter.cs | 6 +++ .../ICCSPlayerPawnMock.cs | 8 --- .../ICCSPlayer_ItemServicesMock.cs | 7 --- .../ICommandInfoMock.cs | 7 --- .../ICounterStrikeSharpMock.cs | 30 ----------- .../CounterStrikeSharpMock/INativeAPIMock.cs | 6 --- .../CounterStrikeSharpMock/IServerMock.cs | 6 --- .../CounterStrikeSharpMock/IUtilitiesMock.cs | 6 --- .../IWeaponServicesMock.cs | 6 --- .../Helpers/OnRoundPostStartHelper.cs | 14 ++--- 21 files changed, 116 insertions(+), 116 deletions(-) rename RetakesAllocatorCore/{CounterStrikeSharpMock/ICCSPlayerControllerMock.cs => CounterStrikeSharpInterfaces/ICCSPlayerControllerAdapter.cs} (56%) create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerPawnAdapter.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayer_ItemServicesAdapter.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICommandInfoAdapter.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICounterStrikeSharpAdapter.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/INativeAPIAdapter.cs rename RetakesAllocatorCore/{CounterStrikeSharpMock/IPlayerWeaponMock.cs => CounterStrikeSharpInterfaces/IPlayerWeaponAdapter.cs} (70%) create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/IServerAdapter.cs create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/IUtilitiesAdapter.cs rename RetakesAllocatorCore/{CounterStrikeSharpMock => CounterStrikeSharpInterfaces}/IVector.cs (58%) create mode 100644 RetakesAllocatorCore/CounterStrikeSharpInterfaces/IWeaponServicesAdapter.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs delete mode 100644 RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs diff --git a/RetakesAllocator/CounterStrikeSharpImpl.cs b/RetakesAllocator/CounterStrikeSharpImpl.cs index 5661a3d9..dbbeafd0 100644 --- a/RetakesAllocator/CounterStrikeSharpImpl.cs +++ b/RetakesAllocator/CounterStrikeSharpImpl.cs @@ -3,11 +3,11 @@ using CounterStrikeSharp.API.Modules.Entities.Constants; using CounterStrikeSharp.API.Modules.Utils; using RetakesAllocatorCore; -using RetakesAllocatorCore.CounterStrikeSharpMock; +using RetakesAllocatorCore.CounterStrikeSharpInterfaces; namespace RetakesAllocator; -public class NativeApiImpl : INativeAPIMock +public class NativeApiImpl : INativeAPIAdapter { public void IssueClientCommand(int clientIndex, string command) { @@ -15,7 +15,7 @@ public void IssueClientCommand(int clientIndex, string command) } } -public class ServerImpl : IServerMock +public class ServerImpl : IServerAdapter { public void PrintToChatAll(string message) { @@ -23,7 +23,7 @@ public void PrintToChatAll(string message) } } -public class PlayerWeaponImpl : IPlayerWeaponMock +public class PlayerWeaponImpl : IPlayerWeaponAdapter { private readonly CHandle _weapon; @@ -48,7 +48,7 @@ public void Remove() } } -public class WeaponServicesImpl : IWeaponServicesMock +public class WeaponServicesImpl : IWeaponServicesAdapter { private readonly CPlayer_WeaponServices _weaponServices; @@ -57,15 +57,15 @@ public WeaponServicesImpl(CPlayer_WeaponServices weaponServices) _weaponServices = weaponServices; } - public ICollection MyWeapons => + public ICollection MyWeapons => _weaponServices .MyWeapons .Select(w => new PlayerWeaponImpl(w)) - .Cast() + .Cast() .ToList(); } -public class CCSPlayerPawnImpl : ICCSPlayerPawnMock +public class CCSPlayerPawnImpl : ICCSPlayerPawnAdapter { private readonly CHandle _pawn; @@ -74,12 +74,12 @@ public CCSPlayerPawnImpl(CHandle pawn) _pawn = pawn; } - public IWeaponServicesMock? WeaponServices => + public IWeaponServicesAdapter? WeaponServices => _pawn.Value?.WeaponServices is not null ? new WeaponServicesImpl(_pawn.Value.WeaponServices) : null; - public void RemovePlayerItem(IPlayerWeaponMock weapon) + public void RemovePlayerItem(IPlayerWeaponAdapter weapon) { if (weapon.Handle is not null) { @@ -88,7 +88,7 @@ public void RemovePlayerItem(IPlayerWeaponMock weapon) } } -public class CCSPlayer_ItemServicesImpl : ICCSPlayer_ItemServicesMock +public class CCSPlayer_ItemServicesImpl : ICCSPlayer_ItemServicesAdapter { private readonly CCSPlayer_ItemServices? _itemServices; @@ -114,7 +114,7 @@ public bool HasDefuser public bool IsValid => _itemServices?.Handle is not null; } -public class CCSPlayerControllerImpl : ICCSPlayerControllerMock +public class CCSPlayerControllerImpl : ICCSPlayerControllerAdapter { private readonly CCSPlayerController? _player; @@ -132,11 +132,11 @@ public CCSPlayerControllerImpl(CCSPlayerController? player) public CsTeam Team => _player?.Team ?? CsTeam.None; - public ICCSPlayerPawnMock? PlayerPawn => _player is not null + public ICCSPlayerPawnAdapter? PlayerPawn => _player is not null ? new CCSPlayerPawnImpl(_player.PlayerPawn) : null; - public ICCSPlayer_ItemServicesMock? ItemServices => + public ICCSPlayer_ItemServicesAdapter? ItemServices => new CCSPlayer_ItemServicesImpl(_player?.PlayerPawn.Value?.ItemServices); public void GiveNamedItem(CsItem item) @@ -145,22 +145,22 @@ public void GiveNamedItem(CsItem item) } } -public class UtilitiesImpl : IUtilitiesMock +public class UtilitiesImpl : IUtilitiesAdapter { - public List GetPlayers() + public List GetPlayers() { return Utilities .GetPlayers() .Select(player => new CCSPlayerControllerImpl(player)) - .Cast().ToList(); + .Cast().ToList(); } } -public class CounterStrikeSharpImpl : ICounterStrikeSharpMock +public class CounterStrikeSharpImpl : ICounterStrikeSharpAdapter { - public INativeAPIMock NativeApi => new NativeApiImpl(); - public IServerMock Server => new ServerImpl(); - public IUtilitiesMock Utilities => new UtilitiesImpl(); + public INativeAPIAdapter NativeApi => new NativeApiImpl(); + public IServerAdapter Server => new ServerImpl(); + public IUtilitiesAdapter Utilities => new UtilitiesImpl(); public string MessagePrefix => PluginInfo.MessagePrefix; private readonly RetakesAllocator _plugin; @@ -170,7 +170,7 @@ public CounterStrikeSharpImpl(RetakesAllocator plugin) _plugin = plugin; } - public void AllocateItemsForPlayer(ICCSPlayerControllerMock player, ICollection items, string? slotToSelect) + public void AllocateItemsForPlayer(ICCSPlayerControllerAdapter player, ICollection items, string? slotToSelect) { // Log.Write($"Allocating items: {string.Join(",", items)}"); AddTimer(0.1f, () => @@ -199,7 +199,7 @@ public void AllocateItemsForPlayer(ICCSPlayerControllerMock player, ICollection< }); } - public void GiveDefuseKit(ICCSPlayerControllerMock player) + public void GiveDefuseKit(ICCSPlayerControllerAdapter player) { _plugin.AddTimer(0.1f, () => { @@ -216,12 +216,12 @@ public void AddTimer(float interval, Action callback) _plugin.AddTimer(interval, callback); } - public bool PlayerIsValid(ICCSPlayerControllerMock? player) + public bool PlayerIsValid(ICCSPlayerControllerAdapter? player) { return (player?.IsValid ?? false) && player.SteamId != 0; } - public ICollection CommandInfoToArgList(ICommandInfoMock commandInfo, bool includeFirst = false) + public ICollection CommandInfoToArgList(ICommandInfoAdapter commandInfo, bool includeFirst = false) { var result = new List(); @@ -233,7 +233,7 @@ public ICollection CommandInfoToArgList(ICommandInfoMock commandInfo, bo return result; } - public bool RemoveWeapons(ICCSPlayerControllerMock player, Func? where = null) + public bool RemoveWeapons(ICCSPlayerControllerAdapter player, Func? where = null) { if (!PlayerIsValid(player) || player.PlayerPawn?.WeaponServices is null) { diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerControllerAdapter.cs similarity index 56% rename from RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs rename to RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerControllerAdapter.cs index 83b5c854..c87deda3 100644 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerControllerMock.cs +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerControllerAdapter.cs @@ -1,18 +1,18 @@ using CounterStrikeSharp.API.Modules.Entities.Constants; using CounterStrikeSharp.API.Modules.Utils; -namespace RetakesAllocatorCore.CounterStrikeSharpMock; +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; -public interface ICCSPlayerControllerMock +public interface ICCSPlayerControllerAdapter { public int? UserId { get; } public bool IsValid { get; } public ulong SteamId { get; } public CsTeam Team { get; } - public ICCSPlayerPawnMock? PlayerPawn { get; } + public ICCSPlayerPawnAdapter? PlayerPawn { get; } - public ICCSPlayer_ItemServicesMock? ItemServices { get; } + public ICCSPlayer_ItemServicesAdapter? ItemServices { get; } public void GiveNamedItem(CsItem item); } diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerPawnAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerPawnAdapter.cs new file mode 100644 index 00000000..3be34258 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayerPawnAdapter.cs @@ -0,0 +1,8 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface ICCSPlayerPawnAdapter +{ + public IWeaponServicesAdapter? WeaponServices { get; } + + public void RemovePlayerItem(IPlayerWeaponAdapter weapon); +} \ No newline at end of file diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayer_ItemServicesAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayer_ItemServicesAdapter.cs new file mode 100644 index 00000000..aaf67081 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICCSPlayer_ItemServicesAdapter.cs @@ -0,0 +1,7 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface ICCSPlayer_ItemServicesAdapter +{ + public bool HasDefuser { get; set; } + public bool IsValid { get; } +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICommandInfoAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICommandInfoAdapter.cs new file mode 100644 index 00000000..3d2256a5 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICommandInfoAdapter.cs @@ -0,0 +1,7 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface ICommandInfoAdapter +{ + public string GetArg(int index); + public int ArgCount { get; } +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICounterStrikeSharpAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICounterStrikeSharpAdapter.cs new file mode 100644 index 00000000..a072e8df --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/ICounterStrikeSharpAdapter.cs @@ -0,0 +1,30 @@ +using CounterStrikeSharp.API.Modules.Entities.Constants; + +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface ICounterStrikeSharpAdapter +{ + public INativeAPIAdapter NativeApi { get; } + public IServerAdapter Server { get; } + public IUtilitiesAdapter Utilities { get; } + + public string MessagePrefix { get; } + + void AllocateItemsForPlayer(ICCSPlayerControllerAdapter player, ICollection items, string? slotToSelect); + + public void GiveDefuseKit(ICCSPlayerControllerAdapter player); + + public void AddTimer(float interval, Action callback); + + public bool PlayerIsValid(ICCSPlayerControllerAdapter? player); + + public ICollection CommandInfoToArgList(ICommandInfoAdapter commandInfo, bool includeFirst = false); + + public bool RemoveWeapons(ICCSPlayerControllerAdapter playerController, Func? where = null); + + public bool IsWarmup(); + + public bool IsWeaponAllocationAllowed(); + + public double GetVectorDistance(IVector v1, IVector v2); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/INativeAPIAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/INativeAPIAdapter.cs new file mode 100644 index 00000000..cea55606 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/INativeAPIAdapter.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface INativeAPIAdapter +{ + public void IssueClientCommand(int clientIndex, string command); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IPlayerWeaponAdapter.cs similarity index 70% rename from RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs rename to RetakesAllocatorCore/CounterStrikeSharpInterfaces/IPlayerWeaponAdapter.cs index 5e6fbc10..32d66091 100644 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/IPlayerWeaponMock.cs +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IPlayerWeaponAdapter.cs @@ -1,8 +1,8 @@ using CounterStrikeSharp.API.Modules.Entities.Constants; -namespace RetakesAllocatorCore.CounterStrikeSharpMock; +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; -public interface IPlayerWeaponMock +public interface IPlayerWeaponAdapter { public bool IsValid { get; } public string DesignerName { get; } diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IServerAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IServerAdapter.cs new file mode 100644 index 00000000..fae5881b --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IServerAdapter.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface IServerAdapter +{ + public void PrintToChatAll(string message); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IUtilitiesAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IUtilitiesAdapter.cs new file mode 100644 index 00000000..ce74198b --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IUtilitiesAdapter.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface IUtilitiesAdapter +{ + List GetPlayers(); +} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IVector.cs similarity index 58% rename from RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs rename to RetakesAllocatorCore/CounterStrikeSharpInterfaces/IVector.cs index 292098f8..7a0a09fb 100644 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/IVector.cs +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IVector.cs @@ -1,4 +1,4 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; public interface IVector { diff --git a/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IWeaponServicesAdapter.cs b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IWeaponServicesAdapter.cs new file mode 100644 index 00000000..54608d32 --- /dev/null +++ b/RetakesAllocatorCore/CounterStrikeSharpInterfaces/IWeaponServicesAdapter.cs @@ -0,0 +1,6 @@ +namespace RetakesAllocatorCore.CounterStrikeSharpInterfaces; + +public interface IWeaponServicesAdapter +{ + public ICollection MyWeapons { get; } +} \ No newline at end of file diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs deleted file mode 100644 index e6e4fc24..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayerPawnMock.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface ICCSPlayerPawnMock -{ - public IWeaponServicesMock? WeaponServices { get; } - - public void RemovePlayerItem(IPlayerWeaponMock weapon); -} \ No newline at end of file diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs deleted file mode 100644 index bc7ed0eb..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/ICCSPlayer_ItemServicesMock.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface ICCSPlayer_ItemServicesMock -{ - public bool HasDefuser { get; set; } - public bool IsValid { get; } -} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs deleted file mode 100644 index 77686ed9..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/ICommandInfoMock.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface ICommandInfoMock -{ - public string GetArg(int index); - public int ArgCount { get; } -} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs deleted file mode 100644 index 9af9b91e..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/ICounterStrikeSharpMock.cs +++ /dev/null @@ -1,30 +0,0 @@ -using CounterStrikeSharp.API.Modules.Entities.Constants; - -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface ICounterStrikeSharpMock -{ - public INativeAPIMock NativeApi { get; } - public IServerMock Server { get; } - public IUtilitiesMock Utilities { get; } - - public string MessagePrefix { get; } - - void AllocateItemsForPlayer(ICCSPlayerControllerMock player, ICollection items, string? slotToSelect); - - public void GiveDefuseKit(ICCSPlayerControllerMock player); - - public void AddTimer(float interval, Action callback); - - public bool PlayerIsValid(ICCSPlayerControllerMock? player); - - public ICollection CommandInfoToArgList(ICommandInfoMock commandInfo, bool includeFirst = false); - - public bool RemoveWeapons(ICCSPlayerControllerMock playerController, Func? where = null); - - public bool IsWarmup(); - - public bool IsWeaponAllocationAllowed(); - - public double GetVectorDistance(IVector v1, IVector v2); -} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs deleted file mode 100644 index d68cfbc3..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/INativeAPIMock.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface INativeAPIMock -{ - public void IssueClientCommand(int clientIndex, string command); -} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs deleted file mode 100644 index 477b3b75..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/IServerMock.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface IServerMock -{ - public void PrintToChatAll(string message); -} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs deleted file mode 100644 index 586da842..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/IUtilitiesMock.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface IUtilitiesMock -{ - List GetPlayers(); -} diff --git a/RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs b/RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs deleted file mode 100644 index 562ddb4d..00000000 --- a/RetakesAllocatorCore/CounterStrikeSharpMock/IWeaponServicesMock.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace RetakesAllocatorCore.CounterStrikeSharpMock; - -public interface IWeaponServicesMock -{ - public ICollection MyWeapons { get; } -} \ No newline at end of file diff --git a/RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs b/RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs index 33e7066b..958f0418 100644 --- a/RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs +++ b/RetakesAllocatorCore/Helpers/OnRoundPostStartHelper.cs @@ -2,7 +2,7 @@ using CounterStrikeSharp.API.Modules.Entities.Constants; using CounterStrikeSharp.API.Modules.Utils; using RetakesAllocatorCore.Config; -using RetakesAllocatorCore.CounterStrikeSharpMock; +using RetakesAllocatorCore.CounterStrikeSharpInterfaces; using RetakesAllocatorCore.Db; namespace RetakesAllocatorCore.Helpers; @@ -11,7 +11,7 @@ public class OnRoundPostStartHelper { public static HookResult Handle( RoundType? nextRoundType, - ICounterStrikeSharpMock counterStrikeSharp, + ICounterStrikeSharpAdapter counterStrikeSharp, out RoundType? currentRoundType ) { @@ -24,8 +24,8 @@ out RoundType? currentRoundType } var allPlayers = counterStrikeSharp.Utilities.GetPlayers(); - var tPlayers = new List(); - var ctPlayers = new List(); + var tPlayers = new List(); + var ctPlayers = new List(); var playerIds = new List(); foreach (var player in allPlayers) { @@ -53,14 +53,14 @@ out RoundType? currentRoundType var defusingPlayer = Utils.Choice(ctPlayers); - HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => + HashSet FilterByPreferredWeaponPreference(IEnumerable ps) => ps.Where(p => userSettingsByPlayerId.TryGetValue(p.SteamId, out var userSetting) && userSetting.GetWeaponPreference(p.Team, WeaponAllocationType.Preferred) is not null) .ToHashSet(); - ICollection tPreferredPlayers = new HashSet(); - ICollection ctPreferredPlayers = new HashSet(); + ICollection tPreferredPlayers = new HashSet(); + ICollection ctPreferredPlayers = new HashSet(); if (roundType == RoundType.FullBuy) { tPreferredPlayers = WeaponHelpers.SelectPreferredPlayers(FilterByPreferredWeaponPreference(tPlayers)); From 65ca7f701a44c592e801be3176035ed5748d6faf Mon Sep 17 00:00:00 2001 From: Yoni Lerner Date: Tue, 16 Jan 2024 21:08:32 -0500 Subject: [PATCH 3/3] it all works now --- RetakesAllocator/CounterStrikeSharpImpl.cs | 2 +- RetakesAllocator/RetakesAllocator.cs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/RetakesAllocator/CounterStrikeSharpImpl.cs b/RetakesAllocator/CounterStrikeSharpImpl.cs index dbbeafd0..b8c7fc51 100644 --- a/RetakesAllocator/CounterStrikeSharpImpl.cs +++ b/RetakesAllocator/CounterStrikeSharpImpl.cs @@ -175,7 +175,7 @@ public void AllocateItemsForPlayer(ICCSPlayerControllerAdapter player, ICollecti // Log.Write($"Allocating items: {string.Join(",", items)}"); AddTimer(0.1f, () => { - if (player.IsValid) + if (!player.IsValid) { // Log.Write($"Player is not valid when allocating item"); return; diff --git a/RetakesAllocator/RetakesAllocator.cs b/RetakesAllocator/RetakesAllocator.cs index 7a30de73..34272827 100644 --- a/RetakesAllocator/RetakesAllocator.cs +++ b/RetakesAllocator/RetakesAllocator.cs @@ -1,4 +1,6 @@ -using CounterStrikeSharp.API; +using System.Collections; +using System.Drawing; +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes; using CounterStrikeSharp.API.Core.Attributes.Registration; @@ -6,7 +8,6 @@ using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Entities.Constants; -using CounterStrikeSharp.API.Modules.Utils; using RetakesAllocatorCore; using RetakesAllocatorCore.Config; using RetakesAllocatorCore.Db; @@ -171,7 +172,7 @@ out var selectedWeapon WeaponAllocationType.Preferred => "slot1", _ => throw new ArgumentOutOfRangeException() }; - AllocateItemsForPlayer(player, new List {selectedWeapon.Value}, slot); + new CounterStrikeSharpImpl(this).AllocateItemsForPlayer(new CCSPlayerControllerImpl(player), new List {selectedWeapon.Value}, slot); } } } @@ -334,7 +335,7 @@ purchasedAllocationType is not null if (replacementItem is not null) { replacedWeapon = true; - AllocateItemsForPlayer(player, new List + new CounterStrikeSharpImpl(this).AllocateItemsForPlayer(new CCSPlayerControllerImpl(player), new List { replacementItem.Value }, slotToSelect);