Repository navigation
Expand file tree
/
Copy pathcreateunit.js
More file actions
76 lines (55 loc) · 2.11 KB
/
Copy pathcreateunit.js
File metadata and controls
76 lines (55 loc) · 2.11 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require("fs");
const readline = require("readline");
const dataPath = "./data/units.json";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function ask(q) {
return new Promise(resolve => rl.question(q, resolve));
}
// find highest ID for rarity inside text
function getMaxId(text, sectionName, rarity) {
const sectionRegex = new RegExp(`"${sectionName}"\\s*:\\s*{[\\s\\S]*?"units"\\s*:\\s*\\[([\\s\\S]*?)\\]`);
const match = text.match(sectionRegex);
if (!match) return 0;
const unitsBlock = match[1];
const idMatches = [...unitsBlock.matchAll(
new RegExp(`{[^}]*"rarity"\\s*:\\s*"${rarity}"[^}]*"id"\\s*:\\s*"(\\d+)"|{[^}]*"id"\\s*:\\s*"(\\d+)"[^}]*"rarity"\\s*:\\s*"${rarity}"`, "g")
)];
const ids = idMatches.map(m => Number(m[1] || m[2]));
return ids.length ? Math.max(...ids) : 0;
}
async function run() {
while (true) {
console.log("\n--- Add Units ---");
let text = fs.readFileSync(dataPath, "utf-8");
const seasonInput = await ask("Page: ");
const rarity = (await ask("Rarity: ")).toLowerCase();
const amount = parseInt(await ask("Amount: "));
// 🔥 find correct season name (case insensitive)
const seasonMatch = [...text.matchAll(/"([^"]+)"\s*:\s*{\s*"units"/g)]
.map(m => m[1])
.find(name => name.toLowerCase() === seasonInput.toLowerCase());
if (!seasonMatch) {
console.log("❌ Page not found!");
continue;
}
const maxId = getMaxId(text, seasonMatch, rarity);
let currentId = maxId;
// 🔥 build new lines
let insertText = "";
for (let i = 0; i < amount; i++) {
currentId++;
insertText += ` { "id": "${currentId}", "rarity": "${rarity}" },\n`;
}
// 🔥 insert before closing ]
const sectionRegex = new RegExp(`("${seasonMatch}"\\s*:\\s*{[\\s\\S]*?"units"\\s*:\\s*\\[)([\\s\\S]*?)(\\])`);
text = text.replace(sectionRegex, (match, start, middle, end) => {
return start + middle + insertText + end;
});
fs.writeFileSync(dataPath, text);
console.log(`\n✅ Added ${amount} units to ${seasonMatch}`);
}
}
run();