-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs2-store-crash.cs
239 lines (193 loc) · 8.24 KB
/
cs2-store-crash.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using StoreApi;
using System.Collections.Concurrent;
using System.Text.Json.Serialization;
namespace Store_Crash;
public class Store_CrashConfig : BasePluginConfig
{
[JsonPropertyName("min_bet")]
public int MinBet { get; set; } = 10;
[JsonPropertyName("max_bet")]
public int MaxBet { get; set; } = 1000;
[JsonPropertyName("min_multiplier")]
public float MinMultiplier { get; set; } = 1.1f;
[JsonPropertyName("max_multiplier")]
public float MaxMultiplier { get; set; } = 9.9f;
[JsonPropertyName("multiplier_increment")]
public float MultiplierIncrement { get; set; } = 0.01f;
[JsonPropertyName("crash_commands")]
public List<string> CrashCommands { get; set; } = ["crash"];
[JsonPropertyName("crash_command_cooldown")]
public int CrashCommandCooldown { get; set; } = 10;
[JsonPropertyName("multiplier_ranges")]
public List<MultiplierRange> MultiplierRanges { get; set; } = new()
{
new MultiplierRange { Start = 1.0f, End = 2.0f, Chance = 55 },
new MultiplierRange { Start = 2.0f, End = 3.0f, Chance = 25 },
new MultiplierRange { Start = 3.0f, End = 4.0f, Chance = 10 },
new MultiplierRange { Start = 4.0f, End = 5.0f, Chance = 7 },
new MultiplierRange { Start = 5.0f, End = 15.0f, Chance = 3 }
};
}
public class MultiplierRange
{
[JsonPropertyName("start")]
public float Start { get; set; }
[JsonPropertyName("end")]
public float End { get; set; }
[JsonPropertyName("chance")]
public int Chance { get; set; }
}
public class CrashGame
{
public CCSPlayerController Player { get; set; }
public int BetCredits { get; set; }
public float TargetMultiplier { get; set; }
public float CurrentMultiplier { get; set; }
public bool IsActive { get; set; }
public float CrashMultiplier { get; set; }
public CrashGame(CCSPlayerController player, int betCredits, float targetMultiplier, float crashMultiplier)
{
Player = player;
BetCredits = betCredits;
TargetMultiplier = targetMultiplier;
CurrentMultiplier = 0.0f;
IsActive = true;
CrashMultiplier = crashMultiplier;
}
}
public class Store_Crash : BasePlugin, IPluginConfig<Store_CrashConfig>
{
public override string ModuleName => "Store Module [Crash]";
public override string ModuleVersion => "0.2.0";
public override string ModuleAuthor => "Nathy";
private readonly Random random = new();
public IStoreApi? StoreApi { get; set; }
public Store_CrashConfig Config { get; set; } = new();
private readonly ConcurrentDictionary<string, CrashGame> activeGames = new();
private readonly ConcurrentDictionary<string, DateTime> playerLastCrashCommandTimes = new();
public override void OnAllPluginsLoaded(bool hotReload)
{
StoreApi = IStoreApi.Capability.Get() ?? throw new Exception("StoreApi could not be located.");
CreateCommands();
RegisterListener<Listeners.OnTick>(OnTick);
}
public void OnConfigParsed(Store_CrashConfig config)
{
config.MinBet = Math.Max(0, config.MinBet);
config.MaxBet = Math.Max(config.MinBet + 1, config.MaxBet);
Config = config;
}
private void CreateCommands()
{
foreach (var cmd in Config.CrashCommands)
{
AddCommand($"css_{cmd}", "Start a crash bet", Command_Crash);
}
}
[CommandHelper(minArgs: 2, usage: "<credits> <multiplier>")]
public void Command_Crash(CCSPlayerController? player, CommandInfo info)
{
if (player == null) return;
if (StoreApi == null) throw new Exception("StoreApi could not be located.");
if (playerLastCrashCommandTimes.TryGetValue(player.SteamID.ToString(), out var lastCommandTime))
{
var cooldownRemaining = (DateTime.Now - lastCommandTime).TotalSeconds;
if (cooldownRemaining < Config.CrashCommandCooldown)
{
var secondsRemaining = (int)(Config.CrashCommandCooldown - cooldownRemaining);
info.ReplyToCommand(Localizer["Prefix"] + Localizer["In cooldown", secondsRemaining]);
return;
}
}
playerLastCrashCommandTimes[player.SteamID.ToString()] = DateTime.Now;
if (!int.TryParse(info.GetArg(1), out int credits))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Invalid amount of credits"]);
return;
}
if (!float.TryParse(info.GetArg(2), out float targetMultiplier))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Invalid multiplier"]);
return;
}
if (credits < Config.MinBet)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Minimum bet amount", Config.MinBet]);
return;
}
if (credits > Config.MaxBet)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Maximum bet amount", Config.MaxBet]);
return;
}
if (targetMultiplier < Config.MinMultiplier || targetMultiplier > Config.MaxMultiplier)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Multiplier range", Config.MinMultiplier, Config.MaxMultiplier]);
return;
}
if (StoreApi.GetPlayerCredits(player) < credits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Not enough credits"]);
return;
}
float crashMultiplier = SimulateCrashMultiplier();
StartCrashGame(player, credits, targetMultiplier, crashMultiplier);
}
private void StartCrashGame(CCSPlayerController player, int credits, float targetMultiplier, float crashMultiplier)
{
StoreApi!.GivePlayerCredits(player, -credits);
player.PrintToChat(Localizer["Prefix"] + Localizer["Bet placed", credits, targetMultiplier]);
var game = new CrashGame(player, credits, targetMultiplier, crashMultiplier);
activeGames[player.SteamID.ToString()] = game;
}
private void OnTick()
{
foreach (var game in activeGames.Values.ToList())
{
if (!game.IsActive) continue;
game.CurrentMultiplier += Config.MultiplierIncrement;
game.Player.PrintToCenter(Localizer["Current multiplier"] + $"{game.CurrentMultiplier:0.00}");
if (game.CurrentMultiplier >= game.CrashMultiplier)
{
EndCrashGame(game);
}
}
}
private void EndCrashGame(CrashGame game)
{
if (game == null) return;
float actualMultiplier = game.CurrentMultiplier;
float targetMultiplier = game.TargetMultiplier;
game.Player.PrintToCenter(Localizer["Multiplier crashed"] + $"{actualMultiplier:0.00}");
if (actualMultiplier >= targetMultiplier)
{
int winnings = (int)(game.BetCredits * targetMultiplier);
StoreApi!.GivePlayerCredits(game.Player, winnings);
game.Player.PrintToChat(Localizer["Prefix"] + Localizer["Bet win", winnings.ToString(), targetMultiplier.ToString("0.00"), actualMultiplier.ToString("0.00")]);
}
else
{
game.Player.PrintToChat(Localizer["Prefix"] + Localizer["Bet lost", actualMultiplier.ToString("0.00"), targetMultiplier.ToString("0.00")]);
}
game.IsActive = false;
activeGames.TryRemove(game.Player.SteamID.ToString(), out _);
}
private float SimulateCrashMultiplier()
{
int randomNumber = random.Next(1, 101);
int accumulatedChance = 0;
foreach (var range in Config.MultiplierRanges)
{
accumulatedChance += range.Chance;
if (randomNumber <= accumulatedChance)
{
return (float)Math.Round(range.Start + random.NextDouble() * (range.End - range.Start), 2);
}
}
return Config.MultiplierRanges.Last().End;
}
}