-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.js
168 lines (151 loc) · 5.01 KB
/
state.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
'use strict'
var sqlite3 = require('sqlite3');
var rp = require('request-promise');
var saleStats = {}, versions = {}, mojangStatus = {}, lastUpdateTime = 0;
var timeInterval = 60000;
var viewsDatabase, waitUntilOpen = new Promise(function (resolve, reject) {
var mode = sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE;
viewsDatabase = new sqlite3.Database(__dirname + '/views.db', mode, function (err) {
if (err) reject(err); else resolve();
});
}).then(function () {
return new Promise(function (resolve, reject) {
var sql = 'CREATE TABLE IF NOT EXISTS software (date INTEGER(32) PRIMARY KEY, count INTEGER(32) DEFAULT 0);';
viewsDatabase.run(sql, [], resolve);
});
}).then(function () {
return new Promise(function (resolve, reject) {
var sql = 'CREATE TABLE IF NOT EXISTS development (date INTEGER(32) PRIMARY KEY, count INTEGER(32) DEFAULT 0);';
viewsDatabase.run(sql, [], resolve);
});
}).then(function () {
return new Promise(function (resolve, reject) {
var sql = 'CREATE TABLE IF NOT EXISTS qanda (date INTEGER(32) PRIMARY KEY, count INTEGER(32) DEFAULT 0);';
viewsDatabase.run(sql, [], resolve);
});
});
function getApproximateTimeStamp () {
var timeDiff = 28800000; // UTC+8
return Math.floor((Date.now() + timeDiff) / 86400000) * 86400 - timeDiff / 1000;
}
function updateStats () {
function updateUntilItIsOK (times) {
return Promise.all([rp({
method: 'POST',
uri: 'https://api.mojang.com/orders/statistics',
body: {metricKeys: ['item_sold_minecraft', 'prepaid_card_redeemed_minecraft']},
json: true
}).catch(function () {
return {};
}), rp({
uri: 'https://launchermeta.mojang.com/mc/game/version_manifest.json',
json: true
}), rp({
uri: 'https://status.mojang.com/check',
json: true
}).catch(function () {
return [];
})]).then(function (data) {
saleStats = data[0];
var minecraftVersions = data[1].versions;
versions.stable = versions.snapshot = undefined;
for (var i in minecraftVersions) {
var type = minecraftVersions[i].type;
if (type === 'release' && !versions.stable) versions.stable = minecraftVersions[i].id;
if (type === 'snapshot' && !versions.snapshot) versions.snapshot = minecraftVersions[i].id;
if (versions.stable && versions.snapshot) break;
}
var statusCollection = data[2];
for (var i in statusCollection) {
var json = statusCollection[i];
if (json['authserver.mojang.com']) {
mojangStatus['authserver'] = json['authserver.mojang.com'];
break;
}
}
lastUpdateTime = Date.now();
return null;
}).catch(function (err) {
if (times > 0) {
return updateUntilItIsOK(times - 1);
}
lastUpdateTime = Date.now();
return Promise.resolve(null);
});
}
if (Math.floor(lastUpdateTime / timeInterval) < Math.floor(Date.now() / timeInterval)) {
var promise = updateUntilItIsOK(3);
if (lastUpdateTime === 0) {
return promise;
} else {
lastUpdateTime = Date.now();
}
}
return Promise.resolve(null);
}
function getMinecraftVersions () {
return updateStats().then(function () {
return versions;
});
}
function getMojangAuthStatus () {
return updateStats().then(function () {
return mojangStatus;
});
}
function getSaleStats () {
return updateStats().then(function () {
return saleStats;
});
}
function getSoftwareViews () {
return getViews('software');
}
function getDevelopmentViews () {
return getViews('development');
}
function getQandaViews () {
return getViews('qanda');
}
function increaseSoftwareViews () {
return increaseViews('software');
}
function increaseDevelopmentViews () {
return increaseViews('development');
}
function increaseQandaViews () {
return increaseViews('qanda');
}
function increaseViews (tableName) {
return new Promise(function (resolve, reject) {
var date = getApproximateTimeStamp();
var sql = 'INSERT OR IGNORE INTO ' + tableName + '(date) VALUES(?)';
viewsDatabase.run(sql, [date], function (err) {
if (err) reject(err); else {
var sql = 'UPDATE ' + tableName + ' SET count=count+1 WHERE date=?';
viewsDatabase.run(sql, [date], function (err) {
if (err) reject(err); else resolve();
});
}
})
});
}
function getViews (tableName) {
return new Promise(function (resolve, reject) {
var sql = 'SELECT count FROM ' + tableName + ' WHERE date=?';
viewsDatabase.get(sql, [getApproximateTimeStamp()], function (err, row) {
if (err) reject(err); else resolve(row ? row.count : 1);
});
});
}
module.exports = {
getSaleStats: getSaleStats,
getSoftwareViews: getSoftwareViews,
getDevelopmentViews: getDevelopmentViews,
getQandaViews: getQandaViews,
getMojangAuthStatus: getMojangAuthStatus,
getMinecraftVersions: getMinecraftVersions,
increaseSoftwareViews: increaseSoftwareViews,
increaseDevelopmentViews: increaseDevelopmentViews,
increaseQandaViews: increaseQandaViews
};