plugins/nominations.sp
author Asher Baker <asherkin@limetech.org>
Sat Apr 14 01:12:24 2012 +0100 (2012-04-14)
changeset 3477 8762e8fcbc50
parent 3187 4ee1c5e7bbc9
child 4060 b4484bd36c22
permissions -rw-r--r--
Backed out changeset: 387b27fb575b (bug 5227)
pred@2133
     1
/**
pred@2133
     2
 * vim: set ts=4 :
pred@2133
     3
 * =============================================================================
pred@2133
     4
 * SourceMod Rock The Vote Plugin
pred@2133
     5
 * Creates a map vote when the required number of players have requested one.
pred@2133
     6
 *
pred@2133
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
pred@2133
     8
 * =============================================================================
pred@2133
     9
 *
pred@2133
    10
 * This program is free software; you can redistribute it and/or modify it under
pred@2133
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
pred@2133
    12
 * Free Software Foundation.
pred@2133
    13
 * 
pred@2133
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
pred@2133
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
pred@2133
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
pred@2133
    17
 * details.
pred@2133
    18
 *
pred@2133
    19
 * You should have received a copy of the GNU General Public License along with
pred@2133
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
pred@2133
    21
 *
pred@2133
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
pred@2133
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
pred@2133
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
pred@2133
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
pred@2133
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
pred@2133
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
pred@2133
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
pred@2133
    29
 * or <http://www.sourcemod.net/license.php>.
pred@2133
    30
 *
pred@2133
    31
 * Version: $Id$
pred@2133
    32
 */
pred@2133
    33
pred@2133
    34
#include <sourcemod>
pred@2133
    35
#include <mapchooser>
pred@2133
    36
pred@2133
    37
#pragma semicolon 1
pred@2133
    38
pred@2133
    39
public Plugin:myinfo =
pred@2133
    40
{
pred@2133
    41
	name = "Map Nominations",
pred@2133
    42
	author = "AlliedModders LLC",
pred@2133
    43
	description = "Provides Map Nominations",
pred@2133
    44
	version = SOURCEMOD_VERSION,
pred@2133
    45
	url = "http://www.sourcemod.net/"
pred@2133
    46
};
pred@2133
    47
pred@2133
    48
new Handle:g_Cvar_ExcludeOld = INVALID_HANDLE;
pred@2133
    49
new Handle:g_Cvar_ExcludeCurrent = INVALID_HANDLE;
pred@2133
    50
pred@2133
    51
new Handle:g_MapList = INVALID_HANDLE;
pred@2133
    52
new Handle:g_MapMenu = INVALID_HANDLE;
pred@2133
    53
new g_mapFileSerial = -1;
pred@2133
    54
pred@2350
    55
#define MAPSTATUS_ENABLED (1<<0)
pred@2350
    56
#define MAPSTATUS_DISABLED (1<<1)
pred@2350
    57
#define MAPSTATUS_EXCLUDE_CURRENT (1<<2)
pred@2350
    58
#define MAPSTATUS_EXCLUDE_PREVIOUS (1<<3)
pred@2350
    59
#define MAPSTATUS_EXCLUDE_NOMINATED (1<<4)
pred@2350
    60
pred@2350
    61
new Handle:g_mapTrie;
pred@2350
    62
pred@2133
    63
public OnPluginStart()
pred@2133
    64
{
pred@2133
    65
	LoadTranslations("common.phrases");
pred@2133
    66
	LoadTranslations("nominations.phrases");
pred@2133
    67
	
pred@2133
    68
	new arraySize = ByteCountToCells(33);	
pred@2133
    69
	g_MapList = CreateArray(arraySize);
pred@2133
    70
	
pred@2133
    71
	g_Cvar_ExcludeOld = CreateConVar("sm_nominate_excludeold", "1", "Specifies if the current map should be excluded from the Nominations list", 0, true, 0.00, true, 1.0);
pred@2133
    72
	g_Cvar_ExcludeCurrent = CreateConVar("sm_nominate_excludecurrent", "1", "Specifies if the MapChooser excluded maps should also be excluded from Nominations", 0, true, 0.00, true, 1.0);
pred@2133
    73
	
pred@2133
    74
	RegConsoleCmd("say", Command_Say);
pred@2133
    75
	RegConsoleCmd("say_team", Command_Say);
pred@2133
    76
	
pred@2133
    77
	RegConsoleCmd("sm_nominate", Command_Nominate);
pred@2133
    78
	
pred@2133
    79
	RegAdminCmd("sm_nominate_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate_addmap <mapname> - Forces a map to be on the next mapvote.");
pred@2350
    80
	
pred@2350
    81
	g_mapTrie = CreateTrie();
pred@2133
    82
}
pred@2133
    83
pred@2133
    84
public OnConfigsExecuted()
pred@2133
    85
{
pred@2133
    86
	if (ReadMapList(g_MapList,
pred@2133
    87
					g_mapFileSerial,
pred@2133
    88
					"nominations",
pred@2133
    89
					MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER)
pred@2133
    90
		== INVALID_HANDLE)
pred@2133
    91
	{
pred@2133
    92
		if (g_mapFileSerial == -1)
pred@2133
    93
		{
pred@2133
    94
			SetFailState("Unable to create a valid map list.");
pred@2133
    95
		}
pred@2133
    96
	}
pred@2133
    97
	
pred@2133
    98
	BuildMapMenu();
pred@2133
    99
}
pred@2133
   100
pred@2350
   101
public OnNominationRemoved(const String:map[], owner)
pred@2133
   102
{
pred@2350
   103
	new status;
pred@2350
   104
	
pred@2350
   105
	/* Is the map in our list? */
pred@2350
   106
	if (!GetTrieValue(g_mapTrie, map, status))
pred@2350
   107
	{
pred@2350
   108
		return;	
pred@2350
   109
	}
pred@2350
   110
	
pred@2350
   111
	/* Was the map disabled due to being nominated */
pred@2350
   112
	if ((status & MAPSTATUS_EXCLUDE_NOMINATED) != MAPSTATUS_EXCLUDE_NOMINATED)
pred@2350
   113
	{
pred@2350
   114
		return;
pred@2350
   115
	}
pred@2350
   116
	
pred@2350
   117
	SetTrieValue(g_mapTrie, map, MAPSTATUS_ENABLED);	
pred@2133
   118
}
pred@2133
   119
pred@2133
   120
public Action:Command_Addmap(client, args)
pred@2133
   121
{
pred@2133
   122
	if (args < 1)
pred@2133
   123
	{
pred@2133
   124
		ReplyToCommand(client, "[SM] Usage: sm_nominate_addmap <mapname>");
pred@2133
   125
		return Plugin_Handled;
pred@2133
   126
	}
pred@2133
   127
	
pred@2133
   128
	decl String:mapname[64];
pred@2133
   129
	GetCmdArg(1, mapname, sizeof(mapname));
pred@2133
   130
pred@2350
   131
	
pred@2350
   132
	new status;
pred@2350
   133
	if (!GetTrieValue(g_mapTrie, mapname, status))
pred@2133
   134
	{
pred@2133
   135
		ReplyToCommand(client, "%t", "Map was not found", mapname);
pred@2350
   136
		return Plugin_Handled;		
pred@2133
   137
	}
pred@2133
   138
	
pred@2133
   139
	new NominateResult:result = NominateMap(mapname, true, 0);
pred@2133
   140
	
pred@2133
   141
	if (result > Nominate_Replaced)
pred@2133
   142
	{
pred@2133
   143
		/* We assume already in vote is the casue because the maplist does a Map Validity check and we forced, so it can't be full */
pred@2133
   144
		ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
pred@2133
   145
		
pred@2133
   146
		return Plugin_Handled;	
pred@2133
   147
	}
pred@2133
   148
	
pred@2350
   149
	
pred@2350
   150
	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
pred@2350
   151
pred@2133
   152
	
pred@2133
   153
	ReplyToCommand(client, "%t", "Map Inserted", mapname);
pred@2133
   154
	LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
pred@2133
   155
pred@2133
   156
	return Plugin_Handled;		
pred@2133
   157
}
pred@2133
   158
pred@2133
   159
public Action:Command_Say(client, args)
pred@2133
   160
{
pred@2133
   161
	if (!client)
pred@2133
   162
	{
pred@2133
   163
		return Plugin_Continue;
pred@2133
   164
	}
pred@2133
   165
pred@2133
   166
	decl String:text[192];
pred@2133
   167
	if (!GetCmdArgString(text, sizeof(text)))
pred@2133
   168
	{
pred@2133
   169
		return Plugin_Continue;
pred@2133
   170
	}
pred@2133
   171
	
pred@2133
   172
	new startidx = 0;
pred@2133
   173
	if(text[strlen(text)-1] == '"')
pred@2133
   174
	{
pred@2133
   175
		text[strlen(text)-1] = '\0';
pred@2133
   176
		startidx = 1;
pred@2133
   177
	}
pred@2133
   178
	
pred@2133
   179
	new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
pred@2133
   180
	
pred@2133
   181
	if (strcmp(text[startidx], "nominate", false) == 0)
pred@2133
   182
	{
pred@2133
   183
		AttemptNominate(client);
pred@2133
   184
	}
pred@2133
   185
	
pred@2133
   186
	SetCmdReplySource(old);
pred@2133
   187
	
pred@2133
   188
	return Plugin_Continue;	
pred@2133
   189
}
pred@2133
   190
pred@2133
   191
public Action:Command_Nominate(client, args)
pred@2133
   192
{
pred@2133
   193
	if (!client)
pred@2133
   194
	{
erik@3187
   195
		return Plugin_Handled;
pred@2133
   196
	}
pred@2133
   197
	
pred@2133
   198
	if (args == 0)
pred@2133
   199
	{
pred@2133
   200
		AttemptNominate(client);
erik@3187
   201
		return Plugin_Handled;
pred@2133
   202
	}
pred@2133
   203
	
pred@2133
   204
	decl String:mapname[64];
pred@2133
   205
	GetCmdArg(1, mapname, sizeof(mapname));
pred@2133
   206
	
pred@2350
   207
	new status;
pred@2350
   208
	if (!GetTrieValue(g_mapTrie, mapname, status))
pred@2133
   209
	{
pred@2350
   210
		ReplyToCommand(client, "%t", "Map was not found", mapname);
pred@2350
   211
		return Plugin_Handled;		
pred@2133
   212
	}
pred@2133
   213
	
pred@2350
   214
	if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
pred@2350
   215
	{
pred@2350
   216
		if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
pred@2350
   217
		{
pred@2350
   218
			ReplyToCommand(client, "[SM] %t", "Can't Nominate Current Map");
pred@2350
   219
		}
pred@2350
   220
		
pred@2350
   221
		if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
pred@2350
   222
		{
pred@2350
   223
			ReplyToCommand(client, "[SM] %t", "Map in Exclude List");
pred@2350
   224
		}
pred@2350
   225
		
pred@2350
   226
		if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
pred@2350
   227
		{
pred@2350
   228
			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
pred@2350
   229
		}
pred@2350
   230
		
pred@2350
   231
		return Plugin_Handled;
pred@2350
   232
	}
pred@2350
   233
	
pred@2350
   234
	new NominateResult:result = NominateMap(mapname, false, client);
pred@2350
   235
	
pred@2350
   236
	if (result > Nominate_Replaced)
pred@2350
   237
	{
pred@2350
   238
		if (result == Nominate_AlreadyInVote)
pred@2350
   239
		{
pred@2350
   240
			ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
pred@2350
   241
		}
pred@2350
   242
		else
pred@2350
   243
		{
pred@2350
   244
			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
pred@2350
   245
		}
pred@2350
   246
		
pred@2350
   247
		return Plugin_Handled;	
pred@2350
   248
	}
pred@2350
   249
	
pred@2350
   250
	/* Map was nominated! - Disable the menu item and update the trie */
pred@2350
   251
	
pred@2350
   252
	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
pred@2350
   253
	
pred@2350
   254
	decl String:name[64];
pred@2350
   255
	GetClientName(client, name, sizeof(name));
pred@2350
   256
	PrintToChatAll("[SM] %t", "Map Nominated", name, mapname);
pred@2350
   257
	
pred@2350
   258
	return Plugin_Continue;
pred@2133
   259
}
pred@2133
   260
pred@2133
   261
AttemptNominate(client)
pred@2133
   262
{
pred@2133
   263
	SetMenuTitle(g_MapMenu, "%t", "Nominate Title", client);
pred@2133
   264
	DisplayMenu(g_MapMenu, client, MENU_TIME_FOREVER);
pred@2133
   265
	
pred@2133
   266
	return;
pred@2133
   267
}
pred@2133
   268
pred@2133
   269
BuildMapMenu()
pred@2133
   270
{
pred@2133
   271
	if (g_MapMenu != INVALID_HANDLE)
pred@2133
   272
	{
pred@2133
   273
		CloseHandle(g_MapMenu);
pred@2133
   274
		g_MapMenu = INVALID_HANDLE;
pred@2133
   275
	}
pred@2133
   276
	
pred@2350
   277
	ClearTrie(g_mapTrie);
pred@2350
   278
	
pred@2350
   279
	g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
pred@2133
   280
pred@2133
   281
	decl String:map[64];
pred@2133
   282
	
fyrenmoo@2865
   283
	new Handle:excludeMaps = INVALID_HANDLE;
pred@2133
   284
	decl String:currentMap[32];
pred@2133
   285
	
pred@2133
   286
	if (GetConVarBool(g_Cvar_ExcludeOld))
pred@2133
   287
	{	
pred@2133
   288
		excludeMaps = CreateArray(ByteCountToCells(33));
pred@2133
   289
		GetExcludeMapList(excludeMaps);
pred@2133
   290
	}
pred@2133
   291
	
pred@2133
   292
	if (GetConVarBool(g_Cvar_ExcludeCurrent))
pred@2133
   293
	{
pred@2133
   294
		GetCurrentMap(currentMap, sizeof(currentMap));
pred@2133
   295
	}
pred@2133
   296
	
pred@2350
   297
		
pred@2133
   298
	for (new i = 0; i < GetArraySize(g_MapList); i++)
pred@2133
   299
	{
pred@2350
   300
		new status = MAPSTATUS_ENABLED;
pred@2133
   301
		
pred@2350
   302
		GetArrayString(g_MapList, i, map, sizeof(map));
pred@2133
   303
		
pred@2133
   304
		if (GetConVarBool(g_Cvar_ExcludeCurrent))
pred@2133
   305
		{
pred@2133
   306
			if (StrEqual(map, currentMap))
pred@2133
   307
			{
pred@2350
   308
				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
pred@2350
   309
			}
pred@2350
   310
		}
pred@2350
   311
		
pred@2350
   312
		/* Dont bother with this check if the current map check passed */
pred@2350
   313
		if (GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
pred@2350
   314
		{
pred@2350
   315
			if (FindStringInArray(excludeMaps, map) != -1)
pred@2350
   316
			{
pred@2350
   317
				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
pred@2133
   318
			}
pred@2133
   319
		}
pred@2133
   320
		
pred@2133
   321
		AddMenuItem(g_MapMenu, map, map);
pred@2350
   322
		SetTrieValue(g_mapTrie, map, status);
pred@2133
   323
	}
pred@2133
   324
	
pred@2133
   325
	SetMenuExitButton(g_MapMenu, true);
fyrenmoo@2865
   326
fyrenmoo@2865
   327
	if (excludeMaps != INVALID_HANDLE)
fyrenmoo@2865
   328
	{
fyrenmoo@2865
   329
		CloseHandle(excludeMaps);
fyrenmoo@2865
   330
	}
pred@2133
   331
}
pred@2133
   332
pred@2133
   333
public Handler_MapSelectMenu(Handle:menu, MenuAction:action, param1, param2)
pred@2133
   334
{
pred@2133
   335
	switch (action)
pred@2133
   336
	{
pred@2133
   337
		case MenuAction_Select:
pred@2133
   338
		{
pred@2133
   339
			decl String:map[64], String:name[64];
pred@2350
   340
			GetMenuItem(menu, param2, map, sizeof(map));		
pred@2133
   341
			
pred@2133
   342
			GetClientName(param1, name, 64);
pred@2133
   343
	
pred@2133
   344
			new NominateResult:result = NominateMap(map, false, param1);
pred@2133
   345
			
pred@2133
   346
			/* Don't need to check for InvalidMap because the menu did that already */
pred@2133
   347
			if (result == Nominate_AlreadyInVote)
pred@2133
   348
			{
pred@2133
   349
				PrintToChat(param1, "[SM] %t", "Map Already Nominated");
pred@2350
   350
				return 0;
pred@2133
   351
			}
pred@2133
   352
			else if (result == Nominate_VoteFull)
pred@2133
   353
			{
pred@2133
   354
				PrintToChat(param1, "[SM] %t", "Max Nominations");
pred@2350
   355
				return 0;
pred@2133
   356
			}
pred@2350
   357
			
pred@2350
   358
			SetTrieValue(g_mapTrie, map, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
pred@2347
   359
pred@2133
   360
			if (result == Nominate_Replaced)
pred@2133
   361
			{
pred@2133
   362
				PrintToChatAll("[SM] %t", "Map Nomination Changed", name, map);
pred@2350
   363
				return 0;	
pred@2133
   364
			}
pred@2133
   365
			
pred@2133
   366
			PrintToChatAll("[SM] %t", "Map Nominated", name, map);
pred@2133
   367
		}
pred@2350
   368
		
pred@2350
   369
		case MenuAction_DrawItem:
pred@2350
   370
		{
pred@2350
   371
			decl String:map[64];
pred@2350
   372
			GetMenuItem(menu, param2, map, sizeof(map));
pred@2350
   373
			
pred@2350
   374
			new status;
pred@2350
   375
			
pred@2350
   376
			if (!GetTrieValue(g_mapTrie, map, status))
pred@2350
   377
			{
pred@2350
   378
				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
pred@2350
   379
				return ITEMDRAW_DEFAULT;
pred@2350
   380
			}
pred@2350
   381
			
pred@2350
   382
			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
pred@2350
   383
			{
pred@2350
   384
				return ITEMDRAW_DISABLED;	
pred@2350
   385
			}
pred@2350
   386
			
pred@2350
   387
			return ITEMDRAW_DEFAULT;
pred@2350
   388
						
pred@2350
   389
		}
pred@2350
   390
		
pred@2350
   391
		case MenuAction_DisplayItem:
pred@2350
   392
		{
pred@2350
   393
			decl String:map[64];
pred@2350
   394
			GetMenuItem(menu, param2, map, sizeof(map));
pred@2350
   395
			
pred@2350
   396
			new status;
pred@2350
   397
			
pred@2350
   398
			if (!GetTrieValue(g_mapTrie, map, status))
pred@2350
   399
			{
pred@2350
   400
				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
pred@2350
   401
				return 0;
pred@2350
   402
			}
pred@2350
   403
			
pred@2350
   404
			decl String:display[100];
pred@2350
   405
			
pred@2350
   406
			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
pred@2350
   407
			{
pred@2350
   408
				if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
pred@2350
   409
				{
pred@2350
   410
					Format(display, sizeof(display), "%s (%T)", map, "Current Map", param1);
pred@2350
   411
					return RedrawMenuItem(display);
pred@2350
   412
				}
pred@2350
   413
				
pred@2350
   414
				if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
pred@2350
   415
				{
pred@2350
   416
					Format(display, sizeof(display), "%s (%T)", map, "Recently Played", param1);
pred@2350
   417
					return RedrawMenuItem(display);
pred@2350
   418
				}
pred@2350
   419
				
pred@2350
   420
				if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
pred@2350
   421
				{
pred@2350
   422
					Format(display, sizeof(display), "%s (%T)", map, "Nominated", param1);
pred@2350
   423
					return RedrawMenuItem(display);
pred@2350
   424
				}
pred@2350
   425
			}
pred@2350
   426
			
pred@2350
   427
			return 0;
pred@2350
   428
		}
pred@2133
   429
	}
pred@2350
   430
	
pred@2350
   431
	return 0;
fyrenmoo@2865
   432
}