-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2-store-cases.cs
594 lines (484 loc) · 20.9 KB
/
cs2-store-cases.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Menu;
using StoreApi;
using System.Collections.Concurrent;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Reflection;
namespace Store_Cases;
public class Store_CasesConfig : BasePluginConfig
{
[JsonPropertyName("Store_Cases_commands")]
public List<string> StoreCasesCommands { get; set; } = ["storecases", "buycase"];
[JsonPropertyName("Store_Quick_Buy_Cases_commands")]
public List<string> StoreQuickBuyCasesCommands { get; set; } = ["quickbuycase"];
[JsonPropertyName("Store_Give_Cases_commands")]
public List<string> StoreGiveCasesCommands { get; set; } = ["givecase"];
[JsonPropertyName("Store_Give_Cases_permission")]
public string StoreGiveCasesPermission { get; set; } = "@css/root";
[JsonPropertyName("Buy_Cases_Cooldown")]
public int BuyCasesCooldown { get; set; } = 10;
[JsonPropertyName("Menu_Type")]
public string MenuType { get; set; } = "html"; // html | chat
[JsonPropertyName("Item_Action_If_Owned")]
public int ItemActionIfOwned { get; set; } = 1; // 0 = Nothing | 1 = Reward with Credits
[JsonPropertyName("Credit_Percentage")]
public int ItemActionCreditPercentage { get; set; } = 50;
[JsonPropertyName("Cases")]
public List<CaseItem> Cases { get; set; } = new()
{
new CaseItem
{
Name = "Standard",
Price = 500,
Flag = "@css/vip",
Rewards = new List<CaseReward>
{
new CaseReward { Type = "credits", Value = "1000", Description = "1000 Credits", Chance = 90, PrintToChatAll = false },
new CaseReward { Type = "playerskin", Value = "characters/models/nozb1/2b_nier_automata_player_model/2b_nier_player_model.vmdl", Description = "2B Player Model", Expiration = 172800, Chance = 10, PrintToChatAll = true }
}
},
new CaseItem
{
Name = "Example",
Price = 1000,
Rewards = new List<CaseReward>
{
new CaseReward { Type = "credits", Value = "3000", Description = "3000 Credits", Chance = 50, PrintToChatAll = false },
new CaseReward { Type = "playerskin", Value = "characters/models/ctm_diver/ctm_diver_variantb.vmdl", Description = "Fernandez Frogman", Expiration = 86400, Chance = 20, PrintToChatAll = false },
new CaseReward { Type = "playerskin", Value = "characters/models/tm_professional/tm_professional_vari.vmdl", Description = "Number K", Expiration = 0, Chance = 15, PrintToChatAll = true },
new CaseReward { Type = "vip", Value = "css_vip_adduser \"{SteamID}\" \"VIP_Bronze\" \"1440\"", Description = "Vip Bronze (1 day)", Chance = 15, PrintToChatAll = true }
}
}
};
[JsonPropertyName("Animation_Duration")]
public float AnimationDuration { get; set; } = 3f;
[JsonPropertyName("Animation_Interval")]
public float AnimationInterval { get; set; } = 0.1f;
[JsonPropertyName("Animation_Html")]
public string AnimationHtml { get; set; } = "Rolling...<br><font color='#FF0000'>{reward}</font>";
[JsonPropertyName("Reward_Html")]
public string FinalHtml { get; set; } = "You won:<br><font color='#00FF00'>{reward}</font>";
[JsonPropertyName("Open_case_sound")]
public string OpenCaseSound { get; set; } = "/sounds/ui/csgo_ui_crate_open.vsnd_c";
[JsonPropertyName("Roll_item_sound")]
public string RollItemSound { get; set; } = "/sounds/ui/csgo_ui_crate_item_scroll.vsnd_c";
[JsonPropertyName("Won_item_sound")]
public string WonItemSound { get; set; } = "/sounds/ui/panorama/inventory_new_item_01.vsnd_c";
[JsonPropertyName("Use_Html")]
public bool UseHtml { get; set; } = false;
[JsonPropertyName("Show_Reward_Chances")]
public bool ShowRewardChances { get; set; } = true;
[JsonPropertyName("ConfigVersion")]
public override int Version { get; set; } = 1;
}
public class CaseItem
{
[JsonPropertyName("Name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("Price")]
public int Price { get; set; } = 0;
[JsonPropertyName("Flag")]
public string? Flag { get; set; } = null;
[JsonPropertyName("Rewards")]
public List<CaseReward> Rewards { get; set; } = new();
}
public class CaseReward
{
[JsonPropertyName("Type")]
public string Type { get; set; } = string.Empty;
[JsonPropertyName("Value")]
public string Value { get; set; } = string.Empty;
[JsonPropertyName("Description")]
public string Description { get; set; } = string.Empty;
[JsonPropertyName("Expiration")]
public int Expiration { get; set; } = 0;
[JsonPropertyName("Chance")]
public float Chance { get; set; } = 100f;
[JsonPropertyName("PrintToChatAll")]
public bool PrintToChatAll { get; set; } = false;
}
public class Store_Cases : BasePlugin, IPluginConfig<Store_CasesConfig>
{
public override string ModuleName => "Store Module [Cases]";
public override string ModuleVersion => "0.1.0";
public override string ModuleAuthor => "Nathy";
public IStoreApi? StoreApi { get; set; }
public Store_CasesConfig Config { get; set; } = new();
private ConcurrentDictionary<string, CaseAnimation> activeAnimations = new();
private readonly ConcurrentDictionary<string, DateTime> playerLastBuyCaseCommandTimes = new();
public void OnConfigParsed(Store_CasesConfig config)
{
Config = config;
}
public override void OnAllPluginsLoaded(bool hotReload)
{
StoreApi = IStoreApi.Capability.Get() ?? throw new Exception("StoreApi could not be located.");
CreateCommands();
}
private void CreateCommands()
{
foreach (var cmd in Config.StoreCasesCommands)
{
AddCommand($"css_{cmd}", "Open the Cases menu", Command_CasesMenu);
}
foreach (var cmd in Config.StoreGiveCasesCommands)
{
AddCommand($"css_{cmd}", "Give a case to a player", Command_GiveCase);
}
foreach (var cmd in Config.StoreQuickBuyCasesCommands)
{
AddCommand($"css_{cmd}", "Quick buy a case by name or index", Command_QuickBuyCase);
}
}
[CommandHelper(minArgs: 1, usage: "[case_name_or_index]")]
public void Command_QuickBuyCase(CCSPlayerController? player, CommandInfo command)
{
if (player == null) return;
if (playerLastBuyCaseCommandTimes.TryGetValue(player.SteamID.ToString(), out var lastCommandTime))
{
var cooldownRemaining = (DateTime.Now - lastCommandTime).TotalSeconds;
if (cooldownRemaining < Config.BuyCasesCooldown)
{
var secondsRemaining = (int)(Config.BuyCasesCooldown - cooldownRemaining);
player.PrintToChat(Localizer["Prefix"] + Localizer["In cooldown", secondsRemaining]);
return;
}
}
playerLastBuyCaseCommandTimes[player.SteamID.ToString()] = DateTime.Now;
string caseArg = command.GetArg(1);
CaseItem? caseItem = null;
if (int.TryParse(caseArg, out int caseIndex))
{
if (caseIndex > 0 && caseIndex <= Config.Cases.Count)
{
caseItem = Config.Cases[caseIndex - 1];
}
else
{
player.PrintToChat(Localizer["Prefix"] + Localizer["Invalid case index", caseArg]);
return;
}
}
else
{
caseItem = Config.Cases.FirstOrDefault(c => c.Name.Equals(caseArg, StringComparison.OrdinalIgnoreCase));
if (caseItem == null)
{
player.PrintToChat(Localizer["Prefix"] + Localizer["Case not found", caseArg]);
return;
}
}
StartCaseAnimation(player, caseItem);
}
[CommandHelper(minArgs: 2, usage: "[target] [case_name]")]
public void Command_GiveCase(CCSPlayerController? caller, CommandInfo command)
{
if (caller != null)
{
if (!AdminManager.PlayerHasPermissions(caller, Config.StoreGiveCasesPermission))
{
caller.PrintToChat(Localizer["Prefix"] + Localizer["No permission to give case"]);
return;
}
var targetResult = command.GetArgTargetResult(1);
string caseName = command.GetArg(2);
var caseItem = Config.Cases.FirstOrDefault(c => c.Name.Equals(caseName, StringComparison.OrdinalIgnoreCase));
if (caseItem == null)
{
caller?.PrintToChat(Localizer["Prefix"] + Localizer["Case not found", caseName]);
return;
}
foreach (var player in targetResult.Players)
{
if (player.IsValid && !player.IsBot && !player.IsHLTV)
{
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Case given", player.PlayerName, caseName]);
StartCaseAnimation(player, caseItem);
}
else
{
caller?.PrintToChat(Localizer["Prefix"] + Localizer["Invalid player"]);
}
}
}
}
public void Command_CasesMenu(CCSPlayerController? player, CommandInfo info)
{
if (player == null) return;
string menuType = Config.MenuType.ToLower();
if (menuType == "html")
{
CenterHtmlMenu menu = new CenterHtmlMenu(Localizer["Cases Menu title"], this);
foreach (var caseItem in Config.Cases)
{
menu.AddMenuOption(Localizer["Case item", caseItem.Name, caseItem.Price], (client, option) =>
{
ShowCaseOptionsMenu(player, caseItem);
});
}
MenuManager.OpenCenterHtmlMenu(this, player, menu);
}
else
{
var menu = new ChatMenu(Localizer["Cases Menu title"]);
foreach (var caseItem in Config.Cases)
{
menu.AddMenuOption(Localizer["Case item", caseItem.Name, caseItem.Price], (client, option) =>
{
ShowCaseOptionsMenu(player, caseItem);
});
}
MenuManager.OpenChatMenu(player, menu);
}
}
private void ShowCaseOptionsMenu(CCSPlayerController player, CaseItem caseItem)
{
string menuType = Config.MenuType.ToLower();
if (menuType == "html")
{
CenterHtmlMenu menu = new CenterHtmlMenu(Localizer["Case Options Menu title"], this);
menu.AddMenuOption(Localizer["Buy Case"], (client, option) =>
{
if (playerLastBuyCaseCommandTimes.TryGetValue(player.SteamID.ToString(), out var lastCommandTime))
{
var cooldownRemaining = (DateTime.Now - lastCommandTime).TotalSeconds;
if (cooldownRemaining < Config.BuyCasesCooldown)
{
var secondsRemaining = (int)(Config.BuyCasesCooldown - cooldownRemaining);
player.PrintToChat(Localizer["Prefix"] + Localizer["In cooldown", secondsRemaining]);
return;
}
}
playerLastBuyCaseCommandTimes[player.SteamID.ToString()] = DateTime.Now;
StartCaseAnimation(player, caseItem);
});
menu.AddMenuOption(Localizer["View Case Content"], (client, option) =>
{
ShowCaseContent(player, caseItem);
});
MenuManager.OpenCenterHtmlMenu(this, player, menu);
}
else
{
var menu = new ChatMenu(Localizer["Case Options Menu title"]);
menu.AddMenuOption(Localizer["Buy Case"], (client, option) =>
{
StartCaseAnimation(player, caseItem);
});
menu.AddMenuOption(Localizer["View Case Content"], (client, option) =>
{
ShowCaseContent(player, caseItem);
});
MenuManager.OpenChatMenu(player, menu);
}
}
private void ShowCaseContent(CCSPlayerController player, CaseItem caseItem)
{
player.PrintToChat(Localizer["Case Content First Line"]);
foreach (var reward in caseItem.Rewards)
{
string content = Localizer["Case Content List", reward.Description];
if (Config.ShowRewardChances)
{
content += Localizer["Case Content Chances", reward.Chance];
}
player.PrintToChat(content);
}
player.PrintToChat(Localizer["Case Content Last Line"]);
}
private void StartCaseAnimation(CCSPlayerController player, CaseItem caseItem)
{
if (StoreApi == null) throw new Exception("StoreApi could not be located.");
if (!string.IsNullOrEmpty(caseItem.Flag) && AdminManager.PlayerHasPermissions(player, caseItem.Flag))
{
player.PrintToChat(Localizer["Prefix"] + Localizer["No permission to buy this case"]);
return;
}
player.ExecuteClientCommand($"play {Config.OpenCaseSound}");
int playerCredits = StoreApi.GetPlayerCredits(player);
if (playerCredits < caseItem.Price)
{
player.PrintToChat(Localizer["Prefix"] + Localizer["Not enough credits"]);
return;
}
StoreApi.GivePlayerCredits(player, -caseItem.Price);
var animation = new CaseAnimation(player, caseItem);
activeAnimations[player.SteamID.ToString()] = animation;
AddTimer(0.1f, () =>
{
UpdateCaseAnimation(animation);
});
MenuManager.CloseActiveMenu(player);
}
private void UpdateCaseAnimation(CaseAnimation animation)
{
if (animation.Timer >= Config.AnimationDuration)
{
EndCaseAnimation(animation);
return;
}
animation.Timer += Config.AnimationInterval;
var rewards = animation.CaseItem.Rewards;
if (rewards.Count == 0)
{
animation.Player.PrintToCenter(Localizer["Prefix"] + Localizer["No rewards available in the case"]);
EndCaseAnimation(animation);
return;
}
animation.RewardIndex = (animation.RewardIndex + 1) % rewards.Count;
string rewardDescription = rewards[animation.RewardIndex].Description;
string animationText = GenerateCaseAnimationHtml(rewardDescription);
if (Config.UseHtml)
{
animation.Player.PrintToCenterHtml(animationText);
}
else
{
animation.Player.PrintToCenter(Localizer["Rolling", rewardDescription]);
}
AddTimer(Config.AnimationInterval, () =>
{
animation.Player.ExecuteClientCommand($"play {Config.RollItemSound}");
UpdateCaseAnimation(animation);
});
}
private int GetRandomRewardIndex(List<CaseReward> rewards)
{
var randomValue = new Random().NextDouble() * 100;
float cumulativeChance = 0f;
for (int i = 0; i < rewards.Count; i++)
{
cumulativeChance += rewards[i].Chance;
if (randomValue <= cumulativeChance)
{
return i;
}
}
return rewards.Count - 1;
}
private string GenerateCaseAnimationHtml(string currentReward)
{
return Config.AnimationHtml
.Replace("{reward}", currentReward);
}
private void EndCaseAnimation(CaseAnimation animation)
{
if (StoreApi == null) throw new Exception("StoreApi could not be located.");
animation.Player.ExecuteClientCommand($"play {Config.WonItemSound}");
activeAnimations.TryRemove(animation.Player.SteamID.ToString(), out _);
var caseItem = animation.CaseItem;
int finalRewardIndex = GetRandomRewardIndex(caseItem.Rewards);
var reward = caseItem.Rewards[finalRewardIndex];
var item = new Dictionary<string, string>
{
{ "type", reward.Type },
{ "value", reward.Value }
};
if (reward.Type == "credits" && int.TryParse(reward.Value, out int credits))
{
StoreApi!.GivePlayerCredits(animation.Player, credits);
ShowFinalReward(animation.Player, reward.Description);
if (reward.PrintToChatAll)
{
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Player won chat all", animation.Player.PlayerName, reward.Description, caseItem.Name]);
}
}
else if (reward.Type == "vip")
{
string command = reward.Value.Replace("{SteamID}", animation.Player.SteamID.ToString());
NativeAPI.IssueServerCommand(command);
if (reward.PrintToChatAll)
{
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Player won chat all", animation.Player.PlayerName, reward.Description, caseItem.Name]);
}
ShowFinalReward(animation.Player, reward.Description);
}
else
{
if (reward.Type != "credits")
{
item["price"] = "0";
item["uniqueid"] = reward.Value;
item["expiration"] = reward.Expiration.ToString();
}
bool playerHasItem = StoreApi.Item_PlayerHas(animation.Player, reward.Type, reward.Value, ignoreVip: false);
if (!playerHasItem)
{
if (StoreApi.Item_Give(animation.Player, item))
{
ShowFinalReward(animation.Player, reward.Description);
if (reward.PrintToChatAll)
{
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Player won chat all", animation.Player.PlayerName, reward.Description, caseItem.Name]);
}
}
else
{
animation.Player.PrintToCenter(Localizer["Prefix"] + Localizer["Item could not be awarded"]);
animation.Player.PrintToChat(Localizer["Prefix"] + Localizer["Item could not be awarded"]);
}
}
else
{
if (Config.ItemActionIfOwned == 1)
{
int itemPrice = caseItem.Price;
int creditReward = itemPrice * Config.ItemActionCreditPercentage / 100;
StoreApi.GivePlayerCredits(animation.Player, creditReward);
animation.Player.PrintToChat(Localizer["Prefix"] + Localizer["You already have this item bonus", creditReward]);
}
else
{
animation.Player.PrintToChat(Localizer["Prefix"] + Localizer["You already have this item"]);
}
}
}
}
private void ShowFinalReward(CCSPlayerController player, string rewardDescription)
{
float displayDuration = 2.0f;
float displayInterval = 0.1f;
float elapsedTime = 0f;
void DisplayReward()
{
if (elapsedTime >= displayDuration)
{
player.PrintToChat(Localizer["Prefix"] + Localizer["You won", rewardDescription]);
return;
}
if (Config.UseHtml)
{
string finalText = Config.FinalHtml
.Replace("{reward}", rewardDescription);
player.PrintToCenterHtml(finalText);
elapsedTime += displayInterval;
AddTimer(displayInterval, DisplayReward);
}
else
{
player.PrintToCenter(Localizer["You won center", rewardDescription]);
player.PrintToChat(Localizer["Prefix"] + Localizer["You won", rewardDescription]);
}
}
DisplayReward();
}
}
public class CaseAnimation
{
public CCSPlayerController Player { get; set; }
public CaseItem CaseItem { get; set; }
public float Timer { get; set; }
public int RewardIndex { get; set; }
public CaseAnimation(CCSPlayerController player, CaseItem caseItem)
{
Player = player;
CaseItem = caseItem;
Timer = 0f;
RewardIndex = 0;
}
}