Created
October 28, 2016 09:50
-
-
Save nvstrs/51715e65127ef4a7e8471dcee7ac4263 to your computer and use it in GitHub Desktop.
Discover winning wager sizes for potluckgame.com
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 { createHmac } = require("crypto") | |
const isaac = require("isaac") | |
const MIN_WAGER = 100 | |
const MAX_WAGER = 25000 | |
function createHMACHash(key, data) { | |
const hmac = createHmac("sha256", key) | |
hmac.update(data) | |
return hmac.digest("hex") | |
} | |
/** | |
* 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} totalTickets - total number of tickets issued for this game | |
* @returns {Object} the user object for winner | |
*/ | |
function findWinningTicket(serverSeed, clientSeed, totalTickets) { | |
const rngSeed = createHMACHash(serverSeed, `${clientSeed}-${totalTickets}`) | |
const luckyFraction = isaacRandom(rngSeed) | |
let winningTicket | |
winningTicket = Math.floor(totalTickets * luckyFraction) | |
return winningTicket // find the owner of this ticket from database. | |
} | |
// findWinningWagers returns an array of winning wager sizes for the given seeds | |
// and number of tickets already disbursed. | |
// | |
// It assumes that no other players are able to join after placing the wager. | |
function findWinningWagers(serverSeed, clientSeed, previousTickets) { | |
const winningWagers = [] | |
for (let tickets = MIN_WAGER; tickets <= MAX_WAGER; tickets++) { | |
const winningTicket = findWinningTicket(serverSeed, clientSeed, previousTickets + tickets) | |
if (winningTicket >= previousTickets) { | |
winningWagers.push(tickets) | |
} | |
} | |
return winningWagers | |
} | |
// seeds from game 438 | |
// assuming ten players have all placed the maximum wager | |
console.time("search") | |
const winners = findWinningWagers("1ffdd9ced662c29aec62641716bb98e0423ee11e597f177f77efff28db7cc9f5", "000000000000000000f0ff7ff2743293fca00e3c0247708822365b353e8d27f6", 10*MAX_WAGER) | |
console.timeEnd("search") | |
console.log(`There are ${winners.length} winning wagers we could place.`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment