plugins/basevotes.sp
author Matt Woodrow <pred@alliedmods.net>
Tue Sep 02 07:10:11 2008 +0000 (2008-09-02)
changeset 2248 d433aa792147
parent 2203 159ab9bad58c
child 2289 44045ead61cd
permissions -rw-r--r--
Added a nominations section to maplists.cfg
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Basic Votes Plugin
ds@1952
     5
 * Implements basic vote commands.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
ds@1952
    37
#undef REQUIRE_PLUGIN
ds@1952
    38
#include <adminmenu>
ds@1952
    39
ds@1952
    40
public Plugin:myinfo =
ds@1952
    41
{
ds@1952
    42
	name = "Basic Votes",
ds@1952
    43
	author = "AlliedModders LLC",
ds@1952
    44
	description = "Basic Vote Commands",
ds@1952
    45
	version = SOURCEMOD_VERSION,
ds@1952
    46
	url = "http://www.sourcemod.net/"
ds@1952
    47
};
ds@1952
    48
ds@1952
    49
#define VOTE_NO "###no###"
ds@1952
    50
#define VOTE_YES "###yes###"
ds@1952
    51
ds@1952
    52
new Handle:g_hVoteMenu = INVALID_HANDLE;
ds@1952
    53
ds@1952
    54
new Handle:g_Cvar_Limits[3] = {INVALID_HANDLE, ...};
ds@1952
    55
//new Handle:g_Cvar_VoteSay = INVALID_HANDLE;
ds@1952
    56
ds@1952
    57
enum voteType
ds@1952
    58
{
ds@1952
    59
	map,
ds@1952
    60
	kick,
ds@1952
    61
	ban,
ds@1952
    62
	question
ds@1952
    63
}
ds@1952
    64
ds@1952
    65
new voteType:g_voteType = voteType:question;
ds@1952
    66
ds@1952
    67
// Menu API does not provide us with a way to pass multiple peices of data with a single
ds@1952
    68
// choice, so some globals are used to hold stuff.
ds@1952
    69
//
ds@1952
    70
#define VOTE_CLIENTID	0
ds@1952
    71
#define VOTE_USERID	1
ds@1952
    72
new g_voteClient[2];		/* Holds the target's client id and user id */
ds@1952
    73
ds@1952
    74
#define VOTE_NAME	0
ds@1952
    75
#define VOTE_AUTHID	1
ds@1952
    76
#define	VOTE_IP		2
ds@1952
    77
new String:g_voteInfo[3][65];	/* Holds the target's name, authid, and IP */
ds@1952
    78
ds@1952
    79
new String:g_voteArg[256];	/* Used to hold ban/kick reasons or vote questions */
ds@1952
    80
ds@1952
    81
ds@1952
    82
new Handle:hTopMenu = INVALID_HANDLE;
ds@1952
    83
ds@1952
    84
#include "basevotes/votekick.sp"
ds@1952
    85
#include "basevotes/voteban.sp"
ds@1952
    86
#include "basevotes/votemap.sp"
ds@1952
    87
ds@1952
    88
public OnPluginStart()
ds@1952
    89
{
ds@1952
    90
	LoadTranslations("common.phrases");
ds@1952
    91
	LoadTranslations("basevotes.phrases");
ds@1952
    92
	LoadTranslations("plugin.basecommands");
ds@1952
    93
	LoadTranslations("basebans.phrases");
ds@1952
    94
	
ds@1952
    95
	RegAdminCmd("sm_votemap", Command_Votemap, ADMFLAG_VOTE|ADMFLAG_CHANGEMAP, "sm_votemap <mapname> [mapname2] ... [mapname5] ");
ds@1952
    96
	RegAdminCmd("sm_votekick", Command_Votekick, ADMFLAG_VOTE|ADMFLAG_KICK, "sm_votekick <player> [reason]");
ds@1952
    97
	RegAdminCmd("sm_voteban", Command_Voteban, ADMFLAG_VOTE|ADMFLAG_BAN, "sm_voteban <player> [reason]");
ds@1952
    98
	RegAdminCmd("sm_vote", Command_Vote, ADMFLAG_VOTE, "sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
ds@1952
    99
ds@1952
   100
	/*
ds@1952
   101
	g_Cvar_Show = FindConVar("sm_vote_show");
ds@1952
   102
	if (g_Cvar_Show == INVALID_HANDLE)
ds@1952
   103
	{
ds@1952
   104
		g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
ds@1952
   105
	}
ds@1952
   106
	*/
ds@1952
   107
ds@1952
   108
	g_Cvar_Limits[0] = CreateConVar("sm_vote_map", "0.60", "percent required for successful map vote.", 0, true, 0.05, true, 1.0);
ds@1952
   109
	g_Cvar_Limits[1] = CreateConVar("sm_vote_kick", "0.60", "percent required for successful kick vote.", 0, true, 0.05, true, 1.0);	
ds@1952
   110
	g_Cvar_Limits[2] = CreateConVar("sm_vote_ban", "0.60", "percent required for successful ban vote.", 0, true, 0.05, true, 1.0);		
ds@1952
   111
	
ds@1952
   112
	/* Account for late loading */
ds@1952
   113
	new Handle:topmenu;
ds@1952
   114
	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
ds@1952
   115
	{
ds@1952
   116
		OnAdminMenuReady(topmenu);
ds@1952
   117
	}
ds@1952
   118
	
ds@1952
   119
	g_SelectedMaps = CreateArray(33);
ds@1952
   120
	
ds@1952
   121
	g_MapList = CreateMenu(MenuHandler_Map, MenuAction_DrawItem);
ds@1952
   122
	SetMenuTitle(g_MapList, "Please select a map");
ds@1952
   123
	SetMenuExitBackButton(g_MapList, true);
ds@1952
   124
	
ds@1952
   125
	decl String:mapListPath[PLATFORM_MAX_PATH];
ds@1952
   126
	BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
ds@1952
   127
	SetMapListCompatBind("sm_votemap menu", mapListPath);
ds@1952
   128
}
ds@1952
   129
ds@1952
   130
public OnConfigsExecuted()
ds@1952
   131
{
ds@1952
   132
	g_mapCount = LoadMapList(g_MapList);
ds@1952
   133
}
ds@1952
   134
ds@1952
   135
public OnAdminMenuReady(Handle:topmenu)
ds@1952
   136
{
ds@1952
   137
	/* Block us from being called twice */
ds@1952
   138
	if (topmenu == hTopMenu)
ds@1952
   139
	{
ds@1952
   140
		return;
ds@1952
   141
	}
ds@1952
   142
	
ds@1952
   143
	/* Save the Handle */
ds@1952
   144
	hTopMenu = topmenu;
ds@1952
   145
	
ds@1952
   146
	/* Build the "Voting Commands" category */
ds@1952
   147
	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
ds@1952
   148
ds@1952
   149
	if (voting_commands != INVALID_TOPMENUOBJECT)
ds@1952
   150
	{
ds@1952
   151
		AddToTopMenu(hTopMenu,
ds@1952
   152
			"sm_votekick",
ds@1952
   153
			TopMenuObject_Item,
ds@1952
   154
			AdminMenu_VoteKick,
ds@1952
   155
			voting_commands,
ds@1952
   156
			"sm_votekick",
ds@1952
   157
			ADMFLAG_VOTE|ADMFLAG_KICK);
ds@1952
   158
			
ds@1952
   159
		AddToTopMenu(hTopMenu,
ds@1952
   160
			"sm_voteban",
ds@1952
   161
			TopMenuObject_Item,
ds@1952
   162
			AdminMenu_VoteBan,
ds@1952
   163
			voting_commands,
ds@1952
   164
			"sm_voteban",
ds@1952
   165
			ADMFLAG_VOTE|ADMFLAG_BAN);
ds@1952
   166
			
ds@1952
   167
		AddToTopMenu(hTopMenu,
ds@1952
   168
			"sm_votemap",
ds@1952
   169
			TopMenuObject_Item,
ds@1952
   170
			AdminMenu_VoteMap,
ds@1952
   171
			voting_commands,
ds@1952
   172
			"sm_votemap",
ds@1952
   173
			ADMFLAG_VOTE|ADMFLAG_CHANGEMAP);
ds@1952
   174
	}
ds@1952
   175
}
ds@1952
   176
ds@1952
   177
public Action:Command_Vote(client, args)
ds@1952
   178
{
ds@1952
   179
	if (args < 1)
ds@1952
   180
	{
ds@1952
   181
		ReplyToCommand(client, "[SM] Usage: sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
ds@1952
   182
		return Plugin_Handled;	
ds@1952
   183
	}
ds@1952
   184
	
ds@1952
   185
	if (IsVoteInProgress())
ds@1952
   186
	{
ds@1952
   187
		ReplyToCommand(client, "[SM] %t", "Vote in Progress");
ds@1952
   188
		return Plugin_Handled;
ds@1952
   189
	}
ds@1952
   190
		
ds@1952
   191
	if (!TestVoteDelay(client))
ds@1952
   192
	{
ds@1952
   193
		return Plugin_Handled;
ds@1952
   194
	}
ds@1952
   195
	
ds@1952
   196
	decl String:text[256];
ds@1952
   197
	GetCmdArgString(text, sizeof(text));
ds@1952
   198
ds@1952
   199
	decl String:answers[5][64];
ds@1952
   200
	new answerCount;	
ds@1952
   201
	new len = BreakString(text, g_voteArg, sizeof(g_voteArg));
ds@1952
   202
	new pos = len;
ds@1952
   203
	
ds@1952
   204
	while (args > 1 && pos != -1 && answerCount < 5)
ds@1952
   205
	{	
ds@1952
   206
		pos = BreakString(text[len], answers[answerCount], sizeof(answers[]));
ds@1952
   207
		answerCount++;
ds@1952
   208
		
ds@1952
   209
		if (pos != -1)
ds@1952
   210
		{
ds@1952
   211
			len += pos;
ds@1952
   212
		}	
ds@1952
   213
	}
ds@1952
   214
ds@1952
   215
	LogAction(client, -1, "\"%L\" initiated a generic vote.", client);
ds@1952
   216
	ShowActivity2(client, "[SM] ", "%t", "Initiate Vote", g_voteArg);
ds@1952
   217
	
ds@1952
   218
	g_voteType = voteType:question;
ds@1952
   219
	
ds@1952
   220
	g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
ds@1952
   221
	SetMenuTitle(g_hVoteMenu, "%s?", g_voteArg);
ds@1952
   222
	
ds@1952
   223
	if (answerCount < 2)
ds@1952
   224
	{
ds@1952
   225
		AddMenuItem(g_hVoteMenu, VOTE_YES, "Yes");
ds@1952
   226
		AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
ds@1952
   227
	}
ds@1952
   228
	else
ds@1952
   229
	{
ds@1952
   230
		for (new i = 0; i < answerCount; i++)
ds@1952
   231
		{
ds@1952
   232
			AddMenuItem(g_hVoteMenu, answers[i], answers[i]);
ds@1952
   233
		}	
ds@1952
   234
	}
ds@1952
   235
	
ds@1952
   236
	SetMenuExitButton(g_hVoteMenu, false);
ds@1952
   237
	VoteMenuToAll(g_hVoteMenu, 20);		
ds@1952
   238
	
ds@1952
   239
	return Plugin_Handled;	
ds@1952
   240
}
ds@1952
   241
ds@1952
   242
public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
ds@1952
   243
{
ds@1952
   244
	if (action == MenuAction_End)
ds@1952
   245
	{
ds@1952
   246
		VoteMenuClose();
ds@1952
   247
	}
ds@1952
   248
	else if (action == MenuAction_Display)
ds@1952
   249
	{
ds@1952
   250
	 	if (g_voteType != voteType:question)
ds@1952
   251
	 	{
ds@1952
   252
			decl String:title[64];
ds@1952
   253
			GetMenuTitle(menu, title, sizeof(title));
ds@1952
   254
			
ds@1952
   255
	 		decl String:buffer[255];
ds@1952
   256
			Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
ds@1952
   257
ds@1952
   258
			new Handle:panel = Handle:param2;
ds@1952
   259
			SetPanelTitle(panel, buffer);
ds@1952
   260
		}
ds@1952
   261
	}
ds@1952
   262
	else if (action == MenuAction_DisplayItem)
ds@1952
   263
	{
ds@1952
   264
		decl String:display[64];
ds@1952
   265
		GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
ds@1952
   266
	 
ds@1952
   267
	 	if (strcmp(display, "No") == 0 || strcmp(display, "Yes") == 0)
ds@1952
   268
	 	{
ds@1952
   269
			decl String:buffer[255];
ds@1952
   270
			Format(buffer, sizeof(buffer), "%T", display, param1);
ds@1952
   271
ds@1952
   272
			return RedrawMenuItem(buffer);
ds@1952
   273
		}
ds@1952
   274
	}
ds@1952
   275
	/* else if (action == MenuAction_Select)
ds@1952
   276
	{
ds@1952
   277
		VoteSelect(menu, param1, param2);
ds@1952
   278
	}*/
ds@1952
   279
	else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
ds@1952
   280
	{
ds@1952
   281
		PrintToChatAll("[SM] %t", "No Votes Cast");
ds@1952
   282
	}	
ds@1952
   283
	else if (action == MenuAction_VoteEnd)
ds@1952
   284
	{
ds@1952
   285
		decl String:item[64], String:display[64];
ds@1952
   286
		new Float:percent, Float:limit, votes, totalVotes;
ds@1952
   287
ds@1952
   288
		GetMenuVoteInfo(param2, votes, totalVotes);
ds@1952
   289
		GetMenuItem(menu, param1, item, sizeof(item), _, display, sizeof(display));
ds@1952
   290
		
ds@1952
   291
		if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
ds@1952
   292
		{
ds@1952
   293
			votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
ds@1952
   294
		}
ds@1952
   295
		
ds@1952
   296
		percent = GetVotePercent(votes, totalVotes);
ds@1952
   297
		
ds@1952
   298
		if (g_voteType != voteType:question)
ds@1952
   299
		{
ds@1952
   300
			limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
ds@1952
   301
		}
ds@1952
   302
		
ds@1952
   303
		/* :TODO: g_voteClient[userid] needs to be checked */
ds@1952
   304
ds@1952
   305
		// A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
ds@1952
   306
		if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
ds@1952
   307
		{
ds@1952
   308
			/* :TODO: g_voteClient[userid] should be used here and set to -1 if not applicable.
ds@1952
   309
			 */
ds@1952
   310
			LogAction(-1, -1, "Vote failed.");
ds@1952
   311
			PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
ds@1952
   312
		}
ds@1952
   313
		else
ds@1952
   314
		{
ds@1952
   315
			PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
ds@1952
   316
			
ds@1952
   317
			switch (g_voteType)
ds@1952
   318
			{
ds@1952
   319
				case (voteType:question):
ds@1952
   320
				{
ds@1952
   321
					if (strcmp(item, VOTE_NO) == 0 || strcmp(item, VOTE_YES) == 0)
ds@1952
   322
					{
ds@1952
   323
						strcopy(item, sizeof(item), display);
ds@1952
   324
					}
ds@1952
   325
					
ds@1952
   326
					PrintToChatAll("[SM] %t", "Vote End", g_voteArg, item);
ds@1952
   327
				}
ds@1952
   328
				
ds@1952
   329
				case (voteType:map):
ds@1952
   330
				{
ds@1952
   331
					LogAction(-1, -1, "Changing map to %s due to vote.", item);
ds@1952
   332
					PrintToChatAll("[SM] %t", "Changing map", item);
ds@1952
   333
					new Handle:dp;
ds@1952
   334
					CreateDataTimer(5.0, Timer_ChangeMap, dp);
ds@1952
   335
					WritePackString(dp, item);		
ds@1952
   336
				}
ds@1952
   337
					
ds@1952
   338
				case (voteType:kick):
ds@1952
   339
				{
ds@1952
   340
					if (g_voteArg[0] == '\0')
ds@1952
   341
					{
ds@1952
   342
						strcopy(g_voteArg, sizeof(g_voteArg), "Votekicked");
ds@1952
   343
					}
ds@1952
   344
					
ds@1952
   345
					PrintToChatAll("[SM] %t", "Kicked target", "_s", g_voteInfo[VOTE_NAME]);					
ds@1952
   346
					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote kick successful, kicked \"%L\" (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
ds@1952
   347
					
ds@1952
   348
					ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);					
ds@1952
   349
				}
ds@1952
   350
					
ds@1952
   351
				case (voteType:ban):
ds@1952
   352
				{
ds@1952
   353
					if (g_voteArg[0] == '\0')
ds@1952
   354
					{
ds@1952
   355
						strcopy(g_voteArg, sizeof(g_voteArg), "Votebanned");
ds@1952
   356
					}
ds@1952
   357
					
ds@1952
   358
					PrintToChatAll("[SM] %t", "Banned player", g_voteInfo[VOTE_NAME], 30);
ds@1952
   359
					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote ban successful, banned \"%L\" (minutes \"30\") (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
ds@1952
   360
ds@1952
   361
					BanClient(g_voteClient[VOTE_CLIENTID],
ds@1952
   362
							  30,
ds@1952
   363
							  BANFLAG_AUTO,
ds@1952
   364
							  g_voteArg,
ds@1952
   365
							  "Banned by vote",
ds@1952
   366
							  "sm_voteban");
ds@1952
   367
				}
ds@1952
   368
			}
ds@1952
   369
		}
ds@1952
   370
	}
ds@1952
   371
	
ds@1952
   372
	return 0;
ds@1952
   373
}
ds@1952
   374
ds@1952
   375
/*
ds@1952
   376
VoteSelect(Handle:menu, param1, param2 = 0)
ds@1952
   377
{
ds@1952
   378
	if (GetConVarInt(g_Cvar_VoteShow) == 1)
ds@1952
   379
	{
ds@1952
   380
		decl String:voter[64], String:junk[64], String:choice[64];
ds@1952
   381
		GetClientName(param1, voter, sizeof(voter));
ds@1952
   382
		GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
ds@1952
   383
		PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
ds@1952
   384
	}
ds@1952
   385
}
ds@1952
   386
*/
ds@1952
   387
ds@1952
   388
VoteMenuClose()
ds@1952
   389
{
ds@1952
   390
	CloseHandle(g_hVoteMenu);
ds@1952
   391
	g_hVoteMenu = INVALID_HANDLE;
ds@1952
   392
}
ds@1952
   393
ds@1952
   394
Float:GetVotePercent(votes, totalVotes)
ds@1952
   395
{
ds@1952
   396
	return FloatDiv(float(votes),float(totalVotes));
ds@1952
   397
}
ds@1952
   398
ds@1952
   399
bool:TestVoteDelay(client)
ds@1952
   400
{
ds@1952
   401
 	new delay = CheckVoteDelay();
ds@1952
   402
 	
ds@1952
   403
 	if (delay > 0)
ds@1952
   404
 	{
ds@1952
   405
 		if (delay > 60)
ds@1952
   406
 		{
ds@1952
   407
 			ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
ds@1952
   408
 		}
ds@1952
   409
 		else
ds@1952
   410
 		{
ds@1952
   411
 			ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
ds@1952
   412
 		}
ds@1952
   413
 		
ds@1952
   414
 		return false;
ds@1952
   415
 	}
ds@1952
   416
 	
ds@1952
   417
	return true;
ds@1952
   418
}
ds@1952
   419
ds@1952
   420
public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
ds@1952
   421
{
ds@1952
   422
	decl String:mapname[65];
ds@1952
   423
	
ds@1952
   424
	ResetPack(dp);
ds@1952
   425
	ReadPackString(dp, mapname, sizeof(mapname));
ds@1952
   426
	
pred@2203
   427
	ForceChangeLevel(mapname, "sm_votemap Result");
ds@1952
   428
	
ds@1952
   429
	return Plugin_Stop;
ds@1952
   430
}