Skip to content

Commit 100f1e5

Browse files
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>
1 parent 69ae224 commit 100f1e5

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

plugins/mapchooser.sp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ConVar g_Cvar_ExtendRoundStep;
6363
ConVar g_Cvar_ExtendFragStep;
6464
ConVar g_Cvar_ExcludeMaps;
6565
ConVar g_Cvar_IncludeMaps;
66+
ConVar g_Cvar_PersistentMaps;
6667
ConVar g_Cvar_NoVoteMode;
6768
ConVar g_Cvar_Extend;
6869
ConVar g_Cvar_DontChange;
@@ -127,6 +128,7 @@ public void OnPluginStart()
127128
g_Cvar_ExtendFragStep = CreateConVar("sm_extendmap_fragstep", "10", "Specifies how many more frags are allowed when map is extended.", _, true, 5.0);
128129
g_Cvar_ExcludeMaps = CreateConVar("sm_mapvote_exclude", "5", "Specifies how many past maps to exclude from the vote.", _, true, 0.0);
129130
g_Cvar_IncludeMaps = CreateConVar("sm_mapvote_include", "5", "Specifies how many maps to include in the vote.", _, true, 2.0, true, 6.0);
131+
g_Cvar_PersistentMaps = CreateConVar("sm_mapvote_persistentmaps", "0", "Specifies if previous maps should be stored persistently.", _, true, 0.0, true, 1.0);
130132
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);
131133
g_Cvar_Extend = CreateConVar("sm_mapvote_extend", "0", "Number of extensions allowed each map.", _, true, 0.0);
132134
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()
217219
}
218220
}
219221

222+
/* First-load previous maps from a text file when persistency is enabled. */
223+
static bool g_FirstConfigExec = true;
224+
if (g_FirstConfigExec)
225+
{
226+
if (g_Cvar_PersistentMaps.BoolValue)
227+
{
228+
ReadPreviousMapsFromText();
229+
}
230+
231+
g_FirstConfigExec = false;
232+
}
233+
220234
CreateNextVote();
221235
SetupTimeleftTimer();
222236

@@ -264,6 +278,11 @@ public void OnMapEnd()
264278
{
265279
g_OldMapList.Erase(0);
266280
}
281+
282+
if (g_Cvar_PersistentMaps.BoolValue)
283+
{
284+
WritePreviousMapsToText();
285+
}
267286
}
268287

269288
public void OnClientDisconnect(int client)
@@ -1223,3 +1242,52 @@ public int Native_GetNominatedMapList(Handle plugin, int numParams)
12231242

12241243
return;
12251244
}
1245+
1246+
/* Add functions for persistent previous map storage */
1247+
void ReadPreviousMapsFromText()
1248+
{
1249+
File file = OpenFile(GetTextFilePath(), "r");
1250+
if (file == null)
1251+
{
1252+
return;
1253+
}
1254+
1255+
g_OldMapList.Clear();
1256+
char map[PLATFORM_MAX_PATH];
1257+
do
1258+
{
1259+
if (file.ReadLine(map, sizeof(map)))
1260+
{
1261+
TrimString(map);
1262+
g_OldMapList.PushString(map);
1263+
}
1264+
}
1265+
while (!file.EndOfFile());
1266+
file.Close();
1267+
}
1268+
1269+
void WritePreviousMapsToText()
1270+
{
1271+
File file = OpenFile(GetTextFilePath(), "w");
1272+
if (file == null)
1273+
{
1274+
return;
1275+
}
1276+
1277+
char lastMap[PLATFORM_MAX_PATH];
1278+
for (int idx=0; idx<g_OldMapList.Length; idx++)
1279+
{
1280+
g_OldMapList.GetString(idx, lastMap, sizeof(lastMap));
1281+
TrimString(lastMap);
1282+
file.WriteLine(lastMap);
1283+
}
1284+
file.Close();
1285+
}
1286+
1287+
char GetTextFilePath()
1288+
{
1289+
static char path[PLATFORM_MAX_PATH];
1290+
if (path[0] == '\0')
1291+
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "data/mapchooser_history.txt");
1292+
return path;
1293+
}

0 commit comments

Comments
 (0)