This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rd_elo.sp
1136 lines (951 loc) · 34.3 KB
/
rd_elo.sp
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SourceMod RD Matchmaking & ELO
Copyright (C) 2020 jhheight
This program is private software. You are not allowed to use, modify, copy
reverse engineer, duplicate or distribute it in any way without the explicit
permission of the owner.
*/
#pragma semicolon 1
/* SM Includes */
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#define VERSION "0.11.1"
/* Plugin Info */
public Plugin myinfo =
{
name = "AS:RD ELO",
author = "Mithrand, jhheight",
description = "ELO module for Reactive Drop",
url = "https://github.com/mithrand0",
version = VERSION
};
/*****************************
* Constants
****************************/
#define UNINITIALIZED -1
#define UNKNOWN 0
#define DEFAULT_ELO 1200
#define OFFSET_ELO 600
#define MAP_RESTART_DELAY 6
#define MAP_PRINT_DELAY 2
// difficulties
#define DIFFICULTY_NOTSET 0
#define DIFFICULTY_EASY 1
#define DIFFICULTY_NORMAL 2
#define DIFFICULTY_HARD 3
#define DIFFICULTY_INSANE 4
#define DIFFICULTY_BRUTAL 5
/*****************************
* Global variables
****************************/
// mapece
int mapEce = UNINITIALIZED;
int mapId = UNINITIALIZED;
int mapRetries = UNINITIALIZED;
char mapName[128];
bool mapStarted = false;
// team
int teamRetries = UNKNOWN;
// some convars we need to check
ConVar currentChallenge;
ConVar currentDifficulty;
ConVar aswVoteFraction;
ConVar readyOverride;
ConVar friendlyFireAbsorbtion;
ConVar hordeOnslaught;
// player scoreboard
int playerElo[MAXPLAYERS+1];
int playerPrevElo[MAXPLAYERS+1];
int playerActive[MAXPLAYERS+1];
int playerSteamId[MAXPLAYERS+1];
int playerRanking[MAXPLAYERS+1];
int playerHealthRemaining[MAXPLAYERS+1];
// player bonusses
int playerRetries[MAXPLAYERS+1]; // elo penalty after 3rd (asbi 4th) retry, or 10% additional loss immediately on ragequit
int playerTeamKills[MAXPLAYERS+1]; // elo penalty after > 2 team kills per mission or 10% additional loss immediately on ragequit
int playerAlienDamageTaken[MAXPLAYERS+1]; // negative bonus if damage > 100 (0.2x elo x damage taken)
int playerTeamDamageDone[MAXPLAYERS+1]; // negative bonus if damage > 200 (0.1x elo x damage done)
int playerSuicide[MAXPLAYERS+1]; // exclusion from mission success elo when no kills/heals bonus achieved. 10% additional loss after 3rd retry.
int playerTeamHeals[MAXPLAYERS+1]; // individual bonus on top of map ece with maximum at 100
int playerAlienKills[MAXPLAYERS+1]; // max bonus at 100
int playerTeamInfestedCures[MAXPLAYERS+1]; // bonus cured marines * 10
int playerFastReloadExpert[MAXPLAYERS+1]; // one time +10 after 5th fast reload
int playerAchievementEarned[MAXPLAYERS+1]; // +10 for every achievement (does this trigger is already achieved?)
int playerBeaconsPlaced[MAXPLAYERS+1]; // +2 for every deployment (heal/amplify)
int playerAmmoDeployments[MAXPLAYERS+1]; // see above, ammo
int playerTeamExtinguishes[MAXPLAYERS+1]; // player has extinguised another player
// player state
int playerDied[MAXPLAYERS+1]; // incicates the player died
int playerRageQuit[MAXPLAYERS+1]; // player has rage quit
// team bonusses
// int teamUsesTesla;
// int teamUsesFlamer;
// int teamUsesGrenadeLauncher;
// database handle
Database db;
// debug message
bool debugEnabled = false;
char debugMessage[512];
// event hooks
new String:eventHooksPost[][] = {
"achievement_earned",
"alien_died",
"damage_amplifier_placed",
"fast_reload",
"heal_beacon_placed",
"marine_hurt",
"marine_extinguished",
"marine_infested_cured",
"marine_selected",
"player_fullyjoined",
"player_heal",
"player_heal_target",
"player_deploy_ammo"
};
new String:eventHooksPre[][] = {
"asw_mission_restart",
"mission_failed",
"mission_success"
};
/*****************************
* Plugin start
****************************/
// on plugin start
public void OnPluginStart()
{
// connect to db
connectDb();
// hook some convars
currentChallenge = FindConVar("rd_challenge");
currentDifficulty = FindConVar("asw_skill");
aswVoteFraction = FindConVar("asw_vote_map_fraction");
readyOverride = FindConVar("rd_ready_mark_override");
hordeOnslaught = FindConVar("asw_horde_override");
friendlyFireAbsorbtion = FindConVar("asw_marine_ff_absorption");
// convar hooks
disableReadyOverride();
HookConVarChange(currentChallenge, Event_OnSettingsChanged);
HookConVarChange(currentDifficulty, Event_OnSettingsChanged);
HookConVarChange(readyOverride, Event_OnSettingsChanged);
HookConVarChange(hordeOnslaught, Event_OnSettingsChanged);
HookConVarChange(friendlyFireAbsorbtion, Event_OnSettingsChanged);
// disable map voting
aswVoteFraction.SetFloat(2.0);
// hook into pre events
for (new v=0; v<sizeof(eventHooksPre); v++) {
HookEvent(eventHooksPre[v], Event_Dispatcher, EventHookMode_Pre);
}
// hook into post events
for (new v=0; v<sizeof(eventHooksPost); v++) {
HookEvent(eventHooksPost[v], Event_Dispatcher, EventHookMode_Post);
}
// log
PrintToServer("[ELO] initialized");
}
/*****************************
* Database related
****************************/
/**
* Connect to database
*/
public Action connectDb()
{
char dbError[256];
db = SQL_Connect("elo", true, dbError, sizeof(dbError));
if (db == null) {
PrintToServer("[ELO] connect error: %s, disabling plugin..", dbError);
return Plugin_Stop;
} else {
PrintToServer("[ELO] connected to db");
}
return Plugin_Continue;
}
public void dbQuery(Database handle, DBResultSet results, const char[] error, any data)
{
if (!StrEqual(error, "")) {
PrintToServer("[ELO] db error: %s", error);
}
delete results;
}
/*****************************
* Player client functions
****************************/
public bool isValidPlayer(int client)
{
return IsClientConnected(client) && IsClientInGame(client) && !IsFakeClient(client) && isActivePlayer(client);
}
public bool isActivePlayer(int client)
{
if (client == UNKNOWN || client > MAXPLAYERS) {
return false;
} else {
return playerActive[client] > 0;
}
}
/**
* When a client connects, reset the slot with default data. This also
* fetches ELO rating from the database.
*/
public void OnClientConnected(int client)
{
// logging
FormatEx(debugMessage, sizeof(debugMessage), "OnClientConnected trigged");
printDebugMessage(client);
if (playerElo[client] == UNKNOWN || playerElo[client] == UNINITIALIZED || !playerElo[client]) {
if (IsClientConnected(client) && !IsFakeClient(client)) {
// db
int steamid = GetSteamAccountID(client);
if (steamid) {
// store steam id
playerSteamId[client] = steamid;
// fetch elo
char query[256];
FormatEx(query, sizeof(query), "SELECT elo, scoreboard, FIND_IN_SET(elo, (SELECT GROUP_CONCAT(elo ORDER BY elo DESC) FROM player_score)) FROM player_score WHERE steamid = %d", steamid);
PrintToServer("[ELO] %s", query);
DBResultSet results = SQL_Query(db, query);
int elo = DEFAULT_ELO;
int rank = UNKNOWN;
char scoreboard[256];
while (SQL_FetchRow(results)) {
elo = SQL_FetchInt(results, 0);
SQL_FetchString(results, 1, scoreboard, sizeof(scoreboard));
rank = SQL_FetchInt(results, 2);
}
playerElo[client] = elo;
playerPrevElo[client] = UNINITIALIZED;
playerRanking[client] = rank;
// load the scoreboard
loadPlayerScoreboard(client, scoreboard);
delete results;
}
}
}
}
/**
* If a client disconnects, free the slot
*
* OnClientDisconnect is triggered on map change, use the _Post
* variance to make sure the client is not coming back
*/
public void OnClientDisconnect(int client)
{
// logging
FormatEx(debugMessage, sizeof(debugMessage), "OnClientDisconnect triggered");
printDebugMessage(client);
// check if the player was playing
if (playerActive[client] > 0 && mapStarted == true) {
// logging
PrintToChatAll("[ELO] player did quit during active game, assigning elo penalty");
FormatEx(debugMessage, sizeof(debugMessage), "Player did ragequit");
printDebugMessage(client);
// player rq
playerRageQuit[client] = 1;
int groupElo = calculateGroupElo();
updatePlayerElo(client, groupElo, false);
}
}
/*****************************
* Map related
****************************/
/**
* Fetch the current map name
*/
public void OnMapStart()
{
// logging
FormatEx(debugMessage, sizeof(debugMessage), "OnMapStart triggered");
printDebugMessage(UNKNOWN);
// check if ff and onslaught are on
if (friendlyFireAbsorbtion.IntValue != 0) {
PrintToChatAll("[ELO] friendly fire needs to be enabled for ranked game");
} else if (hordeOnslaught.IntValue == 0) {
PrintToChatAll("[ELO] onslaught needs to be enabled for ranked game");
} else {
// fetch current map and challenge
GetCurrentMap(mapName, sizeof(mapName));
char challenge[256];
currentChallenge.GetString(challenge, sizeof(challenge));
// if no challege, challenge will be 0
if (StrEqual(challenge, "0")) {
challenge = "";
}
// fetch map elo from db
char query[256];
FormatEx(query, sizeof(query), "SELECT score, retry_limit, id FROM map_score WHERE map_name = '%s' and challenge = '%s'", mapName, challenge);
DBResultSet results = SQL_Query(db, query);
PrintToServer("[ELO:db] %s", query);
while (SQL_FetchRow(results)) {
// params
int dbEce = SQL_FetchInt(results, 0);
int difficulty = currentDifficulty.IntValue;
mapRetries = SQL_FetchInt(results, 1);
mapId = SQL_FetchInt(results, 2);
if (difficulty < DIFFICULTY_HARD) {
// disable for easy and normal
mapEce = 0;
} else if (difficulty == DIFFICULTY_HARD) {
mapEce = RoundFloat(dbEce * 0.65);
} else if (difficulty == DIFFICULTY_INSANE) {
mapEce = RoundFloat(dbEce * 0.85);
} else if (difficulty == DIFFICULTY_BRUTAL) {
mapEce = dbEce;
}
}
delete results;
if (mapEce) {
for (new i = 1; i < MaxClients; i++) {
if (isValidPlayer(i)) {
ShowPlayerElo(i);
}
}
PrintToChatAll("[ELO] ranking enabled, map ece is %d with %d tries", mapEce, mapRetries);
} else {
PrintToChatAll("[ELO] unsupported map, difficulty or challenge, ranking is disabled.");
}
}
disableReadyOverride();
}
public void disableReadyOverride()
{
if (readyOverride.IntValue > 0) {
readyOverride.SetInt(0);
}
}
/**
* Display ELO to players
*/
public void ShowPlayerElo(int client)
{
CreateTimer(MAP_PRINT_DELAY + 0.2, PrintWelcomePlayer, client);
CreateTimer(MAP_PRINT_DELAY + 1.0, PrintPlayerElo, client);
}
public Action PrintPlayerElo(Handle timer, int client)
{
if (isValidPlayer(client)) {
int elo = playerElo[client];
int prevElo = playerPrevElo[client];
if (elo == UNKNOWN || elo == UNINITIALIZED) {
elo = DEFAULT_ELO;
}
if (prevElo == UNINITIALIZED || elo == prevElo || playerRetries[client] > 0) {
if (playerRanking[client] == UNKNOWN) {
PrintToChatAll("[ELO] %N has no ranking", client);
} else {
PrintToChatAll("[ELO] %N is ranked #%d and has %d elo", client, playerRanking[client], elo);
}
} else if (elo > prevElo) {
PrintToChatAll("[ELO] %N has gained %d elo and has now %d", client, elo - prevElo, elo);
} else {
PrintToChatAll("[ELO] %N has lost %d elo and has now %d", client, prevElo - elo, elo);
}
}
}
public Action PrintWelcomePlayer(Handle timer, int client)
{
if (isValidPlayer(client)) {
int elo = playerElo[client];
int prevElo = playerElo[client];
if (prevElo == UNINITIALIZED || elo == prevElo) {
PrintToChat(client, "[ELO] welcome %N. You joined a ranked server, ELO version is %s", client, VERSION);
}
}
}
/*****************************
* Events
****************************/
public Action Event_Dispatcher(Event event, const char[] name, bool dontBroadcast)
{
FormatEx(debugMessage, sizeof(debugMessage), "Event dispatched: %s", name);
printDebugMessage(UNKNOWN);
if (StrEqual(name, "marine_selected")) {
return Event_OnMarineSelected(event);
} else if (StrEqual(name, "player_fullyjoined")) {
return Event_OnPlayerJoined(event);
} else if (StrEqual(name, "mission_success")) {
return Event_OnMapSuccess(event);
} else if (StrEqual(name, "mission_failed") || StrEqual(name, "asw_mission_restart")) {
return Event_OnMapFailed(event);
} else if (StrEqual(name, "marine_hurt")) {
return Event_OnMarineDamage(event);
} else if (StrEqual(name, "player_heal") || StrEqual(name, "player_heal_target")) {
return Event_OnHeal(event);
} else if (StrEqual(name, "player_deploy_ammo")) {
return Event_OnAmmoDeployed(event);
} else if (StrEqual(name, "damage_amplifier_placed") || StrEqual(name, "heal_beacon_placed")) {
return Event_OnStuffDeployed(event);
} else if (StrEqual(name, "alien_died")) {
return Event_OnAlienKilled(event);
} else if (StrEqual(name, "marine_extinguished")) {
return Event_OnMarineExtinguished(event);
} else if (StrEqual(name, "marine_infested_cured")) {
return Event_OnInfestionCure(event);
} else if (StrEqual(name, "achievement_earned")) {
return Event_OnAchievement(event);
} else {
FormatEx(debugMessage, sizeof(debugMessage), "Event not forwarded: %s", name);
}
return Plugin_Continue;
}
public void Event_OnSettingsChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
// relay to map start
OnMapStart();
}
public Action Event_OnPlayerJoined(Event event)
{
int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
if (!playerRanking[client]) {
OnClientConnected(client);
}
ShowPlayerElo(client);
return Plugin_Continue;
}
public Action Event_OnMarineSelected(Event event)
{
// this gets triggered after mission_start, or in game if a player joins
mapStarted = true;
int numMarines = event.GetInt("count");
int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
// re-fetch player's elo, in case it is unknown
if (playerElo[client] == UNINITIALIZED || playerElo[client] == UNKNOWN) {
OnClientConnected(client);
}
if (numMarines == 0) {
// check if he dselected marine, aka asw_afk but played
if (playerActive[client] > numMarines && playerRetries[client] > 0) {
// asw afk award
int groupElo = calculateGroupElo();
updatePlayerElo(client, groupElo, false);
PrintToChatAll("[ELO] %N did afk during map retry, awarding elo penalty", client);
}
} else if (numMarines == 1) {
// mark the player as playing
playerActive[client] = numMarines;
// display what flesh just joined
ShowPlayerElo(client);
} else {
PrintToChatAll("[ELO] %N is using bots, disabling elo rankings", client);
mapStarted = false;
}
return Plugin_Continue;
}
public Action Event_OnMapRestart(Event event)
{
// relay to map failed
if (mapStarted) {
return Event_OnMapFailed(event);
} else {
return Plugin_Continue;
}
}
public Action Event_OnMapFailed(Event event)
{
// group elo
int groupElo = calculateGroupElo();
if (mapStarted == true) {
// raise fail scores only once every map
mapStarted = false;
bool mapChange = false;
// iterate players
teamRetries = 0;
for (new i = 1; i <= MaxClients; i++) {
if (isActivePlayer(i)) {
playerRetries[i]++;
if (playerRetries[i] > teamRetries) {
teamRetries = playerRetries[i];
}
// store player scoreboard
storePlayerScoreboard(i);
if (playerRetries[i] >= mapRetries) {
// one player hit retry limit, probably whole lobby did
mapChange = true;
}
}
}
// change the map
if (mapChange) {
// award elo penalty
for (new i = 1; i <= MaxClients; i++) {
// award if player was active and tried more than one time
if (isActivePlayer(i) && playerRetries[i] > 1) {
updatePlayerElo(i, groupElo, false);
}
}
CreateTimer(MAP_PRINT_DELAY - 0.2, Print_OnTeamFailed);
CreateTimer(MAP_RESTART_DELAY + 0.0, changeRandomMap);
return Plugin_Stop;
}
// display retry count
CreateTimer(MAP_PRINT_DELAY + 0.0, Print_OnMapFailed);
}
return Plugin_Continue;
}
public Action Print_OnMapFailed(Handle timer)
{
PrintToChatAll("[ELO] mission failed, try %d of %d", teamRetries, mapRetries);
}
public Action Print_OnTeamFailed(Handle timer)
{
PrintToChatAll("[ELO] team failed too often, changing map..");
}
public Action Event_OnMapSuccess(Event event)
{
// group elo
int groupElo = calculateGroupElo();
CreateTimer(MAP_PRINT_DELAY - 0.2, Print_OnMapSuccess);
for (new i = 1; i <= MaxClients; i++) {
// check if the player is still here
if (isValidPlayer(i)) {
// award elo
updatePlayerElo(i, groupElo, true);
}
// erase scoreboard afterwards
playerActive[i] = 0;
}
CreateTimer(MAP_RESTART_DELAY + 0.0, changeRandomMap);
return Plugin_Stop;
}
public Action Print_OnMapSuccess(Handle timer)
{
PrintToChatAll("[ELO] mission succeeded, awarding elo to active players");
}
/*****************************
* Mid game events
****************************/
public Action Event_OnMarineDamage(Event event)
{
// find how much the marine is hurt
float health = event.GetFloat("health");
// marine
int victim = event.GetInt("userid");
int victimClient = GetClientOfUserId(victim);
// find out who did this
int attacker = event.GetInt("attacker");
int client = GetClientOfUserId(attacker);
// store attacker damage
int damage = 1;
if (playerHealthRemaining[client] > 0) {
// we know exactly how much
damage = RoundFloat(playerHealthRemaining[client] - health);
}
playerTeamDamageDone[client] += damage;
playerHealthRemaining[client] = RoundFloat(health);
if (playerHealthRemaining[client] == 0) {
playerDied[victimClient] = 1;
// check who did this
if (victim == attacker) {
// self inflicted
playerSuicide[client] = 1;
} else {
playerTeamKills[client]++;
}
}
return Plugin_Continue;
}
public Action Event_OnHeal(Event event)
{
// look who healed
int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
playerTeamHeals[client]++;
return Plugin_Continue;
}
public Action Event_OnAmmoDeployed(Event event)
{
int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
playerAmmoDeployments[client]++;
return Plugin_Continue;
}
public Action Event_OnStuffDeployed(Event event)
{
int client = event.GetInt("marine");
if (isActivePlayer(client)) {
playerBeaconsPlaced[client]++;
}
return Plugin_Continue;
}
public Action Event_OnAlienKilled(Event event)
{
int client = event.GetInt("marine");
if (isActivePlayer(client)) {
playerAlienKills[client]++;
}
return Plugin_Continue;
}
public Action Event_OnFastReload(Event event)
{
int client = event.GetInt("marine");
if (isActivePlayer(client)) {
int reloadSpree = event.GetInt("reloads");
if (reloadSpree >= 5) {
playerFastReloadExpert[client] = 1;
}
}
return Plugin_Continue;
}
public Action Event_OnInfestionCure(Event event)
{
int userid = event.GetInt("curer");
int client = GetClientOfUserId(userid);
playerTeamInfestedCures[client]++;
return Plugin_Continue;
}
public Action Event_OnMarineExtinguished(Event event)
{
int userid = event.GetInt("extinguisher");
int client = GetClientOfUserId(userid);
playerTeamExtinguishes[client]++;
return Plugin_Continue;
}
public Action Event_OnAchievement(Event event)
{
int client = event.GetInt("player");
if (isActivePlayer(client)) {
playerAchievementEarned[client]++;
}
return Plugin_Continue;
}
/*****************************
* Change level
****************************/
public Action changeRandomMap(Handle timer)
{
// fetch current map and challenge
char map[256];
GetCurrentMap(map, sizeof(map));
char challenge[256];
currentChallenge.GetString(challenge, sizeof(challenge));
// if no challege, challenge will be 0
if (StrEqual(challenge, "0")) {
challenge = "";
}
// cleanup old shit
cleanRankings();
// fetch a random map
char query[256];
FormatEx(query, sizeof(query), "SELECT map_name FROM map_score WHERE map_name != '%s' and challenge = '%s' order by rand() limit 1", map, challenge);
PrintToServer("[ELO:db] %s", query);
DBResultSet results = SQL_Query(db, query);
while (SQL_FetchRow(results)) {
char newMap[128];
SQL_FetchString(results, 0, newMap, sizeof(newMap));
char command[192];
FormatEx(command, sizeof(command), "changelevel %s", newMap);
ServerCommand(command);
return Plugin_Handled;
}
return Plugin_Continue;
}
/*****************************
* Score related
****************************/
public calculateGroupElo()
{
int totalElo = 0;
int totalSpectatorElo = 0;
int players = 0;
int spectators = 0;
for (new i = 1; i <= MaxClients; i++) {
if (isValidPlayer(i)) {
totalElo += playerElo[i];
players++;
}
totalSpectatorElo += playerElo[i];
spectators++;
}
// they all went afk
if (players == UNKNOWN) {
players = spectators;
totalElo = totalSpectatorElo;
}
return RoundFloat(totalElo / players + 0.0);
}
public void updatePlayerRetry(int client)
{
// escape map name
char escapedMapName[256];
db.Escape(mapName, escapedMapName, sizeof(escapedMapName));
char query[512];
FormatEx(query, sizeof(query), "UPDATE player_score set last_map = '%s' where steamid = %d", playerRetries[client], escapedMapName, playerSteamId[client]);
PrintToServer("[ELO:db] %s", query);
db.Query(dbQuery, query, client);
}
public void updatePlayerElo(int client, int groupElo, bool success)
{
// calculate new elo
playerPrevElo[client] = playerElo[client];
int elo = calculateElo(client, groupElo, success);
// write it to db
if (elo > UNKNOWN) {
// get some player information
int steamid = playerSteamId[client];
// store elo
char query[1024];
FormatEx(query, sizeof(query), "INSERT INTO player_score (steamid, elo, version) values (%d, %d, '%s') ON DUPLICATE KEY UPDATE elo = %d, last_map = ''", steamid, elo, VERSION, elo);
PrintToServer("[ELO:db] %s", query);
db.Query(dbQuery, query, client);
// store history
int gain = elo - playerPrevElo[client];
FormatEx(query, sizeof(query), "INSERT INTO player_history (steamid, elo, gain, map_challenge, difficulty, version) values (%d, %d, %d, %d, %d, '%s')", steamid, elo, gain, mapId, currentDifficulty.IntValue, VERSION);
PrintToServer("[ELO:db] %s", query);
db.Query(dbQuery, query, client);
// reset scoreboard
resetPlayerScoreboard(client);
// if we can retrieve a name, update it
if (isValidPlayer(client)) {
char name[128];
GetClientName(client, name, sizeof(name));
// escape the player name properly
char escapedName[256];
db.Escape(name, escapedName, sizeof(escapedName));
FormatEx(query, sizeof(query), "UPDATE player_score set name = '%s' where steamid = %d", escapedName, steamid);
PrintToServer("[ELO:db] %s", query);
db.Query(dbQuery, query, client);
}
playerElo[client] = elo;
ShowPlayerElo(client);
}
// reset the player slot
playerActive[client] = 0;
}
public calculateElo(int client, int groupEloScore, bool success)
{
int currentElo = playerElo[client];
float groupElo = groupEloScore + 0.0;
float elo = currentElo + 0.0;
float ece = mapEce + 0.0;
// teamkills
if (playerTeamKills[client] > 0) {
ece = ece * 0.1 * playerTeamKills[client];
}
// suicide
if (playerSuicide[client] > 0) {
ece -= ece * 0.2 * playerSuicide[client];
}
// team heals
if (playerTeamHeals[client] > 0) {
int healPoints = playerTeamHeals[client];
if (healPoints > 100) {
healPoints = 100;
}
ece += ece * 0.001 * healPoints;
}
// alien kills
if (playerAlienKills[client] > 0) {
int kills = playerAlienKills[client];
if (kills > 100) {
kills = 100;
}
ece += ece * 0.001 * kills;
}
// marine cures
if (playerTeamInfestedCures[client] > 0) {
ece += ece * 0.1 * playerTeamInfestedCures[client];
}
// achievements
if (playerAchievementEarned[client] > 0) {
ece += ece * 0.01 * playerAchievementEarned[client];
}
// extinguishes
if (playerTeamExtinguishes[client] > 0) {
ece += ece * 0.01 * playerTeamExtinguishes[client];
}
// support beacon bonus
if (playerBeaconsPlaced[client] > 0) {
ece += ece * 0.01 * playerBeaconsPlaced[client];
}
// ammo deployment bonus, todo: detect if it was for others
if (playerAmmoDeployments[client] > 0) {
ece += ece * 0.01 * playerAmmoDeployments[client];
}
// marine played perfect
if (playerTeamDamageDone[client] < 1 && playerAlienDamageTaken[client] < 1) {
// perfect!
ece += ece * 0.2;
}
// personal damage taken from swarm
if (playerAlienDamageTaken[client] > 0) {
ece -= ece * 0.001 * playerAlienDamageTaken[client];
}
// personal damage done to friendlies
if (playerTeamDamageDone[client] > 0) {
ece -= ece * 0.0001 * playerTeamDamageDone[client];
}
// ragequit award
if (playerRageQuit[client] > 0) {
ece -= ece * 0.3;
}
// do not calculate if client is uninitialized
if (currentElo == UNINITIALIZED) {
// reconnect the client
OnClientConnected(client);
} else if (ece != UNINITIALIZED) {
// success
if (success == true) {
float gain = (ece - groupElo + OFFSET_ELO) / 10;
if (gain <= 4.0) {
elo = elo + 1.0;
} else {
elo = elo + 1.0 / (elo / groupElo) * gain;
}
} else {
// failure
float eloPenalty = (groupElo - ece + OFFSET_ELO) / 10;
if (eloPenalty > 0.0) {
elo = elo - (elo / groupElo) * eloPenalty;
}
}
}
return RoundFloat(elo);
}
public void cleanRankings()
{
// cleanup old entries
db.Query(dbQuery, "UPDATE player_score set last_map = NULL where updated_at < date_sub(now(), interval 1 hour)");
db.Query(dbQuery, "DELETE FROM player_history where updated_at < date_sub(now(), interval 6 month)");
// players loose elo if they (rage)quit with an active scoreboard, and never join back
db.Query(dbQuery,"INSERT INTO player_history (steamid, elo, gain, scoreboard, version) SELECT steamid, elo * 0.90, 0 - elo * 0.10, '{ \"ragequit\": 1, \"score\": \"-10%\" }', version FROM player_score WHERE scoreboard IS NOT NULL AND scoreboard != '' AND updated_at < date_sub(now(), interval 4 hour)"
);
db.Query(dbQuery, "UPDATE player_score set scoreboard = NULL, elo = elo * 0.90 where scoreboard IS NOT NULL AND scoreboard != '' AND updated_at < date_sub(now(), interval 4 hour)");
// players loose elo after some time of inactivity, down to the initial elo of 1500
db.Query(dbQuery,
"INSERT INTO player_history (steamid, elo, gain, scoreboard, version) SELECT steamid, elo * 0.99, 0 - elo * 0.01, '{ \"inactivity\": 1, \"score\": \"-1%\" }', version FROM player_score WHERE elo >= 1200 * 0.01 and updated_at < date_sub(now(), interval 1 week)"
);
db.Query(dbQuery, "UPDATE player_score set elo = elo * 0.99 where elo >= 1200 * 0.01 and updated_at < date_sub(now(), interval 1 week)");
}
public void printDebugMessage(int client)
{
if (debugEnabled == true) {
if (client != UNKNOWN && isValidPlayer(client)) {
PrintToServer("[ELO:debug] %L: %s", client, debugMessage);
PrintToChatAll("[ELO:debug] %L: %s", client, debugMessage);
} else {
PrintToServer("[ELO:debug] %s", debugMessage);
PrintToChatAll("[ELO:debug] %s", debugMessage);
}
}
}
public void storePlayerScoreboard(int client)