[0.4] Vote System

Started by MatheuS, Jul 30, 2019, 12:37 PM

Previous topic - Next topic

MatheuS

Requeriments:


For beginners (to install the SLC Timer System):

1° Add this line inside your onScriptLoad()

[noae][noae][noae][noae][noae][noae][noae]dofile("timers.nut")[/noae][/noae][/noae][/noae][/noae][/noae][/noae]

2° Copy this below and paste it into some file and save as "timers.nut".

[noae][noae][noae][noae][noae][noae][noae]// ------------------------------------------------------------------------------------------------
srand((GetTickCount() % time()) / 3);

// ------------------------------------------------------------------------------------------------
_Timer <- {
    // --------------------------------------------------------------------------------------------
    m__Timers = { /* ... */ }

    // --------------------------------------------------------------------------------------------
    function Create(environment, listener, interval, repeat, ...)
    {
        // ----------------------------------------------------------------------------------------
        // Prepare the arguments pack
        vargv.insert(0, environment);
        // ----------------------------------------------------------------------------------------
        // Store timer information into a table
        local data = {
            Environment = environment,
            Listener = listener,
            Interval = interval,
            Repeat = repeat,
            Timer = null,
            Args = vargv
        };
        // ----------------------------------------------------------------------------------------
        local hash = split(data+"", ":")[1].slice(3, -1).tointeger(16);
        // ----------------------------------------------------------------------------------------
        // Create the timer instance
        data.Timer = NewTimer("tm_TimerProcess", interval, repeat, hash);
        // ----------------------------------------------------------------------------------------
        // Store the timer information
        m__Timers.rawset(hash, data);
        // ----------------------------------------------------------------------------------------
        // Return the hash that identifies this timer
        return hash;
    }

    // --------------------------------------------------------------------------------------------
    function Destroy(hash)
    {
        // See if the specified timer exists
        if (m__Timers.rawin(hash))
        {
            // Destroy the timer instance
            m__Timers.rawget(hash).Timer.Delete();
            // Remove the timer information
            m__Timers.rawdelete(hash);
        }
    }

    // --------------------------------------------------------------------------------------------
    function Exists(hash)
    {
        // See if the specified timer exists
        return m__Timers.rawin(hash);
    }

    // --------------------------------------------------------------------------------------------
    function Fetch(hash)
    {
        // Return the timer information
        return m__Timers.rawget(hash);
    }

    // --------------------------------------------------------------------------------------------
    function Clear()
    {
        // Process all existing timers
        foreach (tm in m__Timers)
        {
            // Destroy the timer instance
            tm.Timer.Delete();
        }
        // Clear existing timers
        m__Timers.clear();
    }
}

// ------------------------------------------------------------------------------------------------
tm_TimerProcess <- function(hash)
{
    // See if the specified timer exists
    if (_Timer.m__Timers.rawin(hash))
    {
        // Get the timer associated with the specified hash
        local tm = _Timer.m__Timers.rawget(hash);
        // Call the specified listener
        tm.Listener.pacall(tm.Args);
        // Calculate the remaining cycles
        if (tm.Repeat && (--tm.Repeat <= 0))
        {
            // Release the timer
            _Timer.Destroy(hash);
        }
    }
}
[/noae][/noae][/noae][/noae][/noae][/noae][/noae]



The beautiful sexy awesome strong code:

Pastebin: (With better formatting)
https://pastebin.com/0JQcu7EH

[noae][noae][noae][noae][noae][noae][noae]/*
VoteSystem
Developed by: Lucas
Credits: Matheus/Takanaue (I did nothing but I'm here)
*/

VoteStarted <- false;
VoteText <- null;
VoteY <- 0;
VoteN <- 0;
VoteTime <- 0;
VoteList <- {};

function onScriptLoad()
{
N <- BindKey( true, 0x4E, 0, 0 );
Y <- BindKey( true, 0x59, 0, 0 );
}

function onPlayerJoin( player )
{
VoteList.rawset( player.ID, "none" );
}

function onPlayerPart( player, reason )
{
VoteList.rawdelete( player.ID );
}

function onKeyDown( player, key )
{
if ( key == N )
{
if ( VoteStarted )
{
switch ( VoteList[ player.ID ] )
{
case "yes":
VoteY > 0 ? VoteY -- : false;
VoteN ++;
VoteList.rawset( player.ID, "no" );
PrivMessage( player, "Okay, you've changed your vote." );
break;

case "none":
VoteN ++;
VoteList.rawset( player.ID, "no" );
PrivMessage( player, "Feedback computed." );
break;
default: break;
}
}
}

else if ( key == Y )
{
if ( VoteStarted )
{
switch ( VoteList[ player.ID ] )
{
case "no":
VoteY ++;
VoteN > 0 ? VoteN -- : false;
VoteList.rawset( player.ID, "yes" );
PrivMessage( player, "Okay, you've changed your vote." );
break;

case "none":
VoteY ++;
VoteList.rawset( player.ID, "yes" );
PrivMessage( player, "Feedback computed." );
break;
default: break;
}
}
}
}

function Result()
{
local y_calc, n_calc;
AnnounceAll( "The time is over!" ,1 );
Message( "[#DCDCDC]Result of the votes:" );

if ( VoteY == VoteN && VoteY > 0 ) y_calc = "50", n_calc = "50";
else if ( VoteY > VoteN ) y_calc = ( !VoteY ? "0" : ( 100 - ( ( VoteN.tofloat() / VoteY ) * 100 ) ) ), n_calc = ( !VoteN ? "0" : ( ( VoteN.tofloat() / VoteY ) * 100 ) );
else y_calc = ( !VoteY ? "0" : ( ( VoteY.tofloat() / VoteN ) * 100 ) ), n_calc = ( !VoteN ? "0" : ( 100 - ( VoteY.tofloat() / VoteN ) * 100 ) );

Message( "[#DCDCDC]- Number of votes yes: [#FF0000]" + VoteY + "[#DCDCDC](" + y_calc + "%)" );
Message( "[#DCDCDC]- Number of votes no: [#FF0000]" + VoteN + "[#DCDCDC](" + n_calc + "%)" );

// Resetting the results
VoteStarted = false;
VoteText = null;
VoteY = 0;
VoteN = 0;
foreach ( idx, val in VoteList ) VoteList.rawset( idx, "none" );
}

function VoteClock()
{
AnnounceAll( format( "Ends in " + VoteTime + " second%s...", VoteTime == 1 ? "" : "s" ), 1 );
VoteTime --;
}

function onPlayerCommand( player, cmd, text )
{
if ( cmd == "vote" )
{
if ( !text ) MessagePlayer( "/" + cmd + " <text/off>.", player );
else
{
if ( text == "off" )
{
if ( !VoteStarted ) MessagePlayer( "No vote in progress." player );
else
{
VoteStarted = false;
VoteText = null;
VoteY != 0 ? VoteY = 0 : false;
VoteN != 0 ? VoteN = 0 : false;
VoteTime = 0;
_Timer.Destroy( _Result );
_Timer.Destroy( _VoteClock );
Message( "[#9B30FF]* Admin. [#B5B5B5]" + player.Name + " [#9B30FF]closed the poll." );
}
}
else
{
if ( VoteStarted ) MessagePlayer( "There is a vote in progress.", player );
else
{
VoteStarted = true;
VoteText = text;
VoteTime = 29;
Message( "[#9B30FF]* Admin. [#B5B5B5]" + player.Name + " [#9B30FF]started a poll." );
Message( "[#DCDCDC]~ " + text + " ~\nPress [#00FF00]\"Y\" [#DCDCDC]to vote yes or [#FF0000]\"N\" [#DCDCDC]to vote no." );
AnnounceAll( "Ends in " + VoteTime + " seconds...", 1 );
_Result <- _Timer.Create( this, Result, 30000, 1 );
_VoteClock <- _Timer.Create( this, VoteClock, 1000, VoteTime );
}
}
}
}
}
[/noae][/noae][/noae][/noae][/noae][/noae][/noae]


Commands:

[noae][noae][noae][noae][noae][noae]/vote TEXT        //if your text is "OFF" the votation will be stopped[/noae][/noae][/noae][/noae][/noae][/noae]
[noae][noae][noae][noae][noae][noae]To reply negatively press N and To reply positively press Y[/noae][/noae][/noae][/noae][/noae][/noae]
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Sebastian

#1

MatheuS

Quote from: Sebastian on Jul 31, 2019, 07:05 AM

I liked it. I will edit original post and put these comments ::)
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.