forked from PapaCharlie9/multi-balancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MULTIbalancer.cs
15237 lines (13210 loc) · 662 KB
/
MULTIbalancer.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
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
/* MULTIbalancer.cs
Copyright 2013, by PapaCharlie9
`
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, without restriction.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;
using System.Net;
using System.Web;
using System.Data;
using System.Threading;
using System.Timers;
using System.Diagnostics;
using System.ComponentModel;
using System.Reflection;
using System.Xml;
using System.Windows.Forms;
using PRoCon.Core;
using PRoCon.Core.Plugin;
using PRoCon.Core.Plugin.Commands;
using PRoCon.Core.Players;
using PRoCon.Core.Players.Items;
using PRoCon.Core.Battlemap;
using PRoCon.Core.Maps;
namespace PRoConEvents
{
/* Aliases */
using EventType = PRoCon.Core.Events.EventType;
using CapturableEvent = PRoCon.Core.Events.CapturableEvents;
/* Main Class */
public class MULTIbalancer : PRoConPluginAPI, IPRoConPluginInterface
{
/* Enums */
public enum GameVersion { BF3, BF4, BFH };
public enum MessageType { Warning, Error, Exception, Normal, Debug };
public enum PresetItems { Standard, Aggressive, Passive, Intensify, Retain, BalanceOnly, UnstackOnly, None };
public enum Speed { Click_Here_For_Speed_Names, Stop, Slow, Adaptive, Fast, Unstack };
public enum DefineStrong { RoundScore, RoundSPM, RoundKills, RoundKDR, PlayerRank, RoundKPM, BattlelogSPM, BattlelogKDR, BattlelogKPM };
public enum PluginState { Disabled, JustEnabled, Active, Error, Reconnected };
public enum GameState { RoundEnding, RoundStarting, Playing, Warmup, Unknown };
public enum MoveType { Balance, Unstack, Unswitch };
public enum ForbidBecause { None, MovedByBalancer, ToWinning, ToBiggest, DisperseByRank, DisperseByList, DisperseByClan };
public enum Phase {Early, Mid, Late};
public enum Population {Low, Medium, High};
public enum UnstackState {Off, SwappedStrong, SwappedWeak};
public enum FetchState {New, InQueue, Requesting, Aborted, Succeeded, Failed};
public enum Scope {SameTeam, SameSquad, Total, TeamOne, TeamTwo, TeamThree, TeamFour};
public enum UnswitchChoice {Always, Never, LatePhaseOnly};
public enum BattlelogStats {ClanTagOnly, AllTime, Reset};
public enum DivideByChoices {None, ClanTag, DispersalGroup};
public enum ScrambleStatus {Success, Failure, PartialSuccess, CompletelyFull};
public enum ChatScope {Global, Team, Squad, Player};
public enum IGCommand {None, Add, Delete, List, New};
public enum ForceMove {Newest, Weakest, Random};
/* Constants & Statics */
public const double SWAP_TIMEOUT = 600; // in seconds
public const double MODEL_TIMEOUT = 24*60; // in minutes
public const int CRASH_COUNT_HEURISTIC = 24; // player count difference signifies a crash
public const int MIN_UPDATE_USAGE_COUNT = 20; // minimum number of plugin updates in use
public const double CHECK_FOR_UPDATES_MINS = 12*60; // 12 hours
public const double MIN_ADAPT_FAST = 30.0;
public const String INVALID_NAME_TAG_GUID = "////////";
public static String[] TEAM_NAMES = new String[] { "None", "US", "RU" };
public static String[] BF4_TEAM_NAMES = new String[] { "US", "RU", "CN" }; // Indexed by faction code!
public static String[] RUSH_NAMES = new String[] { "None", "Attacking", "Defending" };
public static String[] SQUAD_NAMES = new String[] { "None",
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel",
"India", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa",
"Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "Xray",
"Yankee", "Zulu", "Haggard", "Sweetwater", "Preston", "Redford", "Faith", "Celeste"
};
public const String DEFAULT_LIST_ITEM = "[-- name, tag, or EA_GUID --]";
public static String[] ROLE_NAMES = new String[] {"PLAYER", "SPECTATOR", "COMMANDER", "MOBILE COMMANDER"};
public const uint WL_BALANCE = 1<<0; // B option
public const uint WL_UNSTACK = 1<<1; // U option
public const uint WL_SWITCH = 1<<2; // S option
public const uint WL_DISPERSE = 1<<3; // D option
public const uint WL_RANK = 1<<4; // R option
public const uint WL_ALL = (WL_BALANCE | WL_UNSTACK | WL_SWITCH | WL_DISPERSE | WL_RANK);
public const int MIN_SAMPLE_COUNT = 15;
public const int FACTION_US = 0;
public const int FACTION_RU = 1;
public const int FACTION_CN = 2;
public const int ROLE_PLAYER = 0;
public const int ROLE_SPECTATOR = 1;
public const int ROLE_COMMANDER_PC = 2;
public const int ROLE_COMMANDER_MOBILE = 3;
/* Classes */
#region Classes
public class PerModeSettings {
public PerModeSettings() {}
public PerModeSettings(String simplifiedModeName, GameVersion gameVersion) {
DetermineStrongPlayersBy = DefineStrong.RoundScore;
PercentOfTopOfTeamIsStrong = 50;
DisperseEvenlyByRank = 0;
EnableDisperseEvenlyList = false;
EnableStrictDispersal = true;
EnableScrambler = false;
OnlyMoveWeakPlayers = true;
isDefault = false;
EnableTicketLossRatio = false;
TicketLossSampleCount = 180;
DisperseEvenlyByClanPlayers = 0;
EnableLowPopulationAdjustments = false;
// Rush only
Stage1TicketPercentageToUnstackAdjustment = 0;
Stage2TicketPercentageToUnstackAdjustment = 0;
Stage3TicketPercentageToUnstackAdjustment = 0;
Stage4And5TicketPercentageToUnstackAdjustment = 0;
switch (simplifiedModeName) {
case "Conq Small, Dom, Scav": // BF3
case "Conquest Small":
case "Domination": // BF4
MaxPlayers = (gameVersion == GameVersion.BF4 && simplifiedModeName == "Domination") ? 20 : 32;
CheckTeamStackingAfterFirstMinutes = 10;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 100;
DefinitionOfHighPopulationForPlayers = (gameVersion == GameVersion.BF4 && simplifiedModeName == "Domination") ? 16 :24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 50; // assuming 200 tickets typical
DefinitionOfLatePhaseFromEnd = 50; // assuming 200 tickets typical
MetroAdjustedDefinitionOfLatePhase = 100;
EnableMetroAdjustments = false;
break;
case "Conquest Large":
MaxPlayers = 64;
CheckTeamStackingAfterFirstMinutes = 10;
MaxUnstackingSwapsPerRound = 4;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 150;
DefinitionOfHighPopulationForPlayers = 48;
DefinitionOfLowPopulationForPlayers = 16;
DefinitionOfEarlyPhaseFromStart = 100; // assuming 300 tickets typical
DefinitionOfLatePhaseFromEnd = 100; // assuming 300 tickets typical
EnableMetroAdjustments = false;
MetroAdjustedDefinitionOfLatePhase = 200;
break;
case "CTF":
MaxPlayers = 64;
CheckTeamStackingAfterFirstMinutes = 5;
MaxUnstackingSwapsPerRound = 4;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 48;
DefinitionOfLowPopulationForPlayers = 16;
DefinitionOfEarlyPhaseFromStart = 5; // minutes
DefinitionOfLatePhaseFromEnd = 5; // minutes
break;
case "Rush":
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 5;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 40;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 25; // assuming 75 tickets typical
DefinitionOfLatePhaseFromEnd = 25; // assuming 75 tickets typical
// Rush only
Stage1TicketPercentageToUnstackAdjustment = 5;
Stage2TicketPercentageToUnstackAdjustment = 30;
Stage3TicketPercentageToUnstackAdjustment = 80;
Stage4And5TicketPercentageToUnstackAdjustment = -120;
SecondsToCheckForNewStage = 10;
break;
case "Squad Deathmatch":
MaxPlayers = (gameVersion == GameVersion.BF4) ? 20 : 16;
CheckTeamStackingAfterFirstMinutes = 0;
MaxUnstackingSwapsPerRound = 0;
NumberOfSwapsPerGroup = 0;
DelaySecondsBetweenSwapGroups = 60;
MaxUnstackingTicketDifference = 25;
DefinitionOfHighPopulationForPlayers = 14;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 10; // assuming 50 tickets typical
DefinitionOfLatePhaseFromEnd = 10; // assuming 50 tickets typical
break;
case "Superiority":
MaxPlayers = 24;
CheckTeamStackingAfterFirstMinutes = 15;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 125;
DefinitionOfHighPopulationForPlayers = 16;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 25; // assuming 100 tickets typical
DefinitionOfLatePhaseFromEnd = 25; // assuming 100 tickets typical
break;
case "Team Deathmatch":
MaxPlayers = (gameVersion == GameVersion.BF4) ? 20 : 64;
CheckTeamStackingAfterFirstMinutes = 5;
MaxUnstackingSwapsPerRound = 4;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 50;
DefinitionOfHighPopulationForPlayers = (gameVersion == GameVersion.BF4) ? 16 : 48;
DefinitionOfLowPopulationForPlayers = (gameVersion == GameVersion.BF4) ? 8 : 16;
DefinitionOfEarlyPhaseFromStart = 20; // assuming 100 tickets typical
DefinitionOfLatePhaseFromEnd = 20; // assuming 100 tickets typical
break;
case "Squad Rush":
MaxPlayers = 8;
CheckTeamStackingAfterFirstMinutes = 2;
MaxUnstackingSwapsPerRound = 1;
NumberOfSwapsPerGroup = 1;
DelaySecondsBetweenSwapGroups = 60;
MaxUnstackingTicketDifference = 10;
DefinitionOfHighPopulationForPlayers = 6;
DefinitionOfLowPopulationForPlayers = 4;
DefinitionOfEarlyPhaseFromStart = 5; // assuming 20 tickets typical
DefinitionOfLatePhaseFromEnd = 5; // assuming 20 tickets typical
// Rush only
Stage1TicketPercentageToUnstackAdjustment = 5;
Stage2TicketPercentageToUnstackAdjustment = 30;
Stage3TicketPercentageToUnstackAdjustment = 80;
Stage4And5TicketPercentageToUnstackAdjustment = -120;
SecondsToCheckForNewStage = 10;
break;
case "Gun Master":
MaxPlayers = 16;
CheckTeamStackingAfterFirstMinutes = 2;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 12;
DefinitionOfLowPopulationForPlayers = 6;
DefinitionOfEarlyPhaseFromStart = 0;
DefinitionOfLatePhaseFromEnd = 0;
break;
case "Defuse": // BF4
MaxPlayers = 10;
CheckTeamStackingAfterFirstMinutes = 2;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 8;
DefinitionOfLowPopulationForPlayers = 4;
DefinitionOfEarlyPhaseFromStart = 0;
DefinitionOfLatePhaseFromEnd = 0;
break;
case "Obliteration": // BF4
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 2;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 1;
DefinitionOfLatePhaseFromEnd = 1;
break;
case "Squad Obliteration": // BF4
MaxPlayers = 10;
CheckTeamStackingAfterFirstMinutes = 2;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 8;
DefinitionOfLowPopulationForPlayers = 4;
DefinitionOfEarlyPhaseFromStart = 1;
DefinitionOfLatePhaseFromEnd = 1;
break;
case "NS Carrier Large": // BF4
MaxPlayers = 64;
CheckTeamStackingAfterFirstMinutes = 5;
MaxUnstackingSwapsPerRound = 4;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 48;
DefinitionOfLowPopulationForPlayers = 16;
DefinitionOfEarlyPhaseFromStart = 5; // minutes
DefinitionOfLatePhaseFromEnd = 15; // minutes
break;
case "NS Carrier Small": // BF4
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 5;
MaxUnstackingSwapsPerRound = 4;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 5; // minutes
DefinitionOfLatePhaseFromEnd = 15; // minutes
break;
case "Heist": // BFH
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 2;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 40;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 20; // assuming 100 tickets typical
DefinitionOfLatePhaseFromEnd = 20; // assuming 100 tickets typical
MetroAdjustedDefinitionOfLatePhase = 100;
EnableMetroAdjustments = false;
break;
case "Hotwire": // BFH
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 10;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 100;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 100; // assuming 500 tickets typical
DefinitionOfLatePhaseFromEnd = 100; // assuming 500 tickets typical
MetroAdjustedDefinitionOfLatePhase = 100;
EnableMetroAdjustments = false;
break;
case "Blood Money": // BFH
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 10;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 100;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 25; // assuming 150 tickets typical
DefinitionOfLatePhaseFromEnd = 25; // assuming 150 tickets typical
MetroAdjustedDefinitionOfLatePhase = 100;
EnableMetroAdjustments = false;
break;
case "Bounty Hunter": // BFH
MaxPlayers = 20;
CheckTeamStackingAfterFirstMinutes = 5;
MaxUnstackingSwapsPerRound = 4;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 50;
DefinitionOfHighPopulationForPlayers = 16;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 20; // assuming 100 tickets typical
DefinitionOfLatePhaseFromEnd = 20; // assuming 100 tickets typical
break;
case "Unknown or New Mode":
default:
MaxPlayers = 32;
CheckTeamStackingAfterFirstMinutes = 10;
MaxUnstackingSwapsPerRound = 2;
NumberOfSwapsPerGroup = 2;
DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
MaxUnstackingTicketDifference = 0;
DefinitionOfHighPopulationForPlayers = 24;
DefinitionOfLowPopulationForPlayers = 8;
DefinitionOfEarlyPhaseFromStart = 50;
DefinitionOfLatePhaseFromEnd = 50;
break;
}
}
public int MaxPlayers = 64; // will be corrected later
public double CheckTeamStackingAfterFirstMinutes = 10;
public int MaxUnstackingSwapsPerRound = 4;
public double DelaySecondsBetweenSwapGroups = SWAP_TIMEOUT;
public DefineStrong DetermineStrongPlayersBy = DefineStrong.RoundScore;
public int DefinitionOfHighPopulationForPlayers = 48;
public int DefinitionOfLowPopulationForPlayers = 16;
public int DefinitionOfEarlyPhaseFromStart = 50;
public int DefinitionOfLatePhaseFromEnd = 50;
public int DisperseEvenlyByRank = 145;
public bool EnableDisperseEvenlyList = false;
public double PercentOfTopOfTeamIsStrong = 50;
public int NumberOfSwapsPerGroup = 2;
public bool EnableScrambler = false;
public bool EnableMetroAdjustments = false;
public int MetroAdjustedDefinitionOfLatePhase = 50;
public bool OnlyMoveWeakPlayers = true;
public bool EnableStrictDispersal = true;
public bool EnableTicketLossRatio = false;
public int TicketLossSampleCount = 180;
public int MaxUnstackingTicketDifference = 0;
public int DisperseEvenlyByClanPlayers = 0;
public bool EnableUnstackingByPlayerStats = false;
public bool EnableLowPopulationAdjustments = false;
public double RoutPercentage = 0;
// Rush only
public double Stage1TicketPercentageToUnstackAdjustment = 0;
public double Stage2TicketPercentageToUnstackAdjustment = 0;
public double Stage3TicketPercentageToUnstackAdjustment = 0;
public double Stage4And5TicketPercentageToUnstackAdjustment = 0;
public double SecondsToCheckForNewStage = 10;
public bool EnableAdvancedRushUnstacking = false;
public bool isDefault = true; // not a setting
} // end PerModeSettings
public class FetchInfo {
public FetchState State;
public DateTime Since;
public String RequestType;
public FetchInfo() {
State = FetchState.New;
Since = DateTime.Now;
RequestType = String.Empty;
}
public FetchInfo(FetchInfo rhs) {
this.State = rhs.State;
this.Since = rhs.Since;
this.RequestType = rhs.RequestType;
}
}
public class PlayerModel {
// Permanent
public String Name;
public String EAGUID;
// Updated on events
public int Team;
public int Squad;
public DateTime FirstSeenTimestamp; // on player join or plugin enable
public DateTime FirstSpawnTimestamp;
public DateTime LastSeenTimestamp;
public double ScoreRound;
public double KillsRound;
public double DeathsRound;
public int Rounds; // incremented OnRoundOverPlayers
public int Rank;
public bool IsDeployed;
public String SpawnChatMessage;
public String SpawnYellMessage;
public bool QuietMessage;
public MoveInfo DelayedMove;
public int LastMoveTo;
public int LastMoveFrom;
public int ScrambledSquad;
public int OriginalSquad;
public int Role; // BF4
// Battlelog
public String PersonaId;
public String Tag;
public bool TagVerified;
public String FullName { get {return (String.IsNullOrEmpty(Tag) ? Name : "[" + Tag + "]" + Name);}}
public FetchInfo TagFetchStatus;
public double KDR;
public double SPM;
public double KPM;
public bool StatsVerified;
public FetchInfo StatsFetchStatus;
// Computed
public double KDRRound;
public double SPMRound;
public double KPMRound;
// Accumulated
public double ScoreTotal; // not including current round
public double KillsTotal; // not including current round
public double DeathsTotal; // not including current round
public int MovesTotal; // not including current round
public int MovesByMBTotal; // not include current round
// Per-round state
public int MovesRound; // moves NOT made by this plugin
public int MovesByMBRound; // moves made by this plugin
public DateTime MovedTimestamp;
public DateTime MovedByMBTimestamp;
public List<DateTime> MovedByMBHistory;
// Based on settings
public int DispersalGroup;
public int Friendex; // index (key) to friend list
public uint Whitelist; // bitmask flags, see WL_ALL
// Commands
public bool Subscribed;
public PlayerModel() {
Name = null;
Team = -1;
Squad = -1;
EAGUID = String.Empty;
FirstSeenTimestamp = DateTime.Now;
FirstSpawnTimestamp = DateTime.MinValue;
LastSeenTimestamp = DateTime.MinValue;
Tag = String.Empty;
TagVerified = false;
ScoreRound = -1;
KillsRound = -1;
DeathsRound = -1;
Rounds = -1;
Rank = -1;
KDRRound = -1;
SPMRound = -1;
KPMRound = -1;
ScoreTotal = 0;
KillsTotal = 0;
DeathsTotal = 0;
MovesTotal = 0;
MovesByMBTotal = 0;
IsDeployed = false;
MovesRound = 0;
MovesByMBRound = 0;
MovedTimestamp = DateTime.MinValue;
MovedByMBTimestamp = DateTime.MinValue;
MovedByMBHistory = new List<DateTime>();
SpawnChatMessage = String.Empty;
SpawnYellMessage = String.Empty;
QuietMessage = false;
DelayedMove = null;
LastMoveTo = 0;
LastMoveFrom = 0;
TagFetchStatus = new FetchInfo();
ScrambledSquad = -1;
OriginalSquad = -1;
DispersalGroup = 0;
Friendex = -1;
KDR = -1;
SPM = -1;
KPM = -1;
StatsVerified = false;
PersonaId = String.Empty;
StatsFetchStatus = new FetchInfo();
Subscribed = false;
Whitelist = 0;
Role = ROLE_PLAYER;
}
public PlayerModel(String name, int team) : this() {
Name = name;
Team = team;
}
public void ResetRound() {
ScoreTotal = ScoreTotal + ScoreRound;
KillsTotal = KillsTotal + KillsRound;
DeathsTotal = DeathsTotal + DeathsRound;
MovesTotal = MovesTotal + MovesRound;
MovesByMBTotal = MovesByMBTotal + MovesByMBRound;
Rounds = (Rounds > 0) ? Rounds + 1 : 1;
ScoreRound = -1;
KillsRound = -1;
DeathsRound = -1;
KDRRound = -1;
SPMRound = -1;
KPMRound = -1;
IsDeployed = false;
SpawnChatMessage = String.Empty;
SpawnYellMessage = String.Empty;
QuietMessage = false;
DelayedMove = null;
LastMoveTo = 0;
LastMoveFrom = 0;
MovesRound = 0;
MovesByMBRound = 0;
DispersalGroup = 0;
MovedTimestamp = DateTime.MinValue;
// MovedByMBTimestamp reset when minutes exceeds MinutesAfterBeingMoved
}
public PlayerModel ClonePlayer() {
PlayerModel lhs = new PlayerModel();
lhs.Name = this.Name;
lhs.Team = this.Team;
lhs.Squad = this.Squad;
lhs.EAGUID = this.EAGUID;
lhs.FirstSeenTimestamp = this.FirstSeenTimestamp;
lhs.FirstSpawnTimestamp = this.FirstSpawnTimestamp;
lhs.LastSeenTimestamp = this.LastSeenTimestamp;
lhs.Tag = this.Tag;
lhs.TagVerified = this.TagVerified;
lhs.ScoreRound = this.ScoreRound;
lhs.KillsRound = this.KillsRound;
lhs.DeathsRound = this.DeathsRound;
lhs.Rounds = this.Rounds;
lhs.Rank = this.Rank;
lhs.KDRRound = this.KDRRound;
lhs.SPMRound = this.SPMRound;
lhs.KPMRound = this.KPMRound;
lhs.ScoreTotal = this.ScoreTotal;
lhs.KillsTotal = this.KillsTotal;
lhs.DeathsTotal = this.DeathsTotal;
lhs.MovesTotal = this.MovesTotal;
lhs.MovesByMBTotal = this.MovesByMBTotal;
lhs.IsDeployed = this.IsDeployed;
lhs.MovesRound = this.MovesRound;
lhs.MovesByMBRound = this.MovesByMBRound;
lhs.MovedTimestamp = this.MovedTimestamp;
lhs.MovedByMBTimestamp = this.MovedByMBTimestamp;
lhs.MovedByMBHistory = this.MovedByMBHistory;
lhs.SpawnChatMessage = this.SpawnChatMessage;
lhs.SpawnYellMessage = this.SpawnYellMessage;
lhs.QuietMessage = this.QuietMessage;
lhs.DelayedMove = this.DelayedMove;
lhs.LastMoveTo = this.LastMoveTo;
lhs.LastMoveFrom = this.LastMoveFrom;
lhs.TagFetchStatus = new FetchInfo(this.TagFetchStatus);
lhs.ScrambledSquad = this.ScrambledSquad;
lhs.OriginalSquad = this.OriginalSquad;
lhs.Friendex = this.Friendex;
lhs.KDR = this.KDR;
lhs.SPM = this.SPM;
lhs.KPM = this.KPM;
lhs.StatsVerified = this.StatsVerified;
lhs.PersonaId = this.PersonaId;
lhs.StatsFetchStatus = new FetchInfo(this.StatsFetchStatus);
lhs.Subscribed = this.Subscribed;
lhs.Whitelist = this.Whitelist;
lhs.Role = this.Role;
return lhs;
}
} // end PlayerModel
class TeamRoster {
public int Team = 0;
public List<PlayerModel> Roster = null;
public TeamRoster(int team, List<PlayerModel> roster) {
Team = team;
Roster = roster;
}
} // end TeamRoster
public class SquadRoster {
public int Squad = 0;
public double Metric = 0;
public List<PlayerModel> Roster = null;
public int ClanTagCount = 0;
public int DispersalGroup = 0;
public int WhitelistCount = 0;
public SquadRoster(int squad) {
Squad = squad;
Metric = 0;
Roster = new List<PlayerModel>();
ClanTagCount = 0;
DispersalGroup = 0;
WhitelistCount = 0;
}
public SquadRoster(int squad, List<PlayerModel> roster) {
Squad = squad;
Roster = roster;
ClanTagCount = 0;
DispersalGroup = 0;
WhitelistCount = 0;
}
} // end SquadRoster
public class MoveInfo {
public MoveType For = MoveType.Balance;
public ForbidBecause Because = ForbidBecause.None;
public String Name = String.Empty;
public String Tag = String.Empty;
public int Source = -1;
public String SourceName = String.Empty;
public int Destination = -1;
public String DestinationName = String.Empty;
public String ChatBefore = String.Empty;
public String YellBefore = String.Empty;
public String ChatAfter = String.Empty;
public String YellAfter = String.Empty;
public double Delay = 0;
public bool Fast = false;
public bool aborted = false;
public MoveInfo() {}
public MoveInfo(String name, String tag, int fromTeam, String fromName, int toTeam, String toName, double delay) : this() {
Name = name;
Tag = tag;
Source = fromTeam;
SourceName = (String.IsNullOrEmpty(fromName)) ? fromTeam.ToString() : fromName;
Destination = toTeam;
DestinationName = (String.IsNullOrEmpty(toName)) ? toTeam.ToString() : toName;
Delay = delay;
}
public void Format(MULTIbalancer plugin, String fmt, bool isYell, bool isBefore) {
String expanded = fmt;
if (String.IsNullOrEmpty(expanded)) return;
String reason = String.Empty;
if (For == MoveType.Unswitch) {
switch (this.Because) {
case ForbidBecause.MovedByBalancer:
reason = plugin.BadBecauseMovedByBalancer;
break;
case ForbidBecause.DisperseByList:
reason = plugin.BadBecauseDispersalList;
break;
case ForbidBecause.DisperseByRank:
reason = plugin.BadBecauseRank;
break;
case ForbidBecause.ToBiggest:
reason = plugin.BadBecauseBiggestTeam;
break;
case ForbidBecause.ToWinning:
reason = plugin.BadBecauseWinningTeam;
break;
case ForbidBecause.DisperseByClan: // DCE
reason = plugin.BadBecauseClan;
break;
case ForbidBecause.None:
default:
reason = "(no reason)";
break;
}
if (expanded.Contains("%reason%")) expanded = expanded.Replace("%reason%", reason);
}
if (expanded.Contains("%name%")) expanded = expanded.Replace("%name%", Name);
if (expanded.Contains("%tag%")) expanded = expanded.Replace("%tag%", Tag);
if (expanded.Contains("%fromTeam%")) expanded = expanded.Replace("%fromTeam%", SourceName);
if (expanded.Contains("%toTeam%")) expanded = expanded.Replace("%toTeam%", DestinationName);
if (isYell) {
if (isBefore) {
YellBefore = expanded;
} else {
YellAfter = expanded;
}
} else {
if (isBefore) {
ChatBefore = expanded;
} else {
ChatAfter = expanded;
}
}
}
public override String ToString() {
String s = "Move(";
s += "[" + Tag + "]" + Name + ",";
s += For + ",";
s += Source + "(" + SourceName + "),";
s += Destination + "(" + DestinationName + "),";
s += "CB'" + ChatBefore + "',";
s += "YB'" + YellBefore + "',";
s += "CA'" + ChatAfter + "',";
s += "YA'" + YellAfter + "')";
return s;
}
} // end MoveInfo
public class DelayedRequest {
public String Name;
public double MaxDelay; // in seconds
public DateTime LastUpdate;
public Action<DateTime> Request;
public DelayedRequest() {
MaxDelay = 0;
LastUpdate = DateTime.MinValue;
Request = null;
Name = null;
}
public DelayedRequest(double delay, DateTime last) {
MaxDelay = delay;
LastUpdate = last;
Request = null;
Name = null;
}
} // end DelayedRequest
public class PriorityQueue {
/*
This class models a prioritized single queue.
Tag requests are given priority over stats requests.
When the type of request doesn't matter, such as for Count, the unified value is used.
When it does matter, such as distinguishing one request from another for Dequeue,
direct access to the member variable is used.
*/
public Queue<String> TagQueue; // of player names
public Queue<String> StatsQueue; // of player names
private MULTIbalancer fPlugin;
public PriorityQueue() {
TagQueue = new Queue<String>();
StatsQueue = new Queue<String>();
fPlugin = null;
}
public PriorityQueue(MULTIbalancer plugin) : this() {
fPlugin = plugin;
}
public int Count {
get {return (TagQueue.Count + StatsQueue.Count);}
}
public bool Contains(String name) {
return (TagQueue.Contains(name) || StatsQueue.Contains(name));
}
public void Enqueue(String name) {
if (!TagQueue.Contains(name)) TagQueue.Enqueue(name);
if (fPlugin.WhichBattlelogStats != BattlelogStats.ClanTagOnly) {
if (!StatsQueue.Contains(name)) StatsQueue.Enqueue(name);
}
}
public void Clear() {
TagQueue.Clear();
StatsQueue.Clear();
}
}
public class Histogram {
public const int BIN_SIZE = 100;
public SortedDictionary<int,int> Bin;
public int MaxBin;
public int PeakBin;
public int MaxFrequency;
public int Total;
public Histogram() {
this.Bin = new SortedDictionary<int,int>();
this.MaxBin = 0;
this.PeakBin = 1;
this.MaxFrequency = 0;
this.Total = 0;
this.Bin[PeakBin] = 0;
}
public void Clear() {
Bin.Clear();
MaxBin = 0;
PeakBin = 1;
MaxFrequency = 0;
Total = 0;
Bin[PeakBin] = 0;
}
public void Add(int sample) {
if (sample < 100) return;
int binNumber = sample / BIN_SIZE;
// insure bin and all bins up to this bin are initialized
if (!Bin.ContainsKey(binNumber)) {
Bin[binNumber] = 1;
for (int i = 1; i < binNumber; ++i) {
if (!Bin.ContainsKey(i)) Bin[i] = 0;
}
} else {
Bin[binNumber] = Bin[binNumber] + 1;
}
MaxBin = Math.Max(MaxBin, binNumber);
MaxFrequency = Math.Max(MaxFrequency, Bin[binNumber]);
if (Bin[PeakBin] < Bin[binNumber]) PeakBin = binNumber;
++Total;
}
public List<String> Log(int maxLine) {
List<String> log = new List<String>();
// multiply normFactor into each frequency count to get a value less than or equal to maxLine
double normFactor = Convert.ToDouble(maxLine) / Convert.ToDouble(MaxFrequency);
log.Add(String.Format("Total ratios = {0}, bins = {1}, peak bin = {2}, peak count = {3}, scale factor = {4:F4}",
Total,
MaxBin,
PeakBin * BIN_SIZE,
MaxFrequency,
normFactor));
foreach (int bin in Bin.Keys) {
if (bin == 0) continue;
StringBuilder buf = new StringBuilder(String.Format("{0,5}:", bin * BIN_SIZE));
int normFreq = (Bin[bin] == 0) ? 0 : Convert.ToInt32(Math.Ceiling(Bin[bin] * normFactor));
for (int i = 0; i < normFreq; ++i) {
buf.Append("#");
}
log.Add(buf.ToString());
}
return log;
}
}
#endregion
/* Inherited:
this.PunkbusterPlayerInfoList = new Dictionary<String, CPunkbusterInfo>();
this.FrostbitePlayerInfoList = new Dictionary<String, CPlayerInfo>();
*/
// General
private bool fIsEnabled;
private bool fFinalizerActive = false;
private bool fAborted = false;
private Dictionary<String,String> fModeToSimple = null;
private Dictionary<String,int> fPendingTeamChange = null;
private Thread fMoveThread = null;
private Thread fFetchThread = null;
private Thread fListPlayersThread = null;
private Thread fScramblerThread = null;
private Thread fTimerThread = null;
private List<String> fReservedSlots = null;
private bool fRefreshCommand = false;
private int fServerUptime = -1;
private bool fServerCrashed = false; // because fServerUptime > fServerInfo.ServerUptime
private DateTime fLastBalancedTimestamp = DateTime.MinValue;
private DateTime fEnabledTimestamp = DateTime.MinValue;
private String fLastMsg = null;
private DateTime fLastVersionCheckTimestamp = DateTime.MinValue;
private double fTimeOutOfJoint = 0;
private List<PlayerModel>[] fDebugScramblerBefore = null;
private List<PlayerModel>[] fDebugScramblerAfter = null;
private List<PlayerModel>[] fDebugScramblerStartRound = null;
private DelayedRequest fUpdateThreadLock;
private DateTime fLastServerInfoTimestamp;
private String fHost;
private String fPort;
private List<String> fRushMap3Stages = null;
private List<String> fRushMap5Stages = null;
private int[] fGroupAssignments = null; // index is group number, value is team id