plugins/nominations.sp
changeset 2343 19331eae67ad
parent 2156 186f2f16564c
child 2344 517b0050020f
     1.1 --- a/plugins/nominations.sp	Sun Oct 05 15:20:17 2008 -0700
     1.2 +++ b/plugins/nominations.sp	Wed Oct 08 20:58:25 2008 +1300
     1.3 @@ -52,6 +52,14 @@
     1.4  new Handle:g_MapMenu = INVALID_HANDLE;
     1.5  new g_mapFileSerial = -1;
     1.6  
     1.7 +#define MAPSTATUS_ENABLED (1<<0)
     1.8 +#define MAPSTATUS_DISABLED (1<<1)
     1.9 +#define MAPSTATUS_EXCLUDE_CURRENT (1<<2)
    1.10 +#define MAPSTATUS_EXCLUDE_PREVIOUS (1<<3)
    1.11 +#define MAPSTATUS_EXCLUDE_NOMINATED (1<<4)
    1.12 +
    1.13 +new Handle:g_mapTrie;
    1.14 +
    1.15  public OnPluginStart()
    1.16  {
    1.17  	LoadTranslations("common.phrases");
    1.18 @@ -69,6 +77,8 @@
    1.19  	RegConsoleCmd("sm_nominate", Command_Nominate);
    1.20  	
    1.21  	RegAdminCmd("sm_nominate_addmap", Command_Addmap, ADMFLAG_CHANGEMAP, "sm_nominate_addmap <mapname> - Forces a map to be on the next mapvote.");
    1.22 +	
    1.23 +	g_mapTrie = CreateTrie();
    1.24  }
    1.25  
    1.26  public OnConfigsExecuted()
    1.27 @@ -88,9 +98,23 @@
    1.28  	BuildMapMenu();
    1.29  }
    1.30  
    1.31 -public OnNominationRemoved(String:map[], owner)
    1.32 +public OnNominationRemoved(/* const */String:map[], owner)
    1.33  {
    1.34 -	AddMenuItem(g_MapMenu,	map, map);
    1.35 +	new status;
    1.36 +	
    1.37 +	/* Is the map in our list? */
    1.38 +	if (!GetTrieValue(g_mapTrie, map, status))
    1.39 +	{
    1.40 +		return;	
    1.41 +	}
    1.42 +	
    1.43 +	/* Was the map disabled due to being nominated */
    1.44 +	if ((status & MAPSTATUS_EXCLUDE_NOMINATED) != MAPSTATUS_EXCLUDE_NOMINATED)
    1.45 +	{
    1.46 +		return;
    1.47 +	}
    1.48 +	
    1.49 +	SetTrieValue(g_mapTrie, map, MAPSTATUS_ENABLED);	
    1.50  }
    1.51  
    1.52  public Action:Command_Addmap(client, args)
    1.53 @@ -104,10 +128,12 @@
    1.54  	decl String:mapname[64];
    1.55  	GetCmdArg(1, mapname, sizeof(mapname));
    1.56  
    1.57 -	if (FindStringInArray(g_MapList, mapname) == -1)
    1.58 +	
    1.59 +	new status;
    1.60 +	if (!GetTrieValue(g_mapTrie, mapname, status))
    1.61  	{
    1.62  		ReplyToCommand(client, "%t", "Map was not found", mapname);
    1.63 -		return Plugin_Handled;
    1.64 +		return Plugin_Handled;		
    1.65  	}
    1.66  	
    1.67  	new NominateResult:result = NominateMap(mapname, true, 0);
    1.68 @@ -120,16 +146,9 @@
    1.69  		return Plugin_Handled;	
    1.70  	}
    1.71  	
    1.72 -	decl String:item[64];
    1.73 -	for (new i = 0; i < GetMenuItemCount(g_MapMenu); i++)
    1.74 -	{
    1.75 -		GetMenuItem(g_MapMenu, i, item, sizeof(item));
    1.76 -		if (strcmp(item, mapname) == 0)
    1.77 -		{
    1.78 -			RemoveMenuItem(g_MapMenu, i);
    1.79 -			break;
    1.80 -		}			
    1.81 -	}	
    1.82 +	
    1.83 +	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
    1.84 +
    1.85  	
    1.86  	ReplyToCommand(client, "%t", "Map Inserted", mapname);
    1.87  	LogAction(client, -1, "\"%L\" inserted map \"%s\".", client, mapname);
    1.88 @@ -185,41 +204,58 @@
    1.89  	decl String:mapname[64];
    1.90  	GetCmdArg(1, mapname, sizeof(mapname));
    1.91  	
    1.92 -	decl String:item[64];
    1.93 -	for (new i = 0; i < GetMenuItemCount(g_MapMenu); i++)
    1.94 +	new status;
    1.95 +	if (!GetTrieValue(g_mapTrie, mapname, status))
    1.96  	{
    1.97 -		GetMenuItem(g_MapMenu, i, item, sizeof(item));
    1.98 -		if (strcmp(item, mapname) == 0)
    1.99 -		{
   1.100 -			new NominateResult:result = NominateMap(mapname, false, client);
   1.101 -	
   1.102 -			if (result > Nominate_Replaced)
   1.103 -			{
   1.104 -				if (result == Nominate_AlreadyInVote)
   1.105 -				{
   1.106 -					ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   1.107 -				}
   1.108 -				else
   1.109 -				{
   1.110 -					ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   1.111 -				}
   1.112 -				
   1.113 -				return Plugin_Handled;	
   1.114 -			}
   1.115 -			
   1.116 -			RemoveMenuItem(g_MapMenu, i);
   1.117 -			
   1.118 -			decl String:name[64];
   1.119 -			GetClientName(client, name, sizeof(name));
   1.120 -	
   1.121 -			PrintToChatAll("[SM] %t", "Map Nominated", name, mapname);
   1.122 -	
   1.123 -			return Plugin_Continue;
   1.124 -		}			
   1.125 +		ReplyToCommand(client, "%t", "Map was not found", mapname);
   1.126 +		return Plugin_Handled;		
   1.127  	}
   1.128  	
   1.129 -	ReplyToCommand(client, "%t", "Map was not found", mapname);
   1.130 -	return Plugin_Handled;
   1.131 +	if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   1.132 +	{
   1.133 +		if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
   1.134 +		{
   1.135 +			ReplyToCommand(client, "[SM] %t", "Can't Nominate Current Map");
   1.136 +		}
   1.137 +		
   1.138 +		if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
   1.139 +		{
   1.140 +			ReplyToCommand(client, "[SM] %t", "Map in Exclude List");
   1.141 +		}
   1.142 +		
   1.143 +		if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
   1.144 +		{
   1.145 +			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   1.146 +		}
   1.147 +		
   1.148 +		return Plugin_Handled;
   1.149 +	}
   1.150 +	
   1.151 +	new NominateResult:result = NominateMap(mapname, false, client);
   1.152 +	
   1.153 +	if (result > Nominate_Replaced)
   1.154 +	{
   1.155 +		if (result == Nominate_AlreadyInVote)
   1.156 +		{
   1.157 +			ReplyToCommand(client, "%t", "Map Already In Vote", mapname);
   1.158 +		}
   1.159 +		else
   1.160 +		{
   1.161 +			ReplyToCommand(client, "[SM] %t", "Map Already Nominated");
   1.162 +		}
   1.163 +		
   1.164 +		return Plugin_Handled;	
   1.165 +	}
   1.166 +	
   1.167 +	/* Map was nominated! - Disable the menu item and update the trie */
   1.168 +	
   1.169 +	SetTrieValue(g_mapTrie, mapname, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   1.170 +	
   1.171 +	decl String:name[64];
   1.172 +	GetClientName(client, name, sizeof(name));
   1.173 +	PrintToChatAll("[SM] %t", "Map Nominated", name, mapname);
   1.174 +	
   1.175 +	return Plugin_Continue;
   1.176  }
   1.177  
   1.178  AttemptNominate(client)
   1.179 @@ -238,7 +274,9 @@
   1.180  		g_MapMenu = INVALID_HANDLE;
   1.181  	}
   1.182  	
   1.183 -	g_MapMenu = CreateMenu(Handler_MapSelectMenu);
   1.184 +	ClearTrie(g_mapTrie);
   1.185 +	
   1.186 +	g_MapMenu = CreateMenu(Handler_MapSelectMenu, MENU_ACTIONS_DEFAULT|MenuAction_DrawItem|MenuAction_DisplayItem);
   1.187  
   1.188  	decl String:map[64];
   1.189  	
   1.190 @@ -256,27 +294,32 @@
   1.191  		GetCurrentMap(currentMap, sizeof(currentMap));
   1.192  	}
   1.193  	
   1.194 +		
   1.195  	for (new i = 0; i < GetArraySize(g_MapList); i++)
   1.196  	{
   1.197 -		GetArrayString(g_MapList, i, map, sizeof(map));
   1.198 +		new status = MAPSTATUS_ENABLED;
   1.199  		
   1.200 -		if (GetConVarBool(g_Cvar_ExcludeOld))
   1.201 -		{
   1.202 -			if (FindStringInArray(excludeMaps, map) != -1)
   1.203 -			{
   1.204 -				continue;	
   1.205 -			}
   1.206 -		}
   1.207 +		GetArrayString(g_MapList, i, map, sizeof(map));
   1.208  		
   1.209  		if (GetConVarBool(g_Cvar_ExcludeCurrent))
   1.210  		{
   1.211  			if (StrEqual(map, currentMap))
   1.212  			{
   1.213 -				continue;
   1.214 +				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_CURRENT;
   1.215 +			}
   1.216 +		}
   1.217 +		
   1.218 +		/* Dont bother with this check if the current map check passed */
   1.219 +		if (GetConVarBool(g_Cvar_ExcludeOld) && status == MAPSTATUS_ENABLED)
   1.220 +		{
   1.221 +			if (FindStringInArray(excludeMaps, map) != -1)
   1.222 +			{
   1.223 +				status = MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_PREVIOUS;
   1.224  			}
   1.225  		}
   1.226  		
   1.227  		AddMenuItem(g_MapMenu, map, map);
   1.228 +		SetTrieValue(g_mapTrie, map, status);
   1.229  	}
   1.230  	
   1.231  	SetMenuExitButton(g_MapMenu, true);
   1.232 @@ -289,35 +332,96 @@
   1.233  		case MenuAction_Select:
   1.234  		{
   1.235  			decl String:map[64], String:name[64];
   1.236 -			GetMenuItem(menu, param2, map, sizeof(map));
   1.237 -			
   1.238 +			GetMenuItem(menu, param2, map, sizeof(map));		
   1.239  			
   1.240  			GetClientName(param1, name, 64);
   1.241  	
   1.242 -			
   1.243  			new NominateResult:result = NominateMap(map, false, param1);
   1.244  			
   1.245  			/* Don't need to check for InvalidMap because the menu did that already */
   1.246  			if (result == Nominate_AlreadyInVote)
   1.247  			{
   1.248  				PrintToChat(param1, "[SM] %t", "Map Already Nominated");
   1.249 -				return;
   1.250 +				return 0;
   1.251  			}
   1.252  			else if (result == Nominate_VoteFull)
   1.253  			{
   1.254  				PrintToChat(param1, "[SM] %t", "Max Nominations");
   1.255 -				return;
   1.256 +				return 0;
   1.257  			}
   1.258 +			
   1.259 +			SetTrieValue(g_mapTrie, map, MAPSTATUS_DISABLED|MAPSTATUS_EXCLUDE_NOMINATED);
   1.260  
   1.261 -			RemoveMenuItem(menu, param2);
   1.262 -			
   1.263  			if (result == Nominate_Replaced)
   1.264  			{
   1.265  				PrintToChatAll("[SM] %t", "Map Nomination Changed", name, map);
   1.266 -				return;	
   1.267 +				return 0;	
   1.268  			}
   1.269  			
   1.270  			PrintToChatAll("[SM] %t", "Map Nominated", name, map);
   1.271  		}
   1.272 +		
   1.273 +		case MenuAction_DrawItem:
   1.274 +		{
   1.275 +			decl String:map[64];
   1.276 +			GetMenuItem(menu, param2, map, sizeof(map));
   1.277 +			
   1.278 +			new status;
   1.279 +			
   1.280 +			if (!GetTrieValue(g_mapTrie, map, status))
   1.281 +			{
   1.282 +				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
   1.283 +				return ITEMDRAW_DEFAULT;
   1.284 +			}
   1.285 +			
   1.286 +			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   1.287 +			{
   1.288 +				return ITEMDRAW_DISABLED;	
   1.289 +			}
   1.290 +			
   1.291 +			return ITEMDRAW_DEFAULT;
   1.292 +						
   1.293 +		}
   1.294 +		
   1.295 +		case MenuAction_DisplayItem:
   1.296 +		{
   1.297 +			decl String:map[64];
   1.298 +			GetMenuItem(menu, param2, map, sizeof(map));
   1.299 +			
   1.300 +			new status;
   1.301 +			
   1.302 +			if (!GetTrieValue(g_mapTrie, map, status))
   1.303 +			{
   1.304 +				LogError("Menu selection of item not in trie. Major logic problem somewhere.");
   1.305 +				return 0;
   1.306 +			}
   1.307 +			
   1.308 +			decl String:display[100];
   1.309 +			
   1.310 +			if ((status & MAPSTATUS_DISABLED) == MAPSTATUS_DISABLED)
   1.311 +			{
   1.312 +				if ((status & MAPSTATUS_EXCLUDE_CURRENT) == MAPSTATUS_EXCLUDE_CURRENT)
   1.313 +				{
   1.314 +					Format(display, sizeof(display), "%s (%T)", map, "Current Map", param1);
   1.315 +					return RedrawMenuItem(display);
   1.316 +				}
   1.317 +				
   1.318 +				if ((status & MAPSTATUS_EXCLUDE_PREVIOUS) == MAPSTATUS_EXCLUDE_PREVIOUS)
   1.319 +				{
   1.320 +					Format(display, sizeof(display), "%s (%T)", map, "Recently Played", param1);
   1.321 +					return RedrawMenuItem(display);
   1.322 +				}
   1.323 +				
   1.324 +				if ((status & MAPSTATUS_EXCLUDE_NOMINATED) == MAPSTATUS_EXCLUDE_NOMINATED)
   1.325 +				{
   1.326 +					Format(display, sizeof(display), "%s (%T)", map, "Nominated", param1);
   1.327 +					return RedrawMenuItem(display);
   1.328 +				}
   1.329 +			}
   1.330 +			
   1.331 +			return 0;
   1.332 +		}
   1.333  	}
   1.334 +	
   1.335 +	return 0;
   1.336  }
   1.337 \ No newline at end of file