-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomize-games.js
More file actions
44 lines (33 loc) · 1.33 KB
/
Copy pathrandomize-games.js
File metadata and controls
44 lines (33 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { readdir, rename } from 'fs/promises';
import { join } from 'path';
const gamesDir = './public/games';
const shuffle = (array) => {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
};
const main = async () => {
const files = await readdir(gamesDir);
const jsonFiles = files.filter(f => f.endsWith('.json')).sort();
const numbers = jsonFiles.map(f => parseInt(f.replace('.json', ''), 10));
const shuffled = shuffle(numbers);
const tempPrefix = 'temp_';
for (let i = 0; i < jsonFiles.length; i++) {
const oldName = jsonFiles[i];
const tempName = `${tempPrefix}${oldName}`;
await rename(join(gamesDir, oldName), join(gamesDir, tempName));
}
const tempFiles = await readdir(gamesDir);
const tempJsonFiles = tempFiles.filter(f => f.startsWith(tempPrefix) && f.endsWith('.json')).sort();
for (let i = 0; i < tempJsonFiles.length; i++) {
const tempName = tempJsonFiles[i];
const newNumber = shuffled[i].toString().padStart(3, '0');
const newName = `${newNumber}.json`;
await rename(join(gamesDir, tempName), join(gamesDir, newName));
}
console.log(`Randomized ${jsonFiles.length} files`);
};
main().catch(console.error);