Bot Build #7 - Gambling Game

in #utopian-io6 years ago (edited)


Repository

e.g. https://github.com/steemit/steem-js

What Will I Learn?

  • Create Gambling Game Script

Requirements

  • Node.JS, Here
  • Steem Package (Install: npm install steem --save)

Difficulty

  • Intermediate

Tutorial Contents

You will learn how to create gambling game that works with SBD

Curriculum

The Tutorial

Step 1 - Setup, Variables & Functions

, first of all, we need some variables and functions to set up the base.

so we need a percentage, this number will give us the win percentage
we need account name & key Important: At this time you will need to use WIF Active Key, so the guest123 account not going to help you, you can make your own repl.it or nodejs script and put your account and active key!
and we need the minimum gambling amount

const percentage = 50; //If Higher Than This Number, Win.

const ACC_NAME = 'guest123', //Account Name
    ACC_KEY = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg', //Account WIF (Active Key Only Work On This Script)
    MINIMUM_AMOUNT = 0.01; // Minimum Gamble Amount

so just to give it some Life we can add some comments when the script starts

console.log("Gambling Game Script Running...");
setTimeout(function(){console.log("Waiting For Transfers...");}, 1000);

to get the better performance we can connect to a custom WebSocket.

steem.api.setOptions({ url: 'wss://rpc.buildteam.io' }); // Custom Websocket.

You can stay with the default and not change it.

now we need 2 functions, one - the gamble function and second - transfer function

at the gamble function, we will need the gamble account name and the amount of the gamble

function Gamble(Gambler, Amount){

}

at the transfer function, we will need account name, memo and amount to send

function SendTransfer(Name, Memo, Amount){

}

Step 2 - Create The Functions

let's go from the easier to the harder and start with the transfer function.

because the transfer function need a full name of the amount we need to send 0.001 SBD and not 0.001

    Amount = Amount.toString() + " SBD";

We're changing the integer (Amount) to a string and add the SBD to the amount.

and now we just need to send the transfer

    steem.broadcast.transfer(ACC_KEY, ACC_NAME, Name, Amount, Memo, function(err, result) { //Send Transfer
    console.log(err, "Sent " + Amount + " To " + Name + " With The Memo: " + Memo);

ACC_KEY - [Important] The Active Private Key
ACC_NAME - The Sender(You) Account Name
Name - The Reciever Account Name
Amount - The SBD Amount (The Amount Variable)
Memo - The Message (MEMO)

and we send console comment so we can know that we sent the transfer.

And, Here we're done with the transfer function, really simple yea?
let's go to the actual gamble.

first, we need a random number between 1 to 100

const rand = Math.floor(Math.random() * 100) + 1; //Random Number between 1 to 100

this number is the winner / looser number.

now we need to check if the random number is higher than our percentage.

    if(rand > percentage){ //Checking if the random number is higher than our percentage
    let WinnerAmount;
    if(Amount > 0.01){
      WinnerAmount = Amount*1.95;
    }else{
      WinnerAmount = Amount*2; //if the amount is less than 0.01 it will double the win amount because you can't send 0.00195.
    }

    SendTransfer(Gambler, "You Won This Round! Number: " + rand +", Your Gamble - " + Amount + " SBD, You Earned Totally: " + Amount*0.95 + " SBD. Come Back Anytime!", WinnerAmount);
        console.log("Gambler " + Gambler + " Won!");
    }else{
        SendTransfer(Gambler, ":(, You Lose This Round, Your Number: " + rand + ", Best Of Luck Next Time!", 0.001);
        console.log("Gambler " + Gambler + " Lost!");
    }

at first, we check if the random number is higher than the percentage if it is we creating WinnerAmount variable (empty) and check if the gambling amount is higher than 0.01 if it is we can double it by 1.95 (because we need to earn something - 5%)

if it's not we want to double it by 2 because we can't send higher than 3 digit number (example: 0.00195)

Note: this will work only if you down the minimum number

now we just need to send the transfer with a message/memo that he wins and how much he actually earn, for example, if he sends 0.01 he earn 0.0095.

and again we send a comment to the console so we can know if someone wins

now if the gambler lost we send transfer with message/memo that he loose with 0.001 (Amount)

and again we send a comment to the console.

Here we done with the functions, now we just need to "listen"/get transfers and run the function

Step 3 - Read Transfers And Make The Gamble

first of all, like the latest tutorials we want to listen for transfer/transaction

steem.api.streamTransactions('head', function(err, result) { //Streaming The latest Transactions That Goes Through The Steem Blockchain

}

this function receives any transaction that goes through the steem blockchain, and the transfers we need to get.

now we need 2 variables, one is the type of the transaction and the second is the data that we get with the transaction.

    let type = result.operations[0][0]; // transaction type
    let data = result.operations[0][1]; // transaction data

we need to check if the type is transfer and if the receiver is our account.

if(type == 'transfer' && data.to == ACC_NAME){ // Checking if transaction type is `transfer` and check if the reciever is our accout.

}

now because I want the gambles only as SBD gamble we will get every STEEM as a donation, we need to check if the amount is a STEEM token.

if(data.amount.split(' ')[1] == "STEEM"){ // Checking The Token Type
                    SendTransfer(data.from, "Thanks for you donation, we really appreciate that. if this is not a donation please contact us!", 0.001);
                    console.log("Donation From " + data.from + ", " + data.amount);
}

if it is we send a "thank you" message and sending a comment to the console.

now we need to check if the amount is in SBD balance and start the gamble.

else{
    if(data.memo == "Gamble"){ //Checking If The Transfer Is For Gamble
        if(data.amount.split(' ')[0] < MINIMUM_AMOUNT){ // Checking If The Amount Is Below The Minimum Amount
            console.log("Wrong Gamble, reason: below the minimum.");
            SendTransfer(data.from, "Your Gamble Amount Is Less Than The Minimum, The Minimum Is " + MINIMUM_AMOUNT + " SBD", data.amount);
        }else{
            console.log("Incoming Gamble From " + data.from);
            Gamble(data.from, data.amount.split(' ')[0]);
        }
    }
}

first to know that is a gamble we want to check if the sender actually wants to gamble, so we check if the memo is Gamble.
if it is we check if the Amount is higher than the minimum.
if it's not we send a comment to the console and sending back the amount to the sender with a message that the amount is less than our minimum.

if the amount is higher than the minimum we send a comment to the console and start the gamble.

from here the functions make the rest.

And, we're done with the script.
it's little long but it's really cool script.

Note: this script tested on my own server with my user and it's work perfect

SteemD proofs:

the 2 in the middle is an example of a bad test.

You can check the full work at my repl.it account and other bots that I create!

Proof of Work Done

the work made in Repl.it, https://repl.it/@lonelywolf22/Steem-Bots-Gambling-game-V01-Done
user: https://repl.it/@lonelywolf22

GitHub: https://github.com/lonelywolf1
Utopian Tutorials GitHub Repository: https://github.com/lonelywolf1/Bot-Projects-SteemJS

Sort:  

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly & Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Work on improving your post for English mistakes and incorrectly written words and sentences, as those could lead your author to lose track of your purpose.
  • It would be interesting to have the code of your tutorial in github.

Looking forward to your upcoming tutorials.


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

Thank you!
I'll work on my mistakes and I'll upload the projects to github, thanks.

I've added a new repository that I made for the utopian tutorials and I fixed the grammar problems, thank you

Hey @lonelywolf
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!

Coin Marketplace

STEEM 0.32
TRX 0.12
JST 0.034
BTC 64664.11
ETH 3166.18
USDT 1.00
SBD 4.11