-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDecks.js
More file actions
72 lines (64 loc) · 1.84 KB
/
createDecks.js
File metadata and controls
72 lines (64 loc) · 1.84 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
const fs = require("fs");
const ejs = require("ejs");
const configs = require("./config");
const json = require("./decks");
const deckApi = require("./deck");
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
function extractCards(id, name, cards, expansion) {
const jsons = "./json/" + expansion.lang + "/" + expansion.name + "/";
const deck =
"./decks/" + expansion.lang + "/" + expansion.name + "/" + name + ".cod";
console.log("Creating " + deck + "...");
let n = 0;
let c = "";
const xmlCards = [];
cards.forEach(cardId => {
const card = JSON.parse(
fs.readFileSync(jsons + cardId + ".json").toString()
);
if (c !== card.card_title && n > 0) {
xmlCards.push({ number: n, name: c });
n = 0;
}
c = card.card_title;
n++;
});
xmlCards.push({ number: n, name: c });
ejs.renderFile(
"./decks/deck.ejs",
{
id: id,
name: name,
cards: xmlCards
},
{ filename: deck },
function(err, str) {
if (!err) fs.writeFileSync(deck, str, { flag: "w" });
}
);
}
async function processingDecks() {
for (let i = 0; i < json.length; i++) {
try {
// eslint-disable-next-line no-await-in-loop
const deck = await deckApi.retrieveDeck(json[i], configs.lang, false);
console.log(deck.data.name);
extractCards(
deck.data.id,
deck.data.name,
deck.data.cards,
configs.expansion.find(e => e.code === deck.data.expansion)
);
// eslint-disable-next-line no-await-in-loop
await sleep(12000);
} catch (error) {
console.error("[" + i + "] Invalid status code " + error);
if (error === 429) break;
}
}
}
// Noinspection JSUnresolvedVariable
console.log("Setup to processing " + json.length + " decks...");
processingDecks().then(() => console.log("Done!"));