Add option for persistent previous maps#550
Add option for persistent previous maps#550stickz wants to merge 16 commits intoalliedmodders:masterfrom stickz:patch-7
Conversation
KyleSanderson
left a comment
There was a problem hiding this comment.
Definitely needs some work. The feature is good though, I've been running something very similar for a couple years now.
plugins/mapchooser.sp
Outdated
|
|
||
| char map[64]; | ||
|
|
||
| while (!file.EndOfFile() && file.ReadLine(map, sizeof(map))) |
There was a problem hiding this comment.
EndOfFile is undefined if you haven't read a line.
There was a problem hiding this comment.
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.
plugins/mapchooser.sp
Outdated
| char path[PLATFORM_MAX_PATH]; | ||
| BuildPath(Path_SM, path, PLATFORM_MAX_PATH, MAPCHOOSER_TXT); | ||
|
|
||
| DeleteFile(path); |
There was a problem hiding this comment.
If you're opening in write mode, as you're doing below, you don't need to delete the existing file.
There was a problem hiding this comment.
Alright I'll remove that.
plugins/mapchooser.sp
Outdated
| for (int idx = 0; idx < g_OldMapList.Length; idx++) | ||
| { | ||
| g_OldMapList.GetString(idx, lastMap, sizeof(lastMap)); | ||
|
|
There was a problem hiding this comment.
Why is there so much spacing?
There was a problem hiding this comment.
Fixed. The code is bunched closer together, so it's harder to read now.
plugins/mapchooser.sp
Outdated
| if (!FileExists(path)) | ||
| { | ||
| File file = OpenFile(path, "w"); | ||
| file.Close(); |
There was a problem hiding this comment.
What is going on here? FileExist should be used instead of creating empty files, that you later remove and re-add.
There was a problem hiding this comment.
This function creates the text file, if it's not existent OnPluginStart(), to avoid read/write errors.
plugins/mapchooser.sp
Outdated
| g_winCount[i] = 0; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
I think an unneeded space got removed.
There was a problem hiding this comment.
You removed the new line but kept the tab after the closing brace. Could just leave it as it was 😉
peace-maker
left a comment
There was a problem hiding this comment.
You should try to think of better commit messages ;)
| #define VOTE_EXTEND "##extend##" | ||
| #define VOTE_DONTCHANGE "##dontchange##" | ||
|
|
||
| #define MAPCHOOSER_TXT "data/mapchooser.txt" |
There was a problem hiding this comment.
I think maphistory.txt would be a better name than just mapchooser.txt
There was a problem hiding this comment.
I chose mapchooser.txt because it's less likely to conflict with other plugins.
plugins/mapchooser.sp
Outdated
| 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); |
There was a problem hiding this comment.
Nit: two spaces after Specifies
plugins/mapchooser.sp
Outdated
| g_winCount[i] = 0; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
You removed the new line but kept the tab after the closing brace. Could just leave it as it was 😉
plugins/mapchooser.sp
Outdated
| char path[PLATFORM_MAX_PATH]; | ||
| BuildPath(Path_SM, path, PLATFORM_MAX_PATH, MAPCHOOSER_TXT); | ||
|
|
||
| File file = OpenFile(path, "r"); |
There was a problem hiding this comment.
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!
plugins/mapchooser.sp
Outdated
| char path[PLATFORM_MAX_PATH]; | ||
| BuildPath(Path_SM, path, PLATFORM_MAX_PATH, MAPCHOOSER_TXT); | ||
|
|
||
| File file = OpenFile(path, "w"); |
There was a problem hiding this comment.
Add null check here too. User could always be lacking write permissions in the data folder or for that file specifically.
There was a problem hiding this comment.
Done. I told the plugin to log an error as well, so the user knows something is wrong.
plugins/mapchooser.sp
Outdated
|
|
||
| if (file == null) | ||
| { | ||
| file.Close(); |
There was a problem hiding this comment.
If the file is null you don't need to close it...
plugins/mapchooser.sp
Outdated
| g_OldMapList.Clear(); | ||
| char map[64]; | ||
|
|
||
| while (!file.EndOfFile() && file.ReadLine(map, sizeof(map))) |
There was a problem hiding this comment.
This needs to be a do while. You cannot call EndOfFile before reading a line, this is present in almost every libc.
plugins/mapchooser.sp
Outdated
| if (file == null) | ||
| { | ||
| LogError("[SM] Error writing to %s. Check file permissions.", MAPCHOOSER_TXT); | ||
| file.Close(); |
There was a problem hiding this comment.
Ditto on calling CloseHandle against an INVALID_HANDLE: why?
| } | ||
|
|
||
| /* Recall previous maps from a text file, if persistentcy is enabled */ | ||
| if (g_FirstConfigExec) |
plugins/mapchooser.sp
Outdated
| 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); |
There was a problem hiding this comment.
You could learn how to spell things
stickz, 2016
plugins/mapchooser.sp
Outdated
| } | ||
| } | ||
|
|
||
| /* Recall previous maps from a text file, if persistentcy is enabled */ |
There was a problem hiding this comment.
You could learn how to spell things
stickz, 2016
plugins/mapchooser.sp
Outdated
| return; | ||
| } | ||
|
|
||
| /* Persisently store previous maps incase user wants to recall them */ |
There was a problem hiding this comment.
You could learn how to spell things
stickz, 2016
| g_OldMapList.Erase(0); | ||
| } | ||
|
|
||
| WritePreviousMapsToText(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
See PR alliedmodders#550 for more details. This pull request closes the other one.
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.