plugins/nominations.sp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4298 a9ea10a66000
permissions -rw-r--r--
Update CS:GO team score gamedata.
     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("sm_nominate", Command_Nominate);
    75 	
    76 	RegAdminCmd("sm_nominate_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate_addmap <mapname> - Forces a map to be on the next mapvote.");
    77 	
    78 	g_mapTrie = CreateTrie();
    79 }
    80 
    81 public OnConfigsExecuted()
    82 {
    83 	if (ReadMapList(g_MapList,
    84 					g_mapFileSerial,
    85 					"nominations",
    86 					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
    87 		== INVALID_HANDLE)
    88 	{
    89 		if (g_mapFileSerial == -1)
    90 		{
    91 			SetFailState("Unable to create a valid map list.");
    92 		}
    93 	}
    94 	
    95 	BuildMapMenu();
    96 }
    97 
    98 public OnNominationRemoved(const String:map[], owner)
    99 {
   100 	new status;
   101 	
   102 	/* Is the map in our list? */
   103 	if (!GetTrieValue(g_mapTrie, map, status))
   104 	{
   105 		return;	
   106 	}
   107 	
   108 	/* Was the map disabled due to being nominated */
   109 	if ((status & MAPSTATUS_EXCLUDE_NOMINATED) != MAPSTATUS_EXCLUDE_NOMINATED)
   110 	{
   111 		return;
   112 	}
   113 	
   114 	SetTrieValue(g_mapTrie, map, MAPSTATUS_ENABLED);	
   115 }
   116 
   117 public Action:Command_Addmap(client, args)
   118 {
   119 	if (args < 1)
   120 	{
   121 		ReplyToCommand(client, "[SM] Usage: sm_nominate_addmap <mapname>");
   122 		return Plugin_Handled;
   123 	}
   124 	
   125 	decl String:mapname[64];
   126 	GetCmdArg(1, mapname, sizeof(mapname));
   127 
   128 	
   129 	new status;
   130 	if (!GetTrieValue(g_mapTrie, mapname, status))
   131 	{
   132 		ReplyToCommand(client, "%t", "Map was not found", mapname);
   133 		return Plugin_Handled;		
   134 	}
   135 	
   136 	new NominateResult:result = NominateMap(mapname, true, 0);
   137 	
   138 	if (result > Nominate_Replaced)
   139 	{
   140 		/* 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 */
   141 		ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   142 		
   143 		return Plugin_Handled;	
   144 	}
   145 	
   146 	
   147 	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   148 
   149 	
   150 	ReplyToCommand(client, "%t", "Map Inserted", mapname);
   151 	LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
   152 
   153 	return Plugin_Handled;		
   154 }
   155 
   156 public OnClientSayCommand_Post(client, const String:command[], const String:sArgs[])
   157 {
   158 	if (!client)
   159 	{
   160 		return;
   161 	}
   162 	
   163 	if (strcmp(sArgs, "nominate", false) == 0)
   164 	{
   165 		new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
   166 		
   167 		AttemptNominate(client);
   168 		
   169 		SetCmdReplySource(old);
   170 	}
   171 }
   172 
   173 public Action:Command_Nominate(client, args)
   174 {
   175 	if (!client)
   176 	{
   177 		return Plugin_Handled;
   178 	}
   179 	
   180 	if (args == 0)
   181 	{
   182 		AttemptNominate(client);
   183 		return Plugin_Handled;
   184 	}
   185 	
   186 	decl String:mapname[64];
   187 	GetCmdArg(1, mapname, sizeof(mapname));
   188 	
   189 	new status;
   190 	if (!GetTrieValue(g_mapTrie, mapname, status))
   191 	{
   192 		ReplyToCommand(client, "%t", "Map was not found", mapname);
   193 		return Plugin_Handled;		
   194 	}
   195 	
   196 	if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   197 	{
   198 		if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
   199 		{
   200 			ReplyToCommand(client, "[SM] %t", "Can't Nominate Current Map");
   201 		}
   202 		
   203 		if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
   204 		{
   205 			ReplyToCommand(client, "[SM] %t", "Map in Exclude List");
   206 		}
   207 		
   208 		if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
   209 		{
   210 			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   211 		}
   212 		
   213 		return Plugin_Handled;
   214 	}
   215 	
   216 	new NominateResult:result = NominateMap(mapname, false, client);
   217 	
   218 	if (result > Nominate_Replaced)
   219 	{
   220 		if (result == Nominate_AlreadyInVote)
   221 		{
   222 			ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   223 		}
   224 		else
   225 		{
   226 			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   227 		}
   228 		
   229 		return Plugin_Handled;	
   230 	}
   231 	
   232 	/* Map was nominated! - Disable the menu item and update the trie */
   233 	
   234 	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   235 	
   236 	decl String:name[64];
   237 	GetClientName(client, name, sizeof(name));
   238 	PrintToChatAll("[SM] %t", "Map Nominated", name, mapname);
   239 	
   240 	return Plugin_Continue;
   241 }
   242 
   243 AttemptNominate(client)
   244 {
   245 	SetMenuTitle(g_MapMenu, "%T", "Nominate Title", client);
   246 	DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);
   247 	
   248 	return;
   249 }
   250 
   251 BuildMapMenu()
   252 {
   253 	if (g_MapMenu != INVALID_HANDLE)
   254 	{
   255 		CloseHandle(g_MapMenu);
   256 		g_MapMenu = INVALID_HANDLE;
   257 	}
   258 	
   259 	ClearTrie(g_mapTrie);
   260 	
   261 	g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
   262 
   263 	decl String:map[64];
   264 	
   265 	new Handle:excludeMaps = INVALID_HANDLE;
   266 	decl String:currentMap[32];
   267 	
   268 	if (GetConVarBool(g_Cvar_ExcludeOld))
   269 	{	
   270 		excludeMaps = CreateArray(ByteCountToCells(33));
   271 		GetExcludeMapList(excludeMaps);
   272 	}
   273 	
   274 	if (GetConVarBool(g_Cvar_ExcludeCurrent))
   275 	{
   276 		GetCurrentMap(currentMap, sizeof(currentMap));
   277 	}
   278 	
   279 		
   280 	for (new i = 0; i < GetArraySize(g_MapList); i++)
   281 	{
   282 		new status = MAPSTATUS_ENABLED;
   283 		
   284 		GetArrayString(g_MapList, i, map, sizeof(map));
   285 		
   286 		if (GetConVarBool(g_Cvar_ExcludeCurrent))
   287 		{
   288 			if (StrEqual(map, currentMap))
   289 			{
   290 				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
   291 			}
   292 		}
   293 		
   294 		/* Dont bother with this check if the current map check passed */
   295 		if (GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
   296 		{
   297 			if (FindStringInArray(excludeMaps, map) != -1)
   298 			{
   299 				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
   300 			}
   301 		}
   302 		
   303 		AddMenuItem(g_MapMenu, map, map);
   304 		SetTrieValue(g_mapTrie, map, status);
   305 	}
   306 	
   307 	SetMenuExitButton(g_MapMenu, true);
   308 
   309 	if (excludeMaps != INVALID_HANDLE)
   310 	{
   311 		CloseHandle(excludeMaps);
   312 	}
   313 }
   314 
   315 public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
   316 {
   317 	switch (action)
   318 	{
   319 		case MenuAction_Select:
   320 		{
   321 			decl String:map[64], String:name[64];
   322 			GetMenuItem(menu, param2, map, sizeof(map));		
   323 			
   324 			GetClientName(param1, name, 64);
   325 	
   326 			new NominateResult:result = NominateMap(map, false, param1);
   327 			
   328 			/* Don't need to check for InvalidMap because the menu did that already */
   329 			if (result == Nominate_AlreadyInVote)
   330 			{
   331 				PrintToChat(param1, "[SM] %t", "Map Already Nominated");
   332 				return 0;
   333 			}
   334 			else if (result == Nominate_VoteFull)
   335 			{
   336 				PrintToChat(param1, "[SM] %t", "Max Nominations");
   337 				return 0;
   338 			}
   339 			
   340 			SetTrieValue(g_mapTrie, map, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   341 
   342 			if (result == Nominate_Replaced)
   343 			{
   344 				PrintToChatAll("[SM] %t", "Map Nomination Changed", name, map);
   345 				return 0;	
   346 			}
   347 			
   348 			PrintToChatAll("[SM] %t", "Map Nominated", name, map);
   349 		}
   350 		
   351 		case MenuAction_DrawItem:
   352 		{
   353 			decl String:map[64];
   354 			GetMenuItem(menu, param2, map, sizeof(map));
   355 			
   356 			new status;
   357 			
   358 			if (!GetTrieValue(g_mapTrie, map, status))
   359 			{
   360 				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
   361 				return ITEMDRAW_DEFAULT;
   362 			}
   363 			
   364 			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   365 			{
   366 				return ITEMDRAW_DISABLED;	
   367 			}
   368 			
   369 			return ITEMDRAW_DEFAULT;
   370 						
   371 		}
   372 		
   373 		case MenuAction_DisplayItem:
   374 		{
   375 			decl String:map[64];
   376 			GetMenuItem(menu, param2, map, sizeof(map));
   377 			
   378 			new status;
   379 			
   380 			if (!GetTrieValue(g_mapTrie, map, status))
   381 			{
   382 				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
   383 				return 0;
   384 			}
   385 			
   386 			decl String:display[100];
   387 			
   388 			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   389 			{
   390 				if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
   391 				{
   392 					Format(display, sizeof(display), "%s (%T)", map, "Current Map", param1);
   393 					return RedrawMenuItem(display);
   394 				}
   395 				
   396 				if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
   397 				{
   398 					Format(display, sizeof(display), "%s (%T)", map, "Recently Played", param1);
   399 					return RedrawMenuItem(display);
   400 				}
   401 				
   402 				if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
   403 				{
   404 					Format(display, sizeof(display), "%s (%T)", map, "Nominated", param1);
   405 					return RedrawMenuItem(display);
   406 				}
   407 			}
   408 			
   409 			return 0;
   410 		}
   411 	}
   412 	
   413 	return 0;
   414 }