Stfnsn
Stfnsn

Reputation: 131

Limit number of calls some function in JS

Ok, I have little script which run in loop mode. It's for gambling. This is idea: I flip coin infinity times (loop) and i have chance 50/50 for both sides. But, in some point I will get 5 times in row "Number side": ...,head, number,head, NUMBER, NUMBER, NUMBER, NUMBER, NUMBER,... And I need to limit script to call function if get 5 times in row "number side". In my case, I have function multiply() and that function was call sometimes. So, I need to limit number of calls multiply() functions for eg. 5 or 4.. So, if script call function multiply() 5 times in row, it will do something (eg. show alert or something). This is script:

var startValue = '0.00000001', // Don't lower the decimal point more than 4x of current balance
            stopPercentage = 0.001, // In %. I wouldn't recommend going past 0.08
            maxWait = 500, // In milliseconds
            stopped = false,
            stopBefore = 3; // In minutes
     
    var $loButton = $('#double_your_btc_bet_lo_button'),
                    $hiButton = $('#double_your_btc_bet_hi_button');
     
    function multiply(){
            var current = $('#double_your_btc_stake').val();
            var multiply = (current * 2).toFixed(8);
            $('#double_your_btc_stake').val(multiply);
    }
     
    function getRandomWait(){
            var wait = Math.floor(Math.random() * maxWait ) + 100;
     
            console.log('Waiting for ' + wait + 'ms before next bet.');
     
            return wait ;
    }
     
    function startGame(){
            console.log('Game started!');
            reset();
            $loButton.trigger('click');
    }
     
    function stopGame(){
            console.log('Game will stop soon! Let me finish.');
            stopped = true;
    }
     
    function reset(){
            $('#double_your_btc_stake').val(startValue);
    }
     
    // quick and dirty hack if you have very little bitcoins like 0.0000001
    function deexponentize(number){
            return number * 1000000;
    }
     
    function iHaveEnoughMoni(){
            var balance = deexponentize(parseFloat($('#balance').text()));
            var current = deexponentize($('#double_your_btc_stake').val());
     
            return ((balance*2)/100) * (current*2) > stopPercentage/100;
    }
     
    function stopBeforeRedirect(){
            var minutes = parseInt($('title').text());
     
            if( minutes < stopBefore )
            {
                    console.log('Approaching redirect! Stop the game so we don\'t get redirected while loosing.');
                    stopGame();
     
                    return true;
            }
     
            return false;
    }
     
    // Unbind old shit
    $('#double_your_btc_bet_lose').unbind();
    $('#double_your_btc_bet_win').unbind();
     
    // Loser
    $('#double_your_btc_bet_lose').bind("DOMSubtreeModified",function(event){
            if( $(event.currentTarget).is(':contains("lose")') )
            {
                    console.log('You LOST! Multiplying your bet and betting again.');
                   
                    multiply();
     
                    setTimeout(function(){
                            $loButton.trigger('click');
                    }, getRandomWait());
     
                    //$loButton.trigger('click');
            }
    });
     
    // Winner
    $('#double_your_btc_bet_win').bind("DOMSubtreeModified",function(event){
            if( $(event.currentTarget).is(':contains("win")') )
            {
                    if( stopBeforeRedirect() )
                    {
                            return;
                    }
     
                    if( iHaveEnoughMoni() )
                    {
                            console.log('You WON! But don\'t be greedy. Restarting!');
     
                            reset();
     
                            if( stopped )
                            {
                                    stopped = false;
                                    return false;
                            }
                    }
                    else
                    {
                            console.log('You WON! Betting again');
                    }
     
                    setTimeout(function(){
                            $loButton.trigger('click');
                    }, getRandomWait());
            }
    });

I need this to stop losing points more and more if get 5 times in row "lose" because script always multiply by 2 if lose. And script must multiply if lose, it's rule for gambling :)

Upvotes: 1

Views: 2824

Answers (4)

Patrick Murphy
Patrick Murphy

Reputation: 2329

Create Global variable:

var startValue = '0.00000001', // Don't lower the decimal point more than 4x of current balance
            stopPercentage = 0.001, // In %. I wouldn't recommend going past 0.08
            maxWait = 500, // In milliseconds
            stopped = false,
            stopBefore = 3, // In minutes
            multiplyCalls = 0; // <--- Added this global

Add to variable and If to test value

 function multiply(){
            if(multiplyCalls <= 5){ // test multiply
                var current = $('#double_your_btc_stake').val();
                var multiply = (current * 2).toFixed(8);
                $('#double_your_btc_stake').val(multiply);
                multiplyCalls++; // increment
            }else{
                console.log('you lost');
            }
    }

Reset your variable on game reset

function reset(){
    $('#double_your_btc_stake').val(startValue);
    multiplyCalls = 0; // reset value
}

Upvotes: 2

Stfnsn
Stfnsn

Reputation: 131

Ok, I find solution:

var startValue = '0.00000001', // Don't lower the decimal point more than 4x of current balance
        stopPercentage = 0.001, // In %. I wouldn't recommend going past 0.08
        maxWait = 500, // In milliseconds
        stopped = false,
        stopBefore = 3; // In minutes
        multiplyCalls = 0; // <--- Added this global

var $loButton = $('#double_your_btc_bet_lo_button'),
                $hiButton = $('#double_your_btc_bet_hi_button');


function multiply(){
        if(multiplyCalls < 4){ // test multiply
            var current = $('#double_your_btc_stake').val();
            var multiply = (current * 2).toFixed(8);
            $('#double_your_btc_stake').val(multiply);
            multiplyCalls++; // increment
        }else{
            reset();
            console.log('=== RESETING ===');
        }
}

function getRandomWait(){
        var wait = Math.floor(Math.random() * maxWait ) + 100;

        console.log('Waiting for ' + wait + 'ms before next bet.');

        return wait ;
}

function startGame(){
        console.log('Game started!');
        reset();
        $loButton.trigger('click');
}

function stopGame(){
        console.log('Game will stop soon! Let me finish.');
        stopped = true;
}

function reset(){
        $('#double_your_btc_stake').val(startValue);

}

// quick and dirty hack if you have very little bitcoins like 0.0000001
function deexponentize(number){
        return number * 1000000;
}

function iHaveEnoughMoni(){
        var balance = deexponentize(parseFloat($('#balance').text()));
        var current = deexponentize($('#double_your_btc_stake').val());

        return ((balance*2)/100) * (current*2) > stopPercentage/100;
}

function stopBeforeRedirect(){
        var minutes = parseInt($('title').text());

        if( minutes < stopBefore )
        {
                console.log('Approaching redirect! Stop the game so we don\'t get redirected while loosing.');
                stopGame();

                return true;
        }

        return false;
}

// Unbind old shit
$('#double_your_btc_bet_lose').unbind();
$('#double_your_btc_bet_win').unbind();

// Loser
$('#double_your_btc_bet_lose').bind("DOMSubtreeModified",function(event){
        if( $(event.currentTarget).is(':contains("lose")') )
        {
                console.log('You LOST! Multiplying your bet and betting again.');

                multiply();

                setTimeout(function(){
                        $loButton.trigger('click');
                }, getRandomWait());

                //$loButton.trigger('click');
        }
});

// Winner
$('#double_your_btc_bet_win').bind("DOMSubtreeModified",function(event){
        if( $(event.currentTarget).is(':contains("win")') )
        {
                if( stopBeforeRedirect() )
                {
                        return;
                }

                if( iHaveEnoughMoni() )
                {
                        console.log('You WON! But don\'t be greedy. Restarting!');

                        reset();

                        if( stopped )
                        {
                                stopped = false;
                                return false;
                        }
                }
                else
                {
                        console.log('You WON! Betting again');
                }

                setTimeout(function(){
                        $loButton.trigger('click');
                }, getRandomWait());
                multiplyCalls = 0; // reset value
        }
});

Thanks @patrick murphy

SO, I increment counter in "lose" and reset counter in "win" section. Thanks all :)

Upvotes: 1

neuro_sys
neuro_sys

Reputation: 804

You can simulate a c-style block scoped static variable like this:

function foobar() {
    this._callCount = this._callCount || 0; // static scoped variable
    if (this._callCount++ >= 5) {
        // reached over 5, exitting.
        return;
    }
    // do something else
}

Working jsfiddle example: http://jsfiddle.net/z5ojxjke/

Upvotes: 0

Hitokage
Hitokage

Reputation: 803

Can't you just create a global variable for function call count and increment it every time the function is executed? Plus add some conditions in the function what shall happen when you reached the limit.

Upvotes: 0

Related Questions