2018-07-11 11:59:57 +08:00
|
|
|
const EResult = require('../resources/EResult.js');
|
|
|
|
|
2016-03-08 14:23:31 +08:00
|
|
|
exports.isSteamID = function(input) {
|
2019-09-24 16:41:11 +08:00
|
|
|
let keys = Object.keys(input);
|
2016-03-08 14:23:31 +08:00
|
|
|
if (keys.length != 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it has the keys we expect
|
2019-09-24 16:41:11 +08:00
|
|
|
keys.sort();
|
|
|
|
return keys.join(',') == 'accountid,instance,type,universe';
|
2016-03-08 14:23:31 +08:00
|
|
|
};
|
2016-07-10 13:02:36 +08:00
|
|
|
|
|
|
|
exports.decodeSteamTime = function(time) {
|
2019-09-24 16:41:11 +08:00
|
|
|
let date = new Date();
|
2017-02-02 02:30:38 +08:00
|
|
|
|
2019-09-24 16:41:11 +08:00
|
|
|
if (time.includes('@')) {
|
|
|
|
let parts = time.split('@');
|
|
|
|
if (!parts[0].includes(',')) {
|
2017-02-02 02:30:38 +08:00
|
|
|
// no year, assume current year
|
2019-09-24 16:41:11 +08:00
|
|
|
parts[0] += ', ' + date.getFullYear();
|
2017-02-02 02:30:38 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 16:41:11 +08:00
|
|
|
date = new Date(parts.join('@').replace(/(am|pm)/, ' $1') + ' UTC'); // add a space so JS can decode it
|
2017-02-02 02:30:38 +08:00
|
|
|
} else {
|
|
|
|
// Relative date
|
2019-09-24 16:41:11 +08:00
|
|
|
let amount = time.replace(/(\d) (minutes|hour|hours) ago/, '$1');
|
2017-02-02 02:30:38 +08:00
|
|
|
|
2019-09-24 16:41:11 +08:00
|
|
|
if (time.includes('minutes')) {
|
2017-02-02 02:30:38 +08:00
|
|
|
date.setMinutes(date.getMinutes() - amount);
|
2019-09-24 16:41:11 +08:00
|
|
|
} else if (time.match(/hour|hours/)) {
|
2017-02-02 02:30:38 +08:00
|
|
|
date.setHours(date.getHours() - amount);
|
|
|
|
}
|
2016-07-10 13:02:36 +08:00
|
|
|
}
|
|
|
|
|
2017-02-02 02:30:38 +08:00
|
|
|
return date;
|
2016-07-10 13:02:36 +08:00
|
|
|
};
|
2018-07-11 11:59:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an Error object for a particular EResult
|
|
|
|
* @param {int} eresult
|
2021-07-22 15:44:40 +08:00
|
|
|
* @param {string} [message] - If eresult is a failure code and message exists, this message will be used in the Error object instead
|
2018-07-11 11:59:57 +08:00
|
|
|
* @returns {null|Error}
|
|
|
|
*/
|
2021-07-22 15:44:40 +08:00
|
|
|
exports.eresultError = function(eresult, message) {
|
2021-07-22 15:54:38 +08:00
|
|
|
if (eresult == EResult.OK) {
|
2018-07-11 11:59:57 +08:00
|
|
|
// no error
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-22 15:44:40 +08:00
|
|
|
let err = new Error(message || EResult[eresult] || ("Error " + eresult));
|
2018-07-11 11:59:57 +08:00
|
|
|
err.eresult = eresult;
|
|
|
|
return err;
|
|
|
|
};
|