feat: shuffle post data

This commit is contained in:
ktyl 2024-08-08 20:52:04 +01:00
parent 5892679ee7
commit 3f4a1a0064
1 changed files with 16 additions and 2 deletions

18
main.js
View File

@ -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 usersUrl = localMode ? "users.json" : "https://api.wayfarer.games/singularity/users.json";
const postsUrl = localMode ? "posts.json" : "https://api.wayfarer.games/singularity/posts.json"; const postsUrl = localMode ? "posts.json" : "https://api.wayfarer.games/singularity/posts.json";
loadDataFromEndpoint(usersUrl, json => { users = json.users; }); loadDataFromEndpoint(usersUrl, json => { users = json.users; });
loadDataFromEndpoint(postsUrl, json => { loadDataFromEndpoint(postsUrl, json => {
let postsData = json.content;
shuffle(postsData);
// first pass to instantiate all the posts // first pass to instantiate all the posts
for (let i = 0; i < json.content.length; i++) { for (let i = 0; i < postsData.length; i++) {
const post = new Post(json.content[i]); const post = new Post(postsData[i]);
posts[post.id] = post; posts[post.id] = post;
} }