-
Notifications
You must be signed in to change notification settings - Fork 0
/
vhdata-csv2json.ts
149 lines (129 loc) · 4.67 KB
/
vhdata-csv2json.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// npm install -D ts-node typescript
// npx ts-node vhdata-csv2json.ts
// @ts-ignore
const fs = require('fs');
// @ts-ignore
const csv = require('csv-parser');
class Food {
name: string | undefined;
tier: number | undefined;
starred: boolean | undefined;
hp: number | undefined;
stamina: number | undefined;
eitr: number | undefined;
type: string | undefined;
hpPerSecond: number | undefined;
durationInMinutes: number | undefined;
resources: Record<string, number> = {};
}
class VHData {
tiers: string[] = [
"meadows", //1
"black forest", //2
"swamp", //3
"mountain", //4
"plains", //5
"mistlands", //6
"ashlands"//7
];
resourceTiers: string[][] = [
"raspberries\thoney\tneck tail\tboar meat\tdeer meat\tfish\tgreydwarf eye\tmushroom\tdandelion\tcoal\tfeathers\tperch".split("\t").map((s) => s.trim()), // 1 meadows
"blueberries\tcarrot\tyellow mushroom\tthistle\ttrollfish".split("\t").map((s) => s.trim()), // 2 black forest
"turnip\tooze\tentrails\tbloodbag\tserpent meat\ttoadstool\tcured squirrel hamstring\tfresh seaweed\tpowdered dragon eggshells\tpungent pebbles\tfragrant bundle\tfiery spice powder\therbs of the hidden hills\tgrasslands herbalist harvest\tmountain peak pepper\tseafarer's herbs\twoodland herb blend".split("\t").map((s) => s.trim()), // 3 swamp
"onion\twolf meat\tfreeze gland".split("\t").map((s) => s.trim()), // 4 mountain
"cloudberries \tlox meat\tbarley\tgrouper".split("\t").map((s) => s.trim()), // 5 plains
"egg\tchicken meat\tmagecap \tjotun puffs \tseeker meat \thare meat\tblood clot\tsap\troyal jelly\tanglerfish\tscale hide".split("\t").map((s) => s.trim()), // 6 mistlands
"fiddlehead\tsmoke puff\tvineberry cluster\tvolture egg\tvolture meat\tasksvin tail\tbonemaw meat".split("\t").map((s) => s.trim()), // 7 ashlands
];
food: Record<string, Food> = {};
}
(() => {
try {
console.log('Converting CSV to JSON...');
const data = new VHData();
//console.log(data);
const csvHeaders: string[] = [];
fs.createReadStream(`./src/valheim-food.csv`).pipe(csv())
.on('headers', (headers: any) => {
// Assuming headers are processed here if needed
let i = 0;
for (const header of headers) {
if (i >= 10) {
//console.log(`${header}`);
csvHeaders.push(header);
}
i++;
}
})
.on('data', (row: any) => {
//console.log(row);
const food = new Food();
// 0 "name"
food.name = row.name;
data.food[food.name!] = food;
// 1 "T"
food.tier = parseInt(row.T);
// 2 "*"
food.starred = row['*'] === '*';
// 3 "hp"
// 4 "hpS"
const hp = row.hp;
if (hp !== null && hp !== undefined && hp.trim() !== '') {
food.hp = parseInt(hp);
}
// 5 "sta"
// 6 "staS"
const stamina = row.sta;
if (stamina !== null && stamina !== undefined && stamina.trim() !== '') {
food.stamina = parseInt(stamina);
}
// 7 "type"
const type: string = row.type
if (type.charAt(0) == "y") food.type = 'yellow';
else if (type.charAt(0) == "w") food.type = 'white';
else if (type.charAt(0) == "b") food.type = 'blue';
else if (type.charAt(0) == "r") food.type = 'red';
else if (type.charAt(0) == "m") food.type = 'mead';
else if (type.charAt(0) == "f") food.type = 'feast';
else throw new Error(`Invalid type ${type}`);
// 7 "eitr"
if (type.includes(':')) food.eitr = parseInt(type.split(':')[1].trim());
else food.eitr = 0;
// 8 "hp/s"
const hpPerSecond = row['hp/s'];
if (hpPerSecond !== null && hpPerSecond !== undefined && hpPerSecond.trim() !== '') {
food.hpPerSecond = parseInt(hpPerSecond);
}
// 9 "m"
const durationInMinutes = row.m;
if (durationInMinutes !== null && durationInMinutes !== undefined && durationInMinutes.trim() !== '') {
if (durationInMinutes.includes(',')) {
food.durationInMinutes = parseFloat(durationInMinutes.replace(',', '.'));
} else if (durationInMinutes.includes('.')) {
food.durationInMinutes = parseFloat(durationInMinutes);
} else {
food.durationInMinutes = parseInt(durationInMinutes);
}
}
data.food[food.name!] = food;
for (const header of csvHeaders) {
let val = `${row[header]}`;
if (val && val.trim() !== '') {
if (val.toString().includes(',')) {
val = val.toString().replace(',', '.');
}
food.resources[header] = parseFloat(val);
}
}
//console.log(food);
})
.on('end', () => {
//console.log(data);
const outputFile = `./src/assets/valheim-food.json`;
fs.writeFileSync(outputFile, JSON.stringify(data, null, 2));
console.log('Conversion completed successfully.');
});
} catch (ex) {
console.error('Error during conversion:', ex);
}
})();