Creating a SatoshiDice-like game is rather easy. Below are the high level steps involved in making such a game.
For each incoming transaction...
Get the customer amount and payment address
Call bitcoind getrawtransaction [The incoming transaction ID] 1. The 1 at the end will return the data in "verbose mode", which essentially the raw data in JSON format.
From the raw transaction details, take the txid and vout from the vin object. With this transaction, again call bitcoind getrawtransaction on the txid.
From these raw transaction details find the vout object, referenced by the vout index from step #2. Within this vout object, there should be a list of payment recipient addresses. Take and store the first address.
Determine the Win/Loss state of the round and return the results
Hash the txid+vout index of the transaction. Seed your hash function with a secret key. As an aside note, you will need to provide your secret key on a periodic basis. Without updating and providing your secret keys, there is no way for your players to verify your fairness.
Compare the last four bytes of your hash to determine the win/loss condition.
In case of a loss...
Call bitcoind createrawtransaction. You must pass in the originating txid and vout of your customers payment. With the customers address, include a small, fractional value to send back to the customer. This informs them that they've lost the round. Also, include a second payment address that will receive the remaining portion of bitcoins from the customers originating payment. This should be an address within your wallet.
Call bitcoind signrawtransaction to sign the transaction. If it returns a value of complete=true, your transaction is valid.
Call bitcoind sendrawtransaction and include the above constructed raw transaction.
In case of a win
Calculate the customers winning amount.
Call bitcoind listunspent. This will return a list of inputted transactions that have not been spent by your wallet.
Create a list of transactions to process as payments. First, include the customer's originating txid and vout index of their payment. Next, append to the list any number of unspent transactions (from step #1) that are necessary to pay your customer their winnings.
Remember to include an additional payment address that will receive the remaining portion of bitcoins from the final unspent transaction that may contain overages above the player's winnings.
Call bitcoind createrawtransaction, passing in all of the transactions accumulated in step #3.
Call bitcoind signrawtransaction to sign the transaction.
Call bitcoind sendrawtransaction and include the above constructed raw transaction.
That's it! That's a very simplistic, SatoshiDice processor. You may want to add more validation, cache your transactions, monitor orphaned blocks and just integrate some other administrative routines.
It is worth mentioning that you always include a portion of the customers original payment with all of your return transactions. This is because if their payment is orphaned, or is marked as invalid, you want to be sure that your transaction is also removed from the block-chain.
Using this method you can actually respond near-instantaneously and there is no need for confirmations. If their transaction is malformed and rejected by the network, your transaction will also be thrown out by the network since it is a child of their original transaction.
I guess to get "For each incoming transaction...", you would just call listtransactions? – kristianp – 2013-12-01T03:09:31.113
Yes, or
listsinceblock. – RLH – 2013-12-04T14:30:11.883