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