plugins/rockthevote.sp
author Matt Woodrow <pred@alliedmods.net>
Fri Apr 11 07:58:06 2008 +0000 (2008-04-11)
changeset 1988 0eae92e7cfd2
parent 1952 b7aa2b626807
child 2133 cad0a5ea6742
permissions -rw-r--r--
Fixed amb1594 - SetPlayerClass issues with spawning
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Rock The Vote Plugin
     5  * Creates a map vote when the required number of players have requested one.
     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 #include <sourcemod>
    35 
    36 #pragma semicolon 1
    37 
    38 public Plugin:myinfo =
    39 {
    40 	name = "Rock The Vote",
    41 	author = "AlliedModders LLC",
    42 	description = "Provides RTV Map Voting",
    43 	version = SOURCEMOD_VERSION,
    44 	url = "http://www.sourcemod.net/"
    45 };
    46 
    47 new Handle:g_Cvar_Needed = INVALID_HANDLE;
    48 new Handle:g_Cvar_Maps = INVALID_HANDLE;
    49 new Handle:g_Cvar_Nominate = INVALID_HANDLE;
    50 new Handle:g_Cvar_MinPlayers = INVALID_HANDLE;
    51 
    52 new Handle:g_MapList = INVALID_HANDLE;
    53 new Handle:g_RTVMapList = INVALID_HANDLE;
    54 new Handle:g_MapMenu = INVALID_HANDLE;
    55 new Handle:g_RetryTimer = INVALID_HANDLE;
    56 new g_mapFileSerial = -1;
    57 
    58 new bool:g_CanRTV = false;		// True if RTV loaded maps and is active.
    59 new bool:g_RTVAllowed = false;	// True if RTV is available to players. Used to delay rtv votes.
    60 new bool:g_RTVStarted = false;	// Indicates that the actual map vote has started
    61 new bool:g_RTVEnded = false;	// Indicates that the actual map vote has concluded
    62 new g_Voters = 0;				// Total voters connected. Doesn't include fake clients.
    63 new g_Votes = 0;				// Total number of "say rtv" votes
    64 new g_VotesNeeded = 0;			// Necessary votes before map vote begins. (voters * percent_needed)
    65 new bool:g_Voted[MAXPLAYERS+1] = {false, ...};
    66 new bool:g_Nominated[MAXPLAYERS+1] = {false, ...};
    67 
    68 public OnPluginStart()
    69 {
    70 	LoadTranslations("common.phrases");
    71 	LoadTranslations("rockthevote.phrases");
    72 	
    73 	new arraySize = ByteCountToCells(33);	
    74 	g_MapList = CreateArray(arraySize);
    75 	g_RTVMapList = CreateArray(arraySize);
    76 	
    77 	g_Cvar_Needed = CreateConVar("sm_rtv_needed", "0.60", "Percentage of players needed to rockthevote (Def 60%)", 0, true, 0.05, true, 1.0);
    78 	g_Cvar_Maps = CreateConVar("sm_rtv_maps", "4", "Number of maps to be voted on. 2 to 6. (Def 4)", 0, true, 2.0, true, 6.0);
    79 	g_Cvar_Nominate = CreateConVar("sm_rtv_nominate", "1", "Enables nomination system.", 0, true, 0.0, true, 1.0);
    80 	g_Cvar_MinPlayers = CreateConVar("sm_rtv_minplayers", "0", "Number of players required before RTV will be enabled.", 0, true, 0.0, true, float(MAXPLAYERS));
    81 	
    82 	RegConsoleCmd("say", Command_Say);
    83 	RegConsoleCmd("say_team", Command_Say);
    84 	
    85 	RegAdminCmd("sm_rtv_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_rtv_addmap <mapname> - Forces a map to be on the RTV, and lowers the allowed nominations.");
    86 	
    87 	AutoExecConfig(true, "rtv");
    88 }
    89 
    90 public OnMapStart()
    91 {
    92 	g_Voters = 0;
    93 	g_Votes = 0;
    94 	g_VotesNeeded = 0;
    95 	g_RTVStarted = false;
    96 	g_RTVEnded = false;
    97 }
    98 
    99 public OnMapEnd()
   100 {
   101 	g_CanRTV = false;	
   102 	g_RTVAllowed = false;
   103 }
   104 
   105 public OnConfigsExecuted()
   106 {
   107 	if (g_RTVMapList != INVALID_HANDLE)
   108 	{
   109 		ClearArray(g_RTVMapList);
   110 	}
   111 	
   112 	if (ReadMapList(g_MapList,
   113 					g_mapFileSerial,
   114 					"rockthevote",
   115 					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
   116 		== INVALID_HANDLE)
   117 	{
   118 		if (g_mapFileSerial == -1)
   119 		{
   120 			LogError("Unable to create a valid map list.");
   121 		}
   122 	}
   123 	
   124 	BuildMapMenu();
   125 	g_CanRTV = true;
   126 	CreateTimer(30.0, Timer_DelayRTV);
   127 }
   128 
   129 public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
   130 {
   131 	if(!g_CanRTV || IsFakeClient(client))
   132 		return true;
   133 	
   134 	g_Voted[client] = false;
   135 	g_Nominated[client] = false;
   136 
   137 	g_Voters++;
   138 	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
   139 	
   140 	return true;
   141 }
   142 
   143 public OnClientDisconnect(client)
   144 {
   145 	if(!g_CanRTV || IsFakeClient(client))
   146 		return;
   147 	
   148 	if(g_Voted[client])
   149 	{
   150 		g_Votes--;
   151 	}
   152 	
   153 	g_Voters--;
   154 	
   155 	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
   156 	
   157 	if (g_Votes && g_Voters && g_Votes >= g_VotesNeeded && g_RTVAllowed && !g_RTVStarted) 
   158 	{
   159 		g_RTVStarted = true;
   160 		CreateTimer(2.0, Timer_StartRTV, TIMER_FLAG_NO_MAPCHANGE);
   161 	}	
   162 }
   163 
   164 public Action:Command_Addmap(client, args)
   165 {
   166 	if (args < 1)
   167 	{
   168 		ReplyToCommand(client, "[SM] Usage: sm_rtv_addmap <mapname>");
   169 		return Plugin_Handled;
   170 	}
   171 	
   172 	if (!g_CanRTV)
   173 	{
   174 		ReplyToCommand(client, "[SM] RockTheVote is not available.");
   175 		return Plugin_Handled;
   176 	}
   177 	
   178 	decl String:mapname[64];
   179 	GetCmdArg(1, mapname, sizeof(mapname));
   180 
   181 	if (FindStringInArray(g_MapList, mapname) == -1)
   182 	{
   183 		ReplyToCommand(client, "%t", "Map was not found", mapname);
   184 		return Plugin_Handled;
   185 	}
   186 	
   187 	if (GetArraySize(g_RTVMapList) > 0)
   188 	{
   189 		if (FindStringInArray(g_RTVMapList, mapname) != -1)
   190 		{
   191 			ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   192 			return Plugin_Handled;				
   193 		}
   194 		
   195 		ShiftArrayUp(g_RTVMapList, 0);
   196 		SetArrayString(g_RTVMapList, 0, mapname);
   197 		
   198 		while (GetArraySize(g_RTVMapList) > GetConVarInt(g_Cvar_Maps))
   199 		{
   200 			RemoveFromArray(g_RTVMapList, GetConVarInt(g_Cvar_Maps));
   201 		}
   202 	}
   203 	else
   204 	{
   205 		PushArrayString(g_RTVMapList, mapname);
   206 	}
   207 		
   208 	decl String:item[64];
   209 	for (new i = 0; i < GetMenuItemCount(g_MapMenu); i++)
   210 	{
   211 		GetMenuItem(g_MapMenu, i, item, sizeof(item));
   212 		if (strcmp(item, mapname) == 0)
   213 		{
   214 			RemoveMenuItem(g_MapMenu, i);
   215 			break;
   216 		}			
   217 	}	
   218 	
   219 	ReplyToCommand(client, "%t", "Map Inserted", mapname);
   220 	LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
   221 
   222 	return Plugin_Handled;		
   223 }
   224 
   225 public Action:Command_Say(client, args)
   226 {
   227 	if (!g_CanRTV || !client)
   228 	{
   229 		return Plugin_Continue;
   230 	}
   231 
   232 	decl String:text[192];
   233 	if (!GetCmdArgString(text, sizeof(text)))
   234 	{
   235 		return Plugin_Continue;
   236 	}
   237 	
   238 	new startidx = 0;
   239 	if(text[strlen(text)-1] == '"')
   240 	{
   241 		text[strlen(text)-1] = '\0';
   242 		startidx = 1;
   243 	}
   244 	
   245 	if (strcmp(text[startidx], "rtv", false) == 0 || strcmp(text[startidx], "rockthevote", false) == 0)
   246 	{
   247 		if (!g_RTVAllowed)
   248 		{
   249 			PrintToChat(client, "[SM] %t", "RTV Not Allowed");
   250 			return Plugin_Continue;
   251 		}
   252 		
   253 		if (g_RTVEnded)
   254 		{
   255 			PrintToChat(client, "[SM] %t", "RTV Ended");
   256 			return Plugin_Continue;
   257 		}
   258 		
   259 		if (g_RTVStarted)
   260 		{
   261 			PrintToChat(client, "[SM] %t", "RTV Started");
   262 			return Plugin_Continue;
   263 		}
   264 		
   265 		if (GetClientCount(true) < GetConVarInt(g_Cvar_MinPlayers) && g_Votes == 0) // Should we keep checking g_Votes here?
   266 		{
   267 			PrintToChat(client, "[SM] %t", "Minimal Players Not Met");
   268 			return Plugin_Continue;			
   269 		}
   270 		
   271 		if (g_Voted[client])
   272 		{
   273 			PrintToChat(client, "[SM] %t", "Already Voted");
   274 			return Plugin_Continue;
   275 		}	
   276 		
   277 		new String:name[64];
   278 		GetClientName(client, name, sizeof(name));
   279 		
   280 		g_Votes++;
   281 		g_Voted[client] = true;
   282 		
   283 		PrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded);
   284 		
   285 		if (g_Votes >= g_VotesNeeded)
   286 		{
   287 			g_RTVStarted = true;
   288 			CreateTimer(2.0, Timer_StartRTV, TIMER_FLAG_NO_MAPCHANGE);
   289 		}
   290 	}
   291 	else if (GetConVarBool(g_Cvar_Nominate) && strcmp(text[startidx], "nominate", false) == 0)
   292 	{
   293 		if (g_RTVEnded)
   294 		{
   295 			PrintToChat(client, "[SM] %t", "RTV Ended");
   296 			return Plugin_Continue;
   297 		}		
   298 		
   299 		if (g_RTVStarted)
   300 		{
   301 			PrintToChat(client, "[SM] %t", "RTV Started");
   302 			return Plugin_Continue;
   303 		}
   304 		
   305 		if (g_Nominated[client])
   306 		{
   307 			PrintToChat(client, "[SM] %t", "Already Nominated");
   308 			return Plugin_Continue;
   309 		}
   310 		
   311 		if (GetArraySize(g_RTVMapList) >= GetConVarInt(g_Cvar_Maps))
   312 		{
   313 			PrintToChat(client, "[SM] %t", "Max Nominations");
   314 			return Plugin_Continue;			
   315 		}
   316 		
   317 		DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);		
   318 	}
   319 	
   320 	return Plugin_Continue;	
   321 }
   322 
   323 public Action:Timer_DelayRTV(Handle:timer)
   324 {
   325 	g_RTVAllowed = true;
   326 	g_RTVStarted = false;
   327 	g_RTVEnded = false;	
   328 }
   329 
   330 public Action:Timer_StartRTV(Handle:timer)
   331 {
   332 	if (timer == g_RetryTimer)
   333 	{
   334 		g_RetryTimer = INVALID_HANDLE;
   335 	}
   336 	
   337 	if (g_RetryTimer != INVALID_HANDLE)
   338 	{
   339 		return;
   340 	}
   341 
   342 	if (IsVoteInProgress())
   343 	{
   344 		// Can't start a vote, try again in 5 seconds.
   345 		g_RetryTimer = CreateTimer(5.0, Timer_StartRTV, TIMER_FLAG_NO_MAPCHANGE);
   346 		return;
   347 	}			
   348 	
   349 	PrintToChatAll("[SM] %t", "RTV Vote Ready");
   350 		
   351 	new Handle:MapVoteMenu = CreateMenu(Handler_MapMapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
   352 	SetMenuTitle(MapVoteMenu, "Rock The Vote");
   353 	
   354 	new Handle:tempMaps  = CloneArray(g_MapList);
   355 	decl String:map[32];
   356 
   357 	GetCurrentMap(map, sizeof(map));
   358 	new index = FindStringInArray(tempMaps, map);
   359 	if (index != -1)
   360 	{
   361 		RemoveFromArray(tempMaps, index);
   362 	}	
   363 	
   364 	// We assume that g_RTVMapList is within the correct limits, based on the logic for nominations
   365 	for (new i = 0; i < GetArraySize(g_RTVMapList); i++)
   366 	{
   367 		GetArrayString(g_RTVMapList, i, map, sizeof(map));
   368 		AddMenuItem(MapVoteMenu, map, map);
   369 		
   370 		index = FindStringInArray(tempMaps, map);
   371 		if (index != -1)
   372 		{
   373 			RemoveFromArray(tempMaps, index);
   374 		}
   375 	}
   376 	
   377 	new limit = GetConVarInt(g_Cvar_Maps) - GetArraySize(g_RTVMapList);
   378 	if (limit > GetArraySize(tempMaps))
   379 	{
   380 		limit = GetArraySize(tempMaps);
   381 	}
   382 
   383 	for (new i = 0; i < limit; i++)
   384 	{
   385 		new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
   386 		GetArrayString(tempMaps, b, map, sizeof(map));		
   387 		PushArrayString(g_RTVMapList, map);
   388 		AddMenuItem(MapVoteMenu, map, map);			
   389 		RemoveFromArray(tempMaps, b);
   390 	}	
   391 	
   392 	CloseHandle(tempMaps);
   393 	
   394 	AddMenuItem(MapVoteMenu, "Don't Change", "Don't Change");
   395 		
   396 	SetMenuExitButton(MapVoteMenu, false);
   397 	VoteMenuToAll(MapVoteMenu, 20);
   398 		
   399 	LogMessage("[SM] Rockthevote was successfully started.");
   400 }
   401 
   402 public Action:Timer_ChangeMap(Handle:hTimer, Handle:dp)
   403 {
   404 	new String:map[65];
   405 	
   406 	ResetPack(dp);
   407 	ReadPackString(dp, map, sizeof(map));
   408 	
   409 	ServerCommand("changelevel \"%s\"", map);
   410 	
   411 	return Plugin_Stop;
   412 }
   413 
   414 public Handler_MapMapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
   415 {
   416 	switch (action)
   417 	{
   418 		case MenuAction_End:
   419 		{
   420 			CloseHandle(menu);
   421 		}
   422 		
   423 		case MenuAction_Display:
   424 		{
   425 	 		decl String:oldTitle[255], String:buffer[255];
   426 			GetMenuTitle(menu, oldTitle, sizeof(oldTitle));
   427 			Format(buffer, sizeof(buffer), "%T", oldTitle, param1);
   428 
   429 			new Handle:panel = Handle:param2;
   430 			SetPanelTitle(panel, buffer);
   431 		}
   432 		
   433 		case MenuAction_DisplayItem:
   434 		{
   435 			if (GetMenuItemCount(menu) - 1 == param2)
   436 			{
   437 				decl String:buffer[255];
   438 				Format(buffer, sizeof(buffer), "%T", "Don't Change", param1);
   439 				return RedrawMenuItem(buffer);
   440 			}
   441 		}		
   442 
   443 		// Why am I commented out? Because BAIL hasn't decided yet if
   444 		// vote notification will be built into the Vote API.
   445 		/*case MenuAction_Select:
   446 		{
   447 			decl String:Name[32], String:Map[32];
   448 			GetClientName(param1, Name, sizeof(Name));
   449 			GetMenuItem(menu, param2, Map, sizeof(Map));
   450 
   451 			PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
   452 		}*/
   453 		
   454 		case MenuAction_VoteCancel:
   455 		{
   456 			if (param1 == VoteCancel_NoVotes)
   457 			{
   458 				PrintToChatAll("[SM] %t", "No Votes");
   459 				g_RTVEnded = true;
   460 			}
   461 		}
   462 
   463 		case MenuAction_VoteEnd:
   464 		{
   465 			new String:map[64];
   466 			
   467 			GetMenuItem(menu, param1, map, sizeof(map));
   468 			
   469 			if (GetMenuItemCount(menu) - 1 == param1) // This should always match the "Keep Current" option
   470 			{
   471 				PrintToChatAll("[SM] %t", "Current Map Stays");
   472 				LogMessage("[SM] Rockthevote has ended, current map kept.");
   473 			}
   474 			else
   475 			{
   476 				PrintToChatAll("[SM] %t", "Changing Maps", map);
   477 				LogMessage("[SM] Rockthevote has ended, changing to map %s.", map);
   478 				new Handle:dp;
   479 				CreateDataTimer(5.0, Timer_ChangeMap, dp);
   480 				WritePackString(dp, map);
   481 			}
   482 			
   483 			g_RTVEnded = true;
   484 		}
   485 	}
   486 	
   487 	return 0;
   488 }
   489 
   490 public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
   491 {
   492 	switch (action)
   493 	{
   494 		case MenuAction_Select:
   495 		{
   496 			if (GetArraySize(g_RTVMapList) >= GetConVarInt(g_Cvar_Maps)) 
   497 			{
   498 				PrintToChat(param1, "[SM] %t", "Max Nominations");
   499 				return;	
   500 			}
   501 			
   502 			decl String:map[64], String:name[64];
   503 			GetMenuItem(menu, param2, map, sizeof(map));
   504 			
   505 			if (FindStringInArray(g_RTVMapList, map) != -1)
   506 			{
   507 				PrintToChat(param1, "[SM] %t", "Map Already Nominated");
   508 				return;
   509 			}
   510 			
   511 			GetClientName(param1, name, 64);
   512 
   513 			PushArrayString(g_RTVMapList, map);
   514 			RemoveMenuItem(menu, param2);
   515 			
   516 			g_Nominated[param1] = true;
   517 			
   518 			PrintToChatAll("[SM] %t", "Map Nominated", name, map);
   519 		}
   520 	}
   521 }
   522 
   523 BuildMapMenu()
   524 {
   525 	if (g_MapMenu != INVALID_HANDLE)
   526 	{
   527 		CloseHandle(g_MapMenu);
   528 		g_MapMenu = INVALID_HANDLE;
   529 	}
   530 	
   531 	g_MapMenu = CreateMenu(Handler_MapSelectMenu);
   532 	SetMenuTitle(g_MapMenu, "%t", "Nominate Title");
   533 
   534 	decl String:map[64];		
   535 	for (new i = 0; i < GetArraySize(g_MapList); i++)
   536 	{
   537 		GetArrayString(g_MapList, i, map, sizeof(map));
   538 		AddMenuItem(g_MapMenu, map, map);
   539 	}
   540 	
   541 	SetMenuExitButton(g_MapMenu, false);
   542 }