Skip to content

Commit 876ba6d

Browse files
authored
Merge pull request #103 from T3Marius/main
feat: added ScreenMenu and modified the toml config.
2 parents 98f0027 + 9e55e0e commit 876ba6d

File tree

6 files changed

+291
-10
lines changed

6 files changed

+291
-10
lines changed

Store/cs2-store.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.279" />
14+
<PackageReference Include="CS2ScreenMenuAPI" Version="1.8.0" />
1415
<PackageReference Include="Daffer" Version="1.0.3" />
1516
<PackageReference Include="MySqlConnector" Version="2.3.7" />
1617
<PackageReference Include="Tomlyn" Version="0.17.0" />

Store/src/command/command.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,24 @@ public static void Command_Store(CCSPlayerController? player, CommandInfo comman
5252
{
5353
return;
5454
}
55+
switch (Config.Menu.MenuType.ToLower())
56+
{
57+
case "wasd":
58+
Menu.DisplayStore(player, false);
59+
break;
60+
61+
case "html":
62+
case "center":
63+
OldMenu.DisplayStore(player, false);
64+
break;
5565

56-
Menu.DisplayStore(player, false);
66+
case "worldtext":
67+
case "screen":
68+
case "screenmenu":
69+
WorldTextMenu.DisplayStore(player, false);
70+
break;
71+
72+
}
5773
}
5874

5975
[CommandHelper(minArgs: 0, whoCanExecute: CommandUsage.CLIENT_ONLY)]

Store/src/config/config.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private static void LoadConfig(string configPath)
162162
{
163163
EnableSelling = bool.Parse(menuTable["EnableSelling"].ToString()!),
164164
EnableConfirmMenu = bool.Parse(menuTable["EnableConfirmMenu"].ToString()!),
165-
UseWASDMenu = bool.Parse(menuTable["UseWASDMenu"].ToString()!),
165+
MenuType = menuTable["MenuType"].ToString()!,
166166
VipFlag = menuTable["VipFlag"].ToString()!,
167167
MenuPressSoundYes = menuTable["MenuPressSoundYes"].ToString()!,
168168
MenuPressSoundNo = menuTable["MenuPressSoundNo"].ToString()!
@@ -244,7 +244,7 @@ public class Config_Menu
244244
{
245245
public bool EnableSelling { get; set; } = true;
246246
public bool EnableConfirmMenu { get; set; } = true;
247-
public bool UseWASDMenu { get; set; } = true;
247+
public string MenuType { get; set; } = "worldtext";
248248
public string VipFlag { get; set; } = "@css/root";
249249
public string MenuPressSoundYes { get; set; } = "";
250250
public string MenuPressSoundNo { get; set; } = "";

Store/src/menu/WorldTextMenu.cs

+270
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
using System.Text;
2+
using CounterStrikeSharp.API.Core;
3+
using CounterStrikeSharp.API.Core.Translations;
4+
using CS2ScreenMenuAPI;
5+
using CS2ScreenMenuAPI.Interfaces;
6+
using CS2ScreenMenuAPI.Internal;
7+
using static Store.Config_Config;
8+
using static Store.Store;
9+
using static StoreApi.Store;
10+
11+
namespace Store;
12+
13+
public static class WorldTextMenu
14+
{
15+
public static void AddMenuOption(CCSPlayerController player, ScreenMenu menu, Action<CCSPlayerController, IMenuOption> onSelect, bool disabled, string display, params object[] args)
16+
{
17+
using (new WithTemporaryCulture(player.GetLanguage()))
18+
{
19+
StringBuilder builder = new();
20+
builder.AppendFormat(Instance.Localizer[display, args]);
21+
22+
menu.AddOption(builder.ToString(), onSelect, disabled);
23+
}
24+
}
25+
26+
public static void DisplayStore(CCSPlayerController player, bool inventory)
27+
{
28+
using (new WithTemporaryCulture(player.GetLanguage()))
29+
{
30+
StringBuilder builder = new();
31+
builder.AppendFormat(Instance.Localizer["menu_store<title>", Credits.Get(player)]);
32+
33+
ScreenMenu menu = new(builder.ToString(), Instance)
34+
{
35+
IsSubMenu = false,
36+
};
37+
38+
foreach (KeyValuePair<string, Dictionary<string, Dictionary<string, string>>> category in Instance.Config.Items)
39+
{
40+
if (inventory && !category.Value.Values.Any(item => Item.PlayerHas(player, item["type"], item["uniqueid"], false)))
41+
{
42+
continue;
43+
}
44+
45+
StringBuilder builderkey = new();
46+
builderkey.AppendFormat(Instance.Localizer[$"menu_store<{category.Key}>"]);
47+
48+
menu.AddOption(builderkey.ToString(), (CCSPlayerController player, IMenuOption option) =>
49+
{
50+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
51+
DisplayItems(player, builderkey.ToString(), category.Value, inventory, menu);
52+
});
53+
}
54+
55+
MenuAPI.OpenMenu(Instance, player, menu);
56+
}
57+
}
58+
59+
public static void DisplayItems(CCSPlayerController player, string key, Dictionary<string, Dictionary<string, string>> items, bool inventory, ScreenMenu parentMenu)
60+
{
61+
Dictionary<string, Dictionary<string, string>> playerSkinItems = items.Where(p => p.Value["type"] == "playerskin" && p.Value["enable"] == "true").ToDictionary(p => p.Key, p => p.Value);
62+
63+
if (playerSkinItems.Count != 0)
64+
{
65+
ScreenMenu menu = new(key, Instance)
66+
{
67+
IsSubMenu = true,
68+
ParentMenu = parentMenu
69+
};
70+
71+
foreach (int Slot in new[] { 1, 2, 3 })
72+
{
73+
if (!Menu.IsAnyItemExistInPlayerSkins(player, Slot, inventory, playerSkinItems))
74+
{
75+
continue;
76+
}
77+
78+
using (new WithTemporaryCulture(player.GetLanguage()))
79+
{
80+
StringBuilder builder = new();
81+
builder.AppendFormat(Instance.Localizer[$"menu_store<{(Slot == 1 ? "all" : Slot == 2 ? "t" : "ct")}_title>"]);
82+
83+
menu.AddOption(builder.ToString(), (CCSPlayerController player, IMenuOption option) =>
84+
{
85+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
86+
DisplayItem(player, inventory, builder.ToString(), playerSkinItems.Where(p => p.Value.TryGetValue("slot", out string? slot) && !string.IsNullOrEmpty(slot) && int.Parse(p.Value["slot"]) == Slot).ToDictionary(p => p.Key, p => p.Value), menu);
87+
});
88+
}
89+
}
90+
MenuAPI.OpenSubMenu(Instance, player, menu);
91+
}
92+
else
93+
{
94+
DisplayItem(player, inventory, key, items, parentMenu);
95+
}
96+
}
97+
98+
public static void DisplayItem(CCSPlayerController player, bool inventory, string key, Dictionary<string, Dictionary<string, string>> items, ScreenMenu parentMenu)
99+
{
100+
ScreenMenu menu = new(key, Instance)
101+
{
102+
IsSubMenu = true,
103+
ParentMenu = parentMenu
104+
};
105+
106+
foreach (KeyValuePair<string, Dictionary<string, string>> kvp in items)
107+
{
108+
Dictionary<string, string> item = kvp.Value;
109+
110+
if (item["enable"] != "true" || !Menu.CheckFlag(player, item))
111+
{
112+
continue;
113+
}
114+
115+
if (inventory && !Item.PlayerHas(player, item["type"], item["uniqueid"], false))
116+
{
117+
continue;
118+
}
119+
120+
if (Item.PlayerHas(player, item["type"], item["uniqueid"], false))
121+
{
122+
AddMenuOption(player, menu, (player, option) =>
123+
{
124+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
125+
DisplayItemOption(player, item, parentMenu);
126+
}, false, item["name"]);
127+
}
128+
else if (!inventory && !item.IsHidden())
129+
{
130+
if (int.Parse(item["price"]) <= 0)
131+
{
132+
AddMenuOption(player, menu, (player, option) => SelectPurchase(player, item, false, parentMenu), false, "menu_store<purchase1>", item["name"]);
133+
}
134+
else
135+
{
136+
AddMenuOption(player, menu, (player, option) => SelectPurchase(player, item, true, parentMenu), false, "menu_store<purchase>", item["name"], item["price"]);
137+
}
138+
}
139+
}
140+
141+
MenuAPI.OpenSubMenu(Instance, player, menu);
142+
}
143+
144+
private static void SelectPurchase(CCSPlayerController player, Dictionary<string, string> item, bool confirm, ScreenMenu parentMenu)
145+
{
146+
if (confirm && Config.Menu.EnableConfirmMenu)
147+
{
148+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
149+
DisplayConfirmationMenu(player, item, parentMenu);
150+
}
151+
else
152+
{
153+
if (Item.Purchase(player, item))
154+
{
155+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
156+
DisplayItemOption(player, item, parentMenu);
157+
}
158+
else
159+
{
160+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundNo}");
161+
MenuAPI.CloseActiveMenu(player);
162+
}
163+
}
164+
}
165+
166+
public static void DisplayItemOption(CCSPlayerController player, Dictionary<string, string> item, ScreenMenu parentMenu)
167+
{
168+
ScreenMenu menu = new(item["name"], Instance)
169+
{
170+
IsSubMenu = true,
171+
ParentMenu = parentMenu
172+
};
173+
174+
if (Item.PlayerUsing(player, item["type"], item["uniqueid"]))
175+
{
176+
AddMenuOption(player, menu, (player, option) =>
177+
{
178+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
179+
Item.Unequip(player, item, true);
180+
181+
player.PrintToChatMessage("Purchase Unequip", item["name"]);
182+
183+
DisplayItemOption(player, item, parentMenu);
184+
}, false, "menu_store<unequip>");
185+
}
186+
else
187+
{
188+
AddMenuOption(player, menu, (player, option) =>
189+
{
190+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
191+
Item.Equip(player, item);
192+
193+
player.PrintToChatMessage("Purchase Equip", item["name"]);
194+
195+
DisplayItemOption(player, item, parentMenu);
196+
}, false, "menu_store<equip>");
197+
}
198+
199+
Store_Item? PlayerItems = Instance.GlobalStorePlayerItems.FirstOrDefault(p => p.SteamID == player.SteamID && p.Type == item["type"] && p.UniqueId == item["uniqueid"]);
200+
201+
if (Config.Menu.EnableSelling && !Item.IsPlayerVip(player))
202+
{
203+
float sell_ratio = Config.Settings.SellRatio;
204+
205+
int purchase_price = 1;
206+
207+
bool usePurchaseCredit = Config.Settings.SellUsePurchaseCredit;
208+
209+
if (usePurchaseCredit && PlayerItems != null)
210+
{
211+
purchase_price = PlayerItems.Price;
212+
}
213+
214+
int sellingPrice = (int)((usePurchaseCredit ? purchase_price : int.Parse(item["price"])) * sell_ratio);
215+
216+
if (sellingPrice > 1)
217+
{
218+
AddMenuOption(player, menu, (player, option) =>
219+
{
220+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
221+
Item.Sell(player, item);
222+
223+
player.PrintToChatMessage("Item Sell", item["name"]);
224+
225+
MenuAPI.CloseActiveMenu(player);
226+
}, false, "menu_store<sell>", sellingPrice);
227+
}
228+
}
229+
230+
if (PlayerItems != null && PlayerItems.DateOfExpiration > DateTime.MinValue)
231+
{
232+
menu.AddOption(PlayerItems.DateOfExpiration.ToString(), (p, o) => { }, true);
233+
}
234+
MenuAPI.OpenSubMenu(Instance, player, menu);
235+
}
236+
237+
public static void DisplayConfirmationMenu(CCSPlayerController player, Dictionary<string, string> item, ScreenMenu parentMenu)
238+
{
239+
ScreenMenu menu = new(Instance.Localizer["menu_store<confirm_title>"], Instance)
240+
{
241+
IsSubMenu = true,
242+
ParentMenu = parentMenu
243+
};
244+
245+
AddMenuOption(player, menu, (p, o) => { }, true, "menu_store<confirm_item>", item["name"], item["price"]);
246+
247+
AddMenuOption(player, menu, (p, o) =>
248+
{
249+
if (Item.Purchase(p, item))
250+
{
251+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundYes}");
252+
DisplayItemOption(p, item, parentMenu);
253+
}
254+
else
255+
{
256+
MenuAPI.CloseActiveMenu(player);
257+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundNo}");
258+
}
259+
260+
}, false, "menu_store<yes>");
261+
262+
AddMenuOption(player, menu, (p, o) =>
263+
{
264+
player.ExecuteClientCommand($"play {Config.Menu.MenuPressSoundNo}");
265+
MenuAPI.CloseActiveMenu(player);
266+
}, false, "menu_store<no>");
267+
268+
MenuAPI.OpenSubMenu(Instance, player, menu);
269+
}
270+
}

Store/src/menu/menu.cs

-6
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ public static void AddMenuOption(CCSPlayerController player, IWasdMenu menu, Act
8080

8181
public static void DisplayStore(CCSPlayerController player, bool inventory)
8282
{
83-
if (!Config.Menu.UseWASDMenu)
84-
{
85-
OldMenu.DisplayStore(player, inventory);
86-
return;
87-
}
88-
8983
using (new WithTemporaryCulture(player.GetLanguage()))
9084
{
9185
StringBuilder builder = new();

config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ IgnoreWarmup = true
3434
[Menu]
3535
EnableSelling = true
3636
EnableConfirmMenu = true
37-
UseWASDMenu = true # If you mark it as false, it will be used as CentreHtmlMenu.
37+
MenuType = "worldtext" # wasd | html
3838
VipFlag = "@css/root" # Individuals in possession of the requisite authorisation shall be entitled to utilise equipable items free of charge. Making this field empty will result in the cancellation of Vip.
3939
MenuPressSoundYes = ""
4040
MenuPressSoundNo = ""

0 commit comments

Comments
 (0)