plugins/nominations.sp
author Nicholas Hastings <skamonkey@gmail.com>
Tue May 14 11:27:06 2013 -0400 (2013-05-14)
changeset 3918 312fa627bf21
parent 3187 4ee1c5e7bbc9
child 4060 b4484bd36c22
permissions -rw-r--r--
Fixed SDKTools dota build.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Rock The Vote Plugin
     5  * Creates a map vote when the required number of players have requested one.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #include <sourcemod>
    35 #include <mapchooser>
    36 
    37 #pragma semicolon 1
    38 
    39 public Plugin:myinfo =
    40 {
    41 	name = "Map Nominations",
    42 	author = "AlliedModders LLC",
    43 	description = "Provides Map Nominations",
    44 	version = SOURCEMOD_VERSION,
    45 	url = "http://www.sourcemod.net/"
    46 };
    47 
    48 new Handle:g_Cvar_ExcludeOld = INVALID_HANDLE;
    49 new Handle:g_Cvar_ExcludeCurrent = INVALID_HANDLE;
    50 
    51 new Handle:g_MapList = INVALID_HANDLE;
    52 new Handle:g_MapMenu = INVALID_HANDLE;
    53 new g_mapFileSerial = -1;
    54 
    55 #define MAPSTATUS_ENABLED (1<<0)
    56 #define MAPSTATUS_DISABLED (1<<1)
    57 #define MAPSTATUS_EXCLUDE_CURRENT (1<<2)
    58 #define MAPSTATUS_EXCLUDE_PREVIOUS (1<<3)
    59 #define MAPSTATUS_EXCLUDE_NOMINATED (1<<4)
    60 
    61 new Handle:g_mapTrie;
    62 
    63 public OnPluginStart()
    64 {
    65 	LoadTranslations("common.phrases");
    66 	LoadTranslations("nominations.phrases");
    67 	
    68 	new arraySize = ByteCountToCells(33);	
    69 	g_MapList = CreateArray(arraySize);
    70 	
    71 	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);
    72 	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);
    73 	
    74 	RegConsoleCmd("say", Command_Say);
    75 	RegConsoleCmd("say_team", Command_Say);
    76 	
    77 	RegConsoleCmd("sm_nominate", Command_Nominate);
    78 	
    79 	RegAdminCmd("sm_nominate_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate_addmap <mapname> - Forces a map to be on the next mapvote.");
    80 	
    81 	g_mapTrie = CreateTrie();
    82 }
    83 
    84 public OnConfigsExecuted()
    85 {
    86 	if (ReadMapList(g_MapList,
    87 					g_mapFileSerial,
    88 					"nominations",
    89 					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
    90 		== INVALID_HANDLE)
    91 	{
    92 		if (g_mapFileSerial == -1)
    93 		{
    94 			SetFailState("Unable to create a valid map list.");
    95 		}
    96 	}
    97 	
    98 	BuildMapMenu();
    99 }
   100 
   101 public OnNominationRemoved(const String:map[], owner)
   102 {
   103 	new status;
   104 	
   105 	/* Is the map in our list? */
   106 	if (!GetTrieValue(g_mapTrie, map, status))
   107 	{
   108 		return;	
   109 	}
   110 	
   111 	/* Was the map disabled due to being nominated */
   112 	if ((status & MAPSTATUS_EXCLUDE_NOMINATED) != MAPSTATUS_EXCLUDE_NOMINATED)
   113 	{
   114 		return;
   115 	}
   116 	
   117 	SetTrieValue(g_mapTrie, map, MAPSTATUS_ENABLED);	
   118 }
   119 
   120 public Action:Command_Addmap(client, args)
   121 {
   122 	if (args < 1)
   123 	{
   124 		ReplyToCommand(client, "[SM] Usage: sm_nominate_addmap <mapname>");
   125 		return Plugin_Handled;
   126 	}
   127 	
   128 	decl String:mapname[64];
   129 	GetCmdArg(1, mapname, sizeof(mapname));
   130 
   131 	
   132 	new status;
   133 	if (!GetTrieValue(g_mapTrie, mapname, status))
   134 	{
   135 		ReplyToCommand(client, "%t", "Map was not found", mapname);
   136 		return Plugin_Handled;		
   137 	}
   138 	
   139 	new NominateResult:result = NominateMap(mapname, true, 0);
   140 	
   141 	if (result > Nominate_Replaced)
   142 	{
   143 		/* 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 */
   144 		ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   145 		
   146 		return Plugin_Handled;	
   147 	}
   148 	
   149 	
   150 	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   151 
   152 	
   153 	ReplyToCommand(client, "%t", "Map Inserted", mapname);
   154 	LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
   155 
   156 	return Plugin_Handled;		
   157 }
   158 
   159 public Action:Command_Say(client, args)
   160 {
   161 	if (!client)
   162 	{
   163 		return Plugin_Continue;
   164 	}
   165 
   166 	decl String:text[192];
   167 	if (!GetCmdArgString(text, sizeof(text)))
   168 	{
   169 		return Plugin_Continue;
   170 	}
   171 	
   172 	new startidx = 0;
   173 	if(text[strlen(text)-1] == '"')
   174 	{
   175 		text[strlen(text)-1] = '\0';
   176 		startidx = 1;
   177 	}
   178 	
   179 	new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
   180 	
   181 	if (strcmp(text[startidx], "nominate", false) == 0)
   182 	{
   183 		AttemptNominate(client);
   184 	}
   185 	
   186 	SetCmdReplySource(old);
   187 	
   188 	return Plugin_Continue;	
   189 }
   190 
   191 public Action:Command_Nominate(client, args)
   192 {
   193 	if (!client)
   194 	{
   195 		return Plugin_Handled;
   196 	}
   197 	
   198 	if (args == 0)
   199 	{
   200 		AttemptNominate(client);
   201 		return Plugin_Handled;
   202 	}
   203 	
   204 	decl String:mapname[64];
   205 	GetCmdArg(1, mapname, sizeof(mapname));
   206 	
   207 	new status;
   208 	if (!GetTrieValue(g_mapTrie, mapname, status))
   209 	{
   210 		ReplyToCommand(client, "%t", "Map was not found", mapname);
   211 		return Plugin_Handled;		
   212 	}
   213 	
   214 	if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   215 	{
   216 		if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
   217 		{
   218 			ReplyToCommand(client, "[SM] %t", "Can't Nominate Current Map");
   219 		}
   220 		
   221 		if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
   222 		{
   223 			ReplyToCommand(client, "[SM] %t", "Map in Exclude List");
   224 		}
   225 		
   226 		if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
   227 		{
   228 			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   229 		}
   230 		
   231 		return Plugin_Handled;
   232 	}
   233 	
   234 	new NominateResult:result = NominateMap(mapname, false, client);
   235 	
   236 	if (result > Nominate_Replaced)
   237 	{
   238 		if (result == Nominate_AlreadyInVote)
   239 		{
   240 			ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   241 		}
   242 		else
   243 		{
   244 			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   245 		}
   246 		
   247 		return Plugin_Handled;	
   248 	}
   249 	
   250 	/* Map was nominated! - Disable the menu item and update the trie */
   251 	
   252 	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   253 	
   254 	decl String:name[64];
   255 	GetClientName(client, name, sizeof(name));
   256 	PrintToChatAll("[SM] %t", "Map Nominated", name, mapname);
   257 	
   258 	return Plugin_Continue;
   259 }
   260 
   261 AttemptNominate(client)
   262 {
   263 	SetMenuTitle(g_MapMenu, "%t", "Nominate Title", client);
   264 	DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);
   265 	
   266 	return;
   267 }
   268 
   269 BuildMapMenu()
   270 {
   271 	if (g_MapMenu != INVALID_HANDLE)
   272 	{
   273 		CloseHandle(g_MapMenu);
   274 		g_MapMenu = INVALID_HANDLE;
   275 	}
   276 	
   277 	ClearTrie(g_mapTrie);
   278 	
   279 	g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
   280 
   281 	decl String:map[64];
   282 	
   283 	new Handle:excludeMaps = INVALID_HANDLE;
   284 	decl String:currentMap[32];
   285 	
   286 	if (GetConVarBool(g_Cvar_ExcludeOld))
   287 	{	
   288 		excludeMaps = CreateArray(ByteCountToCells(33));
   289 		GetExcludeMapList(excludeMaps);
   290 	}
   291 	
   292 	if (GetConVarBool(g_Cvar_ExcludeCurrent))
   293 	{
   294 		GetCurrentMap(currentMap, sizeof(currentMap));
   295 	}
   296 	
   297 		
   298 	for (new i = 0; i < GetArraySize(g_MapList); i++)
   299 	{
   300 		new status = MAPSTATUS_ENABLED;
   301 		
   302 		GetArrayString(g_MapList, i, map, sizeof(map));
   303 		
   304 		if (GetConVarBool(g_Cvar_ExcludeCurrent))
   305 		{
   306 			if (StrEqual(map, currentMap))
   307 			{
   308 				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
   309 			}
   310 		}
   311 		
   312 		/* Dont bother with this check if the current map check passed */
   313 		if (GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
   314 		{
   315 			if (FindStringInArray(excludeMaps, map) != -1)
   316 			{
   317 				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
   318 			}
   319 		}
   320 		
   321 		AddMenuItem(g_MapMenu, map, map);
   322 		SetTrieValue(g_mapTrie, map, status);
   323 	}
   324 	
   325 	SetMenuExitButton(g_MapMenu, true);
   326 
   327 	if (excludeMaps != INVALID_HANDLE)
   328 	{
   329 		CloseHandle(excludeMaps);
   330 	}
   331 }
   332 
   333 public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
   334 {
   335 	switch (action)
   336 	{
   337 		case MenuAction_Select:
   338 		{
   339 			decl String:map[64], String:name[64];
   340 			GetMenuItem(menu, param2, map, sizeof(map));		
   341 			
   342 			GetClientName(param1, name, 64);
   343 	
   344 			new NominateResult:result = NominateMap(map, false, param1);
   345 			
   346 			/* Don't need to check for InvalidMap because the menu did that already */
   347 			if (result == Nominate_AlreadyInVote)
   348 			{
   349 				PrintToChat(param1, "[SM] %t", "Map Already Nominated");
   350 				return 0;
   351 			}
   352 			else if (result == Nominate_VoteFull)
   353 			{
   354 				PrintToChat(param1, "[SM] %t", "Max Nominations");
   355 				return 0;
   356 			}
   357 			
   358 			SetTrieValue(g_mapTrie, map, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   359 
   360 			if (result == Nominate_Replaced)
   361 			{
   362 				PrintToChatAll("[SM] %t", "Map Nomination Changed", name, map);
   363 				return 0;	
   364 			}
   365 			
   366 			PrintToChatAll("[SM] %t", "Map Nominated", name, map);
   367 		}
   368 		
   369 		case MenuAction_DrawItem:
   370 		{
   371 			decl String:map[64];
   372 			GetMenuItem(menu, param2, map, sizeof(map));
   373 			
   374 			new status;
   375 			
   376 			if (!GetTrieValue(g_mapTrie, map, status))
   377 			{
   378 				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
   379 				return ITEMDRAW_DEFAULT;
   380 			}
   381 			
   382 			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   383 			{
   384 				return ITEMDRAW_DISABLED;	
   385 			}
   386 			
   387 			return ITEMDRAW_DEFAULT;
   388 						
   389 		}
   390 		
   391 		case MenuAction_DisplayItem:
   392 		{
   393 			decl String:map[64];
   394 			GetMenuItem(menu, param2, map, sizeof(map));
   395 			
   396 			new status;
   397 			
   398 			if (!GetTrieValue(g_mapTrie, map, status))
   399 			{
   400 				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
   401 				return 0;
   402 			}
   403 			
   404 			decl String:display[100];
   405 			
   406 			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   407 			{
   408 				if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
   409 				{
   410 					Format(display, sizeof(display), "%s (%T)", map, "Current Map", param1);
   411 					return RedrawMenuItem(display);
   412 				}
   413 				
   414 				if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
   415 				{
   416 					Format(display, sizeof(display), "%s (%T)", map, "Recently Played", param1);
   417 					return RedrawMenuItem(display);
   418 				}
   419 				
   420 				if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
   421 				{
   422 					Format(display, sizeof(display), "%s (%T)", map, "Nominated", param1);
   423 					return RedrawMenuItem(display);
   424 				}
   425 			}
   426 			
   427 			return 0;
   428 		}
   429 	}
   430 	
   431 	return 0;
   432 }