plugins/basevotes.sp
author Peace-Maker <januk@freenet.de>
Sat May 10 05:42:33 2014 -0700 (2014-05-10)
changeset 4354 7011587e0021
parent 3887 3e8a8d9903ce
permissions -rw-r--r--
Remove LogMessageEx definition from logging.inc (bug 5897, r=kyles).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basic Votes Plugin
     5  * Implements basic vote commands.
     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 #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|MenuAction_Display);
   122 	SetMenuTitle(g_MapList, "%T", "Please select a map", LANG_SERVER);
   123 	SetMenuExitBackButton(g_MapList, true);
   124 	
   125 	decl String:mapListPath[PLATFORM_MAX_PATH];
   126 	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
   127 	SetMapListCompatBind("sm_votemap menu", mapListPath);
   128 }
   129 
   130 public OnConfigsExecuted()
   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 					if (g_voteArg[0] == '\0')
   354 					{
   355 						strcopy(g_voteArg, sizeof(g_voteArg), "Votebanned");
   356 					}
   357 					
   358 					PrintToChatAll("[SM] %t", "Banned player", g_voteInfo[VOTE_NAME], 30);
   359 					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote ban successful, banned \"%L\" (minutes \"30\") (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
   360 
   361 					BanClient(g_voteClient[VOTE_CLIENTID],
   362 							  30,
   363 							  BANFLAG_AUTO,
   364 							  g_voteArg,
   365 							  "Banned by vote",
   366 							  "sm_voteban");
   367 				}
   368 			}
   369 		}
   370 	}
   371 	
   372 	return 0;
   373 }
   374 
   375 /*
   376 VoteSelect(Handle:menu, param1, param2 = 0)
   377 {
   378 	if (GetConVarInt(g_Cvar_VoteShow) == 1)
   379 	{
   380 		decl String:voter[64], String:junk[64], String:choice[64];
   381 		GetClientName(param1, voter, sizeof(voter));
   382 		GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
   383 		PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
   384 	}
   385 }
   386 */
   387 
   388 VoteMenuClose()
   389 {
   390 	CloseHandle(g_hVoteMenu);
   391 	g_hVoteMenu = INVALID_HANDLE;
   392 }
   393 
   394 Float:GetVotePercent(votes, totalVotes)
   395 {
   396 	return FloatDiv(float(votes),float(totalVotes));
   397 }
   398 
   399 bool:TestVoteDelay(client)
   400 {
   401  	new delay = CheckVoteDelay();
   402  	
   403  	if (delay > 0)
   404  	{
   405  		if (delay > 60)
   406  		{
   407  			ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
   408  		}
   409  		else
   410  		{
   411  			ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
   412  		}
   413  		
   414  		return false;
   415  	}
   416  	
   417 	return true;
   418 }
   419 
   420 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   421 {
   422 	decl String:mapname[65];
   423 	
   424 	ResetPack(dp);
   425 	ReadPackString(dp, mapname, sizeof(mapname));
   426 	
   427 	ForceChangeLevel(mapname, "sm_votemap Result");
   428 	
   429 	return Plugin_Stop;
   430 }