Skip to content

Instantly share code, notes, and snippets.

@zcrash
Created June 29, 2017 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zcrash/909b31f086a8f172ce6e727c1e738d61 to your computer and use it in GitHub Desktop.
Save zcrash/909b31f086a8f172ce6e727c1e738d61 to your computer and use it in GitHub Desktop.
....
exports.genGameHash = function(serverSeed) {
return crypto.createHash('sha256').update(serverSeed).digest('hex');
};
function divisible(hash, mod) {
// We will read in 4 hex at a time, but the first chunk might be a bit smaller
// So ABCDEFGHIJ should be chunked like AB CDEF GHIJ
var val = 0;
var o = hash.length % 4;
for (var i = o > 0 ? o - 4 : 0; i < hash.length; i += 4) {
val = ((val << 16) + parseInt(hash.substring(i, i+4), 16)) % mod;
}
return val === 0;
}
// newest for zcash block 135400
var clientSeed ='0000000003a96dc6e52672a487ec577fc32e71a8f99bdbbf7329bf27f9772544';
exports.crashPointFromHash = function(serverSeed) {
var hash = crypto.createHmac('sha256', serverSeed).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);
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