-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenCasePlugin.cs
200 lines (168 loc) · 7.29 KB
/
OpenCasePlugin.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using System.Text.Json;
namespace OpenCasePlugin
{
public class OpenCasePlugin : BasePlugin
{
public override string ModuleName => "Open Case Plugin";
public override string ModuleVersion => "1.0.1";
private string JsonDirectoryPath => Path.Combine(ModuleDirectory, "json");
private static readonly string[] CaseNames = new string[]
{
"Gallery", "Kilowatt", "Revolution", "Recoil", "Dreams & Nightmares", "Operation Riptide", "Snakebite",
"Operation Broken Fang", "Fracture", "Prisma 2", "Shattered Web", "CS20", "Prisma", "Danger Zone", "Clutch",
"Spectrum 2", "Operation Hydra", "Spectrum", "Glove", "Gamma 2", "Gamma", "Chroma 3", "Operation Wildfire",
"Revolver", "Shadow", "Falchion", "Chroma 2", "Chroma", "Operation Vanguard", "Operation Breakout",
"Huntsman Weapon", "Operation Phoenix", "CS:GO Weapon Case 3", "Winter Offensive", "eSports 2013 Winter",
"CS:GO Weapon Case 2", "Operation Bravo", "CS:GO Weapon Case", "eSports 2013"
};
private static readonly Dictionary<string, string> RarityColors = new Dictionary<string, string>
{
{ "Mil-Spec Skins", $"{ChatColors.Blue}" },
{ "Restricted Skins", $"{ChatColors.Purple}" },
{ "Classified Skins", $"{ChatColors.Magenta}" },
{ "Covert Skins", $"{ChatColors.Red}" },
{ "Rare Special Items", $"{ChatColors.Gold}" }
};
private static readonly Dictionary<string, double> RarityProbabilities = new Dictionary<string, double>
{
{ "Mil-Spec Skins", 79.92 },
{ "Restricted Skins", 15.98 },
{ "Classified Skins", 3.2 },
{ "Covert Skins", 0.64 },
{ "Rare Special Items", 0.26 }
};
public override void Load(bool hotReload)
{
base.Load(hotReload);
Directory.CreateDirectory(JsonDirectoryPath);
}
[ConsoleCommand("css_case", "Case commands")]
public void OnCaseCommand(CCSPlayerController? player, CommandInfo command)
{
if (player == null) return;
string[] args = command.ArgString.Split(' ');
if (args.Length == 0)
{
string[] helpMessages = new string[]
{
"Available commands:",
"!case list - displays the list of cases.",
"!case open [CASE_NAME] - opens a specific case."
};
foreach (var message in helpMessages)
{
player.PrintToChat(message);
}
return;
}
switch (args[0].ToLower())
{
case "list":
string caseListMessage = string.Join("\n", CaseNames.Select(caseName => $"- \x04{caseName}"));
string[] caseListMessages = caseListMessage.Split('\n');
player.PrintToChat("Available cases:");
foreach (var message in caseListMessages)
{
player.PrintToChat(message);
}
break;
case "open":
if (args.Length < 2)
{
player.PrintToChat("Usage: !case open \x04[CASE_NAME]");
return;
}
string caseName = string.Join(" ", args.Skip(1));
var drop = GetRandomDrop(caseName);
if (drop != null)
{
string colorCode = RarityColors.TryGetValue(drop.Rarity, out var color) ? color : "\x01";
Server.PrintToChatAll($" \x09 • \x04{player.PlayerName}\x01 has opened a container and found: {colorCode}{drop.name}");
}
else
{
player.PrintToChat("\x02Something went wrong while opening the case.");
}
break;
default:
player.PrintToChat("Unknown command. Use '!case list' or '!case open [CASE_NAME]'.");
break;
}
}
private CaseItem? GetRandomDrop(string caseName)
{
try
{
string jsonFileName = CaseNameToJson(caseName);
string filePath = Path.Combine(JsonDirectoryPath, jsonFileName);
if (!File.Exists(filePath))
{
Console.WriteLine($"JSON file not found: {filePath}");
return null;
}
string jsonString = File.ReadAllText(filePath);
var caseData = JsonSerializer.Deserialize<CaseData>(jsonString);
if (caseData?.content == null)
{
Console.WriteLine($"Invalid case data for: {caseName}");
return null;
}
string selectedRarity = GetRandomRarity();
if (!caseData.content.TryGetValue(selectedRarity, out var items) || items.Count == 0)
{
Console.WriteLine($"No items found for rarity: {selectedRarity}");
return null;
}
var randomItem = items[new Random().Next(items.Count)];
if (randomItem.can_be_stattrak && new Random().Next(10) == 0)
{
randomItem.name = selectedRarity == "Rare Special Items" && randomItem.name.StartsWith("★")
? $"★ StatTrak™{randomItem.name[1..]}"
: $"StatTrak™ {randomItem.name}";
}
return new CaseItem { name = randomItem.name, Rarity = selectedRarity };
}
catch (Exception ex)
{
Console.WriteLine($"Error in GetRandomDrop: {ex.Message}");
return null;
}
}
private string GetRandomRarity()
{
double random = new Random().NextDouble() * 100;
double cumulativeProbability = 0;
foreach (var rarity in RarityProbabilities)
{
cumulativeProbability += rarity.Value;
if (random <= cumulativeProbability) return rarity.Key;
}
return RarityProbabilities.Keys.Last();
}
private string CaseNameToJson(string caseName)
{
return $"{caseName.ToLower().Replace(" ", "_")}_case.json";
}
}
public class CaseData
{
public string name { get; set; } = "";
public string ImageUrl { get; set; } = "";
public Dictionary<string, List<SkinItem>> content { get; set; } = new Dictionary<string, List<SkinItem>>();
}
public class SkinItem
{
public string name { get; set; } = "";
public bool can_be_stattrak { get; set; }
}
public class CaseItem
{
public string name { get; set; } = "";
public string Rarity { get; set; } = "";
}
}