Bot Build #7 - Gambling Game by lonelywolf

View this thread on steempeak.com
· @lonelywolf · (edited)
$70.09
Bot Build #7 - Gambling Game
![](https://steemitimages.com/DQmPESUnnEycPYAh7HWVU8XfdU4ypzZXLDgjrGAQWpZsxXS/image.png)

---

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

#### What Will I Learn?
- Create Gambling Game Script

#### Requirements

- Node.JS, [Here](https://nodejs.org/en/)
- Steem Package (Install: npm install steem --save)

#### Difficulty
- Intermediate

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

#### Curriculum

- [Bot Build #1](https://utopian.io/utopian-io/@lonelywolf/build-bot-using-steem-js-1-upvoting-bot-by-friend-list-or-by-trending-posts)
- [Bot Build #2](https://utopian.io/utopian-io/@lonelywolf/bot-build-2-auto-follow-bot)
- [Bot Build #3](https://utopian.io/utopian-io/@lonelywolf/bot-build-3-auto-reward-claimer)
- [Bot Build #4](https://steemit.com/utopian-io/@lonelywolf/bot-build-4-resteem-bot-with-friend-list)
- [Bot Build #5](https://steemit.com/utopian-io/@lonelywolf/bot-build-5-curation-trail)
- [Bot Build #6](https://steemit.com/utopian-io/@lonelywolf/bot-build-6-simple-paid-upvoting-bot)
- [SteemJS Tutorials #1](https://utopian.io/utopian-io/@lonelywolf/steem-js-tutorials-1-basics-validating-wif-key)
- [NodeJS Tutorials #1](https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js)

#### 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:
![](https://steemitimages.com/DQmQD42egcDQPL3X13Nky1FvHnhxMqU2jwLNs1PySQ1bz38/image.png)

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
👍  , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id48,149,897
authorlonelywolf
permlinkbot-build-7-gambling-game
categoryutopian-io
json_metadata"{"links": ["/exit?url=https://github.com/steemit/steem-js", "/exit?url=https://nodejs.org/en/", "/exit?url=https://utopian.io/utopian-io/@lonelywolf/build-bot-using-steem-js-1-upvoting-bot-by-friend-list-or-by-trending-posts", "/exit?url=https://utopian.io/utopian-io/@lonelywolf/bot-build-2-auto-follow-bot", "/exit?url=https://utopian.io/utopian-io/@lonelywolf/bot-build-3-auto-reward-claimer", "/exit?url=https://steemit.com/utopian-io/@lonelywolf/bot-build-4-resteem-bot-with-friend-list", "/exit?url=https://steemit.com/utopian-io/@lonelywolf/bot-build-5-curation-trail", "/exit?url=https://steemit.com/utopian-io/@lonelywolf/bot-build-6-simple-paid-upvoting-bot", "/exit?url=https://utopian.io/utopian-io/@lonelywolf/steem-js-tutorials-1-basics-validating-wif-key", "/exit?url=https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js"], "users": ["lonelywolf", "lonelywolf22"], "community": "busy", "tags": ["utopian-io", "tutorials", "steemjs", "bot", "steem"], "app": "busy/2.4.0", "image": ["https://steemitimages.com/DQmPESUnnEycPYAh7HWVU8XfdU4ypzZXLDgjrGAQWpZsxXS/image.png"], "format": "markdown"}"
created2018-05-12 06:23:45
last_update2018-05-12 10:44:18
depth0
children5
net_rshares15,173,378,887,422
last_payout2018-05-19 06:23:45
cashout_time1969-12-31 23:59:59
total_payout_value52.805 SBD
curator_payout_value17.286 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length8,729
author_reputation16,511,167,294,275
root_title"Bot Build #7 - Gambling Game"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (21)
@resteemable ·
**Your Post Has Been Featured on @Resteemable!** <br> Feature any Steemit post using resteemit.com! <br> **How It Works:** <br> 1. Take Any Steemit URL <br> 2. Erase `https://` <br> 3. Type `re`<br> Get Featured Instantly & Featured Posts are voted every 2.4hrs <br>[Join the Curation Team Here](https://goo.gl/forms/4sr0InoTxcyPRQSj2) | [Vote Resteemable for Witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=resteemable&approve=1)
properties (22)
post_id48,161,304
authorresteemable
permlinkre-resteemable-bot-build-7-gambling-game-20180512t080800593z
categoryutopian-io
json_metadata{}
created2018-05-12 08:08:00
last_update2018-05-12 08:08:00
depth1
children0
net_rshares0
last_payout2018-05-19 08:08:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length453
author_reputation711,577,524,471
root_title"Bot Build #7 - Gambling Game"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@portugalcoin ·
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](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
post_id48,170,726
authorportugalcoin
permlinkre-lonelywolf-bot-build-7-gambling-game-20180512t093620869z
categoryutopian-io
json_metadata"{"links": ["https://support.utopian.io/", "https://discord.gg/uTyJkNm", "https://join.utopian.io/"], "app": "steemit/0.1", "tags": ["utopian-io"]}"
created2018-05-12 09:36:21
last_update2018-05-12 09:36:21
depth1
children2
net_rshares0
last_payout2018-05-19 09:36:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length600
author_reputation214,343,891,436,406
root_title"Bot Build #7 - Gambling Game"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@lonelywolf ·
Thank you!
I'll work on my mistakes and I'll upload the projects to github, thanks.
properties (22)
post_id48,175,899
authorlonelywolf
permlinkre-portugalcoin-re-lonelywolf-bot-build-7-gambling-game-20180512t102246424z
categoryutopian-io
json_metadata"{"app": "busy/2.4.0", "community": "busy", "tags": ["utopian-io"]}"
created2018-05-12 10:22:48
last_update2018-05-12 10:22:48
depth2
children0
net_rshares0
last_payout2018-05-19 10:22:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length83
author_reputation16,511,167,294,275
root_title"Bot Build #7 - Gambling Game"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@lonelywolf · (edited)
I've added a new repository that I made for the utopian tutorials and I fixed the grammar problems, thank you
properties (22)
post_id48,178,520
authorlonelywolf
permlinkre-portugalcoin-re-lonelywolf-bot-build-7-gambling-game-20180512t104511888z
categoryutopian-io
json_metadata"{"app": "busy/2.4.0", "community": "busy", "tags": ["utopian-io"]}"
created2018-05-12 10:45:12
last_update2018-05-12 10:45:39
depth2
children0
net_rshares0
last_payout2018-05-19 10:45:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length109
author_reputation16,511,167,294,275
root_title"Bot Build #7 - Gambling Game"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@utopian-io ·
Hey @lonelywolf
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Contributing on Utopian**
Learn how to contribute on <a href='https://join.utopian.io'>our website</a> or by watching <a href='https://www.youtube.com/watch?v=8S1AtrzYY1Q'>this tutorial</a> on Youtube.

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

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
post_id48,407,485
authorutopian-io
permlinkre-bot-build-7-gambling-game-20180513t230005z
categoryutopian-io
json_metadata"{"app": "beem/0.19.29"}"
created2018-05-13 23:00:06
last_update2018-05-13 23:00:06
depth1
children0
net_rshares0
last_payout2018-05-20 23:00:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length507
author_reputation152,913,012,544,965
root_title"Bot Build #7 - Gambling Game"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000