[V0.0.9] - SteemCasino - Jackpot is here!

in #utopian-io7 years ago (edited)

Join our Official Discord Server.

You can see our github here.

Pull request for v0.0.9.

Bugfix

  • Fixed a bug caused by computer's local time.
    If the local time of the user's computer would have been wrong, then the remaining time would be wrong too.

We no longer pass to you the timestamp, but the seconds remaining.

-       timestamp: timestamp
+       timestamp: rollTime / 1000
+       timestamp: betTime / 1000

New Features

image.png

How did we implement it?

We have created another table in the database named history, that holds all the transactions.

+CREATE TABLE `history` (
+  `ID` int(11) NOT NULL,
+  `transType` int(11) DEFAULT NULL,
+  `amount` float DEFAULT NULL,
+  `gameid` int(11) DEFAULT NULL,
+  `user1` varchar(255) DEFAULT NULL,
+  `user2` varchar(255) DEFAULT NULL,
+  `win` int(11) DEFAULT NULL,
+  `reward` float DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And every game now creates a new entry in this table.
And for the display, the profile.php searches in this table every entry with your name in it, and depending on the transType, it shows something different.

$query = $db->prepare('SELECT * FROM history WHERE user1 = ? OR user2 = ?');
    $query->bind_param('ss', $_COOKIE['username'], $_COOKIE['username']);

    $query->execute();
    
    $result = $query->get_result();
    $history = "<br>";
    while ($row = $result->fetch_assoc()) {
        if($row['transType'] == 1) {
                $date = date("F j, Y, g:i a T", $row['timestamp']);
            $history =  "
            <h4 style=\"display:inline\">Deposit | </h4><h4 style=\"display:inline;color:green\">+".$row['amount']." SBD</h4> | <h4 style=\"display:inline\">".$date."</h4><br>
            ".$history;
            ..................
  • Added current bets and total bet for roulette

image.png

  • Added Jackpot

image.png

image.png

How did we implement it?

We are using 2 new libraries for the Site Bot

var randomstring = require('randomstring');
var sha = require('sha.js');

Once the bot is started, a game of jackpot is created using this function

function createJackpotGame() {
    
    console.log("\nCreating jackpot game.");
    
    clearInterval(getJackpotTotalInterval);
    getJackpotTotalInterval = setInterval(getJackpotBets, 1000 * 5);
    jackpotTimeTick = 0;
    jackpotGame++;
    con.query("TRUNCATE jackpot", function (err, result) {
    });
    con.query("UPDATE info SET value = 0 WHERE name = 'jackpotstate'", function (err, result) {
    });
    con.query("UPDATE info SET value = " + jackpotGame + " WHERE name = 'jackpotgame'", function (err, result) {
    });
    
    var winnerTicket = math.random(0, 500001);
    winnerTicket = math.floor(winnerTicket);
    
    lastJackpotSecret = jackpotSecret;
    
    jackpotSecret = winnerTicket + "-" + randomstring.generate(100);
    jackpotHash = sha('sha256').update(jackpotSecret).digest('hex');
    
    io.sockets.emit('message', {
        messageType: 6,
        currentHash: jackpotHash,
        lastSecret: lastJackpotSecret,
        timeleft: jackpotTimeTickMax * 5,
        gameid: jackpotGame
    });
}

The jackpot table in the database gets Truncated, jackpotstate is set to 0 and the jackpotgame is set to the current id.

We are choosing a random ticket that is the winner (500.000 tickets in total)
And we are creating a secret with that ticket, looking like: ticket_number-random_100_string, and then we are hashing that secret, after that we are emiting to all sockets the current hash, the last secret, the timeleft and the current game id.

Every 5 seconds, this function is ran

function getJackpotBets() {
    jackpotTimeTick++;
    con.query("SELECT * FROM jackpot", function (err, result) {
        var totalBet = 0, playerBet = [];
        for(var val of result) {
            totalBet += val.bet;
            playerBet.push([val.player, val.bet]);
        }
        
        io.sockets.emit('message', {
            messageType: 5,
            totalBet: totalBet,
            playerBet: playerBet
        });
        if(totalBet >= totalBetJackpot) {
            endJackpotGame(totalBet, playerBet);
        }
        else if(jackpotTimeTick == jackpotTimeTickMax) {
            endJackpotGame(totalBet, playerBet);
        }
    });
}

It checks all the bets in the table, and then emits to all sockets the current bets, if totalBets are > 50 SBD, then the game is ended, or if 10 minutes have passed.

And the function for ending a game

function endJackpotGame(totalBet, playerBet) {
    if(totalBet != 0) {
        clearInterval(getJackpotTotalInterval);
        
        con.query("UPDATE info SET value = 1 WHERE name = 'jackpotstate'", function (err, result) {
        });
        
        con.query("TRUNCATE roulette", function (err, result) {
        });
        
        var ticketsPerSBD = 500000 / totalBet;
        var playerAndTickets = [];
        for(var val of playerBet) {
            playerAndTickets.push([val[0], Math.floor(val[1] * ticketsPerSBD)]);
        }
        
        var winningTicket = jackpotSecret.substr(0, jackpotSecret.indexOf('-'));
        var winner = "", tickets = 0;
        for(var val of playerAndTickets) {
            tickets += val[1];
            if(tickets >= winningTicket) {
                winner = val[0];
                break;
            }
        }
        
        if(tickets < winningTicket) {
            var firstTickets = playerAndTickets[0][1];
            firstTickets += (500000 - tickets);
            playerAndTickets[0][1] = firstTickets;
        }
        
        con.query("SELECT * FROM users WHERE username = '" + winner + "'", function (err, result) {
            var balance = result[0].balance;
            var won = result[0].won;
            var losted = result[0].losted;
            
            var reward = totalBet * 99.5 / 100;
            
            balance += reward;
            won += reward;
            var winnerBet = 0;
            
            for(var val of playerBet) {
                if(val[0] == winner) {
                    winnerBet = val[1];
                    break;
                }
            }
            
            losted -= winnerBet;
            
            con.query("UPDATE users SET balance = '" + balance +"', won = '" + won + "', losted = '" + losted + "' WHERE username = '" + winner + "'", function (er, resul) {
                con.query("UPDATE history SET win = 1, reward = '" + reward + "' WHERE transType = '7' AND user1 = '" + winner + "' AND gameid = '" + jackpotGame + "'", function (e, resu) {
                    setTimeout(createJackpotGame, 20 * 1000);
                    io.sockets.emit('message', {
                        messageType: 7,
                        winner: winner,
                        totalBet: reward,
                        winningTicket: winningTicket,
                        playerAndTickets: playerAndTickets
                    });
                    console.log("Jackpot game ended, total bets: " + totalBet + " SBD.");
                    totalBet = 0;
                });
            });
        });
    } else {
        createJackpotGame();
    }
}



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

Hey @andreistalker I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Utopian Witness!

Participate on Discord. Lets GROW TOGETHER!

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.18
JST 0.032
BTC 88295.05
ETH 3086.02
USDT 1.00
SBD 2.76