Skip to content

Add option for persistent previous maps#550

Closed
stickz wants to merge 16 commits intoalliedmodders:masterfrom
stickz:patch-7
Closed

Add option for persistent previous maps#550
stickz wants to merge 16 commits intoalliedmodders:masterfrom
stickz:patch-7

Conversation

@stickz
Copy link
Contributor

@stickz stickz commented Oct 10, 2016

This commit adds an option to the mapchooser allowing for previous maps to be stored persistently. In the event of plug-in termination, it will remember what the previous maps played were. And recall them like nothing happened, if the user has the corresponding ConVar enabled.

It solves the annoyance of having to start from scratch with previous excluded maps, when something like a server reboot or crash happens; or even if the plug-in is updated/reloaded for whatever reason.

Copy link
Member

@KyleSanderson KyleSanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely needs some work. The feature is good though, I've been running something very similar for a couple years now.


char map[64];

while (!file.EndOfFile() && file.ReadLine(map, sizeof(map)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EndOfFile is undefined if you haven't read a line.

Copy link
Contributor Author

@stickz stickz Oct 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just put a safety feature in there, to ensure the loop terminates when the last file line is reached. There isn't any errors while running; but that can be removed, if it's not required.

char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, MAPCHOOSER_TXT);

DeleteFile(path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're opening in write mode, as you're doing below, you don't need to delete the existing file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I'll remove that.

for (int idx = 0; idx < g_OldMapList.Length; idx++)
{
g_OldMapList.GetString(idx, lastMap, sizeof(lastMap));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there so much spacing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The code is bunched closer together, so it's harder to read now.

if (!FileExists(path))
{
File file = OpenFile(path, "w");
file.Close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is going on here? FileExist should be used instead of creating empty files, that you later remove and re-add.

Copy link
Contributor Author

@stickz stickz Oct 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function creates the text file, if it's not existent OnPluginStart(), to avoid read/write errors.

g_winCount[i] = 0;
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an unneeded space got removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed the new line but kept the tab after the closing brace. Could just leave it as it was 😉

Copy link
Member

@peace-maker peace-maker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should try to think of better commit messages ;)

#define VOTE_EXTEND "##extend##"
#define VOTE_DONTCHANGE "##dontchange##"

#define MAPCHOOSER_TXT "data/mapchooser.txt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maphistory.txt would be a better name than just mapchooser.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose mapchooser.txt because it's less likely to conflict with other plugins.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapchooser_history.txt ?

g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 1.0);
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_PersistentMaps = CreateConVar("sm_mapvote_persisentmaps", "0", "Specifies whether or not previous maps should be persistently stored.", _, true, 0.0, true, 1.0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: two spaces after Specifies

g_winCount[i] = 0;
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed the new line but kept the tab after the closing brace. Could just leave it as it was 😉

char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, MAPCHOOSER_TXT);

File file = OpenFile(path, "r");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a null check here, if the file can't be opened and return. That lets you get rid of that CreatePreviousMapsTextFile function too!

char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, PLATFORM_MAX_PATH, MAPCHOOSER_TXT);

File file = OpenFile(path, "w");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add null check here too. User could always be lacking write permissions in the data folder or for that file specifically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I told the plugin to log an error as well, so the user knows something is wrong.


if (file == null)
{
file.Close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file is null you don't need to close it...

g_OldMapList.Clear();
char map[64];

while (!file.EndOfFile() && file.ReadLine(map, sizeof(map)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a do while. You cannot call EndOfFile before reading a line, this is present in almost every libc.

if (file == null)
{
LogError("[SM] Error writing to %s. Check file permissions.", MAPCHOOSER_TXT);
file.Close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on calling CloseHandle against an INVALID_HANDLE: why?

}

/* Recall previous maps from a text file, if persistentcy is enabled */
if (g_FirstConfigExec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be a static.

g_Cvar_ExtendRoundStep = CreateConVar("sm_extendmap_roundstep", "5", "Specifies how many more rounds each extension makes", _, true, 1.0);
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_PersistentMaps = CreateConVar("sm_mapvote_persisentmaps", "0", "Specifies whether or not previous maps should be stored persistently.", _, true, 0.0, true, 1.0);
Copy link

@schrodingerballs schrodingerballs Oct 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could learn how to spell things

stickz, 2016

}
}

/* Recall previous maps from a text file, if persistentcy is enabled */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could learn how to spell things

stickz, 2016

return;
}

/* Persisently store previous maps incase user wants to recall them */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could learn how to spell things

stickz, 2016

g_OldMapList.Erase(0);
}

WritePreviousMapsToText();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might only want to write/create the file if the convar is set to 1. Otherwise people might get error spam from a disabled feature.

Copy link
Contributor Author

@stickz stickz Oct 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll compensate by hiding the error if the feature is disabled. It's best to write to text regardless; otherwise, if something happens between ConVar change and map end, it won't save.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the feature is disabled, it shouldn't change the file. Maybe some admin doesn't want a map to appear in the history and disables that feature for that map?

stickz added a commit to stickz/sourcemod that referenced this pull request Sep 22, 2018
See PR alliedmodders#550 for more details. This pull request closes the other one.
@stickz stickz closed this Sep 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants