plugins/basevotes.sp
author Michael McKoy <ferret99gt@gmail.com>
Fri Nov 09 23:46:05 2007 +0000 (2007-11-09)
changeset 1664 e054be79e28a
parent 1661 b0a4850a8cc2
child 1684 c4e72bd36591
permissions -rw-r--r--
New adt_array functions: CloneArray, FindStringInArray, FindCellInArray
Adjusted mapchooser/randomcycle/rockthevote and LoadMaps stock to use new array functions
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basic Votes Plugin
     5  * Implements basic vote commands.
     6  *
     7  * SourceMod (C)2004-2007 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 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 #undef REQUIRE_PLUGIN
    38 #include <adminmenu>
    39 
    40 public Plugin:myinfo =
    41 {
    42 	name = "Basic Votes",
    43 	author = "AlliedModders LLC",
    44 	description = "Basic Vote Commands",
    45 	version = SOURCEMOD_VERSION,
    46 	url = "http://www.sourcemod.net/"
    47 };
    48 
    49 #define VOTE_NO "###no###"
    50 #define VOTE_YES "###yes###"
    51 
    52 new Handle:g_hVoteMenu = INVALID_HANDLE;
    53 
    54 new Handle:g_hBanForward = INVALID_HANDLE;
    55 
    56 new Handle:g_Cvar_Limits[3] = {INVALID_HANDLE, ...};
    57 //new Handle:g_Cvar_VoteSay = INVALID_HANDLE;
    58 
    59 enum voteType
    60 {
    61 	map,
    62 	kick,
    63 	ban,
    64 	question
    65 }
    66 
    67 new voteType:g_voteType = voteType:question;
    68 
    69 // Menu API does not provide us with a way to pass multiple peices of data with a single
    70 // choice, so some globals are used to hold stuff.
    71 //
    72 #define VOTE_CLIENTID	0
    73 #define VOTE_USERID	1
    74 new g_voteClient[2];		/* Holds the target's client id and user id */
    75 
    76 #define VOTE_NAME	0
    77 #define VOTE_AUTHID	1
    78 #define	VOTE_IP		2
    79 new String:g_voteInfo[3][65];	/* Holds the target's name, authid, and IP */
    80 
    81 new String:g_voteArg[256];	/* Used to hold ban/kick reasons or vote questions */
    82 
    83 
    84 new Handle:hTopMenu = INVALID_HANDLE;
    85 
    86 #include "basevotes/votekick.sp"
    87 #include "basevotes/voteban.sp"
    88 #include "basevotes/votemap.sp"
    89 
    90 public OnPluginStart()
    91 {
    92 	LoadTranslations("common.phrases");
    93 	LoadTranslations("basevotes.phrases");
    94 	LoadTranslations("plugin.basecommands");
    95 	LoadTranslations("basebans.phrases");
    96 	
    97 	RegAdminCmd("sm_votemap", Command_Votemap, ADMFLAG_VOTE|ADMFLAG_CHANGEMAP, "sm_votemap <mapname> [mapname2] ... [mapname5] ");
    98 	RegAdminCmd("sm_votekick", Command_Votekick, ADMFLAG_VOTE|ADMFLAG_KICK, "sm_votekick <player> [reason]");
    99 	RegAdminCmd("sm_voteban", Command_Voteban, ADMFLAG_VOTE|ADMFLAG_BAN, "sm_voteban <player> [reason]");
   100 	RegAdminCmd("sm_vote", Command_Vote, ADMFLAG_VOTE, "sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
   101 
   102 	/*
   103 	g_Cvar_Show = FindConVar("sm_vote_show");
   104 	if (g_Cvar_Show == INVALID_HANDLE)
   105 	{
   106 		g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
   107 	}
   108 	*/
   109 
   110 	g_Cvar_Limits[0] = CreateConVar("sm_vote_map", "0.60", "percent required for successful map vote.", 0, true, 0.05, true, 1.0);
   111 	g_Cvar_Limits[1] = CreateConVar("sm_vote_kick", "0.60", "percent required for successful kick vote.", 0, true, 0.05, true, 1.0);	
   112 	g_Cvar_Limits[2] = CreateConVar("sm_vote_ban", "0.60", "percent required for successful ban vote.", 0, true, 0.05, true, 1.0);		
   113 	
   114 	g_hBanForward = CreateGlobalForward("OnClientBanned", ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_String);
   115 	
   116 	/* Account for late loading */
   117 	new Handle:topmenu;
   118 	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
   119 	{
   120 		OnAdminMenuReady(topmenu);
   121 	}
   122 	
   123 	g_SelectedMaps = CreateArray(33);
   124 	
   125 	g_MapList = CreateMenu(MenuHandler_Map, MenuAction_DrawItem);
   126 	SetMenuTitle(g_MapList, "Please select a map");
   127 	SetMenuExitBackButton(g_MapList, true);
   128 }
   129 
   130 public OnMapStart()
   131 {
   132 	g_mapCount = LoadMapList(g_MapList);
   133 }
   134 
   135 public OnAdminMenuReady(Handle:topmenu)
   136 {
   137 	/* Block us from being called twice */
   138 	if (topmenu == hTopMenu)
   139 	{
   140 		return;
   141 	}
   142 	
   143 	/* Save the Handle */
   144 	hTopMenu = topmenu;
   145 	
   146 	/* Build the "Voting Commands" category */
   147 	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   148 
   149 	if (voting_commands != INVALID_TOPMENUOBJECT)
   150 	{
   151 		AddToTopMenu(hTopMenu,
   152 			"sm_votekick",
   153 			TopMenuObject_Item,
   154 			AdminMenu_VoteKick,
   155 			voting_commands,
   156 			"sm_votekick",
   157 			ADMFLAG_VOTE|ADMFLAG_KICK);
   158 			
   159 		AddToTopMenu(hTopMenu,
   160 			"sm_voteban",
   161 			TopMenuObject_Item,
   162 			AdminMenu_VoteBan,
   163 			voting_commands,
   164 			"sm_voteban",
   165 			ADMFLAG_VOTE|ADMFLAG_BAN);
   166 			
   167 		AddToTopMenu(hTopMenu,
   168 			"sm_votemap",
   169 			TopMenuObject_Item,
   170 			AdminMenu_VoteMap,
   171 			voting_commands,
   172 			"sm_votemap",
   173 			ADMFLAG_VOTE|ADMFLAG_CHANGEMAP);
   174 	}
   175 }
   176 
   177 public Action:Command_Vote(client, args)
   178 {
   179 	if (args < 1)
   180 	{
   181 		ReplyToCommand(client, "[SM] Usage: sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
   182 		return Plugin_Handled;	
   183 	}
   184 	
   185 	if (IsVoteInProgress())
   186 	{
   187 		ReplyToCommand(client, "[SM] %t", "Vote in Progress");
   188 		return Plugin_Handled;
   189 	}
   190 		
   191 	if (!TestVoteDelay(client))
   192 	{
   193 		return Plugin_Handled;
   194 	}
   195 	
   196 	decl String:text[256];
   197 	GetCmdArgString(text, sizeof(text));
   198 
   199 	decl String:answers[5][64];
   200 	new answerCount;	
   201 	new len = BreakString(text, g_voteArg, sizeof(g_voteArg));
   202 	new pos = len;
   203 	
   204 	while (args > 1 && pos != -1 && answerCount < 5)
   205 	{	
   206 		pos = BreakString(text[len], answers[answerCount], sizeof(answers[]));
   207 		answerCount++;
   208 		
   209 		if (pos != -1)
   210 		{
   211 			len += pos;
   212 		}	
   213 	}
   214 
   215 	LogAction(client, -1, "\"%L\" initiated a generic vote.", client);
   216 	ShowActivity2(client, "[SM] ", "%t", "Initiate Vote", g_voteArg);
   217 	
   218 	g_voteType = voteType:question;
   219 	
   220 	g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
   221 	SetMenuTitle(g_hVoteMenu, "%s?", g_voteArg);
   222 	
   223 	if (answerCount < 2)
   224 	{
   225 		AddMenuItem(g_hVoteMenu, VOTE_YES, "Yes");
   226 		AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
   227 	}
   228 	else
   229 	{
   230 		for (new i = 0; i < answerCount; i++)
   231 		{
   232 			AddMenuItem(g_hVoteMenu, answers[i], answers[i]);
   233 		}	
   234 	}
   235 	
   236 	SetMenuExitButton(g_hVoteMenu, false);
   237 	VoteMenuToAll(g_hVoteMenu, 20);		
   238 	
   239 	return Plugin_Handled;	
   240 }
   241 
   242 public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
   243 {
   244 	if (action == MenuAction_End)
   245 	{
   246 		VoteMenuClose();
   247 	}
   248 	else if (action == MenuAction_Display)
   249 	{
   250 	 	if (g_voteType != voteType:question)
   251 	 	{
   252 			decl String:title[64];
   253 			GetMenuTitle(menu, title, sizeof(title));
   254 			
   255 	 		decl String:buffer[255];
   256 			Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
   257 
   258 			new Handle:panel = Handle:param2;
   259 			SetPanelTitle(panel, buffer);
   260 		}
   261 	}
   262 	else if (action == MenuAction_DisplayItem)
   263 	{
   264 		decl String:display[64];
   265 		GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
   266 	 
   267 	 	if (strcmp(display, "No") == 0 || strcmp(display, "Yes") == 0)
   268 	 	{
   269 			decl String:buffer[255];
   270 			Format(buffer, sizeof(buffer), "%T", display, param1);
   271 
   272 			return RedrawMenuItem(buffer);
   273 		}
   274 	}
   275 	/* else if (action == MenuAction_Select)
   276 	{
   277 		VoteSelect(menu, param1, param2);
   278 	}*/
   279 	else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
   280 	{
   281 		PrintToChatAll("[SM] %t", "No Votes Cast");
   282 	}	
   283 	else if (action == MenuAction_VoteEnd)
   284 	{
   285 		decl String:item[64], String:display[64];
   286 		new Float:percent, Float:limit, votes, totalVotes;
   287 
   288 		GetMenuVoteInfo(param2, votes, totalVotes);
   289 		GetMenuItem(menu, param1, item, sizeof(item), _, display, sizeof(display));
   290 		
   291 		if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
   292 		{
   293 			votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
   294 		}
   295 		
   296 		percent = GetVotePercent(votes, totalVotes);
   297 		
   298 		if (g_voteType != voteType:question)
   299 		{
   300 			limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
   301 		}
   302 		
   303 		/* :TODO: g_voteClient[userid] needs to be checked */
   304 
   305 		// A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
   306 		if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
   307 		{
   308 			/* :TODO: g_voteClient[userid] should be used here and set to -1 if not applicable.
   309 			 */
   310 			LogAction(-1, -1, "Vote failed.");
   311 			PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
   312 		}
   313 		else
   314 		{
   315 			PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
   316 			
   317 			switch (g_voteType)
   318 			{
   319 				case (voteType:question):
   320 				{
   321 					if (strcmp(item, VOTE_NO) == 0 || strcmp(item, VOTE_YES) == 0)
   322 					{
   323 						strcopy(item, sizeof(item), display);
   324 					}
   325 					
   326 					PrintToChatAll("[SM] %t", "Vote End", g_voteArg, item);
   327 				}
   328 				
   329 				case (voteType:map):
   330 				{
   331 					LogAction(-1, -1, "Changing map to %s due to vote.", item);
   332 					PrintToChatAll("[SM] %t", "Changing map", item);
   333 					new Handle:dp;
   334 					CreateDataTimer(5.0, Timer_ChangeMap, dp);
   335 					WritePackString(dp, item);		
   336 				}
   337 					
   338 				case (voteType:kick):
   339 				{
   340 					if (g_voteArg[0] == '\0')
   341 					{
   342 						strcopy(g_voteArg, sizeof(g_voteArg), "Votekicked");
   343 					}
   344 					
   345 					PrintToChatAll("[SM] %t", "Kicked target", "_s", g_voteInfo[VOTE_NAME]);					
   346 					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote kick successful, kicked \"%L\" (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
   347 					
   348 					ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);					
   349 				}
   350 					
   351 				case (voteType:ban):
   352 				{
   353 					/* Fire the ban forward */
   354 					Call_StartForward(g_hBanForward);
   355 					Call_PushCell(0);
   356 					Call_PushCell(g_voteClient[VOTE_USERID]);
   357 					Call_PushCell(30);
   358 					Call_PushString(g_voteArg);
   359 					Call_Finish();
   360 				
   361 					if (g_voteArg[0] == '\0')
   362 					{
   363 						strcopy(g_voteArg, sizeof(g_voteArg), "Votebanned");
   364 					}
   365 					
   366 					PrintToChatAll("[SM] %t", "Banned player", g_voteInfo[VOTE_NAME], 30);
   367 					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote ban successful, banned \"%L\" (minutes \"30\") (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
   368 					
   369 					ServerCommand("banid %d %s", 30, g_voteClient[VOTE_AUTHID]);
   370 					ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);				
   371 				}
   372 			}
   373 		}
   374 	}
   375 	
   376 	return 0;
   377 }
   378 
   379 /*
   380 VoteSelect(Handle:menu, param1, param2 = 0)
   381 {
   382 	if (GetConVarInt(g_Cvar_VoteShow) == 1)
   383 	{
   384 		decl String:voter[64], String:junk[64], String:choice[64];
   385 		GetClientName(param1, voter, sizeof(voter));
   386 		GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
   387 		PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
   388 	}
   389 }
   390 */
   391 
   392 VoteMenuClose()
   393 {
   394 	CloseHandle(g_hVoteMenu);
   395 	g_hVoteMenu = INVALID_HANDLE;
   396 }
   397 
   398 Float:GetVotePercent(votes, totalVotes)
   399 {
   400 	return FloatDiv(float(votes),float(totalVotes));
   401 }
   402 
   403 bool:TestVoteDelay(client)
   404 {
   405  	new delay = CheckVoteDelay();
   406  	
   407  	if (delay > 0)
   408  	{
   409  		if (delay > 60)
   410  		{
   411  			ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
   412  		}
   413  		else
   414  		{
   415  			ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
   416  		}
   417  		
   418  		return false;
   419  	}
   420  	
   421 	return true;
   422 }
   423 
   424 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   425 {
   426 	decl String:mapname[65];
   427 	
   428 	ResetPack(dp);
   429 	ReadPackString(dp, mapname, sizeof(mapname));
   430 	
   431 	ServerCommand("changelevel \"%s\"", mapname);
   432 	
   433 	return Plugin_Stop;
   434 }