forked from shobhit-pathak/MatchZy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchZy.cs
539 lines (471 loc) · 22.6 KB
/
MatchZy.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Timers;
namespace MatchZy
{
[MinimumApiVersion(227)]
public partial class MatchZy : BasePlugin
{
public override string ModuleName => "MatchZy";
public override string ModuleVersion => "0.8.3";
public override string ModuleAuthor => "WD- (https://github.com/shobhit-pathak/)";
public override string ModuleDescription => "A plugin for running and managing CS2 practice/pugs/scrims/matches!";
public string chatPrefix = $"[{ChatColors.Green}MatchZy{ChatColors.Default}]";
public string adminChatPrefix = $"[{ChatColors.Red}ADMIN{ChatColors.Default}]";
// Plugin start phase data
public bool isPractice = false;
public bool isSleep = false;
public bool readyAvailable = false;
public bool matchStarted = false;
public bool isWarmup = false;
public bool isKnifeRound = false;
public bool isSideSelectionPhase = false;
public bool isMatchLive = false;
public long liveMatchId = -1;
public int autoStartMode = 1;
public bool mapReloadRequired = false;
// Pause Data
public bool isPaused = false;
public Dictionary<string, object> unpauseData = new Dictionary<string, object> {
{ "ct", false },
{ "t", false },
{ "pauseTeam", "" }
};
bool isPauseCommandForTactical = false;
// Knife Data
public int knifeWinner = 0;
public string knifeWinnerName = "";
// Players Data (including admins)
public int connectedPlayers = 0;
private Dictionary<int, bool> playerReadyStatus = new Dictionary<int, bool>();
private Dictionary<int, CCSPlayerController> playerData = new Dictionary<int, CCSPlayerController>();
// Admin Data
private Dictionary<string, string> loadedAdmins = new Dictionary<string, string>();
// Timers
public CounterStrikeSharp.API.Modules.Timers.Timer? unreadyPlayerMessageTimer = null;
public CounterStrikeSharp.API.Modules.Timers.Timer? sideSelectionMessageTimer = null;
public CounterStrikeSharp.API.Modules.Timers.Timer? pausedStateTimer = null;
// Each message is kept in chat display for ~13 seconds, hence setting default chat timer to 13 seconds.
// Configurable using matchzy_chat_messages_timer_delay <seconds>
public int chatTimerDelay = 13;
// Game Config
public bool isKnifeRequired = true;
public int minimumReadyRequired = 2; // Number of ready players required start the match. If set to 0, all connected players have to ready-up to start the match.
public bool isWhitelistRequired = false;
public bool isSaveNadesAsGlobalEnabled = false;
public bool isPlayOutEnabled = false;
public bool playerHasTakenDamage = false;
// User command - action map
public Dictionary<string, Action<CCSPlayerController?, CommandInfo?>>? commandActions;
// SQLite/MySQL Database
private Database database = new();
public override void Load(bool hotReload) {
LoadAdmins();
database.InitializeDatabase(ModuleDirectory);
// This sets default config ConVars
Server.ExecuteCommand("execifexists MatchZy/config.cfg");
teamSides[matchzyTeam1] = "CT";
teamSides[matchzyTeam2] = "TERRORIST";
reverseTeamSides["CT"] = matchzyTeam1;
reverseTeamSides["TERRORIST"] = matchzyTeam2;
if (!hotReload) {
AutoStart();
} else {
// Pluign should not be reloaded while a match is live (this would messup with the match flags which were set)
// Only hot-reload the plugin if you are testing something and don't want to restart the server time and again.
UpdatePlayersMap();
AutoStart();
}
commandActions = new Dictionary<string, Action<CCSPlayerController?, CommandInfo?>> {
{ ".ready", OnPlayerReady },
{ ".r", OnPlayerReady },
{ ".forceready", OnForceReadyCommandCommand },
{ ".unready", OnPlayerUnReady },
{ ".notready", OnPlayerUnReady },
{ ".ur", OnPlayerUnReady },
{ ".stay", OnTeamStay },
{ ".switch", OnTeamSwitch },
{ ".swap", OnTeamSwitch },
{ ".tech", OnTechCommand },
{ ".p", OnPauseCommand },
{ ".pause", OnPauseCommand },
{ ".unpause", OnUnpauseCommand },
{ ".up", OnUnpauseCommand },
{ ".forcepause", OnForcePauseCommand },
{ ".fp", OnForcePauseCommand },
{ ".forceunpause", OnForceUnpauseCommand },
{ ".fup", OnForceUnpauseCommand },
{ ".tac", OnTacCommand },
{ ".roundknife", OnKnifeCommand },
{ ".rk", OnKnifeCommand },
{ ".playout", OnPlayoutCommand },
{ ".start", OnStartCommand },
{ ".force", OnStartCommand },
{ ".forcestart", OnStartCommand },
{ ".skipveto", OnSkipVetoCommand },
{ ".sv", OnSkipVetoCommand },
{ ".restart", OnRestartMatchCommand },
{ ".endmatch", OnEndMatchCommand },
{ ".forceend", OnEndMatchCommand },
{ ".reloadmap", OnMapReloadCommand },
{ ".settings", OnMatchSettingsCommand },
{ ".whitelist", OnWLCommand },
{ ".globalnades", OnSaveNadesAsGlobalCommand },
{ ".reload_admins", OnReloadAdmins },
{ ".tactics", OnPracCommand },
{ ".prac", OnPracCommand },
{ ".dryrun", OnDryRunCommand },
{ ".dry", OnDryRunCommand },
{ ".noflash", OnNoFlashCommand },
{ ".noblind", OnNoFlashCommand },
{ ".break", OnBreakCommand },
{ ".bot", OnBotCommand },
{ ".cbot", OnCrouchBotCommand },
{ ".crouchbot", OnCrouchBotCommand },
{ ".boost", OnBoostBotCommand },
{ ".crouchboost", OnCrouchBoostBotCommand },
{ ".nobots", OnNoBotsCommand },
{ ".solid", OnSolidCommand },
{ ".impacts", OnImpactsCommand },
{ ".traj", OnTrajCommand },
{ ".pip", OnTrajCommand },
{ ".god", OnGodCommand },
{ ".ff", OnFastForwardCommand },
{ ".fastforward", OnFastForwardCommand },
{ ".clear", OnClearCommand },
{ ".match", OnMatchCommand },
{ ".uncoach", OnUnCoachCommand },
{ ".exitprac", OnMatchCommand },
{ ".stop", OnStopCommand },
{ ".help", OnHelpCommand },
{ ".t", OnTCommand },
{ ".ct", OnCTCommand },
{ ".spec", OnSpecCommand },
{ ".fas", OnFASCommand },
{ ".watchme", OnFASCommand },
{ ".last", OnLastCommand },
{ ".throw", OnRethrowCommand },
{ ".rethrow", OnRethrowCommand },
{ ".rt", OnRethrowCommand },
{ ".throwsmoke", OnRethrowSmokeCommand },
{ ".rethrowsmoke", OnRethrowSmokeCommand },
{ ".thrownade", OnRethrowGrenadeCommand },
{ ".rethrownade", OnRethrowGrenadeCommand },
{ ".rethrowgrenade", OnRethrowGrenadeCommand },
{ ".throwgrenade", OnRethrowGrenadeCommand },
{ ".rethrowflash", OnRethrowFlashCommand },
{ ".throwflash", OnRethrowFlashCommand },
{ ".rethrowdecoy", OnRethrowDecoyCommand },
{ ".throwdecoy", OnRethrowDecoyCommand },
{ ".throwmolotov", OnRethrowMolotovCommand },
{ ".rethrowmolotov", OnRethrowMolotovCommand },
{ ".timer", OnTimerCommand },
{ ".lastindex", OnLastIndexCommand },
{ ".bestspawn", OnBestSpawnCommand },
{ ".worstspawn", OnWorstSpawnCommand },
{ ".bestctspawn", OnBestCTSpawnCommand },
{ ".worstctspawn", OnWorstCTSpawnCommand },
{ ".besttspawn", OnBestTSpawnCommand },
{ ".worsttspawn", OnWorstTSpawnCommand }
};
RegisterEventHandler<EventPlayerConnectFull>(EventPlayerConnectFullHandler);
RegisterEventHandler<EventPlayerDisconnect>(EventPlayerDisconnectHandler);
RegisterEventHandler<EventCsWinPanelRound>(EventCsWinPanelRoundHandler, hookMode: HookMode.Pre);
RegisterEventHandler<EventCsWinPanelMatch>(EventCsWinPanelMatchHandler);
RegisterEventHandler<EventRoundStart>(EventRoundStartHandler);
RegisterEventHandler<EventPlayerDeath>(EventPlayerDeathPreHandler, hookMode: HookMode.Pre);
RegisterListener<Listeners.OnClientDisconnectPost>(playerSlot => {
// May not be required, but just to be on safe side so that player data is properly updated in dictionaries
// Update: Commenting the below function as it was being called multiple times on map change.
// UpdatePlayersMap();
});
RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawnedHandler);
RegisterEventHandler<EventPlayerTeam>((@event, info) => {
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if (matchzyTeam1.coach.Contains(player!) || matchzyTeam2.coach.Contains(player!)) {
@event.Silent = true;
return HookResult.Changed;
}
return HookResult.Continue;
}, HookMode.Pre);
RegisterEventHandler<EventPlayerTeam>((@event, info) =>
{
if (!isMatchSetup && !isVeto) return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if (player!.IsHLTV || player.IsBot)
{
return HookResult.Continue;
}
CsTeam playerTeam = GetPlayerTeam(player);
SwitchPlayerTeam(player, playerTeam);
return HookResult.Continue;
});
AddCommandListener("jointeam", (player, info) =>
{
if ((isMatchSetup || isVeto) && player != null && player.IsValid) {
if (int.TryParse(info.ArgByIndex(1), out int joiningTeam)) {
int playerTeam = (int)GetPlayerTeam(player);
if (joiningTeam != playerTeam) {
return HookResult.Stop;
}
}
}
return HookResult.Continue;
});
RegisterEventHandler<EventRoundEnd>((@event, info) =>
{
if (!isKnifeRound) return HookResult.Continue;
DetermineKnifeWinner();
@event.Winner = knifeWinner;
int finalEvent = 10;
if (knifeWinner == 3) {
finalEvent = 8;
} else if (knifeWinner == 2) {
finalEvent = 9;
}
@event.Reason = finalEvent;
isSideSelectionPhase = true;
isKnifeRound = false;
StartAfterKnifeWarmup();
return HookResult.Changed;
}, HookMode.Pre);
RegisterEventHandler<EventRoundEnd>((@event, info) => {
try
{
if (isDryRun)
{
StartPracticeMode();
isDryRun = false;
return HookResult.Continue;
}
if (!isMatchLive) return HookResult.Continue;
HandlePostRoundEndEvent(@event);
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventRoundEnd FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}, HookMode.Post);
// RegisterEventHandler<EventMapShutdown>((@event, info) => {
// Log($"[EventMapShutdown] Resetting match!");
// ResetMatch();
// return HookResult.Continue;
// });
RegisterListener<Listeners.OnMapStart>(mapName => {
AddTimer(1.0f, () => {
if (!isMatchSetup)
{
AutoStart();
return;
}
if (isWarmup) StartWarmup();
if (isPractice) StartPracticeMode();
});
});
// RegisterListener<Listeners.OnMapEnd>(() => {
// Log($"[Listeners.OnMapEnd] Resetting match!");
// ResetMatch();
// });
RegisterEventHandler<EventPlayerDeath>((@event, info) => {
// Setting money back to 16000 when a player dies in warmup
var player = @event.Userid;
if (!isWarmup) return HookResult.Continue;
if (!IsPlayerValid(player)) return HookResult.Continue;
if (player!.InGameMoneyServices != null) player.InGameMoneyServices.Account = 16000;
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerHurt>((@event, info) =>
{
CCSPlayerController? attacker = @event.Attacker;
CCSPlayerController? victim = @event.Userid;
if (!IsPlayerValid(attacker) || !IsPlayerValid(victim)) return HookResult.Continue;
if (isPractice && victim!.IsBot)
{
int damage = @event.DmgHealth;
int postDamageHealth = @event.Health;
PrintToPlayerChat(attacker!, Localizer["matchzy.pracc.damage", damage, victim.PlayerName, postDamageHealth]);
return HookResult.Continue;
}
if (!attacker!.IsValid || attacker.IsBot && !(@event.DmgHealth > 0 || @event.DmgArmor > 0))
return HookResult.Continue;
if (matchStarted && victim!.TeamNum != attacker.TeamNum)
{
int targetId = (int)victim.UserId!;
UpdatePlayerDamageInfo(@event, targetId);
if (attacker != victim) playerHasTakenDamage = true;
}
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerChat>((@event, info) => {
int currentVersion = Api.GetVersion();
int index = @event.Userid + 1;
var playerUserId = NativeAPI.GetUseridFromIndex(index);
var originalMessage = @event.Text.Trim();
var message = @event.Text.Trim().ToLower();
var parts = message.Split(' ');
var messageCommand = parts.Length > 0 ? parts[0] : string.Empty;
var messageCommandArg = parts.Length > 1 ? string.Join(' ', parts.Skip(1)) : string.Empty;
CCSPlayerController? player = null;
if (playerData.ContainsKey(playerUserId)) {
player = playerData[playerUserId];
}
if (player == null) {
// Somehow we did not had the player in playerData, hence updating the maps again before getting the player
UpdatePlayersMap();
player = playerData[playerUserId];
}
// Handling player commands
if (commandActions.ContainsKey(message)) {
commandActions[message](player, null);
}
if (message.StartsWith(".map"))
{
HandleMapChangeCommand(player, messageCommandArg);
}
if (message.StartsWith(".readyrequired"))
{
HandleReadyRequiredCommand(player, messageCommandArg);
}
if (message.StartsWith(".restore"))
{
HandleRestoreCommand(player, messageCommandArg);
}
if (message.StartsWith(".asay"))
{
if (IsPlayerAdmin(player, "css_asay", "@css/chat"))
{
if (messageCommandArg != "")
{
Server.PrintToChatAll($"{adminChatPrefix} {messageCommandArg}");
}
else
{
// ReplyToUserCommand(player, "Usage: .asay <message>");
ReplyToUserCommand(player, Localizer["matchzy.cc.usage", ".asay <message>"]);
}
}
else
{
SendPlayerNotAdminMessage(player);
}
}
if (message.StartsWith(".savenade") || message.StartsWith(".sn"))
{
HandleSaveNadeCommand(player, messageCommandArg);
}
if (message.StartsWith(".delnade") || message.StartsWith(".dn"))
{
HandleDeleteNadeCommand(player, messageCommandArg);
}
if (message.StartsWith(".deletenade"))
{
HandleDeleteNadeCommand(player, messageCommandArg);
}
if (message.StartsWith(".importnade") || message.StartsWith(".in"))
{
HandleImportNadeCommand(player, messageCommandArg);
}
if (message.StartsWith(".listnades") || message.StartsWith(".lin"))
{
HandleListNadesCommand(player, messageCommandArg);
}
if (message.StartsWith(".loadnade") || message.StartsWith(".ln"))
{
HandleLoadNadeCommand(player, messageCommandArg);
}
if (message.StartsWith(".spawn"))
{
HandleSpawnCommand(player, messageCommandArg, player.TeamNum, "spawn");
}
if (message.StartsWith(".ctspawn") || message.StartsWith(".cts"))
{
HandleSpawnCommand(player, messageCommandArg, (byte)CsTeam.CounterTerrorist, "ctspawn");
}
if (message.StartsWith(".tspawn") || message.StartsWith(".ts"))
{
HandleSpawnCommand(player, messageCommandArg, (byte)CsTeam.Terrorist, "tspawn");
}
if (originalMessage.StartsWith(".team1"))
{
HandleTeamNameChangeCommand(player, messageCommandArg, 1);
}
if (originalMessage.StartsWith(".team2"))
{
HandleTeamNameChangeCommand(player, messageCommandArg, 2);
}
if (originalMessage.StartsWith(".rcon"))
{
if (IsPlayerAdmin(player, "css_rcon", "@css/rcon"))
{
Server.ExecuteCommand(messageCommandArg);
ReplyToUserCommand(player, "Command sent successfully!");
}
else
{
SendPlayerNotAdminMessage(player);
}
}
if (message.StartsWith(".coach"))
{
HandleCoachCommand(player, messageCommandArg);
}
if (message.StartsWith(".ban"))
{
HandeMapBanCommand(player, messageCommandArg);
}
if (message.StartsWith(".pick"))
{
HandeMapPickCommand(player, messageCommandArg);
}
if (message.StartsWith(".back"))
{
HandleBackCommand(player, messageCommandArg);
}
if (message.StartsWith(".delay"))
{
HandleDelayCommand(player, messageCommandArg);
}
if (message.StartsWith(".throwindex"))
{
HandleThrowIndexCommand(player, messageCommandArg);
}
if (message.StartsWith(".throwidx"))
{
HandleThrowIndexCommand(player, messageCommandArg);
}
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerBlind>((@event, info) =>
{
CCSPlayerController? player = @event.Userid;
CCSPlayerController? attacker = @event.Attacker;
if (!isPractice) return HookResult.Continue;
if (!IsPlayerValid(player) || !IsPlayerValid(attacker)) return HookResult.Continue;
if (attacker!.IsValid)
{
double roundedBlindDuration = Math.Round(@event.BlindDuration, 2);
PrintToPlayerChat(attacker, Localizer["matchzy.pracc.blind", player!.PlayerName, roundedBlindDuration]);
}
var userId = player!.UserId;
if (userId != null && noFlashList.Contains((int)userId))
{
Server.NextFrame(() => KillFlashEffect(player));
}
return HookResult.Continue;
});
RegisterEventHandler<EventSmokegrenadeDetonate>(EventSmokegrenadeDetonateHandler);
RegisterEventHandler<EventFlashbangDetonate>(EventFlashbangDetonateHandler);
RegisterEventHandler<EventHegrenadeDetonate>(EventHegrenadeDetonateHandler);
RegisterEventHandler<EventMolotovDetonate>(EventMolotovDetonateHandler);
RegisterEventHandler<EventDecoyDetonate>(EventDecoyDetonateHandler);
Console.WriteLine($"[{ModuleName} {ModuleVersion} LOADED] MatchZy by WD- (https://github.com/shobhit-pathak/)");
}
}
}