[V0.1.3] - SteemCasino - Blackjack added

in #utopian-io7 years ago

Repository

https://github.com/andreistalker/steemcasino

Pull Request

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

Bug Fixes

  • Fixed a bug where you could withdraw/bet negative amounts.
    Simply change $_GET['bet'] != 0 to $_GET['bet'] > 0 to fix it.

New Features

  • Added more bet options to roulette, individual numbers, odd and even numbers, 1 - 12, 13 - 24, 25 - 36.

In the site bot when the bot updates the bets and sends them to the socket.io users a new customPlayers and customBet variables we're added that are the new bets.

 else {
+               customBet += val.bet;
+               customPlayers.push([val.player, val.bet, val.beton]);
            }

Now we send info about the custom bets.

        io.sockets.emit('message', {
            messageType: 4,
            redBet: redBet,
            blackBet: blackBet,
            greenBet: greenBet,
+           customBet: customBet,
            redPlayers: redPlayers,
            blackPlayers: blackPlayers,
            greenPlayers: greenPlayers,
+           customPlayers: customPlayers,
        });
    });
 }

This chunk of code calculates if the number was odd or if it was part of a specific row. (0 and 00 can't be part of odd/even or specific rows, so this calculates only if the roll wasnt 0 or 00.)

+       var odd = 0, row = 0;
+       
+       if(currRoll != 0 || currRoll != 37) {
+           if(currRoll % 2)
+               odd = 7;
+           else if (!(currRoll % 2))
+               odd = 8;
+           
+           if(currRoll <= 12)
+               row = 4;
+           else if(currRoll <= 24)
+               row = 5;
+           else if(currRoll <= 36)
+               row = 6;
+       }
+       
+       win(color, currRoll, odd, row);

And the last thing changed for this to be added is the query, now it searches also for specific numbers, row and odd/even.

-   con.query("SELECT * FROM roulette WHERE beton = " + color, function (err, result) {
+   con.query("SELECT * FROM roulette WHERE beton = " + color + " OR beton = " + (100 + currRoll) + " OR beton = " + odd + " OR beton = " + row, function (err, result) {

In the javascript this was added to display the custom bets in it's specific row.

+       $.each(msg['customPlayers'], function(i, value) {
+           var bettd = "";
+           
+           if(value[2] >= 100) {
+               bettd = value[2] - 100;
+               if(bettd == 37)
+                   bettd = "00";
+           } else if(value[2] == 7)
+               bettd = "Odd";
+           else if(value[2] == 8)
+               bettd = "Even";
+           else if(value[2] == 4)
+               bettd = "1 - 12";
+           else if(value[2] == 5)
+               bettd = "13 - 24";
+           else if(value[2] == 6)
+               bettd = "25 - 36";
+           
+           customPlayers = '<div><img width="10%" style="vertical-align:middle" src="https://steemitimages.com/u/'+ value[0] +'/avatar"> - ' + value[0] + ' - ' + value[1] + ' SBD - ' + bettd + '</div><div style="width:100%;height:1px;margin-top:2px;margin-bot:2px"></div>' + customPlayers;
+       });
  • Added last 3 rolls to dices.

Every time you roll the dice this variable is updating with the info from the roll and it's updating the other variable lastRolls.

var lastRoll = {
+                               rollUnder: data['under'],
+                               multiplier: data['multiplier'],
+                               pick: data['pick'],
+                               reward: (data['reward']).toFixed(3),
+                               win: data['win'],
+                               bet: data['bet'],
+                           };
+                           
+                           lastRolls = {
+                               roll1: lastRoll,
+                               roll2: lastRolls['roll1'],
+                               roll3: lastRolls['roll2'],
+                           };

After they get updated, this function runs that updates the interface with the new info.

function updateLastRolls() {
+               var lastRollsDiv = "", winz, winz2;
+               if(lastRolls['roll3']) {
+                   if(lastRolls['roll3']['win']) {
+                       winz = "<h1 id=\"under-win\">";
+                       winz2 = "+" + lastRolls['roll3']['reward'];
+                   }
+                   else {
+                       winz = "<h1 id=\"under-lose\">";
+                       winz2 = "-" + lastRolls['roll3']['bet'];
+                   }
+                   
+                   lastRollsDiv = "<div id=\"lastRoll\"><center>" + winz + "<" + lastRolls['roll3']['rollUnder'] + " | x" + lastRolls['roll3']['multiplier'] + " | " + winz2 + " SBD | " + lastRolls['roll3']['pick'] + "</h3></center></div>" + lastRollsDiv;
+               }
+               
+               if(lastRolls['roll2']) {
+                   if(lastRolls['roll2']['win']) {
+                       winz = "<h1 id=\"under-win\">";
+                       winz2 = "+" + lastRolls['roll2']['reward'];
+                   }
+                   else {
+                       winz = "<h1 id=\"under-lose\">";
+                       winz2 = "-" + lastRolls['roll2']['bet'];
+                   }
+                   
+                   lastRollsDiv = "<div id=\"lastRoll\"><center>" + winz + "<" + lastRolls['roll2']['rollUnder'] + " | x" + lastRolls['roll2']['multiplier'] + " | " + winz2 + " SBD | " + lastRolls['roll2']['pick'] + "</h3></center></div>" + lastRollsDiv;
+               }
+               
+               if(lastRolls['roll1']) {
+                   if(lastRolls['roll1']['win']) {
+                       winz = "<h1 id=\"under-win\">";
+                       winz2 = "+" + lastRolls['roll1']['reward'];
+                   }
+                   else {
+                       winz = "<h1 id=\"under-lose\">";
+                       winz2 = "-" + lastRolls['roll1']['bet'];
+                   }
+                   
+                   lastRollsDiv = "<div id=\"lastRoll\"><center>" + winz + "<" + lastRolls['roll1']['rollUnder'] + " | x" + lastRolls['roll1']['multiplier'] + " | " + winz2 + " SBD | " + lastRolls['roll1']['pick'] + "</h3></center></div>" + lastRollsDiv;
+               }
+
+               
+               $("#lastRolls").html(lastRollsDiv);
+           }
  • Mines balance now updates when you win/start a new game.

  • Added Blackjack.

There is a new src script, blackjack.php that it's the backend of the blackjack, firstly the script checks if you're logged in, then it checks what action did you want to do, hit, new game, stand, surrender, insurance, double down or split, then it checks if it's your game and if the action is valid at that time.

The new function.

The script checks if you bet more than 0.001, then it checks if you have enough money, if you do then a new deck is created using this function.

+function createDeck () {
+   
+   $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"]);
+       
+       shuffle($deck);
+   }
+   shuffle($deck);
+   return $deck;
+}

And then the player draws the first two cards and the house draws 2. The drawing is done by these 2 functions.

+function drawCards($deck, $howmany = 1, $hand = []) {
+   for($i = 0; $i < $howmany; $i++)
+       array_push($hand, $deck[$i]);
+   
+   return $hand;
+}

+function removeCards($deck, $howmany = 1) {
+   for($i = 0; $i < $howmany; $i++)
+       unset($deck[$i]);
+   
+   $deck = array_values($deck);
+   
+   return $deck;
+}

Then it checks if the player has blackjack, if the player has, he wins if the house does not have blackjack too and draws if the house has.

And the game gets inserted into the database.

The hit function.

The hit function firstly checks if it's your game, then if it's finished, if it's not finished then it draws a card for the player.

$playerHand = drawCards($deck, 1, $playerHand);

Now it checks the points of the player using this function

+function checkPoints($hand) {
+   $points = 0;
+   $aces = 0;
+   
+   for($i = 0; $i < count($hand); $i++) {
+       if($hand[$i][0] <= 10)
+           $points += $hand[$i][0];
+       else if($hand[$i][0] > 11)
+           $points += 10;
+       else if($hand[$i][0] == 11)
+           $aces++;
+   }
+   
+   if($aces == 1) {
+       if($points + 11 <= 21)
+           $points += 11;
+       else
+           $points += 1;
+   } else if($aces == 2) {
+       if($points + 12 <= 21)
+           $points += 12;
+       else
+           $points += 2;
+   } else if($aces == 3) {
+       if($points + 13 <= 21)
+           $points += 13;
+       else
+           $points += 3;
+   } else if($aces == 4) {
+       if($points + 14 <= 21)
+           $points += 14;
+       else
+           $points += 4;
+   }
+   
+   return $points;
+}

If the player has over 21 he loses, if he has under the game continues and if he has 21 then:

  1. If the house has blackjack he loses.
  2. If house has 17 or more he wins.
  3. If house has under 17 then draws, and if house draws to 21 the game it's a draw, if not the player wins.

This is the function that calculates if the house draws or not:

+function drawHouse($house, $deck) {
+   $house = drawCards($deck, 1, $house);
+   $deck = removeCards($deck);
+   
+   if(checkPoints($house) >= 17)
+       return $house;
+   else
+       return drawHouse($house, $deck);
+}

And then the result is registred into the database.

And then the stand function, firstly it checks if it's your game and if it's not finished.
Then it runs the logic for the house, if it has under 17 it draws.
After that it checks who has more points and didn't go over 21.

GitHub Account

https://github.com/andreistalker

Sort:  

Thank you for your Contribution. A nice explanation of the added feature though commenting the code is always better.

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]

Ok, I'll try to do better with the commenting :) Thanks

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!