View Single Post
dustinandband
Senior Member
Join Date: May 2015
Old 04-10-2017 , 17:22   Re: [L4D2] $10 plugin job (requiring native votes)
Reply With Quote #4

Hi,

Thanks for the quick reply. The vote itself is functional, but I was wanting the vote feature to be written with the native votes API for a better looking HUD:
https://forums.alliedmods.net/showthread.php?t=208008

Video demonstration - you can see the nice HUD layout when I call the !fullspawns command as well as voting to change chapters: https://youtu.be/XA_5p7zo6nM


Some example code that uses the Native Votes API (taken from the fullspawns sp file):

PHP Code:
public TurnOffFullSpawns(client)
{
    if(
StartEnableSpawnClearVote(client))
    {
        
FakeClientCommand(client"Vote Yes");
    }
    
//====================
// Voting
//====================
bool:StartDisableSpawnClearVote(client)
{
    if (!
NativeVotes_IsVoteInProgress())
    {
        new 
iNumPlayers;
        
decl iPlayers[MaxClients];
        
        for (new 
i=1i<=MaxClientsi++)
        {
            if (!
IsClientInGame(i) || IsFakeClient(i) || (GetClientTeam(i) == L4D_TEAM_SPECTATE))
            {
                continue;
            }
            
iPlayers[iNumPlayers++] = i;
        }
        
        new 
String:sVote[64];
        
g_hDisableShutdownVote NativeVotes_Create(VoteActionHandler,  NativeVotesType_Custom_YesNoMenuAction_Cancel MenuAction_VoteEnd MenuAction_End);
        
        
Format(sVotesizeof(sVote), "Don't Allow Spawns To Shut Down?");
        
        
NativeVotes_SetTitle(g_hDisableShutdownVotesVote);
        
NativeVotes_SetInitiator(g_hDisableShutdownVoteclient);
        
NativeVotes_SetResultCallback(g_hDisableShutdownVoteVoteResultHandler);
        
NativeVotes_Display(g_hDisableShutdownVoteiPlayersiNumPlayers20);
        return 
true;
    }
    
    return 
false;
}

bool:StartEnableSpawnClearVote(client)
{
    if (!
NativeVotes_IsVoteInProgress())
    {
        new 
iNumPlayers;
        
decl iPlayers[MaxClients];
        
        for (new 
i=1i<=MaxClientsi++)
        {
            if (!
IsClientInGame(i) || IsFakeClient(i) || (GetClientTeam(i) == L4D_TEAM_SPECTATE))
            {
                continue;
            }
            
iPlayers[iNumPlayers++] = i;
        }
        
        new 
String:sVote[64];
        
g_hEnableShutdownVote NativeVotes_Create(VoteActionHandler,  NativeVotesType_Custom_YesNoMenuAction_Cancel MenuAction_VoteEnd MenuAction_End);
        
        
Format(sVotesizeof(sVote), "Allow Spawns To Shut Down?");
        
        
NativeVotes_SetTitle(g_hEnableShutdownVotesVote);
        
NativeVotes_SetInitiator(g_hEnableShutdownVoteclient);
        
NativeVotes_SetResultCallback(g_hEnableShutdownVoteVoteResultHandler);
        
NativeVotes_Display(g_hEnableShutdownVoteiPlayersiNumPlayers20);
        return 
true;
    }
    
    return 
false;
}

public 
VoteActionHandler(Handle:voteMenuAction:actionparam1param2)
{
    switch (
action)
    {
        case 
MenuAction_End:
        {
            
g_hDisableShutdownVote INVALID_HANDLE;
            
g_hEnableShutdownVote INVALID_HANDLE;
            
NativeVotes_Close(vote);
        }
        case 
MenuAction_VoteCancel:
        {
            switch (
param1)
            {
                case 
VoteCancel_Generic:
                {
                    
NativeVotes_DisplayFail(voteNativeVotesFail_Generic);
                }
                
                case 
VoteCancel_NoVotes:
                {
                    
NativeVotes_DisplayFail(voteNativeVotesFail_NotEnoughVotes);
                }
            }
        }
    }
}

public 
VoteResultHandler(Handle:votenum_votesnum_clients, const client_indexes[], const client_votes[], num_items, const item_indexes[], const item_votes[])
{
    for (new 
i=0i<num_itemsi++)
    {
        if (
item_indexes[i] == NATIVEVOTES_VOTE_YES)
        {
            if (
item_votes[i] > (num_clients 2))
            {
                if (
vote == g_hDisableShutdownVote)
                {
                    
NativeVotes_DisplayPass(vote"Disabling spawn clearing...");
                    
                    
DisableSpawnClear();
                    return;
                }
                else if (
vote == g_hEnableShutdownVote)
                {
                    
NativeVotes_DisplayPass(vote"Enabling spawn clearing...");
                    
                    
EnableSpawnClear();
                    return;
                }
            }
        }
    }
    
NativeVotes_DisplayFail(voteNativeVotesFail_Loses);

dustinandband is offline