-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.js
120 lines (112 loc) · 4.15 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
'use strict'
var fs = require('fs');
var path = require('path');
var state = require('./state');
var canvas = require('canvas');
// returns a promise which will contain a png
function drawDirect (fileName) {
return new Promise(function (resolve, reject) {
resolve(fs.createReadStream(path.join(__dirname, 'images', fileName)));
});
}
// returns a promise which will contain a png
function drawSoftwareMojangState () {
return state.getMojangAuthStatus().then(function (status) {
if (status.authserver) {
return fs.createReadStream(__dirname + '/images/software-mojang-' + status.authserver + '.png');
} else {
return fs.createReadStream(__dirname + '/images/software-mojang.png');
}
});
}
// returns a promise which will contain a png
function drawSoftwareHeader () {
return new Promise(function (resolve, reject) {
fs.readFile(__dirname + '/images/software-header.png', function (err, data) {
if (err) reject(err); else resolve(data);
});
}).then(function (data) {
var img = new canvas.Image();
img.src = data;
var ctx = new canvas.Canvas(img.width, img.height).getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
return ctx;
}).then(function (ctx) {
return state.getMinecraftVersions().then(function (data) {
return state.getSoftwareViews().then(function (text) {
text = '' + text;
ctx.font = '18px DejaVu Sans';
ctx.fillStyle = '#fbf2db';
ctx.fillText(text, 177 - 5 * text.length, 27); // magic numbers
ctx.font = '16px DejaVu Sans';
ctx.fillText(data.stable ? 'Minecraft ' + data.stable : 'Unknown', 522, 27); // magic numbers
ctx.fillText(data.snapshot ? 'Minecraft ' + data.snapshot : 'Unknown', 714, 27); // magic numbers
return ctx;
});
});
}).then(function (ctx) {
return ctx.canvas.pngStream();
});
}
// returns a promise which will contain a png
function drawDevelopmentHeader () {
return new Promise(function (resolve, reject) {
fs.readFile(__dirname + '/images/development-header.png', function (err, data) {
if (err) reject(err); else resolve(data);
});
}).then(function (data) {
var img = new canvas.Image();
img.src = data;
var ctx = new canvas.Canvas(img.width, img.height).getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
return ctx;
}).then(function (ctx) {
return state.getSaleStats().then(function (data) {
return state.getDevelopmentViews().then(function (text) {
text = '' + text;
ctx.font = '18px DejaVu Sans';
ctx.fillStyle = '#fbf2db';
ctx.fillText(text, 177 - 5 * text.length, 27); // magic numbers
ctx.font = '16px DejaVu Sans';
ctx.fillText(data.total ? data.total.toLocaleString('en') : '-', 542, 27); // magic numbers
ctx.fillText(data.last24h ? data.last24h.toLocaleString('en') : '-', 820, 27); // magic numbers
return ctx;
});
});
}).then(function (ctx) {
return ctx.canvas.pngStream();
});
}
function drawQandaHeader () {
return new Promise(function (resolve, reject) {
fs.readFile(__dirname + '/images/qanda-header.png', function (err, data) {
if (err) reject(err); else resolve(data);
});
}).then(function (data) {
var img = new canvas.Image();
img.src = data;
var ctx = new canvas.Canvas(img.width, img.height).getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
return ctx;
}).then(function (ctx) {
return state.getQandaViews().then(function (text) {
text = '' + text;
ctx.font = '30px DejaVu Sans';
ctx.fillStyle = '#000000';
ctx.fillText(text, 95 - 10 * text.length, 70); // magic numbers
return ctx;
});
}).then(function (ctx) {
return ctx.canvas.pngStream();
});
}
module.exports = {
drawDirect: drawDirect,
drawSoftwareHeader: drawSoftwareHeader,
drawQandaHeader: drawQandaHeader,
drawDevelopmentHeader: drawDevelopmentHeader,
drawSoftwareMojangState: drawSoftwareMojangState,
increaseSoftwareViews: state.increaseSoftwareViews,
increaseDevelopmentViews: state.increaseDevelopmentViews,
increaseQandaViews: state.increaseQandaViews
}