-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
399 lines (339 loc) · 10.3 KB
/
image.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
const fabric = require('fabric').fabric;
const fs = require('fs');
const path = require('path');
const opentype = require('opentype.js');
const imgur = require('imgur');
const Gradient = require('gradient');
imgur.setClientId(config.private.imgur_client_id);
module.exports.generate = async function(data, pretty_bt, input, user_id, chat_id) {
const canvas = fabric.createCanvasForNode(800, 600);
const mode = input.mode;
const ranks = input.ranks;
console.log('Stats preparing!');
let stats, moreStats, averageStats, overallStats, heroesPlaytime, heroes;
if (mode === 'quickplay') {
stats = data['stats']['quickplay']['overall_stats'];
moreStats = data['stats']['quickplay']['game_stats'];
averageStats = data['stats']['quickplay']['average_stats'];
overallStats = data['stats']['quickplay']['overall_stats'];
heroesPlaytime = data['heroes']['playtime']['quickplay'];
heroes = data['heroes']['stats']['competitive'];
} else if (mode === 'competitive') {
stats = data['stats']['competitive']['overall_stats'];
moreStats = data['stats']['competitive']['game_stats'];
averageStats = data['stats']['competitive']['average_stats'];
overallStats = data['stats']['competitive']['overall_stats'];
heroesPlaytime = data['heroes']['playtime']['competitive'];
heroes = data['heroes']['stats']['competitive'];
}
let level = stats['level'];
if (typeof stats['prestige'] === 'number') {
level += (stats['prestige'] * 100);
}
const heroesColors = {
ana: '#47699e',
bastion: '#5a724f',
doomfist: '#000000', // TEMP
dva: '#fc78bd',
genji: '#80fb00',
hanzo: '#b2a765',
junkrat: '#f7b216',
lucio: '#66c516',
mccree: '#a62626',
mei: '#459af0',
mercy: '#faf2ad',
orisa: '#438a41',
pharah: '#0056bc',
reaper: '#5d0016',
reinhardt: '#7c8a8b',
roadhog: '#ad6e16',
soldier76: '#425175',
sombra: '#4f26a9',
symmetra: '#76b4c8',
torbjorn: '#ba4b3d',
tracer: '#de7900',
widowmaker: '#8a3d8f',
winston: '#8f92ad',
zarya: '#f65da6',
zenyatta: '#fcee5a'
};
const heroesArr = Object.keys(heroesPlaytime).map(function (key) {
return {
name: key,
timePlayed: heroesPlaytime[key]
}
});
heroesArr.sort(function (a, b) {
return b.timePlayed - a.timePlayed
});
console.log('Fonts activating!');
// Fonts plug in
const bignoodle_ot = opentype.loadSync('fonts/BigNoodleToo.ttf');
const bignoodle_italic_ot = opentype.loadSync('fonts/BigNoodleTooOblique.ttf');
const futura_ot = opentype.loadSync('fonts/FuturaPTBold.ttf');
// Linux
//const bignoodle = new canvas.Font('BigNoodleToo.ttf', __dirname + '/fonts/BigNoodleToo.ttf');
//const bignoodle_italic = new canvas.Font('BigNoodleTooOblique.ttf', __dirname + '/fonts/BigNoodleTooOblique.ttf');
//const futura = new canvas.Font('FuturaPTBold.ttf', __dirname + '/fonts/FuturaPTBold.ttf');
// Windows
const bignoodle = new canvas.Font('BigNoodleToo', __dirname + '/fonts/BigNoodleToo.ttf');
const bignoodle_italic = new canvas.Font('BigNoodleTooOblique', __dirname + '/fonts/BigNoodleTooOblique.ttf');
const futura = new canvas.Font('FuturaPTBold', __dirname + '/fonts/FuturaPTBold.ttf');
// Fonts activating
canvas.contextContainer.addFont(bignoodle);
canvas.contextContainer.addFont(bignoodle_italic);
canvas.contextContainer.addFont(futura);
console.log('Image generation started!');
// Background with heroes colors gradient
let outline = new fabric.Rect({
width: canvas.width,
height: canvas.height
});
outline.setGradient('fill', {
x2: outline.width,
colorStops: {
0: heroesColors[heroesArr[0].name],
0.25: heroesColors[heroesArr[1].name],
0.5: heroesColors[heroesArr[2].name],
0.75: heroesColors[heroesArr[3].name],
1: heroesColors[heroesArr[4].name]
}
});
canvas.add(outline);
// White background
const background = new fabric.Rect({
left: 10, top: 10,
fill: 'white',
init: function () {
this.width = outline.width - this.left * 2;
this.height = outline.height - this.top * 2;
return this;
}
}.init());
canvas.add(background);
// Battletag and level/rank
const battletag = new fabric.Text(pretty_bt, {
left: 20, top: 20,
fill: '#555',
fontFamily: 'BigNoodleTooOblique',
fontSize: 50
});
canvas.add(battletag);
let tempValue = 0;
if (mode === 'quickplay')
tempValue = level.toString();
else if (mode === 'competitive')
if (stats.comprank === null)
tempValue = 'НЕИЗВЕСТНО';
else
tempValue = stats.comprank.toString();
const rank_width = bignoodle_ot.getAdvanceWidth(tempValue, 50);
const value = new fabric.Text(tempValue, {
left: canvas.width - rank_width - 25,
top: 20,
fill: 'white',
fontFamily: 'BigNoodleToo',
fontSize: 50
});
const rect = new fabric.Rect({
init: function () {
if (mode === 'quickplay')
this.fill = '#a35435';
else if (mode === 'competitive')
this.fill = '#7749a9';
this.left = canvas.width - this.width - 20;
return this;
},
top: value.top,
width: rank_width + 10,
height: value.height - 5,
}.init());
canvas.add(rect);
canvas.add(value);
// Heroes pics
for (let i = 0; i < 5; i++) {
function addImage() {
return new Promise(function (resolve) {
fabric.Image.fromURL(`images/heroes/${heroesArr[i].name}.png`, function (img) {
img.set({
left: 20 + 155 * i,
top: 80,
width: 140,
height: 240
});
const background = new fabric.Rect({
left: img.left,
top: img.top,
width: img.width,
height: img.height,
fill: '#ccc'
});
const panel = new fabric.Rect({
left: img.left - 1,
top: img.top + 200,
width: img.width + 1,
height: img.height - 200,
fill: heroesColors[heroesArr[i].name]
});
let timeColor;
const regexp = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(heroesColors[heroesArr[i].name]);
const result = {
r: parseInt(regexp[1], 16),
g: parseInt(regexp[2], 16),
b: parseInt(regexp[3], 16)
};
const brightness = Math.sqrt(
result.r * result.r * 0.241 +
result.g * result.g * 0.691 +
result.b * result.b * 0.068
);
if (brightness > 200)
timeColor = '#999';
else
timeColor = 'white';
let timeTemp;
if (heroesArr[i].timePlayed < 1)
timeTemp = `${(heroesArr[i].timePlayed * 60).toFixed()}M`;
else
timeTemp = `${heroesArr[i].timePlayed.toFixed()}H`;
const timePlayed = new fabric.Text(timeTemp, {
left: img.left + img.width - 10 - bignoodle_ot.getAdvanceWidth(timeTemp, 30),
top: img.top + img.height - 35,
fill: timeColor,
fontFamily: 'BigNoodleToo',
fontSize: 30
});
canvas.add(background);
canvas.add(img);
canvas.add(panel);
canvas.add(timePlayed);
resolve();
});
});
}
await addImage();
}
// Stats
const splitedRanks = R.splitEvery(5, ranks);
const grad = Gradient(['#dd0000', '#dddd00', '#00dd00'], 100);
const colors = grad.toArray('hexString');
for (let raw in splitedRanks) {
// First group
const line1 = new fabric.Rect({
width: canvas.width - 40,
height: 1,
left: 20,
top: 355 + 130 * raw
});
const line2 = new fabric.Rect({
width: canvas.width - 40,
height: 1,
left: 20,
top: 405 + 130 * raw
});
canvas.add(line1);
canvas.add(line2);
for (let column in splitedRanks[raw]) {
const name = new fabric.Text(splitedRanks[raw][column].name, {
left: 20 + 155 * column,
top: 330 + 130 * raw,
fill: 'grey',
fontFamily: 'BigNoodleToo',
fontSize: 20
});
const value = new fabric.Text(splitedRanks[raw][column].value.toString(), {
left: 20 + 155 * column,
top: 355 + 130 * raw,
fill: '#555',
fontFamily: 'BigNoodleTooOblique',
fontSize: 50
});
const rank = new fabric.Text(splitedRanks[raw][column].rank, {
left: 20 + 155 * column,
top: 410 + 130 * raw,
fill: 'grey',
fontFamily: 'BigNoodleToo',
fontSize: 25
});
const rankBar = new fabric.Rect({
left: 20 + 155 * column,
top: 440 + 130 * raw,
width: 140 - (parseInt(splitedRanks[raw][column].rank, 10) / 100 * 140),
height: 4,
fill: colors[100 - parseInt(splitedRanks[raw][column].rank, 10)]
});
const rankBarBackground = new fabric.Rect({
left: 20 + 155 * column,
top: 440 + 130 * raw,
width: 140,
height: 4,
fill: 'grey'
});
canvas.add(name);
canvas.add(value);
canvas.add(rank);
canvas.add(rankBarBackground);
canvas.add(rankBar);
}
}
console.log('Image generation completed!');
const stream = canvas.createJPEGStream({
quality: 90
});
console.log('Image rendered!');
return new Promise(function (resolve, reject) {
let temp = [];
console.log('Stream started!');
stream.on('data', function (chunk) {
temp.push(chunk);
console.log('Streaming...');
});
stream.on('end', function () {
// Clean canvas to prevent memory leak!
canvas.clear();
// Create image buffer to send it directly to Telegram
const buffer = Buffer.concat(temp);
console.log('Stream ended, starting sending!');
// Getting deletehash from DB and removing image from Imgur (yes, we save their space to keep it free)
r.table('users').get(user_id).pluck('imgur')
.then(function (res) {
if (res.imgur !== undefined) {
if (mode === 'quickplay' && res.imgur.quickplay.deletehash !== undefined)
return imgur.deleteImage(res.imgur.quickplay.deletehash);
else if (mode === 'competitive' && res.imgur.competitive.deletehash !== undefined)
return imgur.deleteImage(res.imgur.competitive.deletehash);
} else
return 'First generation!';
})
.then(function(status) {
console.log(status);
// Uploading new image and saving link and deletehash
return imgur.uploadBase64(buffer.toString('base64'))
})
.then(function (res) {
console.log(res);
let data = {};
data.quickplay = {};
data.competitive = {};
if (mode === 'quickplay') {
data.quickplay.link = res.data.link;
data.quickplay.deletehash = res.data.deletehash;
} else if (mode === 'competitive') {
data.competitive.link = res.data.link;
data.competitive.deletehash = res.data.deletehash;
}
return r.table('users').get(user_id)
.update({
imgur: data
});
})
.then(function (status) {
console.log(status);
resolve(status);
})
.catch(function (error) {
reject(error);
});
});
});
};