Python - Magic Dice Roller ๐ŸŽฒ

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
ยท@aro.steemยท
0.000 HBD
Python - Magic Dice Roller ๐ŸŽฒ
#### Repository
https://github.com/nnmix/py-magicdice-bot

#### What Will I Learn?
- You will learn to automate a @magicdice Bot given your own rules
- You will learn to use Steem - Python Lib
- You will learn to program your own 'Rule Based' dice rolls

[![Magic-Dice-Google-Chrome-2019-03-24-16-46-44_Trim.gif](https://ipfs.busy.org/ipfs/QmY2MeYeW9wdef5g3jQJvowC1FF1ZxQoJwR459TPoaxTHJ)](https://magic-dice.com/?ref=aro.steem)


#### Requirements
#####   Personal Prerequisite
- **Existing** [STEEM Account](https://www.steemit.com/) 
![](https://cdn.steemitimages.com/DQmbDGEdyn9Fa9cR57EPAvKUtnZVP3tJMrmc7oBhX18ixSb/image.png)
#####   System prerequisites:
- [Jupyter](https://jupyter.org/) notebook for python 
#####   Knowledge prerequisites:
- Basic understanding of python programming

#### Difficulty
- Intermediate

#### Description
In this tutorial you will get the ability to automate your own flavored Dice Roller Bot for @magicdice on STEEM

#### Content
Before getting to the python scripting section you will need a STEEM account token to be used.
#####   1. STEEM Account - Private Active Keys
#####   2. Jupyter / Python

* You can access Jupyter [Online](https://jupyter.org/try) or [Intall](https://jupyter.org/install.html) it locally on your PC
* When done open a new python file: ![](https://cdn.steemitimages.com/DQmYGetfkacNcxCyftrR9AqGWU9444UfnzasdkSiggcBPE2/image.png)

* When the file is open, if you are on the online version, you can delete existing sample cell by clicking the cell border (blue highlight) than double pressing 'D'
![](https://cdn.steemitimages.com/DQmR6iKAeyV4nx2e83dECAJTrNCbjwiZ6qV8ahPjFjjmqmG/image.png)
Until you have an empty cell to write your script within:
![](https://cdn.steemitimages.com/DQmbttAp2p6xcqCpzut1mCcDbPpGFPNAA6Jt23V2UW6ihKW/image.png)

* Now your jupyter editor is up you can start typing your code and run it by pressing **ctrl+enter**

#####   3. [Magic Dice](https://magic-dice.com/?ref=aro.steem) Roller/ Python
Let us start talking about the magicdice roller implementation

* Using [Python Steem Lib](https://steem.readthedocs.io/en/latest/install.html) (pip install steem)
* First Doing The Dice Roll by sending a transaction with Memo inside containing 
 - the bet STEEM amount
 - the target with 'under' or 'over'
 - the client rand seed which is a sha256 of a random of length 10
![](https://cdn.steemitimages.com/DQmQc1N8gbm2B6MWJdTgz39hWVfTyJrdLb4pNov6WBFfHMY/image.png)

![](https://cdn.steemitimages.com/DQme62oxVKHpi2xN1yVMCQKvyi3v74wJZnMTK3SSkiciwQ5/image.png)

* Then Wait For An Incoming Transaction Result From Magic Dice:
![](https://cdn.steemitimages.com/DQmRGhhhg3pjF4xTKKaYMKs6Z49Vmw9i3aSaxQZ2yFGQ2yA/image.png)

````
# Magic Dice Roll Script Sample
# Replace <private active keys by yours>

from steem import Steem
import hashlib, binascii, random, time

def randhash():
    m = hashlib.sha256()
    m.update((str(random.randint(1,101)*time.time())).encode('utf-8'))
    m.update((m.hexdigest() + str(random.randint(1,101))).encode('utf-8'))
    x = m.hexdigest()
    return x[2:12]

def newTx(s, amount, direction, target):
    randh = randhash() 
    query = "{0} {1} {2}".format(direction, target, randh)
    s.commit.transfer('magicdice',amount,'STEEM', memo = query, account="aro.steem")
    return randh

def roll(steem, amt) : 
    

    randh = newTx(steem,  amt, 'under', 30)
    
    txId = '-1'
    won = 0
    
    randh = str(randh)
    
    # wait for the tx id
    while True :
        dta = steem.get_account_history('aro.steem', index_from=-1, limit=20)
        for i in range(len(dta)):
            dd = dta[i][1]
            if dd['op'][0] == 'transfer' and dd['op'][1]['to'] == 'magicdice' and dd['op'][1]['memo'].find(randh) > -1:
                txId = str(dd['trx_id'])
                break
        if str(txId) != '-1':
            break
        time.sleep(2)
    
    # wait for the result
    while True :
        dta = steem.get_account_history('aro.steem', index_from=-1, limit=20)
        for i in range(len(dta)):
            dd = dta[i][1]
            if dd['op'][0] == 'transfer' and dd['op'][1]['from'] == 'magicdice' and dd['op'][1]['memo'].find(txId) > -1:
                if dd['op'][1]['amount'] == '0.001 STEEM':
                    won = -1
                else:
                    won = 1
                break
        if won != 0:
            break
        time.sleep(2)
    return txId, won, randh

s = Steem(keys=["<private active keys>", "<private active keys>"])

st = time.time()

# do one dice roll

txId, won, randh = roll(s, 0.1)

et = time.time()

# show results: wait time, transaction Id, win as 1 and lost as -1
print(et - st, txId, won)

````


#### The End! 
Finally most of the steps are covered, it is your time to configure your own rules / algorithm that may give you your optimal winning payouts.
  
**Feel free** to share and list your own automation python bot script on the project github project page with your fresh good results for others to try ! 
  
#### Related
- [My steemit announcement: https://steemit.com/utopian-io/@aro.steem/dice-roller-bot-python-primedice)

#### Proof of Work Done
https://github.com/nnmix/py-magicdice-bot/blob/master/script.py

๐Ÿ‘ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,