You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
1.9 KiB
96 lines
1.9 KiB
var Decimal = require("decimal.js");
|
|
Decimal8 = Decimal.clone({ precision:8, rounding:8 });
|
|
|
|
function doSmartRedirect(req, res, defaultUrl) {
|
|
if (req.session.redirectUrl) {
|
|
res.redirect(req.session.redirectUrl);
|
|
req.session.redirectUrl = null;
|
|
|
|
} else {
|
|
res.redirect(defaultUrl);
|
|
}
|
|
|
|
res.end();
|
|
}
|
|
|
|
function redirectToConnectPageIfNeeded(req, res) {
|
|
if (!req.session.host) {
|
|
req.session.redirectUrl = req.originalUrl;
|
|
|
|
res.redirect("/");
|
|
res.end();
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function hex2ascii(hex) {
|
|
var str = "";
|
|
for (var i = 0; i < hex.length; i += 2) {
|
|
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
|
|
}
|
|
|
|
return str;
|
|
}
|
|
|
|
function getBlockReward(blockHeight) {
|
|
var eras = [ new Decimal8(50) ];
|
|
for (var i = 1; i < 34; i++) {
|
|
var previous = eras[i - 1];
|
|
eras.push(new Decimal8(previous).dividedBy(2));
|
|
}
|
|
|
|
var index = Math.floor(blockHeight / 210000);
|
|
|
|
return eras[index];
|
|
}
|
|
|
|
function splitArrayIntoChunks(array, chunkSize) {
|
|
var j = array.length;
|
|
var chunks = [];
|
|
|
|
for (var i = 0; i < j; i += chunkSize) {
|
|
chunks.push(array.slice(i, i + chunkSize));
|
|
}
|
|
|
|
return chunks;
|
|
}
|
|
|
|
function getRandomString(length, chars) {
|
|
var mask = '';
|
|
|
|
if (chars.indexOf('a') > -1) {
|
|
mask += 'abcdefghijklmnopqrstuvwxyz';
|
|
}
|
|
|
|
if (chars.indexOf('A') > -1) {
|
|
mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
}
|
|
|
|
if (chars.indexOf('#') > -1) {
|
|
mask += '0123456789';
|
|
}
|
|
|
|
if (chars.indexOf('!') > -1) {
|
|
mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
|
|
}
|
|
|
|
var result = '';
|
|
for (var i = length; i > 0; --i) {
|
|
result += mask[Math.floor(Math.random() * mask.length)];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
doSmartRedirect: doSmartRedirect,
|
|
redirectToConnectPageIfNeeded: redirectToConnectPageIfNeeded,
|
|
hex2ascii: hex2ascii,
|
|
getBlockReward: getBlockReward,
|
|
splitArrayIntoChunks: splitArrayIntoChunks,
|
|
getRandomString: getRandomString
|
|
};
|
|
|