Skip to content

Instantly share code, notes, and snippets.

@potluckgame
Last active August 25, 2023 03:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save potluckgame/0023af9db4c9a7e88ea64176845fc604 to your computer and use it in GitHub Desktop.
Save potluckgame/0023af9db4c9a7e88ea64176845fc604 to your computer and use it in GitHub Desktop.
Potluck Server seed chain generator
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));
}
@rjcomboy
Copy link

How it works??

@kathybesti21
Copy link

Explain how it works

@kathybesti21
Copy link

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