-
Notifications
You must be signed in to change notification settings - Fork 13
/
mapchooser_unlimited.sp
3265 lines (2758 loc) · 97.1 KB
/
mapchooser_unlimited.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
#pragma newdecls required
#pragma semicolon 1
#include <sourcemod>
#include <mapchooser_unlimited>
#include <csgocolors_fix>
#define PLUGIN_VERSION "1.2.4"
//Rewritten from scratch, but influenced by mapchooser_extended
public Plugin myinfo =
{
name = "Mapchooser Unlimited",
author = "tilgep (Based on plugin by Powerlord and AlliedModders LLC)",
// Inspiration from a (private)version by GFL (Vauff and/or Snowy)
description = "Allows players to choose the next map (Now with votes for everyone)",
version = PLUGIN_VERSION,
url = "https://www.github.com/tilgep/Mapchooser-Unlimited"
};
#define VOTE_EXTEND "EXTEND"
#define VOTE_DONTCHANGE "DONTCHANGE"
#define VOTE_RANDOM "RANDOM"
#define VOTE_RETRY_INTERVAL 5.0
#define MAX_DESCRIPTION_LENGTH 256
enum WarningType
{
WarningType_Vote,
WarningType_Revote,
};
enum TimerLocation
{
TimerLocation_Hint = 0,
TimerLocation_Center = 1,
TimerLocation_Chat = 2,
};
enum NominationChooseMode
{
NominationMode_Top = 0,
NominationMode_Random = 1,
NominationMode_Weighted = 2,
};
Menu g_VoteMenu; //Vote menu
Handle g_VoteTimer; //Timer to begin the mapvote
Handle g_WarningTimer; //Timer to show warning countdown
Handle g_RetryTimer;
ArrayList g_MapList; //Maplist found in the map list file specified in maplists.cfg
int g_iSerial = -1; //Used to check the status of maplist loading
bool g_bConfig = false; //Is config loaded
KeyValues g_kvConfig; //Config
int g_iGroups; //Number of groups in the config (to help optimise)
StringMap g_Nominations; //Maps nominated with number of votes
char g_sNominations[MAXPLAYERS+1][PLATFORM_MAX_PATH]; //Nominations made by each client
char g_sRandomMap[PLATFORM_MAX_PATH]; //Current random map
ArrayList g_InsertedMaps; //Maps inserted by admin (these always get added to the vote)
StringMap g_RecentMaps; //Maps on cooldown
MapChange g_ChangeTime;
NominationMode g_NominationMode;
bool g_bWaitingForVote = false;
bool g_bMapChangeInProgress = false; //Is the map currently changing
bool g_bWarningInProgress = false; //Is the warning timer currently showing
bool g_bVoteEnded = false; //Has the end of map vote ended
bool g_bVoteInProgress = false; //Is the map vote in progress
bool g_bCooldownsStepped = false;
bool g_bRestored = false;
int g_iExtends = 0; //Total extends done this map
int g_iRunoffCount = 0; //Number of runoff votes done
ConVar g_cv_Enabled;
ConVar g_cv_Mode;
ConVar g_cv_ExtendedLogging;
ConVar g_cv_CooldownFile;
ConVar g_cv_CooldownMode;
ConVar g_cv_VoteStartTime;
ConVar g_cv_TimerLocation;
ConVar g_cv_HideWarningTimer;
ConVar g_cv_WarningTime;
ConVar g_cv_RunOffWarningTime;
ConVar g_cv_RunOffCount;
ConVar g_cv_RunOffPercent;
ConVar g_cv_VoteDuration;
ConVar g_cv_Extends;
ConVar g_cv_ExtendPosition;
ConVar g_cv_ExtendTime;
ConVar g_cv_NoVoteOption;
ConVar g_cv_NomChooseMode;
ConVar g_cv_RandomVote;
ConVar g_cv_ChooseRandom;
ConVar g_cv_Cooldown;
ConVar g_cv_RandomMap;
ConVar g_cv_Include;
ConVar g_cv_TotalVoteOptions;
ConVar g_cv_ShowNominators;
GlobalForward g_fwdNominationRemoved;
GlobalForward g_fwdMapVoteStarted;
GlobalForward g_fwdWarningTick;
GlobalForward g_fwdRunoffVoteStarted;
GlobalForward g_fwdVoteStarted;
GlobalForward g_fwdMapVoteEnded;
GlobalForward g_fwdMapAddedToVote;
GlobalForward g_fwdMapNominated;
GlobalForward g_fwdMapInserted;
GlobalForward g_fwdMapListReloaded;
public void OnPluginStart()
{
if(GetEngineVersion() != Engine_CSGO)
{
LogMessage("This plugin is only tested and supported on CSGO! Beware of bugs!");
PrintToServer("This plugin is only tested and supported on CSGO! Beware of bugs!");
}
LoadTranslations("common.phrases");
LoadTranslations("mapchooser_unlimited.phrases");
RegAdminCmd("sm_mapvote", Command_Mapvote, ADMFLAG_BAN, "Starts a new mapvote.");
RegAdminCmd("sm_setnextmap", Command_Setnextmap, ADMFLAG_BAN, "Change the current next map.");
RegAdminCmd("sm_reloadmapconfig", Command_ReloadConfig, ADMFLAG_BAN, "Reload mapchooser config.");
RegAdminCmd("sm_reloadmaplist", Command_ReloadMaplist, ADMFLAG_BAN, "Reloads the list of maps that can be in the mapvote.");
RegAdminCmd("sm_excludemap", Command_Exclude, ADMFLAG_BAN, "Put a map on cooldown");
RegAdminCmd("sm_clearcd", Command_ClearCd, ADMFLAG_BAN, "Take a map off cooldown.");
RegConsoleCmd("sm_extendsleft", Command_ExtendsLeft, "Shows how many more times the map can be extended.");
RegConsoleCmd("sm_showmapconfig", Command_ShowConfig, "Shows all config information about the current map.");
RegConsoleCmd("sm_mcversion", Command_Version, "Mapchooser version");
g_cv_Enabled = CreateConVar("mcu_enabled", "1", "Should mapchooser perform an end of map vote?", _, true, 0.0, true, 1.0);
g_cv_Mode = CreateConVar("mcu_nominate_mode", "1", "Nomination mode (1=allow multiple votes for maps, 0=only one vote per map and only allow 'mcu_include' nominations)", _, true, 0.0, true, 1.0);
g_cv_ExtendedLogging = CreateConVar("mcu_logging", "0", "Should the plugin create extended logs of actions, e.g. Restored cooldowns, maps added to vote menu, does not affect logging command usage. (1=yes, 0=no)", _, true, 0.0, true, 1.0);
g_cv_CooldownFile = CreateConVar("mcu_cooldownfile", "configs/mapchooser_unlimited/cooldowns.cfg", "File location to store maps currently on cooldown, relative to sourcemod directory.");
g_cv_CooldownMode = CreateConVar("mcu_cooldownmode", "0", "When should cooldowns be saved (0=mapstart, 1=mapend) Map end means a server crash wont put the current map on cooldown", _, true, 0.0, true, 1.0);
g_cv_VoteStartTime = CreateConVar("mcu_votestart", "4.0", "Timeleft to start the vote", _, true, 1.0);
g_cv_TimerLocation = CreateConVar("mcu_timer_location", "0", "Location to show the warning timer (0 = hint text, 1 = center text, 2 = chat)", _, true, 0.0, true, 2.0);
g_cv_HideWarningTimer = CreateConVar("mcu_warning_timer", "1", "Is the warning timer shown (1=enabled, 0=disabled)", _, true, 0.0, true, 1.0);
g_cv_WarningTime = CreateConVar("mcu_warningtime", "15", "Warning timer duration before vote is shown. (0 - disabled)", _, true, 0.0, true, 60.0);
g_cv_RunOffWarningTime = CreateConVar("mcu_revote_warningtime", "10", "Warning timer duration for runoff votes.", _, true, 0.0, true, 30.0);
g_cv_RunOffCount = CreateConVar("mcu_runoff_count", "1", "Maximum number of runoff votes that can happen for each map vote, 0=disabled", _, true, 0.0);
g_cv_RunOffPercent = CreateConVar("mcu_runoff_percent", "60", "If winning choice has less than this percent of votes, hold a runoff.", _, true, 0.0, true, 100.0);
g_cv_VoteDuration = CreateConVar("mcu_voteduration", "20", "Duration to keep the map vote available for.", _, true, 5.0);
g_cv_Extends = CreateConVar("mcu_extends", "3", "Default maximum number of extends that will be allowed in end-of-map votes.", _, true, 0.0);
g_cv_ExtendPosition = CreateConVar("mcu_extendpos", "1", "Position of 'Extend map' or 'Don't Change' option (0=random, 1=start)", _, true, 0.0, true, 1.0);
g_cv_ExtendTime = CreateConVar("mcu_extendtime", "20", "Number of minutes to extend the map for if extend is chosen.");
g_cv_NoVoteOption = CreateConVar("mcu_novote", "1", "Should a 'No vote' option be given. (1=yes, 0=no)", _,true, 0.0, true, 1.0);
g_cv_NomChooseMode = CreateConVar("mcu_vote_mode", "0", "Mode to add nominations to the vote (0=Add top X voted map, 1=Add X random maps, 2=Weight voted maps and choose X random maps)", _, true, 0.0, true, 2.0);
g_cv_RandomVote = CreateConVar("mcu_randomize", "1", "How should the vote menu be randomized (1=random for each client (requires sourcemod 1.11 or later), 0=same option order for all)", _, true, 0.0, true, 1.0);
g_cv_ChooseRandom = CreateConVar("mcu_novotes_mode", "1", "Should MapChooser choose a random option if no votes are received.", _, true, 0.0, true, 1.0);
g_cv_Cooldown = CreateConVar("mcu_cooldown", "50", "Default number of maps that must be played before a map can be nominated again.", _, true, 0.0);
g_cv_RandomMap = CreateConVar("mcu_randommap", "1", "Should the map vote have a 'Random map' option (0=disabled, 1=enabled)", _, true, 0.0, true, 1.0);
g_cv_Include = CreateConVar("mcu_include", "5", "Maximum number of nominated maps to include in the vote.", _, true, 0.0);
g_cv_TotalVoteOptions = CreateConVar("mcu_total_options", "9", "Number of options that should appear in the vote (including extend, random, noms, no vote)", _, true, 0.0);
g_cv_ShowNominators = CreateConVar("mcu_show_nominators", "1", "Whether or not to show who nominated the chosen map when the vote ends. (0=disabled, 1=enabled)", _, true, 0.0, true, 1.0);
AutoExecConfig(true, "mapchooser_unlimited");
g_fwdNominationRemoved = CreateGlobalForward("OnNominationRemoved", ET_Ignore, Param_Cell, Param_String, Param_Cell);
g_fwdMapVoteStarted = CreateGlobalForward("OnMapVoteWarningStart", ET_Ignore);
g_fwdWarningTick = CreateGlobalForward("OnMapVoteWarningTick", ET_Ignore, Param_Cell);
g_fwdRunoffVoteStarted = CreateGlobalForward("OnRunoffVoteWarningStart", ET_Ignore);
g_fwdVoteStarted = CreateGlobalForward("OnMapVoteStarted", ET_Ignore, Param_Cell);
g_fwdMapVoteEnded = CreateGlobalForward("OnMapVoteEnd", ET_Ignore, Param_String);
g_fwdMapAddedToVote = CreateGlobalForward("OnMapAddedToVote", ET_Ignore, Param_String, Param_Cell, Param_Cell);
g_fwdMapNominated = CreateGlobalForward("OnMapNominated", ET_Ignore, Param_Cell, Param_String, Param_Cell, Param_Cell);
g_fwdMapInserted = CreateGlobalForward("OnMapInserted", ET_Ignore, Param_Cell, Param_String);
g_fwdMapListReloaded = CreateGlobalForward("OnMapListReloaded", ET_Ignore);
int arraySize = ByteCountToCells(PLATFORM_MAX_PATH);
g_Nominations = CreateTrie();
g_RecentMaps = CreateTrie();
g_InsertedMaps = CreateArray(arraySize);
g_MapList = CreateArray(arraySize);
LoadConfig();
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
if(LibraryExists("mapchooser"))
{
strcopy(error, err_max, "Mapchooser already loaded! Aborting.");
return APLRes_Failure;
}
RegPluginLibrary("mapchooser");
CreateNative("EndOfMapVoteEnabled", Native_EndOfMapVoteEnabled);
CreateNative("GetNominationMode", Native_GetNominationMode);
CreateNative("CanMapChooserStartVote", Native_CanVoteStart);
CreateNative("IsMapVoteInProgress", Native_IsMapVoteInProgress);
CreateNative("HasEndOfMapVoteFinished", Native_HasVoteFinished);
CreateNative("GetExtendsUsed", Native_GetExtendsUsed);
CreateNative("InitiateMapChooserVote", Native_InitiateVote);
CreateNative("NominateMap", Native_NominateMap);
CreateNative("CanNominate", Native_CanNominate);
CreateNative("CanMapBeNominated", Native_CanMapBeNominated);
CreateNative("GetNominatedMapList", Native_GetNominatedMapList);
CreateNative("GetMapVotes", Native_GetMapVotes);
CreateNative("IsMapInserted", Native_IsMapInserted);
CreateNative("GetInsertedMapList", Native_GetInsertedMapList);
CreateNative("RemoveNominationByOwner", Native_RemoveNomByOwner);
CreateNative("RemoveNominationsByMap", Native_RemoveNomByMap);
CreateNative("RemoveInsertedMap", Native_RemoveInsertedMap);
CreateNative("GetClientNomination", Native_GetClientNomination);
CreateNative("GetMapNominators", Native_GetMapNominators);
CreateNative("ExcludeMap", Native_ExcludeMap);
CreateNative("GetMapCooldown", Native_GetMapCooldown);
CreateNative("GetMapPlayerRestriction", Native_GetMapPlayerRestriction);
CreateNative("GetMapMinPlayers", Native_GetMapMinPlayers);
CreateNative("GetMapMaxPlayers", Native_GetMapMaxPlayers);
CreateNative("GetMapTimeRestriction", Native_GetMapTimeRestriction);
CreateNative("GetMapMinTime", Native_GetMapMinTime);
CreateNative("GetMapMaxTime", Native_GetMapMaxTime);
CreateNative("IsMapAdminOnly", Native_IsMapAdminOnly);
CreateNative("IsMapNominateOnly", Native_IsMapNominateOnly);
CreateNative("GetMapDescription", Native_GetMapDescription);
CreateNative("GetMapGroupRestriction", Native_GetMapGroupRestriction);
CreateNative("GetMapMaxExtends", Native_GetMapMaxExtends);
return APLRes_Success;
}
public void OnConfigsExecuted()
{
if(!g_bRestored)
{
RestoreCooldowns();
g_bRestored = true;
}
LoadMaplist();
WipeAllNominations(Removed_Ignore);
WipeInsertedMaps();
SetupTimeleftTimer();
g_bMapChangeInProgress = false;
g_bVoteInProgress = false;
g_bVoteEnded = false;
g_iExtends = 0;
g_iRunoffCount = 0;
g_bCooldownsStepped = false;
LoadNominationMode();
LoadConfig();
if(g_cv_CooldownMode.IntValue == 0)
{
StepCooldowns();
g_bCooldownsStepped = true;
}
}
public void OnMapEnd()
{
if(g_cv_CooldownMode.IntValue == 1 && !g_bCooldownsStepped)
{
StepCooldowns();
}
if(g_VoteTimer != null)
{
KillTimer(g_VoteTimer);
g_VoteTimer = null;
}
if(g_WarningTimer != null)
{
KillTimer(g_WarningTimer);
g_WarningTimer = null;
}
}
public void OnClientPutInServer(int client)
{
CheckNomRestrictions(true, true);
}
public void OnClientDisconnect(int client)
{
InternalRemoveNomByOwner(client, Removed_ClientDisconnect);
CheckNomRestrictions(true, true);
}
public void LoadNominationMode()
{
g_NominationMode = view_as<NominationMode>(g_cv_Mode.IntValue);
}
/**
* Loads the list of maps which can be put into a mapvote
*
* @return Was the load successful
*/
bool LoadMaplist(bool command = false)
{
if(ReadMapList(g_MapList, g_iSerial, "mapchooser", MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER) != INVALID_HANDLE)
{
if(g_iSerial == -1)
{
LogError("Unable to create a valid map list.");
return false;
}
if(command)
{
Call_StartForward(g_fwdMapListReloaded);
Call_Finish();
}
return true;
}
return false;
}
/**
* Wipes the current nominations list
*
* @param reason NominateRemoved reason for removal
* @return Number of client nominations cleared
*/
public int WipeAllNominations(NominateRemoved reason)
{
int count = 0;
for(int i = 1; i <= MaxClients; i++)
{
//Call this for the OnNominationRemoved forward as well
InternalRemoveNomByOwner(i, reason);
count++;
}
g_Nominations.Clear();
return count;
}
public void WipeInsertedMaps()
{
g_InsertedMaps.Clear();
}
/**
* Loads cooldowns from the cooldown file into a stringmap
*/
public void RestoreCooldowns()
{
char sCooldownFile[PLATFORM_MAX_PATH];
g_cv_CooldownFile.GetString(sCooldownFile, sizeof(sCooldownFile));
BuildPath(Path_SM, sCooldownFile, sizeof(sCooldownFile), "%s", sCooldownFile);
if(!FileExists(sCooldownFile))
{
LogMessage("Couldn't find cooldown file at: \"%s\". Attempting to create it...", sCooldownFile);
File f = OpenFile(sCooldownFile, "a");
delete f;
if(FileExists(sCooldownFile))
{
LogMessage("Cooldown file successfully created at \"%s\"", sCooldownFile);
}
else
{
ReplaceString(sCooldownFile, PLATFORM_MAX_PATH, "\\", "/");
int last = FindCharInString(sCooldownFile, '/', true);
if(last != -1)
{
char sCooldownDir[PLATFORM_MAX_PATH];
strcopy(sCooldownDir, last+1, sCooldownFile);
if(!DirExists(sCooldownDir))
{
LogMessage("Creating cooldown directory: \"%s\"", sCooldownDir);
CreateDirectory(sCooldownDir, FPERM_U_READ|FPERM_U_WRITE|FPERM_U_EXEC|FPERM_G_EXEC|FPERM_O_EXEC);
}
File f2 = OpenFile(sCooldownFile, "a");
delete f2;
if(FileExists(sCooldownFile))
{
LogMessage("Cooldown file successfully created at \"%s\"", sCooldownFile);
}
else
{
LogError("Could not find or create cooldown file: \"%s\". If this keeps happening create an empty file in the directory.", sCooldownFile);
}
}
}
return;
}
KeyValues kv = new KeyValues("mapchooser");
if(!kv.ImportFromFile(sCooldownFile))
{
LogMessage("Unable to load cooldown keyvalues from file: \"%s\"", sCooldownFile);
delete kv;
return;
}
if(!kv.GotoFirstSubKey())
{
LogMessage("Unable to go to first sub-key in cooldown file: \"%s\"", sCooldownFile);
delete kv;
return;
}
int total = 0;
int cooldown;
char sMap[PLATFORM_MAX_PATH];
do
{
kv.GetSectionName(sMap, sizeof(sMap));
cooldown = kv.GetNum("cooldown", -1);
if(cooldown > 0)
{
g_RecentMaps.SetValue(sMap, cooldown);
if(g_cv_ExtendedLogging.IntValue==1)
{
LogMessage("Cooldown restored. Map:\"%s\" -> %d", sMap, cooldown);
}
total++;
}
}
while(kv.GotoNextKey());
delete kv;
LogMessage("Cooldowns restored for %d maps.", total);
}
/**
* Saves all cooldowns found in the stringmap to the cooldown file
*/
public void StoreCooldowns()
{
char sCooldownFile[PLATFORM_MAX_PATH];
g_cv_CooldownFile.GetString(sCooldownFile, sizeof(sCooldownFile));
BuildPath(Path_SM, sCooldownFile, sizeof(sCooldownFile), "%s", sCooldownFile);
if(!FileExists(sCooldownFile))
{
File file = OpenFile(sCooldownFile, "a");
if(file==null)
{
LogError("Could not find or create cooldown file: \"%s\". If this keeps happening create an empty file in the directory.", sCooldownFile);
return;
}
delete file;
}
KeyValues kv = new KeyValues("mapchooser");
int cooldown;
char map[PLATFORM_MAX_PATH];
int total = 0;
StringMapSnapshot snap = g_RecentMaps.Snapshot();
for(int i = 0; i < snap.Length; i++)
{
snap.GetKey(i, map, sizeof(map));
g_RecentMaps.GetValue(map, cooldown);
if(cooldown <= 0) continue;
kv.JumpToKey(map, true);
kv.SetNum("cooldown", cooldown);
kv.Rewind();
total++;
}
delete snap;
if(!kv.ExportToFile(sCooldownFile))
{
LogMessage("Unable to export cooldowns to file: \"%s\"", sCooldownFile);
}
delete kv;
LogMessage("Saved cooldowns for %d maps.", total);
}
/**
* Decreases all current cooldowns by 1 and sets the current map + groups on cooldown
*/
public void StepCooldowns()
{
char map[PLATFORM_MAX_PATH];
GetCurrentMap(map, sizeof(map));
InternalSetMapCooldown(map, Cooldown_ConfigGreater);
SetMapGroupsCooldown(map);
int cd;
StringMapSnapshot snap = g_RecentMaps.Snapshot();
for(int i = 0; i < snap.Length; i++)
{
GetTrieSnapshotKey(snap, i, map, sizeof(map));
GetTrieValue(g_RecentMaps, map, cd);
cd--;
SetCooldown(map, cd); //SetCooldown handles removing
}
delete snap;
StoreCooldowns();
}
public Action Command_Mapvote(int client, int args)
{
char tag[64];
Format(tag, sizeof(tag), "%t ", "Prefix");
CShowActivity2(client, tag, "%t", "Initiated Map Vote");
LogAction(client, -1, "%L initiated a map vote.", client);
SetupWarningTimer(WarningType_Vote, MapChange_MapEnd, INVALID_HANDLE, true);
return Plugin_Handled;
}
public Action Command_Setnextmap(int client, int args)
{
if(args < 1)
{
CReplyToCommand(client, "%t %t", "Prefix", "Setnextmap Usage");
return Plugin_Handled;
}
char map[PLATFORM_MAX_PATH];
GetCmdArg(1, map, sizeof(map));
if(StrEqual(map, "_random", false))
{
map = GetRandomMap();
}
SetNextMap(map);
char tag[64];
Format(tag, sizeof(tag), "%t ", "Prefix");
CShowActivity2(client, tag, "%t", "Changed Next Map", map);
LogAction(client, -1, "\"%L\" changed nextmap to \"%s\"", client, map);
g_bVoteEnded = true;
return Plugin_Handled;
}
public Action Command_ReloadConfig(int client, int args)
{
if(LoadConfig())
{
CPrintToChat(client, "%t %t", "Prefix", "Config reload successful");
LogAction(client, -1, "%L reloaded the mapchooser config successfully.", client);
}
else
{
CPrintToChat(client, "%t %t", "Prefix", "Config reload unsuccessful");
LogAction(client, -1, "%L reloaded the mapchooser config unsuccessfully.", client);
}
return Plugin_Handled;
}
public Action Command_ReloadMaplist(int client, int args)
{
if(LoadMaplist(true))
{
CPrintToChat(client, "%t %t", "Prefix", "Maplist reload successful");
LogAction(client, -1, "%L reloaded the maplist successfully.", client);
}
else
{
CPrintToChat(client, "%t %t", "Prefix", "Maplist reload unsuccessful");
LogAction(client, -1, "%L reloaded the maplist unsuccessfully.", client);
}
return Plugin_Handled;
}
public Action Command_Exclude(int client, int args)
{
if(args < 1)
{
CReplyToCommand(client, "%t %t", "Prefix", "Exclude Map Usage");
return Plugin_Handled;
}
char map[PLATFORM_MAX_PATH];
GetCmdArg(1, map, sizeof(map));
if(FindStringInArray(g_MapList, map) == -1)
{
CReplyToCommand(client, "%t %t", "Prefix", "Map was not found", map);
return Plugin_Handled;
}
if(args == 1)
{
int cd = GetMapBaseCooldown(map);
InternalSetMapCooldown(map, Cooldown_Config);
CReplyToCommand(client, "%t %t", "Prefix", "Exclude Map Set", map, cd);
LogAction(client, -1, "%L set cooldown for map %s to %d", client, map, cd);
return Plugin_Handled;
}
char buf[8];
GetCmdArg(2, buf, sizeof(buf));
int cd = StringToInt(buf);
if(cd <= 0)
{
CReplyToCommand(client, "%t %t", "Prefix", "Exclude Map Value Error");
return Plugin_Handled;
}
InternalSetMapCooldown(map, Cooldown_Value, cd);
CReplyToCommand(client, "%t %t", "Prefix", "Exclude Map Set", map, cd);
LogAction(client, -1, "%L set cooldown for map %s to %d", client, map, cd);
return Plugin_Handled;
}
public Action Command_ClearCd(int client, int args)
{
if(args < 1)
{
CReplyToCommand(client, "%t %t", "Prefix", "Clear cooldown usage");
return Plugin_Handled;
}
char map[PLATFORM_MAX_PATH];
GetCmdArg(1, map, sizeof(map));
if(FindStringInArray(g_MapList, map) == -1)
{
CReplyToCommand(client, "%t %t", "Prefix", "Map was not found", map);
return Plugin_Handled;
}
if(InternalGetMapCurrentCooldown(map) == 0)
{
CReplyToCommand(client, "%t %t", "Prefix", "Map not on cooldown", map);
return Plugin_Handled;
}
InternalSetMapCooldown(map, Cooldown_Value, 0);
LogAction(client, -1, "%L cleared the cooldown for map %s", client, map);
CPrintToChat(client, "%t %t", "Prefix", "Map cooldown cleared", map);
return Plugin_Handled;
}
public Action Command_ShowConfig(int client, int args)
{
char map[PLATFORM_MAX_PATH];
if(args==0) GetCurrentMap(map, sizeof(map));
else
{
GetCmdArg(1, map, sizeof(map));
if(FindStringInArray(g_MapList, map) == -1)
{
CReplyToCommand(client, "%t %t", "Prefix", "Map was not found", map);
return Plugin_Handled;
}
}
int extends = InternalGetMapMaxExtends(map);
int cooldown = GetMapBaseCooldown(map);
int minplayer = InternalGetMapMinPlayers(map);
int maxplayer = InternalGetMapMaxPlayers(map);
int mintime = InternalGetMapMinTime(map);
int maxtime = InternalGetMapMaxTime(map);
bool adminonly = InternalIsMapAdminOnly(map);
bool nomonly = InternalIsMapNominateOnly(map);
char desc[MAX_DESCRIPTION_LENGTH];
bool descr = InternalGetMapDescription(map, desc, sizeof(desc));
if(GetCmdReplySource() == SM_REPLY_TO_CHAT)
{
CPrintToChat(client, "%t %t", "Prefix", "See console for output");
}
PrintToConsole(client, "-----------------------------------------");
PrintToConsole(client, "Showing config info for: %s", map);
PrintToConsole(client, "-----------------------------------------");
PrintToConsole(client, "%-15s %5d", "Extends: ", extends);
PrintToConsole(client, "%-15s %5d", "Cooldown: ", cooldown);
PrintToConsole(client, "%-15s %5d", "MinPlayers: ", minplayer);
PrintToConsole(client, "%-15s %5d", "MaxPlayers: ", maxplayer);
PrintToConsole(client, "%-15s %5d", "MinTime: ", mintime);
PrintToConsole(client, "%-15s %5d", "MaxTime: ", maxtime);
PrintToConsole(client, "%-15s %5b", "AdminOnly: ", adminonly);
PrintToConsole(client, "%-15s %5b", "NominateOnly: ", nomonly);
PrintToConsole(client, "%-15s %5s %s", "Description: ", descr?"Yes:":"No", desc);
PrintToConsole(client, "-----------------------------------------");
ShowMapGroups(client, map);
return Plugin_Handled;
}
public Action Command_ExtendsLeft(int client, int args)
{
char map[PLATFORM_MAX_PATH];
GetCurrentMap(map, sizeof(map));
int max = InternalGetMapMaxExtends(map);
int left = max - g_iExtends;
CReplyToCommand(client, "%t %t", "Prefix", "Extends Left", g_iExtends, max, left);
return Plugin_Handled;
}
public Action Command_Version(int client, int args)
{
CPrintToChat(client, "%t Version %s", "Prefix", PLUGIN_VERSION);
return Plugin_Handled;
}
void SetupTimeleftTimer()
{
int time;
if(GetMapTimeLeft(time) && time > 0)
{
int startTime;
startTime = g_cv_VoteStartTime.IntValue * 60;
if(time - startTime < 0 && !g_bVoteEnded && !g_bVoteInProgress)
{
SetupWarningTimer(WarningType_Vote);
}
else
{
if(g_WarningTimer == null)
{
delete g_VoteTimer;
g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartWarningTimer);
}
}
}
}
public void OnMapTimeLeftChanged()
{
if(GetArraySize(g_MapList))
SetupTimeleftTimer();
}
public Action Timer_StartWarningTimer(Handle timer)
{
g_VoteTimer = null;
if(!g_bWarningInProgress || g_WarningTimer == null)
SetupWarningTimer(WarningType_Vote);
return Plugin_Stop;
}
/**
* Starts the warning timer for the map vote
* Essentially this should be called to start a new map vote
*
* @param type Type of warning to show
* @param when When the mapchange will occur
* @param mapList Optional list of maps to pass (dont pass nomlist)
* @param force Whether to force a vote now
*/
stock void SetupWarningTimer(WarningType type, MapChange when=MapChange_MapEnd, Handle mapList=INVALID_HANDLE, bool force=false)
{
if(!GetArraySize(g_MapList))
{
LogMessage("Failed to start map vote because maplist does not exist.");
return;
}
if(g_bMapChangeInProgress)
{
LogMessage("Failed to start map vote because a map change is in progress.");
return;
}
if(g_bVoteInProgress && mapList == INVALID_HANDLE)
{
LogMessage("Failed to start map vote because a vote is already in progress.");
return;
}
if(!force)
{
if(when == MapChange_MapEnd)
{
if(g_cv_Enabled.IntValue == 0)
{
LogMessage("Mapvote not starting because of convar.");
return;
}
}
if(g_bVoteEnded)
{
LogMessage("Failed to start map vote because a vote has already happened and ended.");
return;
}
}
bool interrupted = false;
if(g_bWarningInProgress && g_WarningTimer != null)
{
interrupted = true;
KillTimer(g_WarningTimer);
g_WarningTimer = null;
}
g_bWarningInProgress = true;
Handle forwardVote;
ConVar cvarTime;
static char translationKey[64];
switch(type)
{
case WarningType_Vote:
{
forwardVote = g_fwdMapVoteStarted;
cvarTime = g_cv_WarningTime;
strcopy(translationKey, sizeof(translationKey), "Vote Warning");
}
case WarningType_Revote:
{
forwardVote = g_fwdRunoffVoteStarted;
cvarTime = g_cv_RunOffWarningTime;
strcopy(translationKey, sizeof(translationKey), "Revote Warning");
}
}
if(!interrupted)
{
Call_StartForward(forwardVote);
Call_Finish();
}
Handle data;
g_WarningTimer = CreateDataTimer(1.0, Timer_StartMapVote, data, TIMER_REPEAT);
WritePackCell(data, force);
WritePackCell(data, cvarTime.IntValue);
WritePackString(data, translationKey);
WritePackCell(data, view_as<int>(when));
WritePackCell(data, view_as<int>(mapList));
ResetPack(data);
}
public Action Timer_StartMapVote(Handle timer, Handle data)
{
static int timePassed = 0;
ResetPack(data);
bool force = ReadPackCell(data);
bool stop = false;
if(!GetArraySize(g_MapList)) stop = true;
if(!stop && g_cv_Enabled.IntValue == 0) stop = true;
if(!stop && !force)
{
if(g_bVoteEnded) stop = true;
}
if(stop)
{
g_WarningTimer = null;
return Plugin_Stop;
}
int warningMaxTime = ReadPackCell(data);
int warningTimeRemaining = warningMaxTime - timePassed;
Call_StartForward(g_fwdWarningTick);
Call_PushCell(warningTimeRemaining);
Call_Finish();
char warningPhrase[32];
ReadPackString(data, warningPhrase, sizeof(warningPhrase));
if(timePassed >= 0 || !g_cv_HideWarningTimer.BoolValue)
{
TimerLocation timerLocation = view_as<TimerLocation>(g_cv_TimerLocation.IntValue);
switch(timerLocation)
{
case TimerLocation_Center:
{
PrintCenterTextAll("%t", warningPhrase, warningTimeRemaining);
}
case TimerLocation_Chat:
{
PrintToChatAll("%t", warningPhrase, warningTimeRemaining);
}
default:
{
PrintHintTextToAll("%t", warningPhrase, warningTimeRemaining);
}
}
}
timePassed++;
if(timePassed > warningMaxTime)
{
if(timer == g_RetryTimer)
{
g_bWaitingForVote = false;
g_RetryTimer = null;
}
else
g_WarningTimer = null;
timePassed = 0;
MapChange mapChange = view_as<MapChange>(ReadPackCell(data));
Handle hndl = view_as<Handle>(ReadPackCell(data));
StartVote(mapChange, hndl);
return Plugin_Stop;
}
return Plugin_Continue;
}
/**
* Builds and starts the nextmap vote menu
*
* @param when When should the map change once the vote is complete
* @param inputList List of options to use, if none given use random maps and nominations
*/
void StartVote(MapChange when, Handle inputList = INVALID_HANDLE)
{
g_bWaitingForVote = true;
g_bWarningInProgress = false;
CheckNomRestrictions(true, true);
if(IsVoteInProgress())
{
Handle info;
WritePackCell(info, view_as<int>(when));
WritePackCell(info, view_as<int>(inputList));
ResetPack(info);
CreateTimer(VOTE_RETRY_INTERVAL, Timer_VoteRetry, info, TIMER_FLAG_NO_MAPCHANGE);
CPrintToChatAll("%t %t", "Prefix", "Retrying vote", VOTE_RETRY_INTERVAL);
return;
}
g_bWaitingForVote = false;
g_ChangeTime = when;
/* Starting a runoff */
if(inputList != INVALID_HANDLE)
{
StartRunoffVote(when, inputList);
return;
}
if(g_bVoteInProgress)
{
LogMessage("Aborting vote start because a map vote is already in progress.");
return;
}
g_VoteMenu = CreateMenu(Handler_VoteMenu, MENU_ACTIONS_ALL);
SetVoteResultCallback(g_VoteMenu, VoteHandler_VoteMenu);
SetMenuTitle(g_VoteMenu, "Vote for the Next Map");
int itemsToAdd = g_cv_TotalVoteOptions.IntValue;
int nomsAdded = 0;
int inserted = 0;
/* Set 'No Vote' option */
if(g_cv_NoVoteOption.BoolValue)
{
g_VoteMenu.NoVoteButton = true;
itemsToAdd--;
}
else
{
g_VoteMenu.NoVoteButton = false;
}
ArrayList voteList = CreateArray(ByteCountToCells(PLATFORM_MAX_PATH));
bool extendAdded = false; //So we can shuffle correctly
char map[PLATFORM_MAX_PATH];
GetCurrentMap(map, sizeof(map));
/* Add Extend/Dont change option */
if(when == MapChange_MapEnd)
{
if(g_iExtends < InternalGetMapMaxExtends(map))
{