plugins/basevotes/votemap.sp
author Asher Baker <asherkin@gmail.com>
Sun Jul 29 02:51:50 2012 +0100 (2012-07-29)
changeset 3590 6908e80a69af
parent 2864 afdf8778b4fd
child 3887 3e8a8d9903ce
permissions -rw-r--r--
Added some useful IDA scripts (NPOTB).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basevotes Plugin
     5  * Provides map functionality
     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 new Handle:g_MapList = INVALID_HANDLE;
    35 new g_mapCount;
    36 
    37 new Handle:g_SelectedMaps;
    38 new bool:g_VoteMapInUse;
    39 
    40 DisplayVoteMapMenu(client, mapCount, String:maps[5][])
    41 {
    42 	LogAction(client, -1, "\"%L\" initiated a map vote.", client);
    43 	ShowActivity2(client, "[SM] ", "%t", "Initiated Vote Map");
    44 	
    45 	g_voteType = voteType:map;
    46 	
    47 	g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
    48 	
    49 	if (mapCount == 1)
    50 	{
    51 		strcopy(g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]), maps[0]);
    52 			
    53 		SetMenuTitle(g_hVoteMenu, "Change Map To");
    54 		AddMenuItem(g_hVoteMenu, maps[0], "Yes");
    55 		AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
    56 	}
    57 	else
    58 	{
    59 		g_voteInfo[VOTE_NAME][0] = '\0';
    60 		
    61 		SetMenuTitle(g_hVoteMenu, "Map Vote");
    62 		for (new i = 0; i < mapCount; i++)
    63 		{
    64 			AddMenuItem(g_hVoteMenu, maps[i], maps[i]);
    65 		}	
    66 	}
    67 	
    68 	SetMenuExitButton(g_hVoteMenu, false);
    69 	VoteMenuToAll(g_hVoteMenu, 20);		
    70 }
    71 
    72 ResetMenu()
    73 {
    74 	g_VoteMapInUse = false;
    75 	ClearArray(g_SelectedMaps);
    76 }
    77 
    78 ConfirmVote(client)
    79 {
    80 	new Handle:menu = CreateMenu(MenuHandler_Confirm);
    81 	
    82 	decl String:title[100];
    83 	Format(title, sizeof(title), "%T:", "Confirm Vote", client);
    84 	SetMenuTitle(menu, title);
    85 	SetMenuExitBackButton(menu, true);
    86 	
    87 	AddMenuItem(menu, "Confirm", "Start the Vote");
    88 	
    89 	DisplayMenu(menu, client, MENU_TIME_FOREVER);	
    90 }
    91 
    92 public MenuHandler_Confirm(Handle:menu, MenuAction:action, param1, param2)
    93 {
    94 	if (action == MenuAction_End)
    95 	{
    96 		CloseHandle(menu);
    97 		g_VoteMapInUse = false;
    98 	}
    99 	else if (action == MenuAction_Cancel)
   100 	{
   101 		ResetMenu();
   102 		
   103 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
   104 		{
   105 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
   106 		}
   107 	}
   108 	else if (action == MenuAction_Select)
   109 	{
   110 		decl String:maps[5][64];
   111 		new selectedmaps = GetArraySize(g_SelectedMaps);
   112 		
   113 		for (new i = 0; i < selectedmaps; i++)
   114 		{
   115 			GetArrayString(g_SelectedMaps, i, maps[i], sizeof(maps[]));
   116 		}
   117 		
   118 		DisplayVoteMapMenu(param1, selectedmaps, maps);
   119 		
   120 		ResetMenu();
   121 	}
   122 }
   123 
   124 public MenuHandler_Map(Handle:menu, MenuAction:action, param1, param2)
   125 {
   126 	if (action == MenuAction_Cancel)
   127 	{		
   128 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
   129 		{
   130 			ConfirmVote(param1);
   131 		}
   132 		else // no action was selected.
   133 		{
   134 			/* Re-enable the menu option */
   135 			ResetMenu();
   136 		}
   137 	}
   138 	else if (action == MenuAction_DrawItem)
   139 	{
   140 		decl String:info[32], String:name[32];
   141 		
   142 		GetMenuItem(menu, param2, info, sizeof(info), _, name, sizeof(name));
   143 		
   144 		if (FindStringInArray(g_SelectedMaps, info) != -1)
   145 		{
   146 			return ITEMDRAW_IGNORE;
   147 		}
   148 		else
   149 		{
   150 			return ITEMDRAW_DEFAULT;
   151 		}
   152 	}
   153 	else if (action == MenuAction_Select)
   154 	{
   155 		decl String:info[32], String:name[32];
   156 		
   157 		GetMenuItem(menu, param2, info, sizeof(info), _, name, sizeof(name));
   158 		
   159 		PushArrayString(g_SelectedMaps, info);
   160 		
   161 		/* Redisplay the list */
   162 		if (GetArraySize(g_SelectedMaps) < 5)
   163 		{
   164 			DisplayMenu(g_MapList, param1, MENU_TIME_FOREVER);
   165 		}
   166 		else
   167 		{
   168 			ConfirmVote(param1);
   169 		}
   170 	}
   171 	
   172 	return 0;
   173 }
   174 
   175 public AdminMenu_VoteMap(Handle:topmenu, 
   176 							  TopMenuAction:action,
   177 							  TopMenuObject:object_id,
   178 							  param,
   179 							  String:buffer[],
   180 							  maxlength)
   181 {
   182 	if (action == TopMenuAction_DisplayOption)
   183 	{
   184 		Format(buffer, maxlength, "%T", "Map vote", param);
   185 	}
   186 	else if (action == TopMenuAction_SelectOption)
   187 	{
   188 		if (!g_VoteMapInUse)
   189 		{
   190 			ResetMenu();
   191 			g_VoteMapInUse = true;
   192 			DisplayMenu(g_MapList, param, MENU_TIME_FOREVER);
   193 		}
   194 		else 
   195 		{
   196 			PrintToChat(param, "[SM] %T", "Map Vote In Use", param);
   197 		}
   198 	}
   199 	else if (action == TopMenuAction_DrawOption)
   200 	{	
   201 		/* disable this option if a vote is already running, theres no maps listed or someone else has already acessed this menu */
   202 		buffer[0] = (!IsNewVoteAllowed() || g_mapCount < 1 || g_VoteMapInUse) ? ITEMDRAW_IGNORE : ITEMDRAW_DEFAULT;
   203 	}
   204 }
   205 
   206 public Action:Command_Votemap(client, args)
   207 {
   208 	if (args < 1)
   209 	{
   210 		ReplyToCommand(client, "[SM] Usage: sm_votemap <mapname> [mapname2] ... [mapname5]");
   211 		return Plugin_Handled;	
   212 	}
   213 	
   214 	if (IsVoteInProgress())
   215 	{
   216 		ReplyToCommand(client, "[SM] %t", "Vote in Progress");
   217 		return Plugin_Handled;
   218 	}
   219 		
   220 	if (!TestVoteDelay(client))
   221 	{
   222 		return Plugin_Handled;
   223 	}
   224 	
   225 	decl String:text[256];
   226 	GetCmdArgString(text, sizeof(text));
   227 
   228 	decl String:maps[5][64];
   229 	new mapCount;	
   230 	new len, pos;
   231 	
   232 	while (pos != -1 && mapCount < 5)
   233 	{	
   234 		pos = BreakString(text[len], maps[mapCount], sizeof(maps[]));
   235 		
   236 		if (!IsMapValid(maps[mapCount]))
   237 		{
   238 			ReplyToCommand(client, "[SM] %t", "Map was not found", maps[mapCount]);
   239 			return Plugin_Handled;
   240 		}		
   241 
   242 		mapCount++;
   243 		
   244 		if (pos != -1)
   245 		{
   246 			len += pos;
   247 		}	
   248 	}
   249 
   250 	DisplayVoteMapMenu(client, mapCount, maps);
   251 	
   252 	return Plugin_Handled;	
   253 }
   254 
   255 new Handle:g_map_array = INVALID_HANDLE;
   256 new g_map_serial = -1;
   257 
   258 LoadMapList(Handle:menu)
   259 {
   260 	new Handle:map_array;
   261 	
   262 	if ((map_array = ReadMapList(g_map_array,
   263 			g_map_serial,
   264 			"sm_votemap menu",
   265 			MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT|MAPLIST_FLAG_MAPSFOLDER))
   266 		!= INVALID_HANDLE)
   267 	{
   268 		g_map_array = map_array;
   269 	}
   270 	
   271 	if (g_map_array == INVALID_HANDLE)
   272 	{
   273 		return 0;
   274 	}
   275 	
   276 	RemoveAllMenuItems(menu);
   277 	
   278 	decl String:map_name[64];
   279 	new map_count = GetArraySize(g_map_array);
   280 	
   281 	for (new i = 0; i < map_count; i++)
   282 	{
   283 		GetArrayString(g_map_array, i, map_name, sizeof(map_name));
   284 		AddMenuItem(menu, map_name, map_name);
   285 	}
   286 	
   287 	return map_count;
   288 }