[V0.1.4] - SteemCasino - Roulette, Dices and Slots are now provably fair.

in #utopian-io6 years ago (edited)

Repository

https://github.com/andreistalker/steemcasino

Pull Request

https://github.com/andreistalker/steemcasino/pull/27

New Features

  • Jackpot now shows it's secret and hash, it was always there, but hidden in the console.

To implement this, every time a new jackpot starts, a new secret and hash are received and changed using those lines.

$("#hash").text("Current hash: " + msg['hash']);
$("#secret").text("Last round secret: " + msg['lastSecret']);
  • We are now generating the blackjack decks using a seed, so blackjack is now provably fair.

A random seed is created using the mt_rand function, salted using the generateSecret function that returns a random string with the first letter being A or B, and the secret is getting hashed, the hash will be shown to the player after the game starts.

$seed = mt_rand();
+                   
+                   $secret = $seed."-".generateSecret();
+                   $hash = hash("sha256", $secret);
+                   
+                   $deck = createDeck($seed);

The createDeck function, firstly creates a default deck with all the cards in order like this: 2 of hearts, 2 of diamonds, 2 of spades, 2 of clubs, etc.... Then the deck is shuffled using the seed.

function createDeck ($seed) {
    
    $deck = [];
    
    for($i = 2; $i <= 14; $i++ ) {
        array_push($deck, [$i, "A"]);
        array_push($deck, [$i, "B"]);
        array_push($deck, [$i, "C"]);
        array_push($deck, [$i, "D"]);
        
    }
    $deck = shuffleDeck($deck, $seed);
    return $deck;
}

function shuffleDeck(&$items, $seed)
{
    @mt_srand($seed);
    for ($i = count($items) - 1; $i > 0; $i--)
    {
        $j = @mt_rand(0, $i);
        $tmp = $items[$i];
        $items[$i] = $items[$j];
        $items[$j] = $tmp;
    }
    
    return $items;
}

The secret is shown after the game has finished so the player can take the seed out of the secret and recreate the deck using the previous 2 functions.

  • Roulette is now provably fair.

Now, after each game finishes we are generating the next roll and salting it then hashing it and showing it to the players so they know that we don't change the result. The hash is shown when the betting round starts and the secret is shown after the roll happend.

+      rouletteRoll = roll();
+   var secretos = rouletteRoll + "-" + randomstring.generate(100);
+   rouletteHash = sha('sha256').update(secretos).digest('hex');
  • Dices and Slots are now provably fair.
    Those 2 work almost the same and will explain only dices.

Now, you will need to provide your own secret when you play dices, the secret you provide will change the outcome of the game.

You can see a hash on that photo. A new secret is now created when you first join the website or after a game. The secret will only change when you create a new game. The first generateSecret acts as the seed and the second as the salt for the hash.

$dices = generateSecret()."-".generateSecret();

So, how do you influence the outcome of the dice roll????

Simple, we're now using a hash_hmac function that takes your secret and uses it as a seed.
The hash_hmac function will return a 128 hex hash. We're taking the first 5 characters and we're converting in base 10, the number will be between 0 and 1048575, if the number is over 1000000 we're taking the next 5 characters and converting in base 10, if all the numbers are over 1000000 then the roll will always be 4500, but this is extremely rare. When we find a number under 1000000 we're dividing it with 100 giving us a number between 0 and 9999 which is perfect for our roll. Here is the function.

function dicesPick($seed, $secret) {
    $result = hash_hmac('sha512', $seed, $secret);
    
    $found = 0;
    $new = 0;
    
    for($i = 5; $i < 128; $i += 5) {
        $new = substr($result, ($i - 5), $i);
        $new = base_convert($new, 16, 10);
        if($new <= 999999) {
            $found = 1;
            break;
        }
    }
    
    if($found == 0)
        $new = 4500;
    else
        $new /= 100;
    
    return floor($new);
}

And here is the function for the slots. (It is run 3 times)

function slotPick($seed, $secret) {
    $result = hash_hmac('sha512', $seed, $secret);
    
    $found = 0;
    $new = 0;
    
    for($i = 5; $i < 128; $i += 5) {
        $new = substr($result, ($i - 5), $i);
        $new = base_convert($new, 16, 10);
        if($new <= 1000000) {
            $found = 1;
            break;
        }
    }
    
    if($found == 0)
        $new = 1;
    else
        $new /= 10000;
    
    $pick = floor($new);
    
    if($pick <= 30)
        return 0;
    else if($pick <= 60)
        return 3;
    else if($pick <= 75)
        return 2;
    else if($pick <= 90)
        return 1;
    else if($pick <= 97)
        return 4;
    else
        return 5;
}

And if the player wants to verify he will use this functions.

  • Implemented Insurance and Double Down for Blackjack
Sort:  

Thank you for your contribution. Though you have commented and added as a commit, but its always nice to comment as you write a function or code.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


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

Hey @andreistalker
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @andreistalker! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - The results, the winners and the prizes

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @andreistalker! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemFest³ - SteemitBoard support the Travel Reimbursement Fund.

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @andreistalker! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 5000 upvotes. Your next target is to reach 6000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Saint Nicholas challenge for good boys and girls

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @andreistalker! You received a personal award!

DrugWars Early Access
Thank you for taking part in the early access of Drugwars.

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @andreistalker! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @andreistalker! You received a personal award!

Thank you for the witness votes you made to support your Steem community and for keeping the Steem blockchain decentralized

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Use your witness votes and get the Community Badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.23
TRX 0.28
JST 0.042
BTC 104956.85
ETH 3880.98
SBD 3.32