Utility
const crypto = require('crypto');
// --- BEGIN: Fill these values
// Server seed created when new game is created
const serverSeed = '<SERVER SEED HERE>';
// Hash (ID) of the mined EOS block in the future (assigned at the start of the game)
const clientSeed = '<EOS HASH HERE>';
// Numeric ID of the specific PVP bet (used as the nonce)
const pvpBetId = 9999;
// --- END: Fill these values
// Turn this on if you want to see all the messages
const verboseMode = false;
// Get random roll value using provided information
const rollValue = getRandomRollValue(serverSeed, clientSeed, pvpBetId);
console.log(`Roll value: ${rollValue}`);
/**
* Below this line are algorithmic functions used for calculating a roll value
* =============================================================================
*/
function log(message) {
if (verboseMode) {
console.log(message);
}
}
function getCombinedSeed(serverSeed, clientSeed, nonce) {
return [serverSeed, clientSeed, nonce].join('-');
}
function getRandomRollValue(serverSeed, clientSeed, nonce) {
const min = 1;
const max = 100_000_000;
const rollValue = this.getRandomIntValue(serverSeed, clientSeed, nonce, max - min);
// Get random integer
return rollValue + min;
}
function getRandomIntValue(serverSeed, clientSeed, nonce, maxNumber) {
// Generate seed and hash
const seed = getCombinedSeed(serverSeed, clientSeed, nonce);
log(`Combined seed value: ${seed}`);
const hash = crypto.createHmac('sha256', seed).digest('hex');
// Get value from hash
const subHash = hash.substr(0, 8);
const valueFromHash = parseInt(subHash, 16);
// Get dynamic result for this roll
return Math.abs(valueFromHash) % maxNumber;
}Last updated