Skip to content

Commit

Permalink
Fix menu problem
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarper committed Nov 3, 2024
1 parent 7c8c11b commit 98f0027
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 43 deletions.
23 changes: 22 additions & 1 deletion Store/src/api/api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,29 @@ namespace Store;

public class StoreAPI : IStoreApi
{
public StoreAPI()
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerPurchaseItem;
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerEquipItem;
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerUnequipItem;
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerSellItem;

public void PlayerPurchaseItem(CCSPlayerController player, Dictionary<string, string> item)
{
OnPlayerPurchaseItem?.Invoke(player, item);
}

public void PlayerEquipItem(CCSPlayerController player, Dictionary<string, string> item)
{
OnPlayerEquipItem?.Invoke(player, item);
}

public void PlayerUnequipItem(CCSPlayerController player, Dictionary<string, string> item)
{
OnPlayerUnequipItem?.Invoke(player, item);
}

public void PlayerSellItem(CCSPlayerController player, Dictionary<string, string> item)
{
OnPlayerSellItem?.Invoke(player, item);
}

public string GetDatabaseString()
Expand Down
25 changes: 22 additions & 3 deletions Store/src/config/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@ public static class Config_Config
{
public static Cfg Config { get; set; } = new Cfg();

private static readonly string ConfigPath;

static Config_Config()
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? string.Empty;

ConfigPath = Path.Combine(Server.GameDirectory,
"csgo",
"addons",
"counterstrikesharp",
"configs",
"plugins",
assemblyName,
"config.toml"
);
}

public static void Load()
{
string AssemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "";
string CfgPath = $"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{AssemblyName}";
if (!File.Exists(ConfigPath))
{
throw new FileNotFoundException($"Configuration file not found: {ConfigPath}");
}

LoadConfig($"{CfgPath}/config.toml");
LoadConfig(ConfigPath);

Task.Run(async () =>
{
Expand Down
5 changes: 3 additions & 2 deletions Store/src/cs2-store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Store;
public class Store : BasePlugin, IPluginConfig<Item_Config>
{
public override string ModuleName => "Store";
public override string ModuleVersion => "1.6";
public override string ModuleVersion => "1.7";
public override string ModuleAuthor => "schwarper";

public Item_Config Config { get; set; } = new Item_Config();
Expand All @@ -22,10 +22,11 @@ public class Store : BasePlugin, IPluginConfig<Item_Config>
public static Store Instance { get; set; } = new();
public Random Random { get; set; } = new();
public Dictionary<CCSPlayerController, float> GlobalGiftTimeout { get; set; } = [];
public static StoreAPI Api { get; set; } = new();

public override void Load(bool hotReload)
{
Capabilities.RegisterPluginCapability(IStoreApi.Capability, () => new StoreAPI());
Capabilities.RegisterPluginCapability(IStoreApi.Capability, () => Api);

Instance = this;

Expand Down
20 changes: 5 additions & 15 deletions Store/src/event/event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void Load()
Instance.RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath);
Instance.RegisterListener<OnClientAuthorized>(OnClientAuthorized);

Instance.AddTimer(5.0F, () =>
Instance.AddTimer(5.0f, () =>
{
StartCreditsTimer();
});
Expand All @@ -50,16 +50,11 @@ public static void StartCreditsTimer()
return;
}

foreach (CCSPlayerController player in Utilities.GetPlayers())
List<CCSPlayerController> players = Utilities.GetPlayers();

foreach (CCSPlayerController player in players)
{
if (player == null
|| !player.IsValid
|| player.PlayerPawn == null
|| !player.PlayerPawn.IsValid
|| player.PlayerPawn.Value == null
|| player.UserId == null
|| player.IsBot
|| player.IsHLTV)
if (player.IsBot)
{
continue;
}
Expand Down Expand Up @@ -96,11 +91,6 @@ public static void OnMapStart(string mapname)
type.MapStart();
});

Instance.AddTimer(5.0F, () =>
{
GameRules.GlobalGameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!;
}, TimerFlags.STOP_ON_MAPCHANGE);

Database.ExecuteAsync("DELETE FROM store_items WHERE DateOfExpiration < NOW() AND DateOfExpiration > '0001-01-01 00:00:00';", null);

List<Store_Item> itemsToRemove = Instance.GlobalStorePlayerItems
Expand Down
26 changes: 19 additions & 7 deletions Store/src/gamerules/gamerules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@ namespace Store;

public static class GameRules
{
public static CCSGameRules GlobalGameRules { get; set; } = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!;
private static CCSGameRulesProxy? GameRulesProxy;
private static readonly ConVar mp_halftime = ConVar.Find("mp_halftime")!;
private static readonly ConVar mp_maxrounds = ConVar.Find("mp_maxrounds")!;

public static bool IgnoreWarmUp()
{
return Config.Credits.IgnoreWarmup && GlobalGameRules.WarmupPeriod;
if (GameRulesProxy?.IsValid is not true)
{
GameRulesProxy = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").FirstOrDefault();
}

return Config.Credits.IgnoreWarmup && (GameRulesProxy?.GameRules?.WarmupPeriod ?? false);
}

public static bool IsPistolRound()
{
bool halftime = ConVar.Find("mp_halftime")!.GetPrimitiveValue<bool>();
int maxrounds = ConVar.Find("mp_maxrounds")!.GetPrimitiveValue<int>();
if (GameRulesProxy?.IsValid is not true)
{
GameRulesProxy = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").FirstOrDefault();
}

bool halftime = mp_halftime.GetPrimitiveValue<bool>();
int maxrounds = mp_maxrounds.GetPrimitiveValue<int>();

return GlobalGameRules.TotalRoundsPlayed == 0 ||
(halftime && maxrounds / 2 == GlobalGameRules.TotalRoundsPlayed) ||
GlobalGameRules.GameRestart;
return GameRulesProxy?.GameRules?.TotalRoundsPlayed == 0 ||
(halftime && maxrounds / 2 == GameRulesProxy?.GameRules?.TotalRoundsPlayed) ||
(GameRulesProxy?.GameRules?.GameRestart ?? false);
}
}
16 changes: 15 additions & 1 deletion Store/src/item/item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Store;

public static class Item
{
public static bool IsHidden(this Dictionary<string, string> item) => item.ContainsKey("hide") && item["hide"] == "true";

public static bool Give(CCSPlayerController player, Dictionary<string, string> item)
{
Store_Item_Types? type = Instance.GlobalStoreItemTypes.FirstOrDefault(i => i.Type == item["type"]);
Expand Down Expand Up @@ -43,6 +45,8 @@ public static bool Give(CCSPlayerController player, Dictionary<string, string> i

Instance.GlobalStorePlayerItems.Add(playeritem);

Store.Api.PlayerEquipItem(player, item);

Server.NextFrame(() =>
{
Database.SavePlayerItem(player, playeritem);
Expand Down Expand Up @@ -87,7 +91,7 @@ public static bool Purchase(CCSPlayerController player, Dictionary<string, strin
return false;
}

var price = int.Parse(item["price"]);
int price = int.Parse(item["price"]);

if (price > 0)
{
Expand All @@ -96,6 +100,8 @@ public static bool Purchase(CCSPlayerController player, Dictionary<string, strin
player.PrintToChatMessage("Purchase Succeeded", item["name"]);
}

Store.Api.PlayerPurchaseItem(player, item);

if (type.Equipable)
{
item.TryGetValue("expiration", out string? expirationtime);
Expand All @@ -114,6 +120,8 @@ public static bool Purchase(CCSPlayerController player, Dictionary<string, strin

Instance.GlobalStorePlayerItems.Add(playeritem);

Store.Api.PlayerEquipItem(player, item);

Server.NextFrame(() =>
{
Database.SavePlayerItem(player, playeritem);
Expand Down Expand Up @@ -176,6 +184,8 @@ public static bool Equip(CCSPlayerController player, Dictionary<string, string>

Instance.GlobalStorePlayerEquipments.Add(playeritem);

Store.Api.PlayerEquipItem(player, item);

Server.NextFrame(() =>
{
Database.SavePlayerEquipment(player, playeritem);
Expand All @@ -200,6 +210,8 @@ public static bool Unequip(CCSPlayerController player, Dictionary<string, string

Instance.GlobalStorePlayerEquipments.RemoveAll(p => p.SteamID == player.SteamID && p.UniqueId == item["uniqueid"]);

Store.Api.PlayerUnequipItem(player, item);

Server.NextFrame(() =>
{
Database.RemovePlayerEquipment(player, item["uniqueid"]);
Expand All @@ -223,6 +235,8 @@ public static bool Sell(CCSPlayerController player, Dictionary<string, string> i

Instance.GlobalStorePlayerItems.Remove(playeritem);

Store.Api.PlayerSellItem(player, item);

Server.NextFrame(() =>
{
Database.RemovePlayerItem(player, playeritem);
Expand Down
4 changes: 2 additions & 2 deletions Store/src/item/items/playerskin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static bool OnEquip(CCSPlayerController player, Dictionary<string, string
return false;
}

item.TryGetValue("skin", out var skn);
item.TryGetValue("skin", out string? skn);

player.ChangeModelDelay(item["uniqueid"], item["disable_leg"] is "true" or "1", int.Parse(item["slot"]), skn);

Expand Down Expand Up @@ -234,7 +234,7 @@ private static (string modelname, bool disableleg, string? skin)? GetStoreModel(
return null;
}

itemdata.TryGetValue("skin", out var skn);
itemdata.TryGetValue("skin", out string? skn);

return (item.UniqueId, itemdata["disable_leg"] is "true" or "1", skn);
}
Expand Down
26 changes: 20 additions & 6 deletions Store/src/menu/menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ public static void SetSettings(bool hotReload)

if (hotReload)
{
Players.Clear();

foreach (CCSPlayerController pl in Utilities.GetPlayers())
{
if (pl.IsBot)
{
continue;
}

Players[pl.Slot] = new WasdMenuPlayer
{
player = pl,
Expand Down Expand Up @@ -119,8 +126,7 @@ public static void DisplayItems(CCSPlayerController player, string key, Dictiona

foreach (int Slot in new[] { 1, 2, 3 })
{
if ((!playerSkinItems.Any(p => p.Value.TryGetValue("slot", out string? slot) && slot == Slot.ToString())) ||
(inventory && !playerSkinItems.Any(item => Item.PlayerHas(player, item.Value["type"], item.Value["uniqueid"], false))))
if (!IsAnyItemExistInPlayerSkins(player, Slot, inventory, playerSkinItems))
{
continue;
}
Expand Down Expand Up @@ -166,8 +172,6 @@ public static void DisplayItem(CCSPlayerController player, bool inventory, strin
continue;
}

bool isHidden = item.ContainsKey("hide") && item["hide"] == "true";

if (Item.PlayerHas(player, item["type"], item["uniqueid"], false))
{
AddMenuOption(player, menu, (player, option) =>
Expand All @@ -176,7 +180,7 @@ public static void DisplayItem(CCSPlayerController player, bool inventory, strin
DisplayItemOption(player, item, option);
}, item["name"]);
}
else if (!inventory && !isHidden)
else if (!inventory && !item.IsHidden())
{
if (int.Parse(item["price"]) <= 0)
{
Expand Down Expand Up @@ -354,7 +358,7 @@ public static void OnTick()

public static bool CheckFlag(CCSPlayerController player, Dictionary<string, string> item)
{
if (!item.TryGetValue("flag", out var flag) || string.IsNullOrWhiteSpace(flag))
if (!item.TryGetValue("flag", out string? flag) || string.IsNullOrWhiteSpace(flag))
{
return true;
}
Expand Down Expand Up @@ -385,4 +389,14 @@ public static bool CheckPermissionOrSteamID(CCSPlayerController player, string k

return key == player.SteamID.ToString();
}

public static bool IsAnyItemExistInPlayerSkins(CCSPlayerController player, int Slot, bool inventory, Dictionary<string, Dictionary<string, string>> playerSkinItems)
{
return
playerSkinItems.Select(i => i.Value).Any(item =>
CheckFlag(player, item) &&
(item.IsHidden() && Item.PlayerHas(player, item["type"], item["uniqueid"], false) || !item.IsHidden()) &&
item.TryGetValue("slot", out string? slot) && slot == Slot.ToString()) ||
(inventory && playerSkinItems.Any(item => Item.PlayerHas(player, item.Value["type"], item.Value["uniqueid"], false)));
}
}
9 changes: 3 additions & 6 deletions Store/src/menu/oldmenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public static void DisplayItems(CCSPlayerController player, string key, Dictiona

foreach (int Slot in new[] { 1, 2, 3 })
{
if ((!playerSkinItems.Any(p => p.Value.TryGetValue("slot", out string? slot) && slot == Slot.ToString())) ||
(inventory && !playerSkinItems.Any(item => Item.PlayerHas(player, item.Value["type"], item.Value["uniqueid"], false))))
if (!Menu.IsAnyItemExistInPlayerSkins(player, Slot, inventory, playerSkinItems))
{
continue;
}
Expand Down Expand Up @@ -106,8 +105,6 @@ public static void DisplayItem(CCSPlayerController player, bool inventory, strin
continue;
}

bool isHidden = item.ContainsKey("hide") && item["hide"] == "true";

if (Item.PlayerHas(player, item["type"], item["uniqueid"], false))
{
AddMenuOption(player, menu, (player, option) =>
Expand All @@ -116,15 +113,15 @@ public static void DisplayItem(CCSPlayerController player, bool inventory, strin
DisplayItemOption(player, item);
}, false, item["name"]);
}
else if (!inventory && !isHidden)
else if (!inventory && !item.IsHidden())
{
if (int.Parse(item["price"]) <= 0)
{
AddMenuOption(player, menu, (player, option) => SelectPurchase(player, item, false), false, "menu_store<purchase1>", item["name"]);
}
else
{
AddMenuOption(player, menu, (player, option) => SelectPurchase(player, item, true), true, "menu_store<purchase>", item["name"], item["price"]);
AddMenuOption(player, menu, (player, option) => SelectPurchase(player, item, true), false, "menu_store<purchase>", item["name"], item["price"]);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions StoreApi/IStoreApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public interface IStoreApi
{
public static readonly PluginCapability<IStoreApi?> Capability = new("store:api");

public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerPurchaseItem;
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerEquipItem;
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerUnequipItem;
public event Action<CCSPlayerController, Dictionary<string, string>>? OnPlayerSellItem;

public string GetDatabaseString();
public int GetPlayerCredits(CCSPlayerController player);
public int SetPlayerCredits(CCSPlayerController player, int credits);
Expand Down

0 comments on commit 98f0027

Please sign in to comment.