mapchooser: Add option for persistent map storage (#1183)
* Add option for persistent previous map storage * Fix spacer * Recall previous maps before CreateNextVote() * Remove MAPCHOOSER_TXT define * nits and bits * Update mapchooser.sp Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
This commit is contained in:
parent
69ae224938
commit
100f1e56ca
@ -63,6 +63,7 @@ ConVar g_Cvar_ExtendRoundStep;
|
||||
ConVar g_Cvar_ExtendFragStep;
|
||||
ConVar g_Cvar_ExcludeMaps;
|
||||
ConVar g_Cvar_IncludeMaps;
|
||||
ConVar g_Cvar_PersistentMaps;
|
||||
ConVar g_Cvar_NoVoteMode;
|
||||
ConVar g_Cvar_Extend;
|
||||
ConVar g_Cvar_DontChange;
|
||||
@ -127,6 +128,7 @@ public void OnPluginStart()
|
||||
g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
|
||||
g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
|
||||
g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
|
||||
g_Cvar_PersistentMaps = CreateConVar("sm_mapvote_persistentmaps", "0", "Specifies if previous maps should be stored persistently.", _, true, 0.0, true, 1.0);
|
||||
g_Cvar_NoVoteMode = CreateConVar("sm_mapvote_novote", "1", "Specifies whether or not MapChooser should pick a map if no votes are received.", _, true, 0.0, true, 1.0);
|
||||
g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "0", "Number of extensions allowed each map.", _, true, 0.0);
|
||||
g_Cvar_DontChange = CreateConVar("sm_mapvote_dontchange", "1", "Specifies if a 'Don't Change' option should be added to early votes", _, true, 0.0);
|
||||
@ -217,6 +219,18 @@ public void OnConfigsExecuted()
|
||||
}
|
||||
}
|
||||
|
||||
/* First-load previous maps from a text file when persistency is enabled. */
|
||||
static bool g_FirstConfigExec = true;
|
||||
if (g_FirstConfigExec)
|
||||
{
|
||||
if (g_Cvar_PersistentMaps.BoolValue)
|
||||
{
|
||||
ReadPreviousMapsFromText();
|
||||
}
|
||||
|
||||
g_FirstConfigExec = false;
|
||||
}
|
||||
|
||||
CreateNextVote();
|
||||
SetupTimeleftTimer();
|
||||
|
||||
@ -264,6 +278,11 @@ public void OnMapEnd()
|
||||
{
|
||||
g_OldMapList.Erase(0);
|
||||
}
|
||||
|
||||
if (g_Cvar_PersistentMaps.BoolValue)
|
||||
{
|
||||
WritePreviousMapsToText();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnClientDisconnect(int client)
|
||||
@ -1223,3 +1242,52 @@ public int Native_GetNominatedMapList(Handle plugin, int numParams)
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* Add functions for persistent previous map storage */
|
||||
void ReadPreviousMapsFromText()
|
||||
{
|
||||
File file = OpenFile(GetTextFilePath(), "r");
|
||||
if (file == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
g_OldMapList.Clear();
|
||||
char map[PLATFORM_MAX_PATH];
|
||||
do
|
||||
{
|
||||
if (file.ReadLine(map, sizeof(map)))
|
||||
{
|
||||
TrimString(map);
|
||||
g_OldMapList.PushString(map);
|
||||
}
|
||||
}
|
||||
while (!file.EndOfFile());
|
||||
file.Close();
|
||||
}
|
||||
|
||||
void WritePreviousMapsToText()
|
||||
{
|
||||
File file = OpenFile(GetTextFilePath(), "w");
|
||||
if (file == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
char lastMap[PLATFORM_MAX_PATH];
|
||||
for (int idx=0; idx<g_OldMapList.Length; idx++)
|
||||
{
|
||||
g_OldMapList.GetString(idx, lastMap, sizeof(lastMap));
|
||||
TrimString(lastMap);
|
||||
file.WriteLine(lastMap);
|
||||
}
|
||||
file.Close();
|
||||
}
|
||||
|
||||
char GetTextFilePath()
|
||||
{
|
||||
static char path[PLATFORM_MAX_PATH];
|
||||
if (path[0] == '\0')
|
||||
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "data/mapchooser_history.txt");
|
||||
return path;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user