-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2-store-coinflip.cs
534 lines (426 loc) · 17 KB
/
cs2-store-coinflip.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Translations;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Menu;
using StoreApi;
using System.Text;
using System.Text.Json.Serialization;
namespace Store_CoinFlip;
public class Store_CoinFlipConfig : BasePluginConfig
{
[JsonPropertyName("enable_duel")]
public bool EnableDuel { get; set; } = true;
[JsonPropertyName("enable_coinflip_start_of_round")]
public bool EnableCoinflipStartOfRound { get; set; } = true;
[JsonPropertyName("min_coinflip")]
public int MinCoinFlip { get; set; } = 0;
[JsonPropertyName("max_coinflip")]
public int MaxCoinFlip { get; set; } = 1000;
[JsonPropertyName("min_duel")]
public int MinDuel { get; set; } = 0;
[JsonPropertyName("max_duel")]
public int MaxDuel { get; set; } = 1000;
[JsonPropertyName("coinflipBet_commands")]
public List<string> BetCommands { get; set; } = ["coinflip", "cf"];
[JsonPropertyName("coinflipSendDuel_commands")]
public List<string> SendDuelCommands { get; set; } = ["coinflipduel", "cfduel"];
[JsonPropertyName("coinflipAcceptDuel_commands")]
public List<string> AcceptDuelCommands { get; set; } = ["coinflipduelaccept", "cfduelaccept"];
[JsonPropertyName("coinflipDeclineDuel_commands")]
public List<string> DeclineDuelCommands { get; set; } = ["coinflipdueldecline", "cfdueldecline"];
[JsonPropertyName("heads")]
public Dictionary<string, int> Heads { get; set; } = new Dictionary<string, int>
{
{ "multiplier", 2 },
{ "probability", 50 }
};
[JsonPropertyName("tails")]
public Dictionary<string, int> Tails { get; set; } = new Dictionary<string, int>
{
{ "multiplier", 2 },
{ "probability", 50 }
};
[JsonPropertyName("gif_enable")]
public bool ShowWinnerGif { get; set; } = true;
[JsonPropertyName("heads_gif_or_image")]
public string HeadsImage { get; set; } = "https://c.tenor.com/nEu74vu_sT4AAAAC/tenor.gif";
[JsonPropertyName("tails_gif_or_image")]
public string TailsImage { get; set; } = "https://c.tenor.com/Gv5d5zs4sisAAAAC/tenor.gif";
[JsonPropertyName("gif_display_duration")]
public float GifDisplayDuration { get; set; } = 4.0f;
}
public class Store_CoinFlip : BasePlugin, IPluginConfig<Store_CoinFlipConfig>
{
public override string ModuleName => "Store Module [CoinFlip]";
public override string ModuleVersion => "0.0.2";
public override string ModuleAuthor => "Nathy & Schwarper";
public IStoreApi? StoreApi { get; set; }
public Dictionary<string, Dictionary<CCSPlayerController, int>> GlobalCoinFlip { get; set; } = [];
public Random Random { get; set; } = new();
public Store_CoinFlipConfig Config { get; set; } = new Store_CoinFlipConfig();
public List<string> Options = ["Heads", "Tails"];
private string? winnerOption = null;
private bool shouldShowWinnerGif = false;
public override void OnAllPluginsLoaded(bool hotReload)
{
StoreApi = IStoreApi.Capability.Get() ?? throw new Exception("StoreApi could not be located.");
CreateCommands();
foreach (string option in Options)
{
GlobalCoinFlip.Add(option, []);
}
RegisterListener<Listeners.OnTick>(OnTick);
}
private void CreateCommands()
{
foreach (string cmd in Config.BetCommands)
{
AddCommand($"css_{cmd}", "Coinflip Bet", Command_CoinFlip);
}
foreach (string cmd in Config.SendDuelCommands)
{
AddCommand($"css_{cmd}", "Coinflip send duel", Command_DuelCoinFlip);
}
foreach (string cmd in Config.AcceptDuelCommands)
{
AddCommand($"css_{cmd}", "Coinflip accept duel", Command_AcceptDuel);
}
foreach (string cmd in Config.DeclineDuelCommands)
{
AddCommand($"css_{cmd}", "Coinflip decline duel", Command_DeclineDuel);
}
}
public void OnConfigParsed(Store_CoinFlipConfig config)
{
config.MinCoinFlip = Math.Max(0, config.MinCoinFlip);
config.MaxCoinFlip = Math.Max(config.MinCoinFlip + 1, config.MaxCoinFlip);
config.MinDuel = Math.Max(0, config.MinDuel);
config.MaxDuel = Math.Max(config.MinDuel + 1, config.MaxDuel);
static void UpdateMultiplierAndProbability(Dictionary<string, int> option)
{
option["multiplier"] = Math.Max(option["multiplier"], 1);
option["probability"] = Math.Max(option["probability"], 1);
}
UpdateMultiplierAndProbability(config.Heads);
UpdateMultiplierAndProbability(config.Tails);
Config = config;
}
[CommandHelper(minArgs: 1, usage: "[nick] [amount]")]
public void Command_DuelCoinFlip(CCSPlayerController? player, CommandInfo info)
{
if (player == null)
{
return;
}
if (StoreApi == null)
{
throw new Exception("StoreApi could not be located.");
}
if (!Config.EnableDuel)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Duel feature is disabled"]);
return;
}
CounterStrikeSharp.API.Modules.Commands.Targeting.TargetResult targetResult = info.GetArgTargetResult(1);
CCSPlayerController? targetPlayer = targetResult.Players.FirstOrDefault();
if (targetPlayer == player)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Cannot challenge yourself"]);
return;
}
if (targetPlayer == null || !targetPlayer.IsValid || targetPlayer.IsBot || targetPlayer.IsHLTV)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Invalid target player"]);
return;
}
if (!int.TryParse(info.GetArg(2), out int credits))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Must be an integer"]);
return;
}
if (credits < Config.MinDuel)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Min coinflip", Config.MinDuel]);
return;
}
if (credits > Config.MaxDuel)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Max coinflip", Config.MaxDuel]);
return;
}
if (StoreApi.GetPlayerCredits(player) < credits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No enough credits"]);
return;
}
if (StoreApi.GetPlayerCredits(targetPlayer) < credits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Target player has no enough credits"]);
return;
}
SendDuelInvite(player, targetPlayer, credits);
}
public void SendDuelInvite(CCSPlayerController challenger, CCSPlayerController target, int credits)
{
challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel request sent", target.PlayerName, credits]);
target.PrintToChat(Localizer["Prefix"] + Localizer["Duel request", challenger.PlayerName, credits]);
PendingDuels[target] = new DuelInfo
{
Challenger = challenger,
Credits = credits
};
}
public void Command_AcceptDuel(CCSPlayerController? player, CommandInfo info)
{
if (player == null)
{
return;
}
if (!PendingDuels.TryGetValue(player, out DuelInfo? duelInfo))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No duel invitations"]);
return;
}
StartDuel(player, duelInfo);
}
public void Command_DeclineDuel(CCSPlayerController? player, CommandInfo info)
{
if (player == null)
{
return;
}
if (PendingDuels.Remove(player))
{
player.PrintToChat(Localizer["Prefix"] + Localizer["You declined the duel"]);
}
else
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No duel invitations"]);
}
}
public void StartDuel(CCSPlayerController target, DuelInfo duelInfo)
{
CCSPlayerController challenger = duelInfo.Challenger;
int credits = duelInfo.Credits;
if (StoreApi!.GetPlayerCredits(challenger) < credits || StoreApi.GetPlayerCredits(target) < credits)
{
target.PrintToChat(Localizer["Prefix"] + Localizer["No enough credits"]);
challenger.PrintToChat(Localizer["Prefix"] + Localizer["No enough credits"]);
return;
}
StoreApi.GivePlayerCredits(challenger, -credits);
StoreApi.GivePlayerCredits(target, -credits);
string winnerOption = Random.Next(2) == 0 ? "Heads" : "Tails";
CCSPlayerController winner = winnerOption == "Heads" ? challenger : target;
CCSPlayerController loser = winner == challenger ? target : challenger;
int multiplier = FindMultiplier(winnerOption);
StoreApi.GivePlayerCredits(winner, credits * multiplier);
winner.PrintToChat(Localizer["Prefix"] + Localizer["Duel win message", loser.PlayerName, credits * multiplier]);
loser.PrintToChat(Localizer["Prefix"] + Localizer["Duel lose message", winner.PlayerName, credits]);
PendingDuels.Remove(target);
}
private readonly Dictionary<CCSPlayerController, DuelInfo> PendingDuels = [];
public class DuelInfo
{
public CCSPlayerController Challenger { get; set; } = null!;
public int Credits { get; set; }
}
public void Command_CoinFlip(CCSPlayerController? player, CommandInfo info)
{
if (player == null)
{
return;
}
if (StoreApi == null)
{
throw new Exception("StoreApi could not be located.");
}
if (!Config.EnableCoinflipStartOfRound)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Coinflip feature is disabled"]);
return;
}
if (Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!.WarmupPeriod)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Cannot join during warmup"]);
return;
}
if (GlobalCoinFlip.Values.Any(dict => dict.ContainsKey(player)))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["You are already in"]);
return;
}
if (!int.TryParse(info.GetArg(1), out int credits))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Must be an integer"]);
return;
}
if (StoreApi.GetPlayerCredits(player) < credits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No enough credits"]);
return;
}
if (credits < Config.MinCoinFlip)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Min coinflip", Config.MinCoinFlip]);
return;
}
if (credits > Config.MaxCoinFlip)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Max coinflip", Config.MaxCoinFlip]);
return;
}
using (new WithTemporaryCulture(player.GetLanguage()))
{
StringBuilder builder = new();
builder.AppendFormat(Localizer["menu_title", credits]);
CenterHtmlMenu menu = new(builder.ToString(), this)
{
PostSelectAction = PostSelectAction.Close
};
AddCoinFlipOption(menu, info, credits, "Heads", Config.Heads["multiplier"]);
AddCoinFlipOption(menu, info, credits, "Tails", Config.Tails["multiplier"]);
MenuManager.OpenCenterHtmlMenu(this, player, menu);
}
}
public void AddCoinFlipOption(CenterHtmlMenu menu, CommandInfo info, int credits, string option, int multiplier)
{
StringBuilder builder = new();
builder.AppendFormat(Localizer["menu_options", Localizer[option], multiplier]);
menu.AddMenuOption(builder.ToString(), (player, menuOption) =>
{
JoinCoinFlip(player, info, credits, option);
});
}
public void JoinCoinFlip(CCSPlayerController player, CommandInfo info, int credits, string option)
{
if (StoreApi == null)
{
throw new Exception("StoreApi could not be located.");
}
if (StoreApi.GetPlayerCredits(player) < credits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No enough credits"]);
return;
}
StoreApi.GivePlayerCredits(player, -credits);
GlobalCoinFlip[option].Add(player, credits);
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Join coinflip", player.PlayerName, credits, Localizer[option]]);
}
[GameEventHandler(HookMode.Pre)]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
if (StoreApi == null)
{
throw new Exception("StoreApi could not be located.");
}
if (Config.EnableCoinflipStartOfRound)
{
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Announce coinflip"]);
Pay();
}
foreach (string option in Options)
{
GlobalCoinFlip[option].Clear();
}
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
if (!Config.EnableCoinflipStartOfRound)
{
return HookResult.Continue;
}
int totalProbability = Config.Heads["probability"] + Config.Tails["probability"];
int randomNumber = Random.Next(1, totalProbability + 1);
if (randomNumber <= Config.Heads["probability"])
{
GiveCreditsToWinner("Heads");
winnerOption = "Heads";
}
else
{
GiveCreditsToWinner("Tails");
winnerOption = "Tails";
}
shouldShowWinnerGif = true;
AddTimer(Config.GifDisplayDuration, () =>
{
shouldShowWinnerGif = false;
winnerOption = null;
});
return HookResult.Continue;
}
public void GiveCreditsToWinner(string option)
{
if (StoreApi == null)
{
throw new Exception("StoreApi could not be located.");
}
Server.PrintToChatAll(Localizer["Prefix"] + Localizer["Winner coinflip", Localizer[option]]);
Pay(option);
winnerOption = option;
foreach (string opt in Options)
{
GlobalCoinFlip[opt].Clear();
}
}
public void OnTick()
{
if (shouldShowWinnerGif && winnerOption != null && Config.ShowWinnerGif && Config.EnableCoinflipStartOfRound)
{
string gifUrl = winnerOption == "Heads" ? Config.HeadsImage : Config.TailsImage;
foreach (CCSPlayerController player in Utilities.GetPlayers())
{
if (player != null && player.IsValid)
{
player.PrintToCenterHtml($"<img src=\"{gifUrl}\">");
}
}
}
}
public void Pay(string option)
{
int multiplier = FindMultiplier(option);
foreach (KeyValuePair<CCSPlayerController, int> kv in GlobalCoinFlip[option])
{
CCSPlayerController player = kv.Key;
int credits = kv.Value;
if (player == null || !player.IsValid)
{
continue;
}
StoreApi!.GivePlayerCredits(player, credits * multiplier);
}
}
public void Pay()
{
foreach (KeyValuePair<string, Dictionary<CCSPlayerController, int>> kv in GlobalCoinFlip)
{
string option = kv.Key;
Dictionary<CCSPlayerController, int> dictionary = kv.Value;
foreach (KeyValuePair<CCSPlayerController, int> kvp in dictionary)
{
CCSPlayerController player = kvp.Key;
int credits = kvp.Value;
if (player == null || !player.IsValid)
{
continue;
}
StoreApi!.GivePlayerCredits(player, credits);
}
}
}
public int FindMultiplier(string option)
{
return option switch
{
"Heads" => Config.Heads["multiplier"],
"Tails" => Config.Tails["multiplier"],
_ => 1
};
}
}