I’ve been using Google+ Hangouts a lot, to keep up with friends and do business, so I decided to try my hand at creating a game for their API. Because the act of rolling dice seems like a common thing to want to do, I thought I would share my approach and a library that implements it.
My implementation works a differently in order to prevent tampering. Every time an update is made to the shared state, the key that was changed is timestamped with a millisecond resolution by the server. This is recorded in the metadata for the update. Because this has millisecond resolution, it is sufficiently indeterminate enough to act as a random seed. To actually roll the dice, I send a submitDelta request to the hangouts API with a common key (in this case “HangoutDice.roll”) and a value containing the information on the number and type of dice requested. This will then call the onStateChanged handler with notification of the key’s change. All of the clients can then take the timestamp from the metadata of the key, use it to seed a pure JS RNG, and determine what the roll was. The seeded RNG allows all clients to independently determine the roll with the same results. This results in a very cheat-resistant, distributed dice roller.
You can start a hangout using the library here: http://dice.finalmeasure.com/dice-game/
Please note that at this time there is no good way to share a Hangouts app with other people. The only way for anyone to install it is to create a hangout with the link above, and then click on the app’s button in the hangout. There is no way to share it with other people in a hangout, even if you start it with the above link and then invite others to it. The other participants will have to use the link at least once before it is available.