#pragma semicolon 1
#include <sourcemod>
#include <mapchooser>
#include <colors>
public Plugin:myinfo =
{
name = "Map Nominations",
author = "AlliedModders LLC",
description = "Provides Map Nominations",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
new Handle:g_Cvar_ExcludeOld = INVALID_HANDLE;
new Handle:g_Cvar_ExcludeCurrent = INVALID_HANDLE;
new Handle:g_MapList = INVALID_HANDLE;
new Handle:g_MapMenu = INVALID_HANDLE;
new g_mapFileSerial = -1;
#define MAPSTATUS_ENABLED (1<<0)
#define MAPSTATUS_DISABLED (1<<1)
#define MAPSTATUS_EXCLUDE_CURRENT (1<<2)
#define MAPSTATUS_EXCLUDE_PREVIOUS (1<<3)
#define MAPSTATUS_EXCLUDE_NOMINATED (1<<4)
new Handle:g_mapTrie;
public OnPluginStart()
{
LoadTranslations("common.phrases");
LoadTranslations("nominations.phrases");
new arraySize = ByteCountToCells(33);
g_MapList = CreateArray(arraySize);
g_Cvar_ExcludeOld = CreateConVar("sm_nominate_excludeold", "1", "Specifies if the current map should be excluded from the Nominations list", 0, true, 0.00, true, 1.0);
g_Cvar_ExcludeCurrent = CreateConVar("sm_nominate_excludecurrent", "1", "Specifies if the MapChooser excluded maps should also be excluded from Nominations", 0, true, 0.00, true, 1.0);
RegConsoleCmd("say", Command_Say);
RegConsoleCmd("say_team", Command_Say);
RegConsoleCmd("sm_nominate", Command_Nominate);
RegAdminCmd("sm_nominate_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate_addmap <mapname> - Forces a map to be on the next mapvote.");
g_mapTrie = CreateTrie();
}
public OnConfigsExecuted()
{
if (ReadMapList(g_MapList,
g_mapFileSerial,
"nominations",
MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
== INVALID_HANDLE)
{
if (g_mapFileSerial == -1)
{
SetFailState("Unable to create a valid map list.");
}
}
BuildMapMenu();
}
public OnNominationRemoved(const String:map[], owner)
{
new status;
/* Is the map in our list? */
if (!GetTrieValue(g_mapTrie, map, status))
{
return;
}
/* Was the map disabled due to being nominated */
if ((status & MAPSTATUS_EXCLUDE_NOMINATED) != MAPSTATUS_EXCLUDE_NOMINATED)
{
return;
}
SetTrieValue(g_mapTrie, map, MAPSTATUS_ENABLED);
}
public Action:Command_Addmap(client, args)
{
if (args < 1)
{
ReplyToCommand(client, "Usage: sm_nominate_addmap <mapname>");
return Plugin_Handled;
}
decl String:mapname[64];
GetCmdArg(1, mapname, sizeof(mapname));
new status;
if (!GetTrieValue(g_mapTrie, mapname, status))
{
ReplyToCommand(client, "%t", "Map was not found", mapname);
return Plugin_Handled;
}
new NominateResult:result = NominateMap(mapname, true, 0);
if (result > Nominate_Replaced)
{
/* We assume already in vote is the casue because the maplist does a Map Validity check and we forced, so it can't be full */
ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
return Plugin_Handled;
}
SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
ReplyToCommand(client, "%t", "Map Inserted", mapname);
LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
return Plugin_Handled;
}
public Action:Command_Say(client, args)
{
if (!client)
{
return Plugin_Continue;
}
decl String:text[192];
if (!GetCmdArgString(text, sizeof(text)))
{
return Plugin_Continue;
}
new startidx = 0;
if(text[strlen(text)-1] == '"')
{
text[strlen(text)-1] = '\0';
startidx = 1;
}
new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
if (strcmp(text[startidx], "nominate", false) == 0)
{
AttemptNominate(client);
}
SetCmdReplySource(old);
return Plugin_Continue;
}
public Action:Command_Nominate(client, args)
{
if (!client)
{
return Plugin_Handled;
}
if (args == 0)
{
AttemptNominate(client);
return Plugin_Handled;
}
decl String:mapname[64];
GetCmdArg(1, mapname, sizeof(mapname));
new status;
if (!GetTrieValue(g_mapTrie, mapname, status))
{
ReplyToCommand(client, "%t", "Map was not found", mapname);
return Plugin_Handled;
}
if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
{
if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
{
ReplyToCommand(client, "%t", "Can't Nominate Current Map");
}
if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
{
ReplyToCommand(client, "%t", "Map in Exclude List");
}
if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
{
ReplyToCommand(client, "%t", "Map Already Nominated");
}
return Plugin_Handled;
}
new NominateResult:result = NominateMap(mapname, false, client);
if (result > Nominate_Replaced)
{
if (result == Nominate_AlreadyInVote)
{
ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
}
else
{
ReplyToCommand(client, "%t", "Map Already Nominated");
}
return Plugin_Handled;
}
/* Map was nominated! - Disable the menu item and update the trie */
SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
decl String:name[64];
GetClientName(client, name, sizeof(name));
CPrintToChatAll("%t", "Map Nominated", name, mapname);
return Plugin_Continue;
}
AttemptNominate(client)
{
SetMenuTitle(g_MapMenu, "%t", "Nominate Title", client);
DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);
return;
}
BuildMapMenu()
{
if (g_MapMenu != INVALID_HANDLE)
{
CloseHandle(g_MapMenu);
g_MapMenu = INVALID_HANDLE;
}
ClearTrie(g_mapTrie);
g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
decl String:map[64];
new Handle:excludeMaps = INVALID_HANDLE;
decl String:currentMap[32];
if (GetConVarBool(g_Cvar_ExcludeOld))
{
excludeMaps = CreateArray(ByteCountToCells(33));
GetExcludeMapList(excludeMaps);
}
if (GetConVarBool(g_Cvar_ExcludeCurrent))
{
GetCurrentMap(currentMap, sizeof(currentMap));
}
for (new i = 0; i < GetArraySize(g_MapList); i++)
{
new 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;
}
}
AddMenuItem(g_MapMenu, map, map);
SetTrieValue(g_mapTrie, map, status);
}
SetMenuExitButton(g_MapMenu, true);
if (excludeMaps != INVALID_HANDLE)
{
CloseHandle(excludeMaps);
}
}
public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
{
switch (action)
{
case MenuAction_Select:
{
decl String:map[64], String:name[64];
GetMenuItem(menu, param2, map, sizeof(map));
GetClientName(param1, name, 64);
new NominateResult:result = NominateMap(map, false, param1);
/* Don't need to check for InvalidMap because the menu did that already */
if (result == Nominate_AlreadyInVote)
{
CPrintToChat(param1, "%t", "Map Already Nominated");
return 0;
}
else if (result == Nominate_VoteFull)
{
CPrintToChat(param1, "%t", "Max Nominations");
return 0;
}
SetTrieValue(g_mapTrie, map, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
if (result == Nominate_Replaced)
{
CPrintToChatAll("%t", "Map Nomination Changed", name, map);
return 0;
}
CPrintToChatAll("%t", "Map Nominated", name, map);
}
case MenuAction_DrawItem:
{
decl String:map[64];
GetMenuItem(menu, param2, map, sizeof(map));
new status;
if (!GetTrieValue(g_mapTrie, map, status))
{
LogError("Menu selection of item not in trie. Major logic problem somewhere.");
return ITEMDRAW_DEFAULT;
}
if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
{
return ITEMDRAW_DISABLED;
}
return ITEMDRAW_DEFAULT;
}
case MenuAction_DisplayItem:
{
decl String:map[64];
GetMenuItem(menu, param2, map, sizeof(map));
new status;
if (!GetTrieValue(g_mapTrie, map, status))
{
LogError("Menu selection of item not in trie. Major logic problem somewhere.");
return 0;
}
decl String:display[100];
if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
{
if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
{
Format(display, sizeof(display), "%s [%T]", map, "Current Map", param1);
return RedrawMenuItem(display);
}
if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
{
Format(display, sizeof(display), "%s [%T]", map, "Recently Played", param1);
return RedrawMenuItem(display);
}
if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
{
Format(display, sizeof(display), "%s [%T]", map, "Nominated", param1);
return RedrawMenuItem(display);
}
}
return 0;
}
}
return 0;
}