Friday, January 3, 2014

How to create your own satoshidice game

SatoshiDice introduced new and unique way of gaming / gambling online. It's the concept of wallet blockchain games. On the first sight isn't something special and probably we will say: OK no big deal yet another casino web site but there is much more behind it. As tech person my first question was how it's working and surfing all around there were no resources that explain how is created or better  to create similar game as your own product.

Then I started the research and here it's guided PoC framework scripts that show how to create your own blockchain based wallet game giving you freedom to use it on any size project. This could be applied on any crypto currency that is based on bitcoind and has the same API calls and ofc. you can implement any game with it.

Project: https://github.com/Slavco/dicestart

Requirements:
  • feathercoind, bitcoind or any another daemon ( will work with qt clients for test & isn't recomended for production environment )
  • mysql db server
  • php installed  

Configuration:

Follow the comments in the code. Basically you will need to set up your:
  • feathercoind, bitcoind, anycoind username and password 
  • path to the php executable 
  • generate storage address
  • generate play addresses
  • create game secret ( to prove your users that you are fair. Share with clients hash of your server key and not the server key) 
  • mysql username and password 
  • include scripts path 

When everything is set up you can test your system. To start the scripts just go to the console on your operating system and issue this command:  (path_to_php_executable) php go.php

Now you can send some amount between min - max bet and test the game. Lets take a look in the test game script. Here you will notice the following:
  • Get the arguments: plat transaction, odd and odd address
  • Load configuration
  • Connect to the DB
  • Connect to the coindaemon
  • Set max & min bet for this game and later decide what to do with incoming funds that are out of min - max boundary 
  • Calculate player address ( where to return the funds win or loose)
  • Perform the play ( according the play formula )
  • When player loose send the dust to the player address and rest of the coins to the storage address
  • When player wins then you must be careful! Loop trough unspent transactions inputs until you aggregate sum >= win+transaction fee, send to the player win amount and the rest send back to the your storage address because will be given to the network / miners. 
  • Create transaction, sign transaction & broadcast transaction to the network
  • Log the play data in the logs  


Why we create / hold the play logs? From the play logs we can publish data to the betting website where users will be able to see the latest plays. How to do that in most efficient way:

  • get the last N plays with: tail -N /path_to/playlogs/play_log2013-12-24.txt
  • hash the output
  • read output line by line and prepare for sending on the clientside
  • send to the client hash + data and add hash to the session
  • client pools the server with hash
  • if hash is the same, no new plays if not send new hash and data
  • on client side display only new data 

With this approach we show / propose architecture simple but very powerful for blockchain based wallet game.
  • Transactions receiving scripts could be on any machine that is able to run somecoind on it
  • If game is popular this scripts could be split ed on many machines that will use same db server ( to check if incoming tx is processed or not ) 
  • If game is popular ( too many transactions exec command in go.php could be easy changed with call to gearman, rabbitmq, ... )  
  • Logs could be pushed online via any protocol and displayed on the website for the users and because everything is generated this website could be easy hosted on any shared hosting
I hope you like this project.

BTC: 1LuckAsHuFJ3Y59aztSxzHaBtRihc2vDmX
FTC: 6sLaveXfN6Nhi96LVufa4GjrojGnXsbc4w