-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProconRulz.cs
6896 lines (6164 loc) · 633 KB
/
ProconRulz.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
//******************************************************************************************************
// ProconRulz Procon plugin, by bambam
//******************************************************************************************************
/* Copyright 2013 Ian Forster-Lewis
This file is part of my ProconRulz plugin for ProCon.
ProconRulz plugin for ProCon is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ProconRulz plugin for ProCon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ProconRulz plugin for ProCon. If not, see <http://www.gnu.org/licenses/>.
*/
#region release notes
// v44 - a. support for BF4
// b. support for rulz files (Plugins\BF4\proconrulz_*.txt)
// c. 'Not' modifier now also allowed with If and Text conditions
// d. added VictimTeamKey
// e. Enable/Disable rulz.txt files in settings
// f. Linux support for external rulz files (credit FritzE)
// g. On Init trigger added, for var initialisations
// h. On RoundOver trigger added. Bugfix for NULL team
// j. Update to reload .txt rulz files on plugin enable, and display full path if not found
// v43 - a. added 'rounding' to vars, e.g. %x.3% is 3 decimal places
// b. Allow "Set %ini% 0" to reset ini file, Set "%ini_<section>% 0" to delete section,
// c. bugfix for TargetPlayer condition when %targettext% is null. Added '+' for rule continuation
// d. added %score% var for each player (aka %server_score[<playername>]%)
// v42 - a. Support %ini_XXX% variables stored in settings
// b. New 'logging' options in plugin settings
// c. arithmetic in Set clauses e.g. Set %x% %x%+1
// d. another effort at the procon URLencode settings work-around
// e. allow spaces in arithmetic e.g. "If %x% + %y% > 10;"
// f. new subst vars %ts1%, %ts2%, %pts% for teamsize 1, 2, playerteamsize
// g. new subst var %hms% for time, %seconds% for time in seconds, %ymd% for yyyy-mm-dd
// v41 - a. Allow parsing of chat text e.g. to pick out a ban reason
// v40 - a. Quoted strings in Exec e.g. Exec vars.serverName "Example server name - Admins Online"
// b. Quoted strings in Set e.g. Set %a% "hello world", $..$ for subst vars
// c. $ replaced with % in rulz_vars to avoid Procon settings encode bug
// d. fix for admin kill triggering On Suicide
// v39 - a. TeamSay, SquadSay, TeamYell, SquadYell,
// %pcountry%, %pcountrykey%, %vcountry%, %vcountrykey% (country codes for player and victim)
// b. Allow 'Exec' actions to call Procon commands
// Allow 'int' value for yell delay (seconds) in Yell actions
// c. Ban and TempBan ban by 'name' if no GUID available
// d. %team_score% aka %server_team_score[<teamid>]%
// v38 - a. actions and conditions can be mixed in any order,
// b. %streak% is shorthand for %server_streak[%p%]%,
// %team_streak% is %server_team_streak[%ptk%]%
// %squad_streak% is %server_squad_streak[%ptk%][%psk%]%
// rulz vars can be nested using brackets, e.g. %kills[%weapon%]%
// c. modify Exec action to support punkBuster commands
// d. TargetActions are now immediate, TargetPlayer only succeeds IFF 1 player found
// e. Yell delay (default 5 seconds) added to plugin settings
// v37 - string vars (as well as ints from v35)
// b. var names can have embedded subst vars, e.g. "server_%v%_deaths" is a var name with a player name embedded
// c. moved the subst vars processing into assign_keywords
// d. moved most of the Details help text onto the web
// e. bugfix for %c% - set value for actions, temp value for conditions
// f. bugfix for PBBan message
// g. If "%text% word abc" condition,
// allow %vars% in Set %newvar% %p%,%newvar% conditions
// h. added punkBuster.pb_sv_command pb_sv_updbanfile to speed up PB Bans/Kicks
// v36 - Punkbuster bans
// v35 - a. Multiple keys in conditions e.g. "Weapon RPG-7,SMAW"
// b. xyzzy debug
// c. continue
// d. rulz vars Set Incr Decr If
// e. actions thread inline, no decode on rulz, %ps% PlayerSquad
// f. propagate trigger from prior rulz, default continue unless Kill/Kick/TempBan/Ban/End
// v34 - Protected now Admin, Admin_and_Reserved_Slots, Neither
// v33 - BF3 compatibility
// TargetPlayer now auto-TargetConfirm if only one match
// v32 - TargetPlayer,TargetAction,TargetConfirm,TargetCancel, %t% substitution for target
// PlayerCount,TeamCount,ServerCount, %tc% and %sc% substitution for team and server counts
// TempBan action
// Ping condition, %ping% substitution
// PlayerYell,PlayerBoth, AdminSay, %text% substition
// 'Rates' now span round ends
// bugfix for multi-word weapon keys, e.g. "M1A1 Thompson", heli weapons
// bugfix for sp_shotgun_s
// On Join, On Leave triggers
// Whitelist for clans and players
// PlayerFirst, TeamFirst, ServerFirst, PlayerOnce conditions
// Player_loses_item_when_dead bugfix
// v31 - Map, MapMode conditions, On Round trigger, VictimSay action
// (31b bugfix for On Spawn;Damage...)
// (31c bugfix for teamsize)
// v30 - settings options - rules message as PlayerSay, disable rules message
// v29 - TeamKit, TeamDamage, TeamSpec, TeamWeapon counts conditions
// v28 - updated to use latest Procon 1 API
// v27 - "Protected" condition
// v26 - "On Say; Text xxx;" trigger, Headshot %h% substitution
// v25 - "Range N;" condition
// v24 - new conditions Admin (player an admin), Admins (admins on server)
// v23 - fixed Count bug
// v22 - added rule comments ('#' as first char)
// v20 - changed rule to have <list> of Conditions
// v19 - added "Not [Kit|Spec|Damage|Weapon] X" condition
#endregion
#region includes
using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using PRoCon; // added for FileHostNamePort()
using PRoCon.Core;
using PRoCon.Core.Plugin;
using PRoCon.Core.Players;
using PRoCon.Core.Players.Items;
using PRoCon.Core.Plugin.Commands;
using PRoCon.Core.Accounts;
using PRoCon.Core.Remote; // for FileHostNamePort()
using System.IO;
#endregion
namespace PRoConEvents
{
public class ProconRulz : PRoConPluginAPI, IPRoConPluginInterface
{
public string version = "44j.1";
#region Types
// the 'Rates' condition keeps track of this many 'rule trigger times' for each player
const int RATE_HISTORY = 60;
//**********************************************************************************************
//**********************************************************************************************
// DEFINE SOME TYPES
//**********************************************************************************************
//**********************************************************************************************
enum GameIdEnum { BFBC2, MoH, BF3, BF4 }
enum ReserveItemEnum { Player_loses_item_when_dead, Player_reserves_item_until_respawn }
enum LogFileEnum { PluginConsole, Console, Chat, Events, Discard_Log_Messages }
// these are the executable actions
// note that 'TargetAction <action>' does not actually have
// 'TargetAction' as a defined action, instead it
// is treated as a boolean flag on the PartClass to indicate the action is delayed
enum PartEnum
{ Yell, Say, PlayerYell, PlayerSay, VictimSay, TeamYell, TeamSay, SquadYell, SquadSay,
Log, Both, PlayerBoth, All, AdminSay, // various message actions
// actions affecting 'target' e.g. playername in say text:
TargetConfirm, TargetCancel,
Kill, Kick, PlayerBlock, Ban, TempBan, PBBan, PBKick,
Execute, Continue, End,
Headshot, Protected, Admin, Admins, Team, Teamsize,
Map, MapMode,
Kit, Weapon, Spec, Damage, TeamKit, TeamWeapon, TeamSpec, TeamDamage,
Range,
Count, PlayerCount, TeamCount, ServerCount,
Rate, Text, TargetPlayer, Ping,
Set, Incr, Decr, Test
};
// flag for rule to fire either on player spawn, or a kill
// Void mean absence of trigger
enum TriggerEnum { Void, Round, RoundOver, Join, Kill, Spawn, TeamKill, Suicide, PlayerBlock, Say, Leave, Init }
enum ItemTypeEnum { None, Kit, Weapon, Spec, Damage }
// substitution values in messages
enum SubstEnum {Player, Victim,
Weapon, WeaponKey, Damage, DamageKey, Spec, SpecKey,
Kit, VictimKit, KitKey,
Count, TeamCount, ServerCount,
PlayerTeam, PlayerSquad,
PlayerTeamKey, PlayerSquadKey,
PlayerCountry, PlayerCountryKey, // from pb info
VictimCountry, VictimCountryKey,
Teamsize, Teamsize1, Teamsize2, PlayerTeamsize,
VictimTeam, VictimTeamKey,Range, BlockedItem, Headshot,
Map, MapMode, Target, Text, TargetText, Ping, EA_GUID, PB_GUID, IP,
Hhmmss, Seconds, Date
};
static Dictionary<SubstEnum, List<string>> subst_keys = new Dictionary<SubstEnum, List<string>>();
// which players should be 'protected from kicks, kills and bans by ProconRulz
enum ProtectEnum { Admins, Admins_and_Reserved_Slots, Neither };
// a PART is a statement within a rule, i.e. a condition or action
class PartClass
{
// this action should be delayed and applied to another target not current player
public bool target_action;
public PartEnum part_type; // e.g. Kick
public bool negated; // conditions can be 'Not'
public List<string> string_list;
public int int1;
public int int2;
public bool has_count; // true if this part refers to a rule count (%c% or PlayerCount etc)
// e.g. "You were kicked for teamkills". Can also be an integer string for Kill action
public PartClass()
{
target_action = false;
string_list = new List<string>();
int1 = 0;
int2 = 0;
negated = false;
has_count = false;
}
public string ToString()
{
string part_string;
part_string = (negated ? " Not" : "");
switch (part_type)
{
case PartEnum.Headshot:
part_string += " Headshot;";
break;
case PartEnum.Protected:
part_string += " Player protected from kick/kill;";
break;
case PartEnum.Admin:
part_string += " Admin player;";
break;
case PartEnum.Admins:
part_string += " Admins are online;";
break;
case PartEnum.Ping:
part_string += String.Format(" Ping >= {0};", int1);
break;
case PartEnum.Team:
part_string += String.Format(" Team \"{0}\";", keys_join(string_list));
break;
case PartEnum.Teamsize:
part_string += String.Format(" Teamsize <= {0};", int1);
break;
case PartEnum.Map:
part_string += String.Format(" Map name includes \"{0}\";", keys_join(string_list));
break;
case PartEnum.MapMode:
part_string += String.Format(" Map mode is \"{0}\";", keys_join(string_list));
break;
case PartEnum.Kit:
part_string += " Kit \"" +
keys_join(string_list) + (int1 == 0 ? "\";" : String.Format("\" max({0});", int1));
break;
case PartEnum.Weapon:
part_string += " Weapon is \"" +
keys_join(string_list) + (int1 == 0 ? "\";" : String.Format("\" max({0});", int1));
break;
case PartEnum.Spec:
part_string += " Spec \"" +
keys_join(string_list) + (int1 == 0 ? "\";" : String.Format("\" max({0});", int1));
break;
case PartEnum.Damage:
part_string += " Damage \"" +
keys_join(string_list) + (int1 == 0 ? "\";" : String.Format("\" max({0});", int1));
break;
case PartEnum.TeamKit:
part_string += " TeamKit \"" +
keys_join(string_list) + String.Format("\" max({0});", int1);
break;
case PartEnum.TeamWeapon:
part_string += " TeamWeapon \"" +
keys_join(string_list) + String.Format("\" max({0});", int1);
break;
case PartEnum.TeamSpec:
part_string += " TeamSpec \"" +
keys_join(string_list) + String.Format("\" max({0});", int1);
break;
case PartEnum.TeamDamage:
part_string += " TeamDamage \"" +
keys_join(string_list) + String.Format("\" max({0});", int1);
break;
case PartEnum.Range:
part_string += " Range " + String.Format("over {0};", int1);
break;
case PartEnum.Count:
case PartEnum.PlayerCount:
part_string += " Player Rule Count is more than " + String.Format("{0};", int1);
break;
case PartEnum.TeamCount:
part_string += " Team Rule Count is more than " + String.Format("{0};", int1);
break;
case PartEnum.ServerCount:
part_string += " Server Rule Count is more than " + String.Format("{0};", int1);
break;
case PartEnum.Rate:
part_string += " Rate " + String.Format("{0} in {1} seconds;", int1, int2);
break;
case PartEnum.Text:
part_string += " Text " + String.Format("key \"{0}\";", keys_join(string_list));
break;
case PartEnum.TargetPlayer:
if (string_list != null)
part_string += " Target player contains " +
String.Format("\"{0}\";", keys_join(string_list));
else
part_string += " Target player %t% found;";
break;
case PartEnum.Incr:
part_string += String.Format(" Incr {0};", string_list[0]);
break;
case PartEnum.Decr:
part_string += String.Format(" Decr {0};", string_list[0]);
break;
case PartEnum.Set:
part_string += String.Format(" Set {0};", keys_join(string_list));
break;
case PartEnum.Test:
part_string += String.Format(" If [{0}];", keys_join(string_list));
break;
default:
part_string += (String.Format(" {0}{1} [int: {2}] [string: {3}];",
target_action ? "TargetAction " : "",
Enum.GetName(typeof(PartEnum), part_type),
int1 == null || int1 == 0 ? "" : int1.ToString(),
string_list[0]));
break;
}
return part_string;
}
} // end class PartClass
// deferred actions from 'TargetAction' actions in a triggered rule
class TargetActions
{
public string target;
public List<PartClass> actions;
public TargetActions(string t)
{
actions = new List<PartClass>();
target = t;
}
}
// here's how the rules are stored in ProconRulz (trigger, list of parts)
class ParsedRule
{
public ParsedRule()
{
parts = new List<PartClass>();
comment = false;
trigger = TriggerEnum.Spawn;
}
public int id; // rule identifier 1..n
public List<PartClass> parts; // e.g. [Not Kit Recon 2]
public TriggerEnum trigger; // trigger rule e.g. on spawn or kill
public string unparsed_rule; // the original string parsed into this rule
public bool comment; // this 'rule' is a comment to ignore at runtime
}
#endregion
#region PlayerList class
class PlayerData
{
public bool updated; // set to true during admin.listPlayers processing
public string name;
public string squad;
public string team;
public string ip;
public string ea_guid;
public string pb_guid;
public string clan;
public int ping;
public int score;
public string country_key;
public string country_name;
public PlayerData()
{
updated = false;
squad = "-1";
team = "-1";
//ip = "no IP";
//ea_guid = "No_EA_GUID";
//pb_guid = "No_PB_GUID";
clan = "No clan";
ping = -1;
score = 0;
country_key = ""; // country code
country_name = ""; // country
}
}
// this plugin maintains its own list of playernames on the server
class PlayerList
{
// player info is stored as a dictionary playername->CPlayerInfo
Dictionary<string, PlayerData> info;
// when a player first joins, we cache their name/team in here (don't have a CPlayerInfo yet)
Dictionary<string, string> new_player_teams;
public PlayerList()
{
// info is the main player list
info = new Dictionary<string, PlayerData>();
// new_player_teams is the cache of players from OnPlayerJoin before
// they get CPlayerInfo from OnListPlayers
new_player_teams = new Dictionary<string, string>();
}
public void reset()
{
info.Clear();
new_player_teams.Clear();
}
// remove player entries that don't have 'updated' true
public void scrub()
{
new_player_teams.Clear();
List<string> scrub_keys = new List<string>();
foreach (string player_name in info.Keys)
if (info[player_name].updated == false) scrub_keys.Add(player_name);
foreach (string player_name in scrub_keys) info.Remove(player_name);
}
public void pre_scrub()
{
foreach (string player_name in info.Keys) info[player_name].updated = false;
}
public void remove(string player_name)
{
// remove from main list
info.Remove(player_name);
//and remove from new player cache if necessary
new_player_teams.Remove(player_name);
}
// called by OnPlayerJoin
// a new player has a name and maybe a team, that's all. No CPlayerInfo
public void new_player(string player_name)
{
if (info.ContainsKey(player_name)) return; // already in main player list
if (new_player_teams.ContainsKey(player_name)) return; // already in new player cache
// create entry for the player, but put them in team -1 (i.e. they're not in a team yet)
new_player_teams.Add(player_name, "-1");
}
// here's where we add a player to the main list
// and remove them from the new player cache if necessary
public void update(CPlayerInfo inf)
{
string player_name = inf.SoldierName;
// remove from new player cache
new_player_teams.Remove(player_name);
// add to main player list (update existing entry if necessary)
if (!info.ContainsKey(player_name) || info[player_name] == null)
info[player_name] = new PlayerData();
info[player_name].name = player_name;
info[player_name].squad = inf.SquadID.ToString();
info[player_name].team = inf.TeamID.ToString();
info[player_name].ea_guid = inf.GUID;
info[player_name].clan = inf.ClanTag;
info[player_name].score = inf.Score;
info[player_name].updated = true;
}
// update based on Punkbuster info
public void update(CPunkbusterInfo inf)
{
string player_name = inf.SoldierName;
if (!info.ContainsKey(player_name) || info[player_name] == null)
info[player_name] = new PlayerData();
info[player_name].name = player_name;
info[player_name].pb_guid = inf.GUID;
info[player_name].ip = inf.Ip;
info[player_name].country_key = inf.PlayerCountryCode;
info[player_name].country_name = inf.PlayerCountry;
}
public void team_move(string player_name, string team_id, string squad_id)
{
// attempt 1: update player entry in main list
if (info.ContainsKey(player_name))
{
info[player_name].team = team_id;
info[player_name].squad = squad_id;
}
// attempt 2: maybe they've just joined - update player entry in new player list
if (new_player_teams.ContainsKey(player_name))
new_player_teams[player_name] = team_id;
return;
}
// return the current team_id of player
public string team_id(string player_name)
{
if (player_name == null) return "-1";
// attempt #1: try main player list
if (info.ContainsKey(player_name)) return info[player_name].team;
// attempt #2: try cache of new players
if (new_player_teams.ContainsKey(player_name)) return new_player_teams[player_name];
else return "-1";
}
// return the current squad_id of player
public string squad_id(string player_name)
{
if (player_name == null) return "-1";
// attempt #1: try main player list
if (info.ContainsKey(player_name)) return info[player_name].squad;
return "-1";
}
// return the number of players in team
public int teamsize(string team_id)
{
return list_players(team_id).Count;
}
// return current ping for this player, as updated in the latest admin.listPlayers
public int ping(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].ping == null) return -1;
return info[player_name].ping;
}
// return current score for this player, as updated in the latest admin.listPlayers
public int score(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].score == null) return -1;
return info[player_name].score;
}
// return EA GUID for this player, as updated in the latest admin.listPlayers
public string ea_guid(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].ea_guid == null) return "";
return info[player_name].ea_guid;
}
// return Punkbuster GUID for this player, as updated in the latest OnPunkbusterInfo
public string pb_guid(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].pb_guid == null) return "";
return info[player_name].pb_guid;
}
// return IP address for this player, as updated in the latest OnPunkbusterInfo
public string ip(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].ip == null) return "";
return info[player_name].ip;
}
// return country name for this player, as updated in the latest OnPunkbusterInfo
public string cname(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].country_name == null) return "";
return info[player_name].country_name;
}
// return country KEY for this player, as updated in the latest OnPunkbusterInfo
public string ckey(string player_name)
{
if (player_name == null ||
player_name == "" ||
!info.ContainsKey(player_name) ||
info[player_name].country_key == null) return "";
return info[player_name].country_key;
}
public List<string> list_new_players()
{
return new List<string>(new_player_teams.Keys);
}
public List<string> list_players()
{
return new List<string>(info.Keys);
}
public List<string> list_players(string team_id)
{
List<string> player_list = new List<string>();
foreach (string player_name in info.Keys)
if (info[player_name].team == team_id) player_list.Add(player_name);
// also check new players
foreach (string player_name in new_player_teams.Keys)
if (new_player_teams[player_name] == team_id) player_list.Add(player_name);
return player_list;
}
public int min_teamsize()
{
int size1 = teamsize("1");
int size2 = teamsize("2");
return size1 < size2 ? size1 : size2;
}
// return a list os all the team_ids found in the player list
public List<string> list_team_ids()
{
List<string> team_ids = new List<string>();
foreach (string player_name in info.Keys)
try {
if (!team_ids.Contains(info[player_name].team))
team_ids.Add(info[player_name].team);
} catch {}
foreach (string player_name in new_player_teams.Keys)
try {
if (!team_ids.Contains(new_player_teams[player_name]))
team_ids.Add(new_player_teams[player_name]);
} catch { }
team_ids.Sort(); // sort ascending
return team_ids;
}
public string clan(string player_name)
{
if (player_name == null) return "No clan";
if (player_name == "") return "No clan";
try
{
if (info.ContainsKey(player_name))
{
string clan_name = info[player_name].clan;
if (clan_name == null) return "No clan";
if (clan_name.Trim() == "") return "No clan";
return clan_name;
}
}
catch { }
return "No clan";
}
}
#endregion
#region SpawnCounts class
// counts of spawned items in team_1 and team_2 - lists player names with each item
// each entry is <team_id> -><item name>::[<player_name>,...]
// e.g. Recon -> [sleepy, grumpy, doc]
class SpawnCounts
{
Dictionary<string, Dictionary<string, List<string>>> counts;
List<string> watched_items;
public SpawnCounts()
{
// team_id -> (item_name) -> (List of player names)
counts = new Dictionary<string, Dictionary<string, List<string>>>();
watched_items = new List<string>();
}
public List<string> list_items()
{
return watched_items;
}
// return a list of team_ids that have spawn counts recorded against them
//public List<int> list_team_ids()
//{
// return new List<int>(counts.Keys);
//}
// list the players spawned with an item in a given team
public List<string> list_players(string item_name, string team_id)
{
if (!counts.ContainsKey(team_id)) return new List<string>(); // team has no watch items
// team doesn't have this item watched:
if (!counts[team_id].ContainsKey(item_name)) return new List<string>();
return counts[team_id][item_name]; // return list of player names
}
// this is a bit subtle - the way we 'watch' items (kits, weapons, specs, damagetypes) is by
// creating a dictionary key entry for the string name of that item (e.g. "recon").
// The 'value' of that dictionary entry is the list of string playernames that have spawned
// with that item.
// This enables us to count the number of players with this item
// (i.e. the length of the player
// name list in that dictionary entry.).
public void watch(List<string> items_mixed)
{
foreach (string item_mixed in items_mixed)
if (!watched_items.Contains(item_mixed.ToLower())) watched_items.Add(item_mixed.ToLower());
}
// scrub player "name" from both spawn_counts (on spawn, so we can add a fresh entry)
public void zero_player(string player_name)
{
if (player_name == null) return;
if (player_name == "") return;
foreach (string team_id in counts.Keys)
{
foreach (string item in counts[team_id].Keys)
{
counts[team_id][item].Remove(player_name);
}
}
}
// keep watch list but zero all counts (called at round end)
public void zero()
{
counts.Clear();
}
// reset to startup status
public void reset()
{
counts.Clear();
watched_items.Clear();
}
// player "name" has just spawned with item 'item', so add to counts for that team
public void add(string item_mixed, string team_id, string player_name)
{
if (player_name == null) return;
if (player_name == "") return;
string item_lcase = item_mixed.ToLower();
if (!watched_items.Contains(item_lcase)) return; // ignore if item is not 'watched'
if (!counts.ContainsKey(team_id)) // if no entry for team then create one
counts[team_id] = new Dictionary<string, List<string>>();
if (!counts[team_id].ContainsKey(item_lcase)) // if no entry for item then create one
counts[team_id][item_lcase] = new List<string>();
// add team/item/player_name if it's not already there
if (!counts[team_id][item_lcase].Contains(player_name))
counts[team_id][item_lcase].Add(player_name);
}
// how many players on team currently have any 'item' on list
public int count(List<string> items_mixed, string team_id)
{
if (!counts.ContainsKey(team_id)) return 0; // if team has no watched items then 0
List<string> items_lcase = new List<string>();
if (items_mixed != null)
foreach (string i in items_mixed) items_lcase.Add(i.ToLower());
List<string> player_list = new List<string>(); // list of players with these items
foreach (string item_lcase in items_lcase)
{
if (!watched_items.Contains(item_lcase)) continue; // if not watched then 0
// if team does not have this item then 0
if (!counts[team_id].ContainsKey(item_lcase)) continue;
// return length of player name list for this team/item
player_list.AddRange(counts[team_id][item_lcase]);
}
return player_list.Count;
}
// return 'true' if player has any item on the list
public bool has_item(List<string> items_mixed, string player_name)
{
if (player_name == null) return false;
if (player_name == "") return false;
List<string> items_lcase = new List<string>();
if (items_mixed != null)
foreach (string i in items_mixed) items_lcase.Add(i.ToLower());
foreach (string item_lcase in items_lcase)
{
if (!watched_items.Contains(item_lcase)) continue;// if not watched then 0
foreach (string team_id in counts.Keys)
if (counts[team_id].ContainsKey(item_lcase) &&
counts[team_id][item_lcase].Contains(player_name)) return true;
}
return false;
}
}
#endregion
#region VarsClass class - this manages the rulz variables (Set, Incr, Decr, If)
// stores values of rulz variables
class VarsClass
{
// basic proconrulz vars
Dictionary<string, string> vars;
// persistent vars - stored in Configs/proconrulz.ini
Dictionary<string, Dictionary<string, string>> ini_vars;
string ini_filename;
public VarsClass(string file_hostname_port)
{
ini_filename = "Configs" + Path.DirectorySeparatorChar + file_hostname_port + "_proconrulz.ini";
// as of ver 37 all vars stored as strings
vars = new Dictionary<string, string>();
ini_vars = new Dictionary<string, Dictionary<string, string>>();
}
// keep list but zero all values (called at round end)
public void zero()
{
vars.Clear();
}
// reset to startup status
public void reset()
{
vars.Clear();
ini_vars.Clear();
ini_load(ini_filename);
}
// MANGLE is a fairly important function in ProconRulz vars processing...
// all vars are converted to 'server' variables but the usage in rulz allows
// them to appear as 'per-player' variables.
// i.e. %streak% in a rule is a UNIQUE variable for each player
// the rulz processing converts this to %server_streak[<playername>]% as the unique name
// so in effect, %streak% is shorthand for %server_streak[%p%]%
//
// We convert the var name into something valid globally
// i.e. %kills% -> %server_kills[<playername>]%
// %squad_kills% -> %server_squad_kills[1][2]% (where 1 = team, 2 = squad id's)
// %team_kills% -> %server_team_kills[1]% (where "1" is team id for player)
// %server_kills% -> unchanged
private string mangle(string player_name, string var_name)
{
var_name = var_name.Replace("$", "%");
// check for a valid var name %...%
if (var_name == null ||
var_name.Length < 3 ||
!var_name.StartsWith("%") ||
!var_name.EndsWith("%")) return null;
// replace [%vars%] in this var name with their value
var_name = replace_index_vars(player_name, var_name);
// if it's a 'server variable' return it unchanged
if (var_name.ToLower().StartsWith("%server_")) return var_name;
// ini var name - return unchanged
if (var_name.ToLower() == "%ini%") return var_name;
if (var_name.ToLower().StartsWith("%ini_")) return var_name;
// see if it's a team, squad or a player variable which means we much have a valid player name
if (player_name == null) return null;
// raw_name is a var name without the % %
string raw_name = var_name.Substring(1, var_name.Length - 2);
if (var_name.ToLower().StartsWith("%team_"))
// e.g. %team_kills% -> %server_team_kills[1]% (where "1" is team id for player)
return "%server_" + raw_name + "[" + players.team_id(player_name).ToString() + "]%";
if (var_name.ToLower().StartsWith("%squad_"))
// e.g. %squad_kills% -> %server_squad_kills[1][2]% (where 1 = team, 2 = squad id's)
return "%server_" + raw_name + "[" + players.team_id(player_name).ToString() + "][" +
players.squad_id(player_name).ToString() + "]%";
// proc has been called with a 'player variable',
// e.g. "%streak%", mangle to "server_streak[playername]%"
return "%server_" + raw_name + "[" + player_name + "]%";
}
// value of an expression : replace %v%-type subst values, then replace %streak%-type rulz vars:
private string get_value(string player_name, string input_exp, Dictionary<SubstEnum, string> keywords)
{
// replace substitution variables, e.g. %p% with playername from keywords
string substituted_exp = replace_keys(input_exp, keywords);
// now exp doesn't contain any substitution vars
// replace user vars with their values, e.g. %server_kills% or whatever user has used in rulz
string replaced_exp = replace_vars(player_name, substituted_exp);
// now exp has no vars, but may include arithmetic
string reduced_exp = reduce(replaced_exp); // "1+1" -> "2"
return reduced_exp;
}
// convert %ini_<section>_<var>% to Array[0..2]<string> [ini,<section_name>,<var_name>]
string[] var_to_ini(string full_var_name)
{
string[] ini_parts = new string[3];
if (full_var_name == "%ini%")
{
ini_parts[0] ="ini";
}
else if (full_var_name.StartsWith("%ini_"))
{
ini_parts[0] = "ini";
// e.g. %ini_vars_plugin_settings%
string[] var_parts = full_var_name.Split('_');
if (var_parts.Length >= 2)
{
if (var_parts.Length == 2) // no section name, e.g. %ini_myvar% so default to [vars]
{
ini_parts[1] = var_parts[1].TrimEnd(new char[] { '%' }); // var name
}
else
{
ini_parts[1] = var_parts[1]; // section name
ini_parts[2] = full_var_name.Substring(5 + var_parts[1].Length + 1).TrimEnd(new char[] { '%' });
}
}
}
return ini_parts;
}
// find the value of an 'atom' i.e. a string, int or variable
// exp can be a variable name or an integer or a string
private string atom_value(string player_name, string exp)
{
if (exp == null || exp == "") return "";
// try to get a number
float i;
try
{
i = float.Parse(exp);
return i.ToString();
}
catch { }
// ok, we didn't get an int, so try a variable lookup
// if not a %..% var just return the string
if (!exp.StartsWith("%") || !exp.EndsWith("%")) return exp;
string full_var_name = mangle(player_name, exp);
if (full_var_name == null) return "";
// return the variable value if there is one
if (vars.ContainsKey(full_var_name))
return vars[full_var_name];
else // we'll try the ini values
{
//convert %ini_<section>_<varname>% into list [section_name, var_name]
string[] ini_parts = var_to_ini(full_var_name);
if (ini_parts[0] != null)
{
string section_name = ((ini_parts[1] == null) ? "vars" : ini_parts[1]);
string var_name = ini_parts[2];
if ( ini_vars.ContainsKey(section_name) &&
var_name != null &&
ini_vars[section_name].ContainsKey(var_name))
{
return ini_vars[section_name][var_name];
}