plugins/rockthevote.sp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 22:36:21 2007 +0000 (2007-10-14)
changeset 1564 e8fbc247f7f2
parent 1539 74121104e67d
child 1606 4fb3959c2c96
permissions -rw-r--r--
re-added sdktools to the build script
     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 	g_MapList = CreateArray(33);
    75 	g_RTVMapList = CreateArray(33);
    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_File = CreateConVar("sm_rtv_file", "configs/maps.ini", "Map file to use. (Def configs/maps.ini)");
    79 	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);
    80 	g_Cvar_Nominate = CreateConVar("sm_rtv_nominate", "1", "Enables nomination system.", 0, true, 0.0, true, 1.0);
    81 	g_Cvar_MinPlayers = CreateConVar("sm_rtv_minplayers", "0", "Number of players required before RTV will be enabled.", 0, true, 0.0, true, float(MAXPLAYERS));
    82 	
    83 	RegConsoleCmd("say", Command_Say);
    84 	RegConsoleCmd("say_team", Command_Say);
    85 	
    86 	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.");
    87 	
    88 	AutoExecConfig(true, "rtv");
    89 }
    90 
    91 public OnMapStart()
    92 {
    93 	if (g_RTVMapList != INVALID_HANDLE)
    94 	{
    95 		ClearArray(g_RTVMapList);
    96 	}
    97 	
    98 	g_Voters = 0;
    99 	g_Votes = 0;
   100 	g_VotesNeeded = 0;
   101 	g_RTVStarted = false;
   102 	g_RTVEnded = false;
   103 	
   104 	if (LoadMaps())
   105 	{
   106 		BuildMapMenu();
   107 		g_CanRTV = true;
   108 		CreateTimer(30.0, Timer_DelayRTV);
   109 	}
   110 	else
   111 	{
   112 		LogMessage("[SM] Cannot find map cycle file, RTV not active.");
   113 	}
   114 }
   115 
   116 public OnMapEnd()
   117 {
   118 	g_CanRTV = false;	
   119 	g_RTVAllowed = false;
   120 }
   121 
   122 
   123 public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
   124 {
   125 	if(IsFakeClient(client))
   126 		return true;
   127 	
   128 	g_Voted[client] = false;
   129 	g_Nominated[client] = false;
   130 
   131 	g_Voters++;
   132 	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
   133 	
   134 	return true;
   135 }
   136 
   137 public OnClientDisconnect(client)
   138 {
   139 	if(IsFakeClient(client))
   140 		return;
   141 	
   142 	if(g_Voted[client])
   143 	{
   144 		g_Votes--;
   145 	}
   146 	
   147 	g_Voters--;
   148 	
   149 	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
   150 	
   151 	if (g_Votes && g_Voters && g_Votes >= g_VotesNeeded && g_RTVAllowed) 
   152 	{
   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 	decl String:mapname[64];
   166 	GetCmdArg(1, mapname, sizeof(mapname));
   167 
   168 	if (!IsStringInArray(g_MapList, mapname))
   169 	{
   170 		ReplyToCommand(client, "%t", "Map was not found", mapname);
   171 		return Plugin_Handled;
   172 	}
   173 	
   174 	if (GetArraySize(g_RTVMapList) > 0)
   175 	{
   176 		for (new i = 0; i < GetArraySize(g_RTVMapList); i++)
   177 		{
   178 			decl String:nextmap[64];
   179 			GetArrayString(g_RTVMapList, i, nextmap, sizeof(nextmap));
   180 			if (strcmp(mapname, nextmap, false) == 0)
   181 			{
   182 				ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   183 				return Plugin_Handled;			
   184 			}		
   185 		}
   186 		
   187 		ShiftArrayUp(g_RTVMapList, 0);
   188 		SetArrayString(g_RTVMapList, 0, mapname);
   189 		
   190 		while (GetArraySize(g_RTVMapList) > GetConVarInt(g_Cvar_Maps))
   191 		{
   192 			RemoveFromArray(g_RTVMapList, GetConVarInt(g_Cvar_Maps));
   193 		}
   194 	}
   195 	else
   196 	{
   197 		PushArrayString(g_RTVMapList, mapname);
   198 	}
   199 		
   200 	decl String:item[64];
   201 	for (new i = 0; i < GetMenuItemCount(g_MapMenu); i++)
   202 	{
   203 		GetMenuItem(g_MapMenu, i, item, sizeof(item));
   204 		if (strcmp(item, mapname) == 0)
   205 		{
   206 			RemoveMenuItem(g_MapMenu, i);
   207 			break;
   208 		}			
   209 	}	
   210 	
   211 	ReplyToCommand(client, "%t", "Map Inserted", mapname);
   212 	LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
   213 
   214 	return Plugin_Handled;		
   215 }
   216 
   217 public Action:Command_Say(client, args)
   218 {
   219 	if (!g_CanRTV || !client)
   220 		return Plugin_Continue;
   221 
   222 	decl String:text[192];
   223 	if (!GetCmdArgString(text, sizeof(text)))
   224 	{
   225 		return Plugin_Continue;
   226 	}
   227 	
   228 	new startidx = 0;
   229 	if(text[strlen(text)-1] == '"')
   230 	{
   231 		text[strlen(text)-1] = '\0';
   232 		startidx = 1;
   233 	}
   234 	
   235 	if (strcmp(text[startidx], "rtv", false) == 0 || strcmp(text[startidx], "rockthevote", false) == 0)
   236 	{
   237 		if (!g_RTVAllowed)
   238 		{
   239 			PrintToChat(client, "[SM] %t", "RTV Not Allowed");
   240 			return Plugin_Continue;
   241 		}
   242 		
   243 		if (g_RTVEnded)
   244 		{
   245 			PrintToChat(client, "[SM] %t", "RTV Ended");
   246 			return Plugin_Continue;
   247 		}
   248 		
   249 		if (g_RTVStarted)
   250 		{
   251 			PrintToChat(client, "[SM] %t", "RTV Started");
   252 			return Plugin_Continue;
   253 		}
   254 		
   255 		if (GetClientCount(true) < GetConVarInt(g_Cvar_MinPlayers) && g_Votes == 0) // Should we keep checking g_Votes here?
   256 		{
   257 			PrintToChat(client, "[SM] %t", "Minimal Players Not Met");
   258 			return Plugin_Continue;			
   259 		}
   260 		
   261 		if (g_Voted[client])
   262 		{
   263 			PrintToChat(client, "[SM] %t", "Already Voted");
   264 			return Plugin_Continue;
   265 		}	
   266 		
   267 		new String:name[64];
   268 		GetClientName(client, name, sizeof(name));
   269 		
   270 		g_Votes++;
   271 		g_Voted[client] = true;
   272 		
   273 		PrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded);
   274 		
   275 		if (g_Votes >= g_VotesNeeded)
   276 		{
   277 			CreateTimer(2.0, Timer_StartRTV, TIMER_FLAG_NO_MAPCHANGE);
   278 		}
   279 	}
   280 	else if (GetConVarBool(g_Cvar_Nominate) && strcmp(text[startidx], "nominate", false) == 0)
   281 	{
   282 		if (g_RTVEnded)
   283 		{
   284 			PrintToChat(client, "[SM] %t", "RTV Ended");
   285 			return Plugin_Continue;
   286 		}		
   287 		
   288 		if (g_RTVStarted)
   289 		{
   290 			PrintToChat(client, "[SM] %t", "RTV Started");
   291 			return Plugin_Continue;
   292 		}
   293 		
   294 		if (g_Nominated[client])
   295 		{
   296 			PrintToChat(client, "[SM] %t", "Already Nominated");
   297 			return Plugin_Continue;
   298 		}
   299 		
   300 		if (GetArraySize(g_RTVMapList) >= GetConVarInt(g_Cvar_Maps))
   301 		{
   302 			PrintToChat(client, "[SM] %t", "Max Nominations");
   303 			return Plugin_Continue;			
   304 		}
   305 		
   306 		DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);		
   307 	}
   308 	
   309 	return Plugin_Continue;	
   310 }
   311 
   312 public Action:Timer_DelayRTV(Handle:timer)
   313 {
   314 	g_RTVAllowed = true;
   315 }
   316 
   317 public Action:Timer_StartRTV(Handle:timer)
   318 {
   319 	if (timer == g_RetryTimer)
   320 	{
   321 		g_RetryTimer = INVALID_HANDLE;
   322 	}
   323 	
   324 	if (g_RetryTimer != INVALID_HANDLE)
   325 	{
   326 		return;
   327 	}
   328 
   329 	g_RTVStarted = true;
   330 
   331 	if (IsVoteInProgress())
   332 	{
   333 		// Can't start a vote, try again in 5 seconds.
   334 		g_RetryTimer = CreateTimer(5.0, Timer_StartRTV, TIMER_FLAG_NO_MAPCHANGE);
   335 		return;
   336 	}			
   337 	
   338 	PrintToChatAll("[SM] %t", "RTV Vote Ready");
   339 		
   340 	new Handle:MapVoteMenu = CreateMenu(Handler_MapMapVoteMenu, MenuAction:MENU_ACTIONS_ALL);
   341 	SetMenuTitle(MapVoteMenu, "Rock The Vote");
   342 	
   343 	new Handle:tempMaps  = CreateArray(33);
   344 	decl String:map[32];
   345 	for (new i = 0; i < GetArraySize(g_MapList); i++)
   346 	{
   347 		GetArrayString(g_MapList, i, map, sizeof(map));
   348 		PushArrayString(tempMaps, map);		
   349 	}
   350 	
   351 	// We assume that g_RTVMapList is within the correct limits, based on the logic for nominations
   352 	for (new i = 0; i < GetArraySize(g_RTVMapList); i++)
   353 	{
   354 		GetArrayString(g_RTVMapList, i, map, sizeof(map));
   355 		AddMenuItem(MapVoteMenu, map, map);
   356 		
   357 		for (new j = 0; j < GetArraySize(tempMaps); j++)
   358 		{
   359 			decl String:temp[32];
   360 			GetArrayString(tempMaps, j, temp, sizeof(temp));
   361 			if (strcmp(map, temp) == 0)
   362 			{
   363 				RemoveFromArray(tempMaps, j);
   364 				break;
   365 			}
   366 		}
   367 	}
   368 	
   369 	new limit = GetConVarInt(g_Cvar_Maps) - GetArraySize(g_RTVMapList);
   370 	if (limit > GetArraySize(tempMaps))
   371 	{
   372 		limit = GetArraySize(tempMaps);
   373 	}
   374 
   375 	for (new i = 0; i < limit; i++)
   376 	{
   377 		new b = GetRandomInt(0, GetArraySize(tempMaps) - 1);
   378 		GetArrayString(tempMaps, b, map, sizeof(map));		
   379 		PushArrayString(g_RTVMapList, map);
   380 		AddMenuItem(MapVoteMenu, map, map);			
   381 		RemoveFromArray(tempMaps, b);
   382 	}	
   383 	
   384 	AddMenuItem(MapVoteMenu, "Don't Change", "Don't Change");
   385 		
   386 	SetMenuExitButton(MapVoteMenu, false);
   387 	VoteMenuToAll(MapVoteMenu, 20);
   388 		
   389 	LogMessage("[SM] Rockthevote was successfully started.");
   390 }
   391 
   392 public Action:Timer_ChangeMap(Handle:hTimer, Handle:dp)
   393 {
   394 	new String:map[65];
   395 	
   396 	ResetPack(dp);
   397 	ReadPackString(dp, map, sizeof(map));
   398 	
   399 	ServerCommand("changelevel \"%s\"", map);
   400 	
   401 	return Plugin_Stop;
   402 }
   403 
   404 public Handler_MapMapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
   405 {
   406 	switch (action)
   407 	{
   408 		case MenuAction_End:
   409 		{
   410 			CloseHandle(menu);
   411 		}
   412 		
   413 		case MenuAction_Display:
   414 		{
   415 	 		decl String:oldTitle[255], String:buffer[255];
   416 			GetMenuTitle(menu, oldTitle, sizeof(oldTitle));
   417 			Format(buffer, sizeof(buffer), "%T", oldTitle, param1);
   418 
   419 			new Handle:panel = Handle:param2;
   420 			SetPanelTitle(panel, buffer);
   421 		}
   422 		
   423 		case MenuAction_DisplayItem:
   424 		{
   425 			if (GetMenuItemCount(menu) - 1 == param2)
   426 			{
   427 				decl String:buffer[255];
   428 				Format(buffer, sizeof(buffer), "%T", "Don't Change", param1);
   429 				return RedrawMenuItem(buffer);
   430 			}
   431 		}		
   432 
   433 		// Why am I commented out? Because BAIL hasn't decided yet if
   434 		// vote notification will be built into the Vote API.
   435 		/*case MenuAction_Select:
   436 		{
   437 			decl String:Name[32], String:Map[32];
   438 			GetClientName(param1, Name, sizeof(Name));
   439 			GetMenuItem(menu, param2, Map, sizeof(Map));
   440 
   441 			PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
   442 		}*/
   443 		
   444 		case MenuAction_VoteCancel:
   445 		{
   446 			if (param1 == VoteCancel_NoVotes)
   447 			{
   448 				PrintToChatAll("[SM] %t", "No Votes");		
   449 			}
   450 		}
   451 
   452 		case MenuAction_VoteEnd:
   453 		{
   454 			new String:map[64];
   455 			
   456 			GetMenuItem(menu, param1, map, sizeof(map));
   457 			
   458 			if (GetMenuItemCount(menu) - 1 == param1) // This should always match the "Keep Current" option
   459 			{
   460 				PrintToChatAll("[SM] %t", "Current Map Stays");
   461 				LogMessage("[SM] Rockthevote has ended, current map kept.");
   462 			}
   463 			else
   464 			{
   465 				PrintToChatAll("[SM] %t", "Changing Maps", map);
   466 				LogMessage("[SM] Rockthevote has ended, changing to map %s.", map);
   467 				new Handle:dp;
   468 				CreateDataTimer(5.0, Timer_ChangeMap, dp);
   469 				WritePackString(dp, map);
   470 			}
   471 			
   472 			g_RTVEnded = true;
   473 		}
   474 	}
   475 	
   476 	return 0;
   477 }
   478 
   479 public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
   480 {
   481 	switch (action)
   482 	{
   483 		case MenuAction_Select:
   484 		{
   485 			if (GetArraySize(g_RTVMapList) >= GetConVarInt(g_Cvar_Maps)) 
   486 			{
   487 				PrintToChat(param1, "[SM] %t", "Max Nominations");
   488 				return;	
   489 			}
   490 			
   491 			decl String:map[64], String:name[64];
   492 			GetMenuItem(menu, param2, map, sizeof(map));
   493 			
   494 			if (IsStringInArray(g_RTVMapList, map))
   495 			{
   496 				PrintToChat(param1, "[SM] %t", "Map Already Nominated");
   497 				return;
   498 			}
   499 			
   500 			GetClientName(param1, name, 64);
   501 
   502 			PushArrayString(g_RTVMapList, map);
   503 			RemoveMenuItem(menu, param2);
   504 			
   505 			g_Nominated[param1] = true;
   506 			
   507 			PrintToChatAll("[SM] %t", "Map Nominated", name, map);
   508 		}
   509 	}
   510 }
   511 
   512 BuildMapMenu()
   513 {
   514 	if (g_MapMenu != INVALID_HANDLE)
   515 	{
   516 		CloseHandle(g_MapMenu);
   517 		g_MapMenu = INVALID_HANDLE;
   518 	}
   519 	
   520 	g_MapMenu = CreateMenu(Handler_MapSelectMenu);
   521 	SetMenuTitle(g_MapMenu, "%t", "Nominate Title");
   522 
   523 	decl String:map[64];		
   524 	for (new i = 0; i < GetArraySize(g_MapList); i++)
   525 	{
   526 		GetArrayString(g_MapList, i, map, sizeof(map));
   527 		AddMenuItem(g_MapMenu, map, map);
   528 	}
   529 	
   530 	SetMenuExitButton(g_MapMenu, false);
   531 }
   532 
   533 LoadMaps()
   534 {
   535 	new bool:fileFound;
   536 
   537 	decl String:mapPath[256], String:mapFile[64];
   538 	GetConVarString(g_Cvar_File, mapFile, 64);
   539 	BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile);
   540 	fileFound = FileExists(mapPath);
   541 	if (!fileFound)
   542 	{
   543 		new Handle:mapCycleFile = FindConVar("mapcyclefile");
   544 		GetConVarString(mapCycleFile, mapPath, sizeof(mapPath));
   545 		fileFound = FileExists(mapPath);
   546 	}
   547 	
   548 	if (!fileFound)
   549 	{
   550 		LogError("Unable to locate sm_rtv_file or mapcyclefile, no maps loaded.");
   551 		
   552 		if (g_MapList != INVALID_HANDLE)
   553 		{
   554 			ClearArray(g_MapList);
   555 		}
   556 		
   557 		return 0;		
   558 	}
   559 
   560 	// If the file hasn't changed, there's no reason to reload
   561 	// all of the maps.
   562 	new fileTime =  GetFileTime(mapPath, FileTime_LastChange);
   563 	if (g_mapFileTime == fileTime)
   564 	{
   565 		return GetArraySize(g_MapList);
   566 	}
   567 	
   568 	g_mapFileTime = fileTime;
   569 	
   570 	// Reset the array
   571 	if (g_MapList != INVALID_HANDLE)
   572 	{
   573 		ClearArray(g_MapList);
   574 	}
   575 
   576 	LogMessage("[SM] Loading RTV map file [%s]", mapPath);
   577 
   578 	decl String:currentMap[32];
   579 	GetCurrentMap(currentMap, sizeof(currentMap));
   580 
   581 	new Handle:file = OpenFile(mapPath, "rt");
   582 	if (file == INVALID_HANDLE)
   583 	{
   584 		LogError("[SM] Could not open file: %s", mapPath);
   585 		return 0;
   586 	}
   587 
   588 	decl String:buffer[64], len;
   589 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   590 	{
   591 		TrimString(buffer);
   592 
   593 		if ((len = StrContains(buffer, ".bsp", false)) != -1)
   594 		{
   595 			buffer[len] = '\0';
   596 		}
   597 
   598 		if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer)
   599 			|| strcmp(currentMap, buffer, false) == 0)
   600 		{
   601 			continue;
   602 		}
   603 
   604 		PushArrayString(g_MapList, buffer);
   605 	}
   606 
   607 	CloseHandle(file);
   608 	return GetArraySize(g_MapList);
   609 }