Added search/filter to nominate and nominate_addmap

Refactor rockthevote
This commit is contained in:
BotoX 2016-12-19 08:34:52 +01:00
parent bb5e3aa572
commit 5cd37b4e7d
2 changed files with 172 additions and 177 deletions

View File

@ -56,8 +56,8 @@ Handle g_Cvar_ExcludeCurrent = INVALID_HANDLE;
Handle g_MapList = INVALID_HANDLE; Handle g_MapList = INVALID_HANDLE;
Handle g_AdminMapList = INVALID_HANDLE; Handle g_AdminMapList = INVALID_HANDLE;
Handle g_MapMenu = INVALID_HANDLE; Menu g_MapMenu;
Handle g_AdminMapMenu = INVALID_HANDLE; Menu g_AdminMapMenu;
int g_mapFileSerial = -1; int g_mapFileSerial = -1;
int g_AdminMapFileSerial = -1; int g_AdminMapFileSerial = -1;
@ -129,6 +129,7 @@ public void OnConfigsExecuted()
SetFailState("Unable to create a valid map list."); SetFailState("Unable to create a valid map list.");
} }
} }
if(ReadMapList(g_AdminMapList, if(ReadMapList(g_AdminMapList,
g_AdminMapFileSerial, g_AdminMapFileSerial,
"sm_nominate_addmap menu", "sm_nominate_addmap menu",
@ -155,8 +156,54 @@ public void OnConfigsExecuted()
g_NominationDelay = GetTime() + GetConVarInt(g_Cvar_InitialDelay); g_NominationDelay = GetTime() + GetConVarInt(g_Cvar_InitialDelay);
BuildMapMenu(); if(g_MapMenu != INVALID_HANDLE)
BuildAdminMapMenu(); delete g_MapMenu;
g_MapMenu = BuildMapMenu("");
static char map[PLATFORM_MAX_PATH];
static char currentMap[PLATFORM_MAX_PATH];
ArrayList excludeMaps;
if(GetConVarBool(g_Cvar_ExcludeOld))
{
excludeMaps = CreateArray(ByteCountToCells(PLATFORM_MAX_PATH));
GetExcludeMapList(excludeMaps);
}
if(GetConVarBool(g_Cvar_ExcludeCurrent))
GetCurrentMap(currentMap, sizeof(currentMap));
ClearTrie(g_mapTrie);
for(int i = 0; i < GetArraySize(g_MapList); i++)
{
int status = MAPSTATUS_ENABLED;
GetArrayString(g_MapList, i, map, sizeof(map));
if(GetConVarBool(g_Cvar_ExcludeCurrent))
{
if(StrEqual(map, currentMap))
status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
}
/* Dont bother with this check if the current map check passed */
if(GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
{
if(FindStringInArray(excludeMaps, map) != -1)
status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
}
SetTrieValue(g_mapTrie, map, status);
}
if(excludeMaps)
delete excludeMaps;
if(g_AdminMapMenu != INVALID_HANDLE)
delete g_AdminMapMenu;
g_AdminMapMenu = BuildAdminMapMenu("");
} }
public void OnNominationRemoved(const char[] map, int owner) public void OnNominationRemoved(const char[] map, int owner)
@ -194,6 +241,7 @@ public Action Command_Addmap(int client, int args)
if(!IsMapValid(mapname)) if(!IsMapValid(mapname))
{ {
CReplyToCommand(client, "%t", "Map was not found", mapname); CReplyToCommand(client, "%t", "Map was not found", mapname);
AttemptAdminNominate(client, mapname);
return Plugin_Handled; return Plugin_Handled;
} }
@ -393,6 +441,7 @@ public Action Command_Nominate(int client, int args)
if(!GetTrieValue(g_mapTrie, mapname, status)) if(!GetTrieValue(g_mapTrie, mapname, status))
{ {
CPrintToChat(client, "%t", "Map was not found", mapname); CPrintToChat(client, "%t", "Map was not found", mapname);
AttemptNominate(client, mapname);
return Plugin_Handled; return Plugin_Handled;
} }
@ -484,7 +533,7 @@ public Action Command_NominateList(int client, int args)
return Plugin_Handled; return Plugin_Handled;
} }
public int Handler_NominateListMenu(Handle menu, MenuAction action, int param1, int param2) public int Handler_NominateListMenu(Menu menu, MenuAction action, int param1, int param2)
{ {
switch(action) switch(action)
{ {
@ -497,86 +546,52 @@ public int Handler_NominateListMenu(Handle menu, MenuAction action, int param1,
return 0; return 0;
} }
void AttemptNominate(int client) void AttemptNominate(int client, const char[] filter = "")
{ {
SetMenuTitle(g_MapMenu, "%T", "Nominate Title", client); Menu menu = g_MapMenu;
DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER); if(filter[0])
menu = BuildMapMenu(filter);
SetMenuTitle(menu, "%T", "Nominate Title", client);
DisplayMenu(menu, client, MENU_TIME_FOREVER);
return; return;
} }
void AttemptAdminNominate(int client) void AttemptAdminNominate(int client, const char[] filter = "")
{ {
SetMenuTitle(g_AdminMapMenu, "%T", "Nominate Title", client); Menu menu = g_AdminMapMenu;
DisplayMenu(g_AdminMapMenu, client, MENU_TIME_FOREVER); if(filter[0])
menu = BuildAdminMapMenu(filter);
SetMenuTitle(menu, "%T", "Nominate Title", client);
DisplayMenu(menu, client, MENU_TIME_FOREVER);
return; return;
} }
void BuildMapMenu() Menu BuildMapMenu(const char[] filter)
{ {
if(g_MapMenu != INVALID_HANDLE) Menu menu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
{
CloseHandle(g_MapMenu);
g_MapMenu = INVALID_HANDLE;
}
ClearTrie(g_mapTrie);
g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
static char map[PLATFORM_MAX_PATH]; static char map[PLATFORM_MAX_PATH];
ArrayList excludeMaps;
static char currentMap[32];
if(GetConVarBool(g_Cvar_ExcludeOld))
{
excludeMaps = CreateArray(ByteCountToCells(PLATFORM_MAX_PATH));
GetExcludeMapList(excludeMaps);
}
if(GetConVarBool(g_Cvar_ExcludeCurrent))
GetCurrentMap(currentMap, sizeof(currentMap));
for(int i = 0; i < GetArraySize(g_MapList); i++) for(int i = 0; i < GetArraySize(g_MapList); i++)
{ {
int status = MAPSTATUS_ENABLED;
GetArrayString(g_MapList, i, map, sizeof(map)); GetArrayString(g_MapList, i, map, sizeof(map));
if(GetConVarBool(g_Cvar_ExcludeCurrent)) if(!filter[0] || StrContains(map, filter, false) != -1)
AddMenuItem(menu, map, map);
}
SetMenuExitButton(menu, true);
return menu;
}
Menu BuildAdminMapMenu(const char[] filter)
{ {
if(StrEqual(map, currentMap)) Menu menu = CreateMenu(Handler_AdminMapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
}
/* Dont bother with this check if the current map check passed */
if(GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
{
if(FindStringInArray(excludeMaps, map) != -1)
status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
}
AddMenuItem(g_MapMenu, map, map);
SetTrieValue(g_mapTrie, map, status);
}
SetMenuExitButton(g_MapMenu, true);
if(excludeMaps)
delete excludeMaps;
}
void BuildAdminMapMenu()
{
if(g_AdminMapMenu != INVALID_HANDLE)
{
CloseHandle(g_AdminMapMenu);
g_AdminMapMenu = INVALID_HANDLE;
}
g_AdminMapMenu = CreateMenu(Handler_AdminMapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
static char map[PLATFORM_MAX_PATH]; static char map[PLATFORM_MAX_PATH];
@ -584,16 +599,24 @@ void BuildAdminMapMenu()
{ {
GetArrayString(g_AdminMapList, i, map, sizeof(map)); GetArrayString(g_AdminMapList, i, map, sizeof(map));
AddMenuItem(g_AdminMapMenu, map, map); if(!filter[0] || StrContains(map, filter, false) != -1)
AddMenuItem(menu, map, map);
} }
SetMenuExitButton(g_AdminMapMenu, true); SetMenuExitButton(menu, true);
return menu;
} }
public int Handler_MapSelectMenu(Handle menu, MenuAction action, int param1, int param2) public int Handler_MapSelectMenu(Menu menu, MenuAction action, int param1, int param2)
{ {
switch(action) switch(action)
{ {
case MenuAction_End:
{
if(menu != g_MapMenu)
delete menu;
}
case MenuAction_Select: case MenuAction_Select:
{ {
if(g_Player_NominationDelay[param1] > GetTime()) if(g_Player_NominationDelay[param1] > GetTime())
@ -770,10 +793,15 @@ stock bool IsNominateAllowed(int client)
return true; return true;
} }
public int Handler_AdminMapSelectMenu(Handle menu, MenuAction action, int param1, int param2) public int Handler_AdminMapSelectMenu(Menu menu, MenuAction action, int param1, int param2)
{ {
switch(action) switch(action)
{ {
case MenuAction_End:
{
if(menu != g_AdminMapMenu)
delete menu;
}
case MenuAction_Select: case MenuAction_Select:
{ {
static char map[PLATFORM_MAX_PATH]; static char map[PLATFORM_MAX_PATH];

View File

@ -41,10 +41,11 @@
#include <colors> #include <colors>
#pragma semicolon 1 #pragma semicolon 1
#pragma newdecls required
#define MCE_VERSION "1.11.0" #define MCE_VERSION "1.11.0"
public Plugin:myinfo = public Plugin myinfo =
{ {
name = "Rock The Vote Extended", name = "Rock The Vote Extended",
author = "Powerlord and AlliedModders LLC", author = "Powerlord and AlliedModders LLC",
@ -53,23 +54,24 @@ public Plugin:myinfo =
url = "https://forums.alliedmods.net/showthread.php?t=156974" url = "https://forums.alliedmods.net/showthread.php?t=156974"
}; };
new Handle:g_Cvar_Needed = INVALID_HANDLE; Handle g_Cvar_Needed = INVALID_HANDLE;
new Handle:g_Cvar_MinPlayers = INVALID_HANDLE; Handle g_Cvar_MinPlayers = INVALID_HANDLE;
new Handle:g_Cvar_InitialDelay = INVALID_HANDLE; Handle g_Cvar_InitialDelay = INVALID_HANDLE;
new Handle:g_Cvar_Interval = INVALID_HANDLE; Handle g_Cvar_Interval = INVALID_HANDLE;
new Handle:g_Cvar_ChangeTime = INVALID_HANDLE; Handle g_Cvar_ChangeTime = INVALID_HANDLE;
new Handle:g_Cvar_RTVPostVoteAction = INVALID_HANDLE; Handle g_Cvar_RTVPostVoteAction = INVALID_HANDLE;
new bool:g_CanRTV = false; // True if RTV loaded maps and is active. bool g_CanRTV = false; // True if RTV loaded maps and is active.
new bool:g_RTVAllowed = false; // True if RTV is available to players. Used to delay rtv votes. bool g_RTVAllowed = false; // True if RTV is available to players. Used to delay rtv votes.
new g_Voters = 0; // Total voters connected. Doesn't include fake clients. int g_Voters = 0; // Total voters connected. Doesn't include fake clients.
new g_Votes = 0; // Total number of "say rtv" votes int g_Votes = 0; // Total number of "say rtv" votes
new g_VotesNeeded = 0; // Necessary votes before map vote begins. (voters * percent_needed) int g_VotesNeeded = 0; // Necessary votes before map vote begins. (voters * percent_needed)
new bool:g_Voted[MAXPLAYERS+1] = {false, ...}; bool g_Voted[MAXPLAYERS+1] = {false, ...};
new bool:g_InChange = false; bool g_InChange = false;
bool g_bLate = false;
public OnPluginStart() public void OnPluginStart()
{ {
LoadTranslations("common.phrases"); LoadTranslations("common.phrases");
LoadTranslations("rockthevote.phrases"); LoadTranslations("rockthevote.phrases");
@ -96,39 +98,43 @@ public OnPluginStart()
CreateConVar("rtve_version", MCE_VERSION, "Rock The Vote Extended Version", FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_DONTRECORD); CreateConVar("rtve_version", MCE_VERSION, "Rock The Vote Extended Version", FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_DONTRECORD);
AutoExecConfig(true, "rtv"); AutoExecConfig(true, "rtv");
/* Handle late load */
g_bLate = true;
} }
public OnMapStart() public void OnMapStart()
{ {
g_Voters = 0; g_Voters = 0;
g_Votes = 0; g_Votes = 0;
g_VotesNeeded = 0; g_VotesNeeded = 0;
g_InChange = false; g_InChange = false;
/* Handle late load */ if(g_bLate)
for (new i=1; i<=MaxClients; i++)
{ {
if (IsClientConnected(i)) for(int client = 1; client <= MaxClients; client++)
{ {
OnClientConnected(i); if(IsClientConnected(client))
OnClientConnected(client);
} }
g_bLate = false;
} }
} }
public OnMapEnd() public void OnMapEnd()
{ {
g_CanRTV = false; g_CanRTV = false;
g_RTVAllowed = false; g_RTVAllowed = false;
} }
public OnConfigsExecuted() public void OnConfigsExecuted()
{ {
g_CanRTV = true; g_CanRTV = true;
g_RTVAllowed = false; g_RTVAllowed = false;
CreateTimer(GetConVarFloat(g_Cvar_InitialDelay), Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE); CreateTimer(GetConVarFloat(g_Cvar_InitialDelay), Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
} }
public OnClientConnected(client) public void OnClientConnected(int client)
{ {
if(IsFakeClient(client)) if(IsFakeClient(client))
return; return;
@ -141,24 +147,19 @@ public OnClientConnected(client)
return; return;
} }
public OnClientDisconnect(client) public void OnClientDisconnect(int client)
{ {
if(IsFakeClient(client)) if(IsFakeClient(client))
return; return;
if(g_Voted[client]) if(g_Voted[client])
{
g_Votes--; g_Votes--;
}
g_Voters = GetTeamClientCount(2) + GetTeamClientCount(3); g_Voters = GetTeamClientCount(2) + GetTeamClientCount(3);
g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed)); g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
if(!g_CanRTV) if(!g_CanRTV)
{
return; return;
}
if(g_Votes && if(g_Votes &&
g_Voters && g_Voters &&
@ -166,30 +167,19 @@ public OnClientDisconnect(client)
g_RTVAllowed ) g_RTVAllowed )
{ {
if(GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished()) if(GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished())
{
return; return;
}
StartRTV(); StartRTV();
} }
} }
public OnPlayerChangedTeam(Handle:event, const String:name[], bool:dontBroadcast) public void OnPlayerChangedTeam(Handle event, const char[] name, bool dontBroadcast)
{ {
new Client = GetClientOfUserId(GetEventInt(event, "userid")); int client = GetClientOfUserId(GetEventInt(event, "userid"));
if(client == 0 || !IsClientConnected(client) || !IsClientInGame(client) || IsFakeClient(client) ||
if(IsFakeClient(Client)) GetClientTeam(client) == 0 || GetClientTeam(client) == 1)
return; return;
if(Client == 0)
{
return;
}
if (IsClientInGame(Client) && IsClientConnected(Client))
{
if (GetClientTeam(Client) == 1)
{
g_Voters = GetTeamClientCount(2) + GetTeamClientCount(3); g_Voters = GetTeamClientCount(2) + GetTeamClientCount(3);
g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed)); g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
@ -199,61 +189,49 @@ public OnPlayerChangedTeam(Handle:event, const String:name[], bool:dontBroadcast
g_RTVAllowed ) g_RTVAllowed )
{ {
if(GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished()) if(GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished())
{
return; return;
}
StartRTV(); StartRTV();
} }
} }
}
}
public Action:Command_RTV(client, args) public Action Command_RTV(int client, int args)
{ {
if(!g_CanRTV || !client) if(!g_CanRTV || !client)
{
return Plugin_Handled; return Plugin_Handled;
}
AttemptRTV(client); AttemptRTV(client);
return Plugin_Handled; return Plugin_Handled;
} }
public Action:Command_Say(client, args) public Action Command_Say(int client, int args)
{ {
if(!g_CanRTV || !client) if(!g_CanRTV || !client)
{
return Plugin_Continue; return Plugin_Continue;
}
decl String:text[192]; static char text[192];
if(!GetCmdArgString(text, sizeof(text))) if(!GetCmdArgString(text, sizeof(text)))
{
return Plugin_Continue; return Plugin_Continue;
}
new startidx = 0; int startidx = 0;
if(text[strlen(text)-1] == '"') if(text[strlen(text)-1] == '"')
{ {
text[strlen(text)-1] = '\0'; text[strlen(text)-1] = '\0';
startidx = 1; startidx = 1;
} }
new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT); ReplySource old = SetCmdReplySource(SM_REPLY_TO_CHAT);
if(strcmp(text[startidx], "rtv", false) == 0 || strcmp(text[startidx], "rockthevote", false) == 0) if(strcmp(text[startidx], "rtv", false) == 0 || strcmp(text[startidx], "rockthevote", false) == 0)
{
AttemptRTV(client); AttemptRTV(client);
}
SetCmdReplySource(old); SetCmdReplySource(old);
return Plugin_Continue; return Plugin_Continue;
} }
AttemptRTV(client) void AttemptRTV(int client)
{ {
if(!g_RTVAllowed || (GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished())) if(!g_RTVAllowed || (GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished()))
{ {
@ -279,7 +257,7 @@ AttemptRTV(client)
return; return;
} }
new String:name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
GetClientName(client, name, sizeof(name)); GetClientName(client, name, sizeof(name));
g_Votes++; g_Votes++;
@ -288,27 +266,23 @@ AttemptRTV(client)
CPrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded); CPrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded);
if(g_Votes >= g_VotesNeeded) if(g_Votes >= g_VotesNeeded)
{
StartRTV(); StartRTV();
} }
}
public Action:Timer_DelayRTV(Handle:timer) public Action Timer_DelayRTV(Handle timer)
{ {
g_RTVAllowed = true; g_RTVAllowed = true;
} }
StartRTV() void StartRTV()
{ {
if(g_InChange) if(g_InChange)
{
return; return;
}
if(EndOfMapVoteEnabled() && HasEndOfMapVoteFinished()) if(EndOfMapVoteEnabled() && HasEndOfMapVoteFinished())
{ {
/* Change right now then */ /* Change right now then */
new String:map[PLATFORM_MAX_PATH]; char map[PLATFORM_MAX_PATH];
if(GetNextMap(map, sizeof(map))) if(GetNextMap(map, sizeof(map)))
{ {
CPrintToChatAll("[SM] %t", "Changing Maps", map); CPrintToChatAll("[SM] %t", "Changing Maps", map);
@ -319,12 +293,13 @@ StartRTV()
g_RTVAllowed = false; g_RTVAllowed = false;
} }
return; return;
} }
if(CanMapChooserStartVote()) if(CanMapChooserStartVote())
{ {
new MapChange:when = MapChange:GetConVarInt(g_Cvar_ChangeTime); MapChange when = view_as<MapChange>(GetConVarInt(g_Cvar_ChangeTime));
InitiateMapChooserVote(when); InitiateMapChooserVote(when);
ResetRTV(); ResetRTV();
@ -334,39 +309,33 @@ StartRTV()
} }
} }
ResetRTV() void ResetRTV()
{ {
g_Votes = 0; g_Votes = 0;
for (new i=1; i<=MAXPLAYERS; i++) for(int i = 1; i <= MAXPLAYERS; i++)
{
g_Voted[i] = false; g_Voted[i] = false;
} }
}
public Action:Timer_ChangeMap(Handle:hTimer) public Action Timer_ChangeMap(Handle hTimer)
{ {
g_InChange = false; g_InChange = false;
LogMessage("RTV changing map manually"); LogMessage("RTV changing map manually");
new String:map[PLATFORM_MAX_PATH]; char map[PLATFORM_MAX_PATH];
if(GetNextMap(map, sizeof(map))) if(GetNextMap(map, sizeof(map)))
{
ForceChangeLevel(map, "RTV after mapvote"); ForceChangeLevel(map, "RTV after mapvote");
}
return Plugin_Stop; return Plugin_Stop;
} }
// Rock The Vote Extended functions // Rock The Vote Extended functions
public Action:Command_ForceRTV(client, args) public Action Command_ForceRTV(int client, int args)
{ {
if(!g_CanRTV || !client) if(!g_CanRTV || !client)
{
return Plugin_Handled; return Plugin_Handled;
}
ShowActivity2(client, "[RTVE] ", "%t", "Initiated Vote Map"); ShowActivity2(client, "[RTVE] ", "%t", "Initiated Vote Map");
@ -374,5 +343,3 @@ public Action:Command_ForceRTV(client, args)
return Plugin_Handled; return Plugin_Handled;
} }