forked from kokarn/tarkov-image-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hq-gen.js
219 lines (213 loc) · 8.4 KB
/
hq-gen.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const path = require('path');
const dotenv = require('dotenv');
const sharp = require('sharp');
const got = require('got');
dotenv.config();
const imageFunctions = require('./image-functions');
const api = require('./scanner-api');
const cloudflarePurgeLimit = 1000;
(async () => {
if (!process.env.HQ_IMAGE_DIR) {
console.log('Must set HQ_IMG_DIR env var');
return;
}
const imageFields = [];
for (const imageType in imageFunctions.imageSizes) {
imageFields.push(imageFunctions.imageSizes[imageType].api);
}
const response = await got.post('https://api.tarkov.dev/graphql', {
body: JSON.stringify({query: `{
items {
id
name
shortName
iconLink
gridImageLink
baseImageLink
inspectImageLink
image512pxLink
image8xLink
backgroundColor
types
width
height
properties {
...on ItemPropertiesPreset {
baseItem {
id
}
}
...on ItemPropertiesWeapon {
defaultPreset {
id
width
height
}
}
}
}
questItems {
id
name
shortName
iconLink
gridImageLink
baseImageLink
inspectImageLink
image512pxLink
image8xLink
width
height
}
}`}),
responseType: 'json',
resolveBodyOnly: true
});
let items = [];
items.push(...response.data.items);
items.push(...response.data.questItems.map(qItem => {
qItem.types = ['quest'];
qItem.backgroundColor = 'yellow';
return qItem;
}));
items = items.map(itemData => {
/*for (const imageField of imageFields) {
if (itemData[imageField].includes('unknown-item')) {
return itemData;
}
}
return false;*/
/*itemData.needsGridImage = false;
itemData.needsIconImage = false;
itemData.needsBaseImage = false;
itemData.needsInspectImage = false;
itemData.needs512pxImage = false;
itemData.needs8xImage = false;
if (itemData.gridImageLink.includes('unknown-item')) {
itemData.needsGridImage = true;
}
if (itemData.iconLink.includes('unknown-item')) {
itemData.needsIconImage = true;
}
if (itemData.baseImageLink.includes('unknown-item')) {
itemData.needsBaseImage = true;
}
if (itemData.inspectImageLink.includes('unknown-item')) {
itemData.needsInspectImage = true;
}
if (itemData.image512pxLink.includes('unknown-item')) {
itemData.needs512pxImage = true;
}
if (itemData.image8xLink.includes('unknown-item')) {
itemData.needs8xImage = true;
}*/
//if (!itemData.needs8xImage) return false;
return itemData;
}).filter(Boolean);
let purgeCount = 0;
for (let index = 0; index < items.length; index++) {
const item = items[index];
console.log(`${index+1}/${items.length} ${item.name}`);
let id = item.id;
const sourceImage = sharp(path.join(process.env.HQ_IMAGE_DIR, `${id}.png`));
/*await Promise.all([
imageFunctions.createIcon(sourceImage, item).then(image => {
return image.toFile(`./generated-images/${item.id}-icon.webp`);
}),
imageFunctions.createGridImage(sourceImage, item).then(image => {
return image.toFile(`./generated-images/${item.id}-grid-image.webp`);
}),
imageFunctions.createBaseImage(sourceImage, item).then(image => {
return image.toFile(`./generated-images/${item.id}-base-image.webp`);
}),
imageFunctions.createInspectImage(sourceImage, item).then(image => {
return image.toFile(`./generated-images/${item.id}-image.webp`);
}),
imageFunctions.create512Image(sourceImage, item).then(image => {
return image.toFile(`./generated-images/${item.id}-512.webp`);
}),
imageFunctions.create8xImage(sourceImage, item).then(image => {
return image.toFile(`./generated-images/${item.id}-8x.webp`);
}),
]);*/
let success = false;
while (!success) {
if (purgeCount + 4 >= cloudflarePurgeLimit) {
await new Promise(resolve => {
setTimeout(() => {
purgeCount = 0;
resolve();
}, 60000);
});
}
try {
const uploadResults = await Promise.allSettled([
imageFunctions.createIcon(sourceImage, item).then(iconImage => {
return api.submitImage(item.id, 'icon', iconImage.toBuffer(), true).then(response => {
if (response.data[0].purged) {
purgeCount++;
}
return response;
});
}),
imageFunctions.createGridImage(sourceImage, item).then(gridImage => {
return api.submitImage(item.id, 'grid-image', gridImage.toBuffer(), true).then(response => {
if (response.data[0].purged) {
purgeCount++;
}
return response;
});
}),
imageFunctions.createBaseImage(sourceImage, item).then(baseImage => {
return api.submitImage(item.id, 'base-image', baseImage.toBuffer(), true).then(response => {
if (response.data[0].purged) {
purgeCount++;
}
return response;
});
}),
imageFunctions.createInspectImage(sourceImage, item).then(inspectImage => {
return api.submitImage(item.id, 'image', inspectImage.toBuffer(), true).then(response => {
if (response.data[0].purged) {
purgeCount++;
}
return response;
});
}),
imageFunctions.create512Image(sourceImage, item).then(largeImage => {
return api.submitImage(item.id, '512', largeImage.toBuffer(), true).then(response => {
if (response.purged) {
purgeCount++;
}
return response;
});
}),
imageFunctions.create8xImage(sourceImage, item).then(xlImage => {
return api.submitImage(item.id, '8x', xlImage.toBuffer(), true).then(response => {
if (response.purged) {
purgeCount++;
}
return response;
});
}),
]);
for (const result of uploadResults) {
if (result.status === 'rejected') {
console.log(result);
throw result.reason;
}
}
success = true;
} catch (error) {
if (error.message.includes('Input file is missing')) {
console.log(`Could not load source image for ${item.name} ${item.id}`);
success = true;
continue;
}
console.log(`Error processing image for ${item.name} ${item.id}`, error);
console.log('Cloudflare purge count:', purgeCount);
//break;
}
}
}
})();