-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunRandomRounds.cs
More file actions
623 lines (525 loc) · 18.6 KB
/
Copy pathFunRandomRounds.cs
File metadata and controls
623 lines (525 loc) · 18.6 KB
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
using System.Text.Json;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using FunRandomRounds.Rules;
namespace FunRandomRounds;
public class FunRandomRoundsPlugin : BasePlugin
{
public override string ModuleName => "FunRandomRounds";
public override string ModuleVersion => "1.5.8";
public override string ModuleAuthor => "CS2Server";
public override string ModuleDescription => "MatchZy companion: random rule each round";
private const string Prefix = " \x04[随机规则]\x01 ";
private const float CenterBannerSeconds = 10f;
private bool _enabled;
private List<RoundRule> _rules = [];
private RoundRule? _current;
private int _lastIndex = -1;
private readonly HashSet<int> _usedRuleIndices = [];
private int? _forcedIndex;
private readonly Random _random = new();
private readonly CvarSnapshot _cvars = new();
private WeaponGuard? _weaponGuard;
private int _pendingApply;
private int _internalGiveDepth;
private int _buyStateTick;
private string? _centerBannerHtml;
private float _centerBannerUntil;
public override void Load(bool hotReload)
{
_rules = RuleRegistry.CreateAll(this);
RegisterListener<Listeners.OnTick>(OnTick);
RegisterListener<Listeners.OnServerPostEntityThink>(OnPostEntityThink);
RegisterListener<Listeners.CheckTransmit>(OnCheckTransmit);
AddCommandListener("say", OnSay);
AddCommandListener("say_team", OnSay);
AddCommandListener("buy", OnBuyCommand);
AddCommandListener("buyammo1", OnBuyCommand);
AddCommandListener("buyammo2", OnBuyCommand);
AddCommandListener("autobuy", OnBuyCommand);
AddCommandListener("rebuy", OnBuyCommand);
_weaponGuard = new WeaponGuard(this);
_weaponGuard.Hook();
if (hotReload)
Disable(announce: false);
}
public override void Unload(bool hotReload)
{
Disable(announce: false);
_weaponGuard?.Unhook();
}
public bool BlocksRestrictedWeapons => _enabled && (_current?.BlocksBuyAndPickup ?? false);
public bool IsInternalGive => _internalGiveDepth > 0;
public void BeginInternalGive() => _internalGiveDepth++;
public void EndInternalGive()
{
if (_internalGiveDepth > 0)
_internalGiveDepth--;
}
public bool AllowsRestrictedWeapon(string designerName)
{
if (_current == null)
return true;
return _current.AllowsWeapon(designerName);
}
private HookResult OnBuyCommand(CCSPlayerController? player, CommandInfo info)
{
if (_enabled && (_current?.BlocksBuy == true || BlocksRestrictedWeapons))
return HookResult.Handled;
return HookResult.Continue;
}
[ConsoleCommand("css_funrand", "开关随机规则模式。css_funrand [off|list|set <编号>]")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnFunCommand(CCSPlayerController? player, CommandInfo info)
{
var arg = info.ArgCount > 1 ? info.ArgByIndex(1) : string.Empty;
var extra = info.ArgCount > 2 ? info.ArgByIndex(2) : string.Empty;
HandleCommand(player, arg, extra);
}
private HookResult OnSay(CCSPlayerController? player, CommandInfo info)
{
if (player == null || !player.IsValid)
return HookResult.Continue;
var text = info.ArgCount > 1 ? info.GetArg(1).Trim() : string.Empty;
if (string.IsNullOrEmpty(text) || text[0] != '.')
return HookResult.Continue;
var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var cmd = parts[0].ToLowerInvariant();
if (cmd is not (".funrand" or ".rand" or ".random"))
return HookResult.Continue;
HandleCommand(player, parts.Length > 1 ? parts[1] : string.Empty, parts.Length > 2 ? parts[2] : string.Empty);
return HookResult.Handled;
}
private void HandleCommand(CCSPlayerController? player, string arg, string extra)
{
arg = arg.Trim().ToLowerInvariant();
if (arg is "list" or "ls")
{
Reply(player, "规则列表:");
for (var i = 0; i < _rules.Count; i++)
Reply(player, $"{i + 1}. {_rules[i].Name} — {_rules[i].Description}");
return;
}
if (player != null && !IsAdmin(player))
{
Reply(player, "只有管理员可以开关随机规则模式。");
return;
}
if (arg is "off" or "0" or "disable")
{
Disable(announce: true);
return;
}
if (arg is "set" or "force")
{
if (!TryParseRuleIndex(extra, out var index))
{
Reply(player, "用法:.funrand set <编号>,先用 .funrand list 查看。");
return;
}
_forcedIndex = index;
BeginMode();
CancelPendingApply();
if (IsWarmup())
{
StopCurrentRule();
Reply(player, $"热身中已指定,开赛重启后生效:{_rules[index].Name}");
return;
}
ApplyRule(index);
Reply(player, $"已指定本回合规则:{_rules[index].Name}");
return;
}
if (int.TryParse(arg, out _) && TryParseRuleIndex(arg, out var directIndex))
{
_forcedIndex = directIndex;
BeginMode();
CancelPendingApply();
if (IsWarmup())
{
StopCurrentRule();
Reply(player, $"热身中已指定,开赛重启后生效:{_rules[directIndex].Name}");
return;
}
ApplyRule(directIndex);
Reply(player, $"已指定本回合规则:{_rules[directIndex].Name}");
return;
}
if (_enabled)
{
Reply(player, "随机规则模式已在运行。聊天输入 .funrand off 关闭,.funrand list 查看规则。");
return;
}
BeginMode();
Server.PrintToChatAll($"{Prefix}随机规则模式已开启:正常竞技,每回合随机一条规则。");
Server.PrintToChatAll($"{Prefix}仍用 MatchZy 开赛:.ready 或管理员 .start");
Server.PrintToChatAll($"{Prefix}请不要同时开 .fun1v5。");
if (!IsWarmup())
PickAndApply();
}
private void BeginMode()
{
if (_enabled)
return;
Server.ExecuteCommand("css_fun1v5 off");
_cvars.Capture();
_usedRuleIndices.Clear();
_enabled = true;
}
private void Disable(bool announce)
{
var wasEnabled = _enabled;
_enabled = false;
_forcedIndex = null;
_centerBannerHtml = null;
CancelPendingApply();
StopCurrentRule();
_lastIndex = -1;
_usedRuleIndices.Clear();
if (wasEnabled)
{
_cvars.Restore();
ResetPlayers();
}
if (announce)
Server.PrintToChatAll($"{Prefix}随机规则模式已关闭,已还原娱乐模式开启前的服务器设置。");
}
private static void ResetPlayers()
{
foreach (var player in Utilities.GetPlayers())
{
if (player == null || !player.IsValid)
continue;
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid)
continue;
pawn.MaxHealth = 100;
if (pawn.LifeState == (byte)LifeState_t.LIFE_ALIVE)
pawn.Health = 100;
if (pawn.ArmorValue > 100)
pawn.ArmorValue = 100;
Utilities.SetStateChanged(pawn, "CBaseEntity", "m_iHealth");
Utilities.SetStateChanged(pawn, "CBaseEntity", "m_iMaxHealth");
Utilities.SetStateChanged(pawn, "CCSPlayerPawn", "m_ArmorValue");
}
}
private void StopCurrentRule()
{
try
{
_current?.Stop();
}
finally
{
_current = null;
RestrictedLoadout.UnlockBuy();
}
}
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
if (!_enabled)
return HookResult.Continue;
// Stop before the next round's spawn, otherwise the old rule (e.g. grenade-only)
// still strips weapons on player_spawn. Also runs before MatchZy mp_restartgame.
StopCurrentRule();
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundPrestart(EventRoundPrestart @event, GameEventInfo info)
{
if (!_enabled)
return HookResult.Continue;
// mp_restartgame can spawn players without a clean round_end. Unlock buy and
// stop restricted loadouts before the restart spawn.
StopCurrentRule();
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
if (!_enabled || IsWarmup())
return HookResult.Continue;
// MatchZy prints LIVE and runs mp_restartgame several times. Each restart
// fires round_start; applying a new rule every time strips players empty.
QueuePickAndApply(2.5f);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnWarmupEnd(EventWarmupEnd @event, GameEventInfo info)
{
if (!_enabled)
return HookResult.Continue;
StopCurrentRule();
_usedRuleIndices.Clear();
_lastIndex = -1;
Server.PrintToChatAll($"{Prefix}热身结束,开赛重启后开始随机规则。");
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnFreezeEnd(EventRoundFreezeEnd @event, GameEventInfo info)
{
if (_enabled)
_current?.OnFreezeEnd();
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
if (!_enabled)
return HookResult.Continue;
var player = @event.Userid;
if (player != null && player.IsValid)
_current?.OnPlayerSpawn(player);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
{
if (_enabled)
_current?.OnPlayerHurt(@event);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
if (_enabled)
_current?.OnPlayerDeath(@event);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnGrenadeThrown(EventGrenadeThrown @event, GameEventInfo info)
{
if (!_enabled)
return HookResult.Continue;
var player = @event.Userid;
if (player != null && player.IsValid)
_current?.OnGrenadeThrown(player, @event.Weapon ?? string.Empty);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnWeaponFire(EventWeaponFire @event, GameEventInfo info)
{
if (!_enabled)
return HookResult.Continue;
_current?.OnWeaponFire(@event);
var weapon = @event.Weapon ?? string.Empty;
if (!WeaponUtil.IsUtilityName(weapon))
return HookResult.Continue;
var player = @event.Userid;
if (player != null && player.IsValid)
_current?.OnGrenadeThrown(player, weapon);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerSound(EventPlayerSound @event, GameEventInfo info)
{
if (_enabled)
_current?.OnPlayerSound(@event);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnBulletImpact(EventBulletImpact @event, GameEventInfo info)
{
if (_enabled)
_current?.OnBulletImpact(@event);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnBombBeginPlant(EventBombBeginplant @event, GameEventInfo info)
{
if (_enabled)
_current?.OnBombBeginPlant(@event);
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnBombBeginDefuse(EventBombBegindefuse @event, GameEventInfo info)
{
if (_enabled)
_current?.OnBombBeginDefuse(@event);
return HookResult.Continue;
}
private void OnTick()
{
if (_enabled)
{
_current?.OnTick();
_buyStateTick++;
if (_buyStateTick >= 64)
{
_buyStateTick = 0;
if (_current?.BlocksBuy != true)
RestrictedLoadout.EnsureBuyUnlocked();
}
}
RefreshCenterBanner();
}
private void RefreshCenterBanner()
{
if (_centerBannerHtml == null)
return;
if (Server.CurrentTime >= _centerBannerUntil)
{
_centerBannerHtml = null;
return;
}
foreach (var player in Utilities.GetPlayers())
{
if (player == null || !player.IsValid || player.IsBot)
continue;
try
{
player.PrintToCenterHtml(_centerBannerHtml);
}
catch
{
// ignored
}
}
}
private void OnPostEntityThink()
{
if (_enabled)
_current?.OnPostEntityThink();
}
private void OnCheckTransmit(CCheckTransmitInfoList infoList)
{
if (_enabled)
_current?.OnCheckTransmit(infoList);
}
private void CancelPendingApply() => _pendingApply++;
private void QueuePickAndApply(float delay)
{
var token = ++_pendingApply;
AddTimer(delay, () =>
{
if (!_enabled || token != _pendingApply || IsWarmup())
return;
PickAndApply();
});
}
private void PickAndApply()
{
int index;
if (_forcedIndex.HasValue)
{
index = _forcedIndex.Value;
_forcedIndex = null;
}
else
{
index = PickRandomIndex();
}
if (index < 0)
{
StopCurrentRule();
Server.PrintToChatAll($"{Prefix}本场比赛的所有规则均已使用,本回合按正常规则进行。");
return;
}
ApplyRule(index);
}
private int PickRandomIndex()
{
var available = Enumerable.Range(0, _rules.Count)
.Where(index => !_usedRuleIndices.Contains(index))
.ToArray();
return available.Length == 0
? -1
: available[_random.Next(available.Length)];
}
private void ApplyRule(int index)
{
if (index < 0 || index >= _rules.Count)
return;
StopCurrentRule();
_current = _rules[index];
_lastIndex = index;
_usedRuleIndices.Add(index);
_current.Start();
BroadcastRule(_current);
}
private void BroadcastRule(RoundRule rule)
{
Server.PrintToChatAll($" \x10{rule.Name}");
Server.PrintToChatAll($" \x01{rule.Description}");
_centerBannerHtml =
$"<b><font color='#FFD700'>{rule.Name}</font></b><br/>{rule.Description}";
_centerBannerUntil = Server.CurrentTime + CenterBannerSeconds;
RefreshCenterBanner();
}
public IEnumerable<CCSPlayerController> GetHumanPlayers()
{
return Utilities.GetPlayers().Where(p =>
p != null && p.IsValid && !p.IsBot && p.Connected == PlayerConnectedState.PlayerConnected);
}
public IEnumerable<CCSPlayerController> GetCombatPlayers()
{
return Utilities.GetPlayers().Where(p =>
p != null && p.IsValid &&
p.PawnIsAlive &&
(p.Team == CsTeam.Terrorist || p.Team == CsTeam.CounterTerrorist));
}
private bool TryParseRuleIndex(string text, out int index)
{
index = -1;
if (string.IsNullOrWhiteSpace(text))
return false;
if (int.TryParse(text.Trim(), out var number) && number >= 1 && number <= _rules.Count)
{
index = number - 1;
return true;
}
var match = _rules.FindIndex(r => r.Name.Equals(text.Trim(), StringComparison.OrdinalIgnoreCase));
if (match < 0)
return false;
index = match;
return true;
}
private static bool IsWarmup()
{
var proxy = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").FirstOrDefault();
return proxy?.GameRules?.WarmupPeriod == true;
}
private bool IsAdmin(CCSPlayerController player)
{
try
{
if (AdminManager.PlayerHasPermissions(player, "@css/generic") ||
AdminManager.PlayerHasPermissions(player, "@css/root"))
return true;
}
catch
{
// ignored
}
try
{
var path = Path.Combine(Server.GameDirectory, "csgo", "cfg", "MatchZy", "admins.json");
if (!File.Exists(path))
return false;
using var doc = JsonDocument.Parse(File.ReadAllText(path));
var steamId = player.SteamID.ToString();
foreach (var prop in doc.RootElement.EnumerateObject())
{
if (prop.Name == steamId)
return true;
}
}
catch
{
// ignored
}
return false;
}
private static void Reply(CCSPlayerController? player, string message)
{
if (player == null || !player.IsValid)
Server.PrintToConsole($"[随机规则] {message}");
else
player.PrintToChat($"{Prefix}{message}");
}
}