plugins/funvotes.sp
author David Anderson <dvander@alliedmods.net>
Sat May 17 05:12:52 2008 +0000 (2008-05-17)
changeset 2056 f1fcde46ac7c
parent 2020 87ea1cf0fe76
child 2289 44045ead61cd
permissions -rw-r--r--
added amb1119 - sm_revote command
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Fun Votes Plugin
ds@1952
     5
 * Implements extra fun 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
#include <sdktools>
ds@1952
    38
ds@1952
    39
#undef REQUIRE_PLUGIN
ds@1952
    40
#include <adminmenu>
ds@1952
    41
ds@1952
    42
public Plugin:myinfo =
ds@1952
    43
{
ds@1952
    44
	name = "Fun Votes",
ds@1952
    45
	author = "AlliedModders LLC",
ds@1952
    46
	description = "Fun Vote Commands",
ds@1952
    47
	version = SOURCEMOD_VERSION,
ds@1952
    48
	url = "http://www.sourcemod.net/"
ds@1952
    49
};
ds@1952
    50
ds@1952
    51
#define VOTE_NO "###no###"
ds@1952
    52
#define VOTE_YES "###yes###"
ds@1952
    53
ds@1952
    54
new Handle:g_hVoteMenu = INVALID_HANDLE;
ds@1952
    55
ds@1952
    56
new Handle:g_Cvar_Limits[5] = {INVALID_HANDLE, ...};
ds@1952
    57
new Handle:g_Cvar_Gravity = INVALID_HANDLE;
ds@1952
    58
new Handle:g_Cvar_Alltalk = INVALID_HANDLE;
ds@1952
    59
new Handle:g_Cvar_FF = INVALID_HANDLE;
ds@1952
    60
ds@1952
    61
// new Handle:g_Cvar_Show = INVALID_HANDLE;
ds@1952
    62
ds@1952
    63
enum voteType
ds@1952
    64
{
ds@1952
    65
	gravity = 0,
ds@1952
    66
	burn,
ds@1952
    67
	slay,
ds@1952
    68
	alltalk,
ds@1952
    69
	ff
ds@1952
    70
};
ds@1952
    71
ds@1952
    72
new voteType:g_voteType = voteType:gravity;
ds@1952
    73
ds@1952
    74
// Menu API does not provide us with a way to pass multiple peices of data with a single
ds@1952
    75
// choice, so some globals are used to hold stuff.
ds@1952
    76
//
ds@1952
    77
#define VOTE_CLIENTID	0
ds@1952
    78
#define VOTE_USERID	1
ds@1952
    79
new g_voteClient[2];		/* Holds the target's client id and user id */
ds@1952
    80
ds@1952
    81
#define VOTE_NAME	0
ds@1952
    82
#define VOTE_AUTHID	1
ds@1952
    83
#define	VOTE_IP		2
ds@1952
    84
new String:g_voteInfo[3][65];	/* Holds the target's name, authid, and IP */
ds@1952
    85
ds@1952
    86
new Handle:hTopMenu = INVALID_HANDLE;
ds@1952
    87
ds@1952
    88
#include "funvotes/votegravity.sp"
ds@1952
    89
#include "funvotes/voteburn.sp"
ds@1952
    90
#include "funvotes/voteslay.sp"
ds@1952
    91
#include "funvotes/votealltalk.sp"
ds@1952
    92
#include "funvotes/voteff.sp"
ds@1952
    93
ds@1952
    94
public OnPluginStart()
ds@1952
    95
{
ds@1952
    96
	if (FindPluginByFile("basefunvotes.smx") != INVALID_HANDLE)
ds@1952
    97
	{
ds@1952
    98
		ThrowError("This plugin replaces basefuncommands.  You cannot run both at once.");
ds@1952
    99
	}
ds@1952
   100
	
ds@1952
   101
	LoadTranslations("common.phrases");
ds@1952
   102
	LoadTranslations("basevotes.phrases");
dvander@2020
   103
	LoadTranslations("funvotes.phrases");
dvander@2020
   104
	LoadTranslations("funcommands.phrases");
ds@1952
   105
	
ds@1952
   106
	RegAdminCmd("sm_votegravity", Command_VoteGravity, ADMFLAG_VOTE, "sm_votegravity <amount> [amount2] ... [amount5]");
ds@1952
   107
	RegAdminCmd("sm_voteburn", Command_VoteBurn, ADMFLAG_VOTE|ADMFLAG_SLAY, "sm_voteburn <player>");
ds@1952
   108
	RegAdminCmd("sm_voteslay", Command_VoteSlay, ADMFLAG_VOTE|ADMFLAG_SLAY, "sm_voteslay <player>");
ds@1952
   109
	RegAdminCmd("sm_votealltalk", Command_VoteAlltalk, ADMFLAG_VOTE, "sm_votealltalk");
ds@1952
   110
	RegAdminCmd("sm_voteff", Command_VoteFF, ADMFLAG_VOTE, "sm_voteff");
ds@1952
   111
ds@1952
   112
	g_Cvar_Limits[0] = CreateConVar("sm_vote_gravity", "0.60", "percent required for successful gravity vote.", 0, true, 0.05, true, 1.0);
ds@1952
   113
	g_Cvar_Limits[1] = CreateConVar("sm_vote_burn", "0.60", "percent required for successful burn vote.", 0, true, 0.05, true, 1.0);
ds@1952
   114
	g_Cvar_Limits[2] = CreateConVar("sm_vote_slay", "0.60", "percent required for successful slay vote.", 0, true, 0.05, true, 1.0);
ds@1952
   115
	g_Cvar_Limits[3] = CreateConVar("sm_vote_alltalk", "0.60", "percent required for successful alltalk vote.", 0, true, 0.05, true, 1.0);
ds@1952
   116
	g_Cvar_Limits[4] = CreateConVar("sm_vote_ff", "0.60", "percent required for successful friendly fire vote.", 0, true, 0.05, true, 1.0);
ds@1952
   117
	
ds@1952
   118
	g_Cvar_Gravity = FindConVar("sv_gravity");
ds@1952
   119
	g_Cvar_Alltalk = FindConVar("sv_alltalk");
ds@1952
   120
	g_Cvar_FF = FindConVar("mp_friendlyfire");
ds@1952
   121
	
ds@1952
   122
	/*
ds@1952
   123
	g_Cvar_Show = FindConVar("sm_vote_show");
ds@1952
   124
	if (g_Cvar_Show == INVALID_HANDLE)
ds@1952
   125
	{
ds@1952
   126
		g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
ds@1952
   127
	}
ds@1952
   128
	*/
ds@1952
   129
	
ds@1952
   130
	/* Account for late loading */
ds@1952
   131
	new Handle:topmenu;
ds@1952
   132
	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
ds@1952
   133
	{
ds@1952
   134
		OnAdminMenuReady(topmenu);
ds@1952
   135
	}
ds@1952
   136
}
ds@1952
   137
ds@1952
   138
public OnAdminMenuReady(Handle:topmenu)
ds@1952
   139
{
ds@1952
   140
	/* Block us from being called twice */
ds@1952
   141
	if (topmenu == hTopMenu)
ds@1952
   142
	{
ds@1952
   143
		return;
ds@1952
   144
	}
ds@1952
   145
	
ds@1952
   146
	/* Save the Handle */
ds@1952
   147
	hTopMenu = topmenu;
ds@1952
   148
	
ds@1952
   149
	/* Build the "Voting Commands" category */
ds@1952
   150
	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
ds@1952
   151
ds@1952
   152
	if (voting_commands != INVALID_TOPMENUOBJECT)
ds@1952
   153
	{
ds@1952
   154
		AddToTopMenu(hTopMenu,
ds@1952
   155
			"sm_votegravity",
ds@1952
   156
			TopMenuObject_Item,
ds@1952
   157
			AdminMenu_VoteGravity,
ds@1952
   158
			voting_commands,
ds@1952
   159
			"sm_votegravity",
ds@1952
   160
			ADMFLAG_VOTE);
ds@1952
   161
			
ds@1952
   162
		AddToTopMenu(hTopMenu,
ds@1952
   163
			"sm_voteburn",
ds@1952
   164
			TopMenuObject_Item,
ds@1952
   165
			AdminMenu_VoteBurn,
ds@1952
   166
			voting_commands,
ds@1952
   167
			"sm_voteburn",
ds@1952
   168
			ADMFLAG_VOTE|ADMFLAG_SLAY);
ds@1952
   169
			
ds@1952
   170
		AddToTopMenu(hTopMenu,
ds@1952
   171
			"sm_voteslay",
ds@1952
   172
			TopMenuObject_Item,
ds@1952
   173
			AdminMenu_VoteSlay,
ds@1952
   174
			voting_commands,
ds@1952
   175
			"sm_voteslay",
ds@1952
   176
			ADMFLAG_VOTE|ADMFLAG_SLAY);
ds@1952
   177
			
ds@1952
   178
		AddToTopMenu(hTopMenu,
ds@1952
   179
			"sm_votealltalk",
ds@1952
   180
			TopMenuObject_Item,
ds@1952
   181
			AdminMenu_VoteAllTalk,
ds@1952
   182
			voting_commands,
ds@1952
   183
			"sm_votealltalk",
ds@1952
   184
			ADMFLAG_VOTE);
ds@1952
   185
			
ds@1952
   186
		AddToTopMenu(hTopMenu,
ds@1952
   187
			"sm_voteff",
ds@1952
   188
			TopMenuObject_Item,
ds@1952
   189
			AdminMenu_VoteFF,
ds@1952
   190
			voting_commands,
ds@1952
   191
			"sm_voteff",
ds@1952
   192
			ADMFLAG_VOTE);
ds@1952
   193
	}
ds@1952
   194
}
ds@1952
   195
ds@1952
   196
public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
ds@1952
   197
{
ds@1952
   198
	if (action == MenuAction_End)
ds@1952
   199
	{
ds@1952
   200
		VoteMenuClose();
ds@1952
   201
	}
ds@1952
   202
	else if (action == MenuAction_Display)
ds@1952
   203
	{
ds@1952
   204
	 	decl String:title[64];
ds@1952
   205
		GetMenuTitle(menu, title, sizeof(title));
ds@1952
   206
ds@1952
   207
	 	decl String:buffer[255];
ds@1952
   208
		Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
ds@1952
   209
ds@1952
   210
		new Handle:panel = Handle:param2;
ds@1952
   211
		SetPanelTitle(panel, buffer);
ds@1952
   212
	}
ds@1952
   213
	else if (action == MenuAction_DisplayItem)
ds@1952
   214
	{
ds@1952
   215
		decl String:display[64];
ds@1952
   216
		GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
ds@1952
   217
	 
ds@1952
   218
	 	if (strcmp(display, VOTE_NO) == 0 || strcmp(display, VOTE_YES) == 0)
ds@1952
   219
	 	{
ds@1952
   220
			decl String:buffer[255];
ds@1952
   221
			Format(buffer, sizeof(buffer), "%T", display, param1);
ds@1952
   222
ds@1952
   223
			return RedrawMenuItem(buffer);
ds@1952
   224
		}
ds@1952
   225
	}
ds@1952
   226
	/* else if (action == MenuAction_Select)
ds@1952
   227
	{
ds@1952
   228
		VoteSelect(menu, param1, param2);
ds@1952
   229
	}*/
ds@1952
   230
	else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
ds@1952
   231
	{
ds@1952
   232
		PrintToChatAll("[SM] %t", "No Votes Cast");
ds@1952
   233
	}
ds@1952
   234
	else if (action == MenuAction_VoteEnd)
ds@1952
   235
	{
ds@1952
   236
		decl String:item[64];
ds@1952
   237
		new Float:percent, Float:limit, votes, totalVotes;
ds@1952
   238
ds@1952
   239
		GetMenuVoteInfo(param2, votes, totalVotes);
ds@1952
   240
		GetMenuItem(menu, param1, item, sizeof(item));
ds@1952
   241
		
ds@1952
   242
		if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
ds@1952
   243
		{
ds@1952
   244
			votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
ds@1952
   245
		}
ds@1952
   246
		
ds@1952
   247
		percent = GetVotePercent(votes, totalVotes);
ds@1952
   248
		
ds@1952
   249
		limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
ds@1952
   250
		
ds@1952
   251
		/* :TODO: g_voteClient[userid] needs to be checked.
ds@1952
   252
		 */
ds@1952
   253
ds@1952
   254
		// A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
ds@1952
   255
		if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
ds@1952
   256
		{
ds@1952
   257
			/* :TODO: g_voteClient[userid] should be used here and set to -1 if not applicable.
ds@1952
   258
			 */
ds@1952
   259
			LogAction(-1, -1, "Vote failed.");
ds@1952
   260
			PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
ds@1952
   261
		}
ds@1952
   262
		else
ds@1952
   263
		{
ds@1952
   264
			PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
ds@1952
   265
			
ds@1952
   266
			switch (g_voteType)
ds@1952
   267
			{
ds@1952
   268
				case (voteType:gravity):
ds@1952
   269
				{
ds@1952
   270
					PrintToChatAll("[SM] %t", "Cvar changed", "sv_gravity", item);					
ds@1952
   271
					LogAction(-1, -1, "Changing gravity to %s due to vote.", item);
ds@1952
   272
					SetConVarInt(g_Cvar_Gravity, StringToInt(item));
ds@1952
   273
				}
ds@1952
   274
				
ds@1952
   275
				case (voteType:burn):
ds@1952
   276
				{
ds@1952
   277
					PrintToChatAll("[SM] %t", "Set target on fire", "_s", g_voteInfo[VOTE_NAME]);					
ds@1952
   278
					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote burn successful, igniting \"%L\"", g_voteClient[VOTE_CLIENTID]);
ds@1952
   279
					
ds@1952
   280
					IgniteEntity(g_voteClient[VOTE_CLIENTID], 19.8);	
ds@1952
   281
				}
ds@1952
   282
				
ds@1952
   283
				case (voteType:slay):
ds@1952
   284
				{
ds@1952
   285
					PrintToChatAll("[SM] %t", "Slayed player", g_voteInfo[VOTE_NAME]);					
ds@1952
   286
					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote slay successful, slaying \"%L\"", g_voteClient[VOTE_CLIENTID]);
ds@1952
   287
					
ds@1952
   288
					ExtinguishEntity(g_voteClient[VOTE_CLIENTID]);
ds@1952
   289
					ForcePlayerSuicide(g_voteClient[VOTE_CLIENTID]);
ds@1952
   290
				}
ds@1952
   291
				
ds@1952
   292
				case (voteType:alltalk):
ds@1952
   293
				{
ds@1952
   294
					PrintToChatAll("[SM] %t", "Cvar changed", "sv_alltalk", (GetConVarBool(g_Cvar_Alltalk) ? "0" : "1"));
ds@1952
   295
					LogAction(-1, -1, "Changing alltalk to %s due to vote.", (GetConVarBool(g_Cvar_Alltalk) ? "0" : "1"));
ds@1952
   296
					SetConVarBool(g_Cvar_Alltalk, !GetConVarBool(g_Cvar_Alltalk));
ds@1952
   297
				}
ds@1952
   298
				
ds@1952
   299
				case (voteType:ff):
ds@1952
   300
				{
ds@1952
   301
					PrintToChatAll("[SM] %t", "Cvar changed", "mp_friendlyfire", (GetConVarBool(g_Cvar_FF) ? "0" : "1"));
ds@1952
   302
					LogAction(-1, -1, "Changing friendly fire to %s due to vote.", (GetConVarBool(g_Cvar_FF) ? "0" : "1"));
ds@1952
   303
					SetConVarBool(g_Cvar_FF, !GetConVarBool(g_Cvar_FF));
ds@1952
   304
				}				
ds@1952
   305
			}
ds@1952
   306
		}
ds@1952
   307
	}
ds@1952
   308
	
ds@1952
   309
	return 0;
ds@1952
   310
}
ds@1952
   311
ds@1952
   312
/*
ds@1952
   313
VoteSelect(Handle:menu, param1, param2 = 0)
ds@1952
   314
{
ds@1952
   315
	if (GetConVarInt(g_Cvar_VoteShow) == 1)
ds@1952
   316
	{
ds@1952
   317
		decl String:voter[64], String:junk[64], String:choice[64];
ds@1952
   318
		GetClientName(param1, voter, sizeof(voter));
ds@1952
   319
		GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
ds@1952
   320
		PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
ds@1952
   321
	}
ds@1952
   322
}
ds@1952
   323
*/
ds@1952
   324
ds@1952
   325
VoteMenuClose()
ds@1952
   326
{
ds@1952
   327
	CloseHandle(g_hVoteMenu);
ds@1952
   328
	g_hVoteMenu = INVALID_HANDLE;
ds@1952
   329
}
ds@1952
   330
ds@1952
   331
Float:GetVotePercent(votes, totalVotes)
ds@1952
   332
{
ds@1952
   333
	return FloatDiv(float(votes),float(totalVotes));
ds@1952
   334
}
ds@1952
   335
ds@1952
   336
bool:TestVoteDelay(client)
ds@1952
   337
{
ds@1952
   338
 	new delay = CheckVoteDelay();
ds@1952
   339
 	
ds@1952
   340
 	if (delay > 0)
ds@1952
   341
 	{
ds@1952
   342
 		if (delay > 60)
ds@1952
   343
 		{
ds@1952
   344
 			ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
ds@1952
   345
 		}
ds@1952
   346
 		else
ds@1952
   347
 		{
ds@1952
   348
 			ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
ds@1952
   349
 		}
ds@1952
   350
 		
ds@1952
   351
 		return false;
ds@1952
   352
 	}
ds@1952
   353
 	
ds@1952
   354
	return true;
ds@1952
   355
}