-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse-csv.js
69 lines (59 loc) · 1.55 KB
/
parse-csv.js
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
var path = './public/data.js';
var path2 = './build/output.json';
var fs = require('fs');
const getCards = require('./parse-card-text');
function write(path, data) {
try {
fs.unlinkSync(path);
} catch (e) {}
fs.writeFile(path, data, function(err) {
if (err) {
return console.log(err);
}
console.log('Saved js file in ' + path);
});
}
const cardText = getCards();
function parseCardLine(line) {
var parts = line.split(';');
var codexId = Number(parts[13]);
if (parts.length !== 15) {
return false;
}
return {
id: Number(parts[0]),
color: parts[1],
name: parts[2],
type: parts[3],
creatureType: parts[4],
faeriaCost: Number(parts[5]),
wild: Number(parts[6]),
islands: Number(parts[7]),
forests: Number(parts[8]),
mountains: Number(parts[9]),
deserts: Number(parts[10]),
attack: Number(parts[11]),
health: Number(parts[12]),
text: (cardText[Number(parts[0])] || '').replace(/"/g, ''), //strip double quotes
//b: parts[14], //unused
//questReward: parts[13] === '2' ? true : false,
codexId: codexId,
rarity: parts[14],
uncollectable: codexId === 106666 ? true : false
};
}
var data = fs.readFileSync('./tmp/merlin_shortened.csv', 'utf-8');
console.log(data);
//solit it and trash the last one
var lines = data.split('\n');
//parse all the lines into cards
var cards = [];
lines.forEach(function(line) {
var card = parseCardLine(line);
if (!card) { return; }
cards.push(card);
});
//write the two versions
var jsonCards = JSON.stringify(cards, null, 2)
write(path, 'var data=' + jsonCards+';');
write(path2, jsonCards)