Created
November 14, 2014 03:26
-
-
Save kungfuant/60710b68a45d56399ec7 to your computer and use it in GitHub Desktop.
MoneyPot new crashpoint generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'); | |
function divisible(hash, mod) { | |
/* Reduce the hash digit by digit to stay in the signed 32-bit integer range. */ | |
var val = hash.split('').reduce(function(r,d) { | |
return ((r << 4) + parseInt(d,16)) % mod ; }, 0); | |
return val == 0; | |
} | |
function genCrashpoint(serverSeed, clientSeed) { | |
/* Calculate the hash using HMAC-SHA256 */ | |
var hash = crypto.createHmac('sha256', serverSeed); | |
hash = hash.update(clientSeed).digest('hex'); | |
/* In 1 of 101 games the game crashes instantly. */ | |
if (divisible(hash, 101)) | |
return 0; | |
/* Use the most significant 52-bit from the hash | |
to calculate the crash point */ | |
var h = parseInt(hash.slice(0,52/4),16); | |
var e = Math.pow(2,52); | |
/* Assuming the 52-bit prefix is uniformly distributed | |
then r is uniformly distributed over [0,1). */ | |
var r = h / e; | |
/* Perfect is the perfectly continuous distributed | |
multiplier for a zero sum game. */ | |
var perfect = 1 / (1 - r); | |
/* Apply a house edge to the perfect distribution. */ | |
var houseEdge = (perfect-1) * 0.01; | |
var multiplier = perfect - houseEdge; | |
// return Math.floor(multiplier * 100); | |
/* Inlining and simplifying the above yields the following version | |
which is slightly more numerically stable. The multiplication | |
100 * e still leaves the exactly representable integers. | |
*/ | |
return Math.floor((100 * e - h) / (e - h)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment