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