-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAFKManager.as
668 lines (466 loc) · 15.8 KB
/
AFKManager.as
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
Copyright (c) 2017 Drake "MrOats" Denston
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
Current Status: Unstable/Under Development, report bugs on forums.
Documentation: https://github.com/MrOats/AngelScript_SC_Plugins/wiki/AFKManager.as
*/
final class AFK_Data
{
private Vector m_lastOrigin;
private float m_lastMove;
private bool m_isSpectate = false;
private int m_secondsUntilSpec = 0;
private int m_secondsUntilKick = 0;
private int m_secondsLastWarn = 0;
private CBasePlayer@ m_pPlayer;
private string m_szPlayerName = "";
private string m_szSteamID = "";
private Status m_afkstatus;
private CScheduledFunction@ initTimer = null;
//AFK Data Properties
Vector lastOrigin
{
get const { return m_lastOrigin; }
set { m_lastOrigin = value; }
}
float lastMove
{
get const { return m_lastMove; }
set { m_lastMove = value; }
}
bool isSpectate
{
get const { return m_isSpectate; }
set { m_isSpectate = value; }
}
int secondsUntilSpec
{
get const { return m_secondsUntilSpec; }
set { m_secondsUntilSpec = value; }
}
int secondsUntilKick
{
get const { return m_secondsUntilKick; }
set { m_secondsUntilKick = value; }
}
int secondsLastWarn
{
get const { return m_secondsLastWarn; }
set { m_secondsLastWarn = value; }
}
CBasePlayer@ pPlayer
{
get const { return m_pPlayer; }
set { @m_pPlayer = value; }
}
string szSteamID
{
get const { return m_szSteamID; }
}
string szPlayerName
{
get const { return m_szPlayerName; }
}
Status afkstatus
{
get const { return m_afkstatus; }
set { m_afkstatus = value; }
}
//AFK Data Functions
void ClearInitTimer()
{
g_Scheduler.RemoveTimer(@initTimer);
@initTimer = null;
}
void Initiate()
{
@initTimer = g_Scheduler.SetInterval(this, "CheckAFK", 1, g_Scheduler.REPEAT_INFINITE_TIMES);
}
void UpdateLastOrigin()
{
lastOrigin = pPlayer.GetOrigin();
}
void UpdateLastMove()
{
lastMove = pPlayer.m_flLastMove;
}
void KickPlayer()
{
if (g_KickAdmins.GetBool())
{
g_Log.PrintF("[AFKM] Kicking \"" + szPlayerName + "\" for being AFK \n");
g_EngineFuncs.ServerCommand("kick #" + string(g_EngineFuncs.GetPlayerUserId(pPlayer.edict())) + " \"Kicked for being AFK. You may rejoin\" " + "\n");
//g_AdminControl.KickPlayer(pPlayer, 1);
}
else if ( !g_KickAdmins.GetBool() &&
!(g_PlayerFuncs.AdminLevel(pPlayer) >= ADMIN_YES) )
{
g_Log.PrintF("[AFKM] Kicking \"" + szPlayerName + "\" for being AFK \n");
g_EngineFuncs.ServerCommand("kick #" + string(g_EngineFuncs.GetPlayerUserId(pPlayer.edict())) + " \"Kicked for being AFK. You may rejoin\"" + "\n");
//g_AdminControl.KickPlayer(pPlayer, -1);
}
else
g_Log.PrintF("[AFKM] Error Code 8! KickPlayer() could not resolve. \n");
}
bool HasMoved()
{
if ( (pPlayer !is null) && (pPlayer.IsConnected()) )
{
if (lastMove == pPlayer.m_flLastMove)
{
//Player did not move
return false;
}
else
{
//Player moved!
return true;
}
}
else
return false;
/*
//Use LastOrigin var instead?
if ( (pPlayer !is null) && (pPlayer.IsConnected()) )
{
if (lastOrigin.opEquals(pPlayer.GetOrigin()))
{
//Player did not move
return false;
}
else
{
//Player moved!
return true;
}
}
else return false;
*/
}
void CheckAFK()
{
if ( (pPlayer !is null) && (pPlayer.IsConnected()) )
{
if (HasMoved())
{
//Reset Vars
if (g_ShouldSpec.GetBool())
secondsUntilSpec = g_SecondsUntilSpec.GetInt();
if (g_ShouldKick.GetBool())
secondsUntilKick = g_SecondsUntilKick.GetInt();
secondsLastWarn = 0;
//Get them out of Spectate because they are not AFK!
if (afkstatus == AFKSPEC)
{
MessageWarnPlayer(pPlayer, "Moving you back to the game through respawn...");
g_PlayerFuncs.RespawnPlayer(pPlayer, true, true);
g_AdminControl.KillPlayer(pPlayer, 0); /* Set to 0 because we don't want to add onto mp_respawndelay */
}
//Set status back to NOTAFK
afkstatus = NOTAFK;
UpdateLastMove();
}
else if (!HasMoved())
{
//Set player's status to AFK if they haven't been set anything yet
if (afkstatus < AFKALIVE)
afkstatus = AFKALIVE;
if (g_ShouldSpec.GetBool())
{
if (afkstatus == AFKALIVE)
{
if (secondsUntilSpec == 0)
{
MoveToSpectate();
MessageWarnAllPlayers(pPlayer, (szPlayerName + " has been moved to spectate for being AFK."));
MessageWarnPlayer(pPlayer, "Move around a bit if you want to get out of Specate.");
secondsLastWarn = 0;
}
else if (secondsLastWarn == g_WarnInterval.GetInt())
{
MessageWarnPlayer(pPlayer, string(secondsUntilSpec) + " seconds until you are moved to Specate for being AFK.");
secondsLastWarn = 0;
}
//Decrease time until moving to Spectate, and increase warning time
secondsUntilSpec -= 1;
secondsLastWarn += 1;
}
else if (afkstatus == AFKSPEC)
{
if (secondsUntilKick == 0)
{
MessageWarnAllPlayers(pPlayer, (szPlayerName) + " has been kicked for being AFK.");
KickPlayer();
}
else if (secondsLastWarn == g_WarnInterval.GetInt())
{
//Make sure to warn properly based on KickAdmin and ShouldKick CVar
if (g_ShouldKick.GetBool())
{
if (g_KickAdmins.GetBool())
{
MessageWarnPlayer(pPlayer, "Move around a bit if you want to get out of Specate.");
MessageWarnPlayer(pPlayer, string(secondsUntilKick) + " seconds until you are kicked for being AFK.");
secondsLastWarn = 0;
}
else if (!g_KickAdmins.GetBool())
{
if (g_PlayerFuncs.AdminLevel(pPlayer) >= ADMIN_YES)
{
MessageWarnPlayer(pPlayer, "Move around a bit if you want to get out of Specate.");
secondsLastWarn = 0;
}
else
{
MessageWarnPlayer(pPlayer, "Move around a bit if you want to get out of Specate.");
MessageWarnPlayer(pPlayer, string(secondsUntilKick) + " seconds until you are kicked for being AFK.");
secondsLastWarn = 0;
}
}
else
g_Log.PrintF("[AFKM] Error Code 5! KickAdmins CVar configured incorrectly.\n");
}
else if (!g_ShouldKick.GetBool())
{
MessageWarnPlayer(pPlayer, "Move around a bit if you want to get out of Specate.");
secondsLastWarn = 0;
}
else
g_Log.PrintF("[AFKM] Error Code 10! ShouldKick CVar configured incorrectly.\n");
}
//Handle seconds variables based on AdminLevel and ShouldKick CVar
if (g_ShouldKick.GetBool())
{
if (g_KickAdmins.GetBool())
{
secondsUntilKick -= 1;
secondsLastWarn += 1;
}
else if (!g_KickAdmins.GetBool())
{
if (g_PlayerFuncs.AdminLevel(pPlayer) >= ADMIN_YES)
{
secondsLastWarn += 1;
}
else
{
secondsUntilKick -= 1;
secondsLastWarn += 1;
}
}
else
g_Log.PrintF("[AFKM] Error Code 5! KickAdmins CVar configured incorrectly.\n");
}
else if (!g_ShouldKick.GetBool())
{
secondsLastWarn += 1;
}
else
g_Log.PrintF("[AFKM] Error Code 10! ShouldKick CVar configured incorrectly.\n");
}
else
g_Log.PrintF("[AFKM] Error Code 4! Player's status was not set to AFK??\n");
UpdateLastMove();
}
else if (g_ShouldKick.GetBool())
{
if (secondsUntilKick == 0)
{
MessageWarnAllPlayers(pPlayer, (szPlayerName) + " has been kicked for being AFK.");
KickPlayer();
}
else if (secondsLastWarn == g_WarnInterval.GetInt())
{
//Make sure to warn properly based on KickAdmin config
if (g_KickAdmins.GetBool())
{
MessageWarnPlayer(pPlayer, string(secondsUntilKick) + " seconds until you are kicked for being AFK.");
secondsLastWarn = 0;
}
else if (!g_KickAdmins.GetBool())
{
if (g_PlayerFuncs.AdminLevel(pPlayer) < ADMIN_YES)
{
MessageWarnPlayer(pPlayer, string(secondsUntilKick) + " seconds until you are kicked for being AFK.");
secondsLastWarn = 0;
}
else if (g_PlayerFuncs.AdminLevel(pPlayer) >= ADMIN_YES)
{
//Understandable, have a great day.
//MessageWarnPlayer(pPlayer, "You are now AFK, but no action will be taken against you.");
//secondsLastWarn = 0;
}
}
else
g_Log.PrintF("[AFKM] Error Code 5! KickAdmins CVar configured incorrectly.\n");
}
//Handle seconds variables based on AdminLevel and CVar
if (g_KickAdmins.GetBool())
{
secondsUntilKick -= 1;
secondsLastWarn += 1;
}
else if (!g_KickAdmins.GetBool())
{
if (g_PlayerFuncs.AdminLevel(pPlayer) >= ADMIN_YES)
{
secondsLastWarn += 1;
}
else
{
secondsUntilKick -= 1;
secondsLastWarn += 1;
}
}
else
g_Log.PrintF("[AFKM] Error Code 5! KickAdmins CVar configured incorrectly.\n");
UpdateLastMove();
}
else
g_Log.PrintF("[AFKM] Error Code 1! ShouldSpec or ShouldKick has been configured incorrectly. One of them needs to be true.\n");
}
else
g_Log.PrintF("[AFKM] Error Code 2! Could not determine if player has moved. Null pointer?\n");
}
else
g_Log.PrintF("[AFKM] Error Code 3! Player not found, null pointer! Failed to remove object data?\n");
}
void MoveToSpectate()
{
g_Log.PrintF("[AFKM] Moving \"" + szPlayerName + "\" to spectate for being AFK. \n");
afkstatus = AFKSPEC;
pPlayer.GetObserver().StartObserver( pPlayer.GetOrigin(), pPlayer.pev.angles, false );
g_Scheduler.SetTimeout("SetRespawnTime", .75f, @pPlayer);
}
//Constructor
AFK_Data(CBasePlayer@ pPlr)
{
@m_pPlayer = pPlr;
m_szSteamID = g_EngineFuncs.GetPlayerAuthId( pPlayer.edict() );
m_szPlayerName = pPlayer.pev.netname;
//lastOrigin = pPlayer.GetOrigin();
m_lastMove = pPlayer.m_flLastMove;
m_secondsUntilSpec = g_SecondsUntilSpec.GetInt();
m_secondsUntilKick = g_SecondsUntilKick.GetInt();
m_secondsLastWarn = 0;
m_afkstatus = NOTAFK;
}
//Adding a default constructor for the SetInterval @ Initiate();
AFK_Data()
{
}
}
//Global Vars
array<AFK_Data@> afk_plr_data;
CCVar@ g_ShouldSpec;
CCVar@ g_SecondsUntilSpec;
CCVar@ g_ShouldKick;
CCVar@ g_SecondsUntilKick;
CCVar@ g_KickAdmins;
CCVar@ g_WarnInterval;
enum Status
{
NOTAFK,
AFKALIVE,
AFKSPEC
}
//Begin!
void PluginInit()
{
g_Module.ScriptInfo.SetAuthor("MrOats");
g_Module.ScriptInfo.SetContactInfo("http://forums.svencoop.com/showthread.php/44666-Plugin-AFK-Manager");
g_Hooks.RegisterHook(Hooks::Player::ClientPutInServer, @AddPlayer);
g_Hooks.RegisterHook(Hooks::Player::PlayerSpawn, @CheckSpectate);
g_Hooks.RegisterHook(Hooks::Player::ClientDisconnect, @DisconnectCleanUp);
g_Hooks.RegisterHook(Hooks::Game::MapChange, @ResetVars);
@g_ShouldSpec = CCVar("bShouldSpec", true, "Should player be moved to spectate for being AFK?", ConCommandFlag::AdminOnly);
@g_SecondsUntilSpec = CCVar("secondsUntilSpec", 180, "Seconds until player should be moved to Spectate for being AFK", ConCommandFlag::AdminOnly);
@g_ShouldKick = CCVar("bShouldKick", true, "Should player be kicked for being AFK?", ConCommandFlag::AdminOnly);
@g_SecondsUntilKick = CCVar("secondsUntilKick", 600, "Seconds until player ", ConCommandFlag::AdminOnly);
@g_KickAdmins = CCVar("bKickAdmins", true, "Should admins/owners be kicked for being AFK?", ConCommandFlag::AdminOnly);
@g_WarnInterval = CCVar("secondsWarnInterval", 30, "How many seconds should lapse until player is warned that AFK action will be taken", ConCommandFlag::AdminOnly);
}
void MapActivate()
{
afk_plr_data.resize(g_Engine.maxClients);
for (uint i = 0; i < afk_plr_data.length(); i++)
{
@afk_plr_data[i] = null;
}
}
HookReturnCode AddPlayer(CBasePlayer@ pPlayer)
{
if (g_ShouldSpec.GetBool() || g_ShouldKick.GetBool())
{
AFK_Data@ afkdataobj = AFK_Data(pPlayer);
@afk_plr_data[pPlayer.entindex() - 1] = @afkdataobj;
//afkdataobj.UpdateLastMove();
afkdataobj.Initiate();
return HOOK_HANDLED;
}
else
{
g_Game.AlertMessage(at_logged, "AFK Manager disabled due to both functions being turned off.\n");
return HOOK_HANDLED;
}
}
HookReturnCode CheckSpectate(CBasePlayer@ pPlayer)
{
AFK_Data@ afkdataobj = @afk_plr_data[pPlayer.entindex() - 1];
if (afkdataobj !is null && pPlayer.IsConnected())
{
if (afkdataobj.isSpectate)
{
afkdataobj.MoveToSpectate();
return HOOK_HANDLED;
}
else
return HOOK_HANDLED;
}
else
return HOOK_HANDLED;
}
HookReturnCode DisconnectCleanUp(CBasePlayer@ pPlayer)
{
AFK_Data@ afkdataobj = @afk_plr_data[pPlayer.entindex() - 1];
afkdataobj.ClearInitTimer();
@afkdataobj = null;
return HOOK_HANDLED;
}
HookReturnCode ResetVars()
{
for (uint i = 0; i < afk_plr_data.length(); i++)
{
if (afk_plr_data[i] !is null)
afk_plr_data[i].ClearInitTimer();
}
return HOOK_HANDLED;
}
CBasePlayer@ PickRandomPlayer()
{
CBasePlayer@ pPlayer;
for (int i = 1; i <= g_Engine.maxClients; i++)
{
@pPlayer = g_PlayerFuncs.FindPlayerByIndex(i);
if ( (pPlayer !is null) && (pPlayer.IsConnected()) )
break;
}
return @pPlayer;
}
void MessageWarnPlayer(CBasePlayer@ pPlayer, string msg)
{
g_PlayerFuncs.SayText(pPlayer, "[AFKM] " + msg + "\n");
}
void MessageWarnAllPlayers(CBasePlayer@ pPlayer, string msg)
{
g_PlayerFuncs.SayTextAll(pPlayer, "[AFKM] " + msg + "\n");
}
void SetRespawnTime(CBasePlayer@ pPlayer)
{
pPlayer.m_flRespawnDelayTime = Math.FLOAT_MAX;
return;
}