diff --git a/src/js/browser.js b/src/js/browser.js index a8c10ed7..a0b9bf00 100644 --- a/src/js/browser.js +++ b/src/js/browser.js @@ -1,8 +1,17 @@ import {createElem as el} from './elem.js'; +import { PseudoRandom } from './random.js'; +const r = new PseudoRandom(); const tocElem = document.querySelector('#toc'); const toc = await (await fetch('toc.json')).json(); +const seed = parseInt(sessionStorage.getItem('seed') || (Math.random() * 0x7FFF_FFFF)); +sessionStorage.setItem('seed', seed); +r.reset(seed); + +toc.forEach(v => v.rank = r.rand()); +toc.sort((a, b) => a.rank - b.rank); + for (const {id, name, username, avatarUrl, screenshotURL} of toc) { const artUrl = `art/${id}`; tocElem.appendChild(el('div', [ diff --git a/src/js/main.js b/src/js/main.js index 2bc1de6b..de6e838b 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -980,14 +980,6 @@ define([ }; } - document.querySelectorAll('.parentLink').forEach(elem => { - elem.addEventListener('click', function(event) { - if (parent) { - parent.location.href = this.href; - } - }); - }); - var notifier = new Notifier({ timeout: 7.5, container: document.body, @@ -2145,6 +2137,14 @@ define([ } } + document.querySelectorAll('.parentLink').forEach(elem => { + elem.addEventListener('click', function(event) { + if (parent) { + parent.location.href = this.href; + } + }); + }); + vs.setSettings(settings, options); } diff --git a/src/js/random.js b/src/js/random.js new file mode 100644 index 00000000..2098eb80 --- /dev/null +++ b/src/js/random.js @@ -0,0 +1,47 @@ +const RANDOM_RANGE = Math.pow(2, 32); + +export class PseudoRandom { + #randomSeed = 0; + + /** + * Returns a deterministic pseudo random number between 0 and 1 + * @return {number} a random number between 0 and 1 + */ + random() { + return (this.#randomSeed = + (134775813 * this.#randomSeed + 1) % + RANDOM_RANGE) / RANDOM_RANGE; + }; + + /** + * Reset the pseudo random number generator (whit optional seed) + * @param {number} [seed] + */ + reset(seed = 0) { + this.#randomSeed = seed; + } + + /** + * Return a random number between 0 and n (but not n) + * @param {number} [min] + * @param {number} [max] + */ + rand(min, max) { + if (min === undefined) { + min = 0; + max = 1; + } else if (max === undefined) { + max = min; + min = 0; + } + return min + this.random() * (max - min); + } + + /** + * Return a random integer between 0 and n-1 + * @param {number} n + */ + randInt(min, max) { + return Math.floor(this.rand(min, max)); + } +}