From 3f4a1a00647d9bf242277253cf9b397f2aeb45cc Mon Sep 17 00:00:00 2001 From: ktyl Date: Thu, 8 Aug 2024 20:52:04 +0100 Subject: [PATCH] feat: shuffle post data --- main.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 842eb58..405e505 100644 --- a/main.js +++ b/main.js @@ -582,14 +582,28 @@ function loadDataFromEndpoint(endpoint, callback) { }); } +function shuffle(array) { + let idx = array.length; + + while (idx != 0) { + const r = Math.floor(Math.random() * idx); + idx--; + + [array[idx], array[r]] = [array[r], array[idx]]; + } +} const usersUrl = localMode ? "users.json" : "https://api.wayfarer.games/singularity/users.json"; const postsUrl = localMode ? "posts.json" : "https://api.wayfarer.games/singularity/posts.json"; loadDataFromEndpoint(usersUrl, json => { users = json.users; }); loadDataFromEndpoint(postsUrl, json => { + + let postsData = json.content; + shuffle(postsData); + // first pass to instantiate all the posts - for (let i = 0; i < json.content.length; i++) { - const post = new Post(json.content[i]); + for (let i = 0; i < postsData.length; i++) { + const post = new Post(postsData[i]); posts[post.id] = post; }