plugins/mapmanager/votes.sp
author David Anderson <dvander@alliedmods.net>
Sat May 17 05:12:52 2008 +0000 (2008-05-17)
changeset 2056 f1fcde46ac7c
parent 1952 b7aa2b626807
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 Map Manager Plugin
ds@1952
     5
 * Contains vote callbacks
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
// sm_votemap
ds@1952
    35
public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
ds@1952
    36
{
ds@1952
    37
	if (action == MenuAction_End)
ds@1952
    38
	{
ds@1952
    39
		CloseHandle(menu);
ds@1952
    40
	}
ds@1952
    41
	else if (action == MenuAction_Display)
ds@1952
    42
	{
ds@1952
    43
	 	decl String:title[64];
ds@1952
    44
		GetMenuTitle(menu, title, sizeof(title));
ds@1952
    45
			
ds@1952
    46
		decl String:map[64];
ds@1952
    47
		GetMenuItem(menu, 1, map, sizeof(map));
ds@1952
    48
		
ds@1952
    49
 		decl String:buffer[255];
ds@1952
    50
		Format(buffer, sizeof(buffer), "%T", title, param1, map);
ds@1952
    51
ds@1952
    52
		new Handle:panel = Handle:param2;
ds@1952
    53
		SetPanelTitle(panel, buffer);
ds@1952
    54
	}
ds@1952
    55
	else if (action == MenuAction_DisplayItem)
ds@1952
    56
	{
ds@1952
    57
		decl String:display[64];
ds@1952
    58
		GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
ds@1952
    59
	 
ds@1952
    60
	 	if (strcmp(display, "No") == 0 || strcmp(display, "Yes") == 0)
ds@1952
    61
	 	{
ds@1952
    62
			decl String:buffer[255];
ds@1952
    63
			Format(buffer, sizeof(buffer), "%T", display, param1);
ds@1952
    64
ds@1952
    65
			return RedrawMenuItem(buffer);
ds@1952
    66
		}
ds@1952
    67
	}
ds@1952
    68
	else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
ds@1952
    69
	{
ds@1952
    70
		PrintToChatAll("[SM] %t", "No Votes Cast");
ds@1952
    71
	}	
ds@1952
    72
	else if (action == MenuAction_VoteEnd)
ds@1952
    73
	{
ds@1952
    74
		decl String:item[64], String:display[64];
ds@1952
    75
		new Float:percent, Float:limit, votes, totalVotes;
ds@1952
    76
ds@1952
    77
		GetMenuVoteInfo(param2, votes, totalVotes);
ds@1952
    78
		GetMenuItem(menu, param1, item, sizeof(item), _, display, sizeof(display));
ds@1952
    79
		
ds@1952
    80
		if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
ds@1952
    81
		{
ds@1952
    82
			votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
ds@1952
    83
		}
ds@1952
    84
		
ds@1952
    85
		percent = GetVotePercent(votes, totalVotes);
ds@1952
    86
		
ds@1952
    87
		limit = GetConVarFloat(g_Cvar_VoteMap);
ds@1952
    88
		
ds@1952
    89
		// A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
ds@1952
    90
		if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
ds@1952
    91
		{
ds@1952
    92
			LogAction(-1, -1, "Vote failed.");
ds@1952
    93
			PrintToChatAll("[SM] %t", "Votemap Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
ds@1952
    94
		}
ds@1952
    95
		else
ds@1952
    96
		{
ds@1952
    97
			PrintToChatAll("[SM] %t", "Votemap Successful", RoundToNearest(100.0*percent), totalVotes);
ds@1952
    98
ds@1952
    99
			if (g_VoteMapInUse && IsClientInGame(g_VoteMapInUse))
ds@1952
   100
			{
ds@1952
   101
				DisplayAcceptVoteMenu(item);
ds@1952
   102
			}
ds@1952
   103
			else
ds@1952
   104
			{
ds@1952
   105
				LogAction(-1, -1, "Changing map to %s due to vote.", item);
ds@1952
   106
				PrintToChatAll("[SM] %t", "Changing map", item);
ds@1952
   107
				SetMapChange(0, item, g_Client_Data[g_VoteMapInUse][0])
ds@1952
   108
			}	
ds@1952
   109
		}
ds@1952
   110
	}
ds@1952
   111
	
ds@1952
   112
	return 0;
ds@1952
   113
}
ds@1952
   114
ds@1952
   115
public Handler_MapMapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
ds@1952
   116
{
ds@1952
   117
	switch (action)
ds@1952
   118
	{
ds@1952
   119
		case MenuAction_End:
ds@1952
   120
		{
ds@1952
   121
			CloseHandle(menu);
ds@1952
   122
		}
ds@1952
   123
		
ds@1952
   124
		case MenuAction_Display:
ds@1952
   125
		{
ds@1952
   126
	 		decl String:oldTitle[255], String:buffer[255];
ds@1952
   127
			GetMenuTitle(menu, oldTitle, sizeof(oldTitle));
ds@1952
   128
			Format(buffer, sizeof(buffer), "%T", oldTitle, param1);
ds@1952
   129
ds@1952
   130
			new Handle:panel = Handle:param2;
ds@1952
   131
			SetPanelTitle(panel, buffer);
ds@1952
   132
		}
ds@1952
   133
		
ds@1952
   134
		case MenuAction_DisplayItem:
ds@1952
   135
		{
ds@1952
   136
			if (GetMenuItemCount(menu) - 1 == param2)
ds@1952
   137
			{
ds@1952
   138
				decl String:buffer[255];
ds@1952
   139
				Format(buffer, sizeof(buffer), "%T", "Don't Change", param1);
ds@1952
   140
				return RedrawMenuItem(buffer);
ds@1952
   141
			}
ds@1952
   142
		}		
ds@1952
   143
ds@1952
   144
		// Why am I commented out? Because BAIL hasn't decided yet if
ds@1952
   145
		// vote notification will be built into the Vote API.
ds@1952
   146
		/*case MenuAction_Select:
ds@1952
   147
		{
ds@1952
   148
			decl String:Name[32], String:Map[32];
ds@1952
   149
			GetClientName(param1, Name, sizeof(Name));
ds@1952
   150
			GetMenuItem(menu, param2, Map, sizeof(Map));
ds@1952
   151
ds@1952
   152
			PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
ds@1952
   153
		}*/
ds@1952
   154
		
ds@1952
   155
		case MenuAction_VoteCancel:
ds@1952
   156
		{
ds@1952
   157
			if (param1 == VoteCancel_NoVotes)
ds@1952
   158
			{
ds@1952
   159
				PrintToChatAll("[SM] %t", "No Votes");
ds@1952
   160
				g_RTVEnded = true;
ds@1952
   161
			}
ds@1952
   162
		}
ds@1952
   163
ds@1952
   164
		case MenuAction_VoteEnd:
ds@1952
   165
		{
ds@1952
   166
			new String:map[64];
ds@1952
   167
			
ds@1952
   168
			GetMenuItem(menu, param1, map, sizeof(map));
ds@1952
   169
			
ds@1952
   170
			if (GetMenuItemCount(menu) - 1 == param1) // This should always match the "Keep Current" option
ds@1952
   171
			{
ds@1952
   172
				PrintToChatAll("[SM] %t", "Current Map Stays");
ds@1952
   173
				LogMessage("[SM] Rockthevote has ended, current map kept.");
ds@1952
   174
			}
ds@1952
   175
			else
ds@1952
   176
			{
ds@1952
   177
				PrintToChatAll("[SM] %t", "Changing Maps", map);
ds@1952
   178
				LogMessage("[SM] Rockthevote has ended, changing to map %s.", map);
ds@1952
   179
				new Handle:dp;
ds@1952
   180
				CreateDataTimer(5.0, Timer_ChangeMap, dp);
ds@1952
   181
				WritePackString(dp, map);
ds@1952
   182
			}
ds@1952
   183
			
ds@1952
   184
			g_RTVEnded = true;
ds@1952
   185
		}
ds@1952
   186
	}
ds@1952
   187
	
ds@1952
   188
	return 0;
ds@1952
   189
}
ds@1952
   190
ds@1952
   191
public Handler_MapVoteMenu(Handle:menu, MenuAction:action, param1, param2)
ds@1952
   192
{
ds@1952
   193
	switch (action)
ds@1952
   194
	{
ds@1952
   195
		case MenuAction_End:
ds@1952
   196
		{
ds@1952
   197
			g_VoteMenu = INVALID_HANDLE;
ds@1952
   198
			CloseHandle(menu);
ds@1952
   199
		}
ds@1952
   200
		
ds@1952
   201
		case MenuAction_Display:
ds@1952
   202
		{
ds@1952
   203
	 		decl String:buffer[255];
ds@1952
   204
			Format(buffer, sizeof(buffer), "%T", "Vote Nextmap", param1);
ds@1952
   205
ds@1952
   206
			new Handle:panel = Handle:param2;
ds@1952
   207
			SetPanelTitle(panel, buffer);
ds@1952
   208
		}		
ds@1952
   209
		
ds@1952
   210
		case MenuAction_DisplayItem:
ds@1952
   211
		{
ds@1952
   212
			if (GetMenuItemCount(menu) - 1 == param2)
ds@1952
   213
			{
ds@1952
   214
				decl String:map[64], String:buffer[255];
ds@1952
   215
				GetMenuItem(menu, param2, map, sizeof(map));
ds@1952
   216
				if (strcmp(map, VOTE_EXTEND, false) == 0)
ds@1952
   217
				{
ds@1952
   218
					Format(buffer, sizeof(buffer), "%T", "Extend Map", param1);
ds@1952
   219
					return RedrawMenuItem(buffer);
ds@1952
   220
				}
ds@1952
   221
			}
ds@1952
   222
		}		
ds@1952
   223
ds@1952
   224
		// Why am I commented out? Because BAIL hasn't decided yet if
ds@1952
   225
		// vote notification will be built into the Vote API.
ds@1952
   226
		/*case MenuAction_Select:
ds@1952
   227
		{
ds@1952
   228
			decl String:Name[32], String:Map[32];
ds@1952
   229
			GetClientName(param1, Name, sizeof(Name));
ds@1952
   230
			GetMenuItem(menu, param2, Map, sizeof(Map));
ds@1952
   231
ds@1952
   232
			PrintToChatAll("[SM] %s has voted for map '%s'", Name, Map);
ds@1952
   233
		}*/
ds@1952
   234
		
ds@1952
   235
		case MenuAction_VoteCancel:
ds@1952
   236
		{
ds@1952
   237
			// If we receive 0 votes, pick at random.
ds@1952
   238
			if (param1 == VoteCancel_NoVotes && GetConVarBool(g_Cvar_NoVoteMode))
ds@1952
   239
			{
ds@1952
   240
				new count = GetMenuItemCount(menu);
ds@1952
   241
				new item = GetRandomInt(0, count - 1);
ds@1952
   242
				decl String:map[32];
ds@1952
   243
				GetMenuItem(menu, item, map, sizeof(map));
ds@1952
   244
				
ds@1952
   245
				while (strcmp(map, VOTE_EXTEND, false) == 0)
ds@1952
   246
				{
ds@1952
   247
					item = GetRandomInt(0, count - 1);
ds@1952
   248
					GetMenuItem(menu, item, map, sizeof(map));
ds@1952
   249
				}
ds@1952
   250
				
ds@1952
   251
				SetNextMap(map);			
ds@1952
   252
			}
ds@1952
   253
			else
ds@1952
   254
			{
ds@1952
   255
				// We were actually cancelled. What should we do?
ds@1952
   256
			}
ds@1952
   257
		}
ds@1952
   258
ds@1952
   259
		case MenuAction_VoteEnd:
ds@1952
   260
		{
ds@1952
   261
			decl String:map[32];
ds@1952
   262
			GetMenuItem(menu, param1, map, sizeof(map));
ds@1952
   263
ds@1952
   264
			if (strcmp(map, VOTE_EXTEND, false) == 0)
ds@1952
   265
			{
ds@1952
   266
				new time;
ds@1952
   267
				if (GetMapTimeLimit(time))
ds@1952
   268
				{
ds@1952
   269
					if (time > 0 && time < GetConVarInt(g_Cvar_ExtendTimeMax))
ds@1952
   270
					{
ds@1952
   271
						ExtendMapTimeLimit(GetConVarInt(g_Cvar_ExtendTimeStep)*60);						
ds@1952
   272
					}
ds@1952
   273
				}
ds@1952
   274
				
ds@1952
   275
				if (g_Cvar_Winlimit != INVALID_HANDLE)
ds@1952
   276
				{
ds@1952
   277
					new winlimit = GetConVarInt(g_Cvar_Winlimit);
ds@1952
   278
					if (winlimit && winlimit < GetConVarInt(g_Cvar_ExtendRoundMax))
ds@1952
   279
					{
ds@1952
   280
						SetConVarInt(g_Cvar_Winlimit, winlimit + GetConVarInt(g_Cvar_ExtendRoundStep));
ds@1952
   281
					}					
ds@1952
   282
				}
ds@1952
   283
				
ds@1952
   284
				if (g_Cvar_Maxrounds != INVALID_HANDLE)
ds@1952
   285
				{
ds@1952
   286
					new maxrounds = GetConVarInt(g_Cvar_Maxrounds);
ds@1952
   287
					if (maxrounds && maxrounds < GetConVarInt(g_Cvar_ExtendRoundMax))
ds@1952
   288
					{
ds@1952
   289
						SetConVarInt(g_Cvar_Maxrounds, maxrounds + GetConVarInt(g_Cvar_ExtendRoundStep));
ds@1952
   290
					}
ds@1952
   291
				}
ds@1952
   292
				
ds@1952
   293
				if (g_Cvar_Fraglimit != INVALID_HANDLE)
ds@1952
   294
				{
ds@1952
   295
					new fraglimit = GetConVarInt(g_Cvar_Fraglimit);
ds@1952
   296
					if (fraglimit && fraglimit < GetConVarInt(g_Cvar_ExtendFragMax))
ds@1952
   297
					{
ds@1952
   298
						SetConVarInt(g_Cvar_Fraglimit, fraglimit + GetConVarInt(g_Cvar_ExtendFragStep));						
ds@1952
   299
					}
ds@1952
   300
				}
ds@1952
   301
ds@1952
   302
				PrintToChatAll("[SM] %t", "Current Map Extended");
ds@1952
   303
				LogMessage("Voting for next map has finished. The current map has been extended.");
ds@1952
   304
				
ds@1952
   305
				// We extended, so we'll have to vote again.
ds@1952
   306
				g_HasVoteStarted = false;
ds@1952
   307
				CreateNextVote();
ds@1952
   308
				SetupTimeleftTimer();
ds@1952
   309
			}
ds@1952
   310
			else
ds@1952
   311
			{
ds@1952
   312
				SetNextMap(map);
ds@1952
   313
			}
ds@1952
   314
		}
ds@1952
   315
	}
ds@1952
   316
	
ds@1952
   317
	return 0;
ds@1952
   318
}