Skip to content
Open
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
7 changes: 5 additions & 2 deletions Modules/Config/RetakesAllocatorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public class RetakesAllocatorConfig : BasePluginConfig
public string[] TriggerWords { get; set; } = { "guns", "gun", "weapon", "weapons" };

[JsonPropertyName("AddSkipOption")]
public bool AddSkipOption { get; set; } = true;
public bool AddSkipOption { get; set; } = false;

[JsonPropertyName("AwpPermission")]
public string AwpPermission { get; set; } = string.Empty;

[JsonPropertyName("Weapons")]
public WeaponsSection Weapons { get; set; } = new();
Expand Down Expand Up @@ -123,7 +126,7 @@ public class PrefixConfig

public class PistolRoundConfig
{
public int RoundAmount { get; init; } = 2;
public int RoundAmount { get; init; } = 4;
public string WeaponT { get; init; } = "weapon_glock";
public string WeaponCt { get; init; } = "weapon_usp_silencer";
}
Expand Down
2 changes: 2 additions & 0 deletions Modules/Db/MySqlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public MySqlProvider(string connectionString)
`ct_primary` INT NOT NULL DEFAULT 0,
`t_secondary` INT NOT NULL DEFAULT 0,
`ct_secondary` INT NOT NULL DEFAULT 0,
`t_pistol_round` INT NOT NULL DEFAULT 0,
`ct_pistol_round` INT NOT NULL DEFAULT 0,
`give_awp` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE (`auth`)
Expand Down
2 changes: 2 additions & 0 deletions Modules/Db/SqliteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ name VARCHAR(128) NOT NULL,
ct_primary INTEGER NOT NULL DEFAULT 0,
t_secondary INTEGER NOT NULL DEFAULT 0,
ct_secondary INTEGER NOT NULL DEFAULT 0,
t_pistol_round INTEGER NOT NULL DEFAULT 0,
ct_pistol_round INTEGER NOT NULL DEFAULT 0,
give_awp INTEGER NOT NULL DEFAULT 0
);
""";
Expand Down
2 changes: 2 additions & 0 deletions Modules/Db/WeaponPreference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class WeaponPreference
public int CtPrimary { get; set; }
public int TSecondary { get; set; }
public int CtSecondary { get; set; }
public int TPistolRound { get; set; }
public int CtPistolRound { get; set; }
public int GiveAwp { get; set; }
}
18 changes: 17 additions & 1 deletion Modules/Db/WeaponStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public async Task InitializeAsync()
await using var conn = _provider.CreateConnection();
await conn.OpenAsync();
await conn.ExecuteAsync(_provider.CreateTableSql);
await AddColumnIfMissing(conn, "t_pistol_round");
await AddColumnIfMissing(conn, "ct_pistol_round");
}

private static async Task AddColumnIfMissing(System.Data.Common.DbConnection conn, string column)
{
try
{
await conn.ExecuteAsync($"ALTER TABLE weapons ADD COLUMN {column} INTEGER NOT NULL DEFAULT 0");
}
catch
{
// Column already exists. CREATE TABLE handles fresh installs; this keeps old DBs working.
}
}

/// <summary>Returns the stored preferences for a SteamID, or null if none exist.</summary>
Expand All @@ -36,7 +50,7 @@ public async Task InitializeAsync()
await conn.OpenAsync();
return await conn.QuerySingleOrDefaultAsync<WeaponPreference>(
"""
SELECT auth, name, t_primary, ct_primary, t_secondary, ct_secondary, give_awp
SELECT auth, name, t_primary, ct_primary, t_secondary, ct_secondary, t_pistol_round, ct_pistol_round, give_awp
FROM weapons
WHERE auth = @auth
""",
Expand Down Expand Up @@ -68,6 +82,8 @@ UPDATE weapons
ct_primary = @CtPrimary,
t_secondary = @TSecondary,
ct_secondary = @CtSecondary,
t_pistol_round = @TPistolRound,
ct_pistol_round = @CtPistolRound,
give_awp = @GiveAwp
WHERE auth = @Auth
""",
Expand Down
2 changes: 1 addition & 1 deletion Modules/Handlers/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void GunsCommand(CCSPlayerController? player, CommandInfo command
return;
}

OpenTPrimaryMenu(player);
OpenPistolRoundTMenu(player);
}

private static void CTGunsCommand(CCSPlayerController? player, CommandInfo commandInfo)
Expand Down
2 changes: 1 addition & 1 deletion Modules/Handlers/Listeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static HookResult OnSay(CCSPlayerController? player, CommandInfo command
return HookResult.Continue;
}

OpenTPrimaryMenu(player);
OpenPistolRoundTMenu(player);

return HookResult.Continue;
}
Expand Down
36 changes: 32 additions & 4 deletions Modules/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Admin;
using Microsoft.Extensions.Logging;
using static RetakesAllocator.Modules.Core;
using RetakesAllocator.Modules.Weapons;
Expand Down Expand Up @@ -34,6 +35,24 @@ public static Player FindPlayer(CCSPlayerController controller)
return Players.Find(player => player.PlayerIndex == controller.Index)!;
}

public static bool HasAwpAccess(CCSPlayerController player)
{
var permission = Core.Config.AwpPermission.Trim();

if (permission.Length == 0)
{
return true;
}

if (!permission.StartsWith('@'))
{
permission = $"@{permission}";
}

return AdminManager.PlayerHasPermissions(player, permission)
|| AdminManager.GetPlayerAdminData(player.AuthorizedSteamID) != null;
}

public static void ServerCommand(string command, params object[] args)
{
Server.ExecuteCommand(string.Format(command, args));
Expand Down Expand Up @@ -102,13 +121,20 @@ private static void ApplyPreferences(Player playerObj, WeaponPreference pref)
{
var allocator = playerObj.WeaponsAllocator;

allocator.PrimaryWeaponT = pref.TPrimary > Allocator.PrimaryT.Count ? 0 : pref.TPrimary;
allocator.PrimaryWeaponCt = pref.CtPrimary > Allocator.PrimaryCt.Count ? 0 : pref.CtPrimary;
allocator.SecondaryWeaponT = pref.TSecondary > Allocator.PistolsT.Count ? 0 : pref.TSecondary;
allocator.SecondaryWeaponCt = pref.CtSecondary > Allocator.PistolsCT.Count ? 0 : pref.CtSecondary;
allocator.PrimaryWeaponT = SafeWeaponIndex(pref.TPrimary, Allocator.PrimaryT.Count);
allocator.PrimaryWeaponCt = SafeWeaponIndex(pref.CtPrimary, Allocator.PrimaryCt.Count);
allocator.SecondaryWeaponT = SafeWeaponIndex(pref.TSecondary, Allocator.PistolsT.Count);
allocator.SecondaryWeaponCt = SafeWeaponIndex(pref.CtSecondary, Allocator.PistolsCT.Count);
allocator.PistolRoundWeaponT = SafeWeaponIndex(pref.TPistolRound, Allocator.PistolsT.Count);
allocator.PistolRoundWeaponCt = SafeWeaponIndex(pref.CtPistolRound, Allocator.PistolsCT.Count);
allocator.GiveAwp = (GiveAwp)pref.GiveAwp;
}

private static int SafeWeaponIndex(int index, int count)
{
return index < 0 || index >= count ? 0 : index;
}

public static void RemovePlayerFromList(CCSPlayerController player, bool flush = false)
{
if (player == null || !player.IsValid || player.IsBot)
Expand All @@ -131,6 +157,8 @@ public static void RemovePlayerFromList(CCSPlayerController player, bool flush =
CtPrimary = playerObj.WeaponsAllocator.PrimaryWeaponCt,
TSecondary = playerObj.WeaponsAllocator.SecondaryWeaponT,
CtSecondary = playerObj.WeaponsAllocator.SecondaryWeaponCt,
TPistolRound = playerObj.WeaponsAllocator.PistolRoundWeaponT,
CtPistolRound = playerObj.WeaponsAllocator.PistolRoundWeaponCt,
GiveAwp = (int)playerObj.WeaponsAllocator.GiveAwp,
};

Expand Down
68 changes: 62 additions & 6 deletions Modules/Weapons/Allocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using RetakesAllocator.Modules.Votes;

using static RetakesAllocator.Modules.Weapons.Menu;
using static RetakesAllocator.Modules.Utils;
using static RetakesAllocator.Modules.Votes.Votes;

namespace RetakesAllocator.Modules.Weapons;
Expand All @@ -23,32 +24,46 @@ public enum WeaponType
PrimaryT,
PrimaryCt,
SecondaryT,
SecondaryCt
SecondaryCt,
PistolRoundT,
PistolRoundCt
};

public static List<Weapon> PrimaryT = new()
{
new Weapon("weapon_ak47", "AK-47"),
new Weapon("weapon_galilar", "Galil"),
new Weapon("weapon_sg556", "SG 553")
};

public static List<Weapon> PrimaryCt = new()
{
new Weapon("weapon_m4a1", "M4A4"),
new Weapon("weapon_m4a1_silencer", "M4A1-S"),
new Weapon("weapon_m4a1", "M4A4"),
new Weapon("weapon_famas", "FAMAS"),
new Weapon("weapon_aug", "AUG")
};

public static List<Weapon> PistolsT = new()
{
new Weapon("weapon_glock", "Glock-18"),
new Weapon("weapon_deagle", "Desert Eagle"),
new Weapon("weapon_p250", "P250"),
new Weapon("weapon_tec9", "Tec-9"),
new Weapon("weapon_elite", "Dual Berettas"),
new Weapon("weapon_cz75a", "CZ75"),
new Weapon("weapon_revolver", "Revolver")
};

public static List<Weapon> PistolsCT = new()
{
new Weapon("weapon_usp_silencer", "USP-S"),
new Weapon("weapon_deagle", "Desert Eagle"),
new Weapon("weapon_p250", "P250"),
new Weapon("weapon_fiveseven", "Five-SeveN"),
new Weapon("weapon_elite", "Dual Berettas"),
new Weapon("weapon_cz75a", "CZ75"),
new Weapon("weapon_revolver", "Revolver"),
new Weapon("weapon_hkp2000", "P2000")
};

Expand All @@ -61,6 +76,8 @@ public enum WeaponType
public int PrimaryWeaponCt = 0;
public int SecondaryWeaponT = 0;
public int SecondaryWeaponCt = 0;
public int PistolRoundWeaponT = GetWeaponIndex(Core.Config?.PistolRound.WeaponT ?? "weapon_glock", PistolsT);
public int PistolRoundWeaponCt = GetWeaponIndex(Core.Config?.PistolRound.WeaponCt ?? "weapon_usp_silencer", PistolsCT);

public GiveAwp GiveAwp = GiveAwp.Never;
public bool ShouldGiveAwp = false;
Expand All @@ -71,6 +88,12 @@ public static void ResetNades()
_nades = new Nades(Core.NadesConfig.TNades);
}

private static int GetWeaponIndex(string item, List<Weapon> weapons)
{
var index = weapons.FindIndex(w => w.Item == item);
return index < 0 ? 0 : index;
}

public static int GetWeaponIndex(string weapon, WeaponType type)
{
switch(type)
Expand All @@ -83,13 +106,22 @@ public static int GetWeaponIndex(string weapon, WeaponType type)
return PistolsT.FindIndex(w => w.DisplayName == weapon);
case WeaponType.SecondaryCt:
return PistolsCT.FindIndex(w => w.DisplayName == weapon);
case WeaponType.PistolRoundT:
return PistolsT.FindIndex(w => w.DisplayName == weapon);
case WeaponType.PistolRoundCt:
return PistolsCT.FindIndex(w => w.DisplayName == weapon);
}

return -1;
}

public bool SetupGiveAwp()
{
if (!HasAwpAccess(CCsPlayerController))
{
return false;
}

var giveAwp = GiveAwp switch
{
GiveAwp.Always => true,
Expand Down Expand Up @@ -205,7 +237,7 @@ public void Allocate()
}
else
{
primary = CCsPlayerController.Team == CsTeam.Terrorist ? PrimaryT[PrimaryWeaponT].Item : PrimaryCt[PrimaryWeaponCt].Item;
primary = GetPrimaryWeapon();
}

string secondary;
Expand All @@ -229,6 +261,21 @@ public void Allocate()
}
}

private string GetPrimaryWeapon()
{
var weapons = CCsPlayerController.Team == CsTeam.Terrorist ? PrimaryT : PrimaryCt;
var selectedIndex = CCsPlayerController.Team == CsTeam.Terrorist ? PrimaryWeaponT : PrimaryWeaponCt;
var primary = weapons[selectedIndex].Item;

if (HasAwpAccess(CCsPlayerController) || !primary.Equals("weapon_awp", StringComparison.OrdinalIgnoreCase))
{
return primary;
}

return weapons.FirstOrDefault(w => !w.Item.Equals("weapon_awp", StringComparison.OrdinalIgnoreCase))?.Item
?? (CCsPlayerController.Team == CsTeam.Terrorist ? "weapon_ak47" : "weapon_m4a1");
}

public void AllocatePistolRound()
{
if (!player.Controller.IsValid)
Expand All @@ -246,7 +293,9 @@ public void AllocatePistolRound()
return;
}

var secondary = (CCsPlayerController.Team == CsTeam.Terrorist) ? Core.Config.PistolRound.WeaponT : Core.Config.PistolRound.WeaponCt;
var secondary = CCsPlayerController.Team == CsTeam.Terrorist
? PistolsT[PistolRoundWeaponT].Item
: PistolsCT[PistolRoundWeaponCt].Item;

CCsPlayerController.GiveNamedItem(secondary);
CCsPlayerController.GiveNamedItem(CsItem.Knife);
Expand Down Expand Up @@ -276,10 +325,17 @@ public void AllocateVote(Vote vote)

var weapons = CCsPlayerController.Team == CsTeam.Terrorist ? vote.WeaponsT : vote.WeaponsCt;

if (!HasAwpAccess(CCsPlayerController))
{
weapons = weapons.Where(w => !w.Equals("awp", StringComparison.OrdinalIgnoreCase)).ToList();
}

if(weapons.Count > 1)
{
ShowWeaponSelectionMenu(CCsPlayerController, weapons, WeaponSelectionTime);
} else {
}
else if(weapons.Count == 1)
{
CCsPlayerController.GiveNamedItem("weapon_" + weapons.First());
}

Expand Down Expand Up @@ -324,4 +380,4 @@ private void GiveCtEquipment()
};
}
}
}
}
Loading
Loading