Last active
August 25, 2023 03:34
-
-
Save potluckgame/0023af9db4c9a7e88ea64176845fc604 to your computer and use it in GitHub Desktop.
Potluck Server seed chain generator
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 Bluebird = require('bluebird'); | |
/** | |
* Create a `sha256` hash of the seed. | |
* @param {String} seed - Previous server `seed` | |
* @returns {String} `sha256` hash of the `seed` | |
*/ | |
function createHash(seed) { | |
return crypto.createHash('sha256').update(seed).digest('hex'); | |
} | |
/** | |
* Create an iterable to create seeds | |
* @param {Number} total - total number of iterations | |
* @returns {Object} an iterable that iterates for `total` times. | |
*/ | |
function createIterable(total) { | |
const seedIterable = {}; | |
seedIterable[Symbol.iterator] = function *steps() { | |
let count = 0; | |
while (count <= total) { | |
yield count === 0; | |
count += 1; | |
} | |
}; | |
return seedIterable; | |
} | |
/** | |
* Save the server seed in database | |
* @param {String} seed -seed to save | |
* @returns saved server seed. | |
*/ | |
function saveSeed(seed) { | |
// save the seed somewhere in database. | |
return Bluebird.resolve(seed); | |
} | |
/** | |
* Create the server seed chain | |
* @param {String} initialServerSeed - the value of first seed. (this is kept secret) | |
* @param {Number} totalSeeds - total number of seeds to create. | |
* @returns `sha256` hash of the final seed | |
*/ | |
function generateSeeds(initialServerSeed, totalSeeds) { | |
const seedIterable = createIterable(totalSeeds); | |
// Create seeds one by one | |
return Bluebird.reduce(seedIterable, (seed) => { | |
const seedHash = createHash(seed); | |
return saveSeed(seedHash); | |
}, initialServerSeed); | |
} | |
if (require.main === module) { | |
generateSeeds('secret initial seed ;-)', 1e7).then((finalSeed) => { | |
const finalSeedHash = createHash(finalSeed); | |
console.log(finalSeedHash); // Publish the final seed hash on forum before starting the game. | |
}).catch((error) => console.error(error)); | |
} |
Explain how it works
As in the end, it is now
).catch((error) => console.error(error))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How it works??