Last active
November 6, 2016 23:26
-
-
Save potluckgame/59e299feaf9f8931196dc4dffd295a3c to your computer and use it in GitHub Desktop.
Potluck Winner calculation logic
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
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