Skip to content

Instantly share code, notes, and snippets.

@potluckgame
Last active November 6, 2016 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potluckgame/59e299feaf9f8931196dc4dffd295a3c to your computer and use it in GitHub Desktop.
Save potluckgame/59e299feaf9f8931196dc4dffd295a3c to your computer and use it in GitHub Desktop.
Potluck Winner calculation logic
const isaac = require('isaac'); // https://github.com/rubycon/isaac.js
/**
* generate a random number using isaac RNG
* @param {String} seed - the seed for isaac RNG
* @returns {Number} Random number between 0.0 and 1.0
*/
function isaacRandom(seed) {
isaac.seed(seed);
return isaac.random();
}
/**
* find the winner.
* @param {String} serverSeed - the server seed for current game
* @param {String} clientSeed
* @param {Number} totalTicketsWithoutLastBet - Number of tickets issued for this game excluding the last bet.
* @returns {Object} the user object for winner
*/
function findWinningTicket(serverSeed, clientSeed, totalTicketsWithoutLastBet) {
// The last bet is excluded from the calculation of `rngSeed` to make it difficult for the operators to change the game outcome in their favor.
const rngSeed = createHMACHash(serverSeed, `${clientSeed}-${totalTicketsWithoutLastBet}`);
const luckyFraction = isaacRandom(rngSeed);
let winningTicket;
winningTicket = Math.floor(totalTickets * luckyFraction);
return findOwnerOfTicket(winningTicket); // find the owner of this ticket from database.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment